1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "tooling/client/utils/cli_command.h"
17
18 namespace OHOS::ArkCompiler::Toolchain {
19 const std::string HELP_MSG = "usage: <command> <options>\n"
20 " These are common commands list:\n"
21 " allocationtrack(at) allocation-track-start with options\n"
22 " allocationtrack-stop(at-stop) allocation-track-stop\n"
23 " heapdump(hd) heapdump with options\n"
24 " heapprofiler-enable(hp-enable) heapdump enable\n"
25 " heapprofiler-disable(hp-disable) heapdump disable\n"
26 " sampling(sampling) heapprofiler sampling\n"
27 " sampling-stop(sampling-stop) heapprofiler sampling stop\n"
28 " collectgarbage(gc) heapprofiler collectgarbage\n"
29 " cpuprofile(cp) cpuprofile start\n"
30 " cpuprofile-stop(cp-stop) cpuprofile stop\n"
31 " cpuprofile-enable(cp-enable) cpuprofile enable\n"
32 " cpuprofile-disable(cp-disable) cpuprofile disable\n"
33 " cpuprofile-show(cp-show) cpuprofile show\n"
34 " cpuprofile-setSamplingInterval(cp-ssi) cpuprofile setSamplingInterval\n"
35 " runtime-enable(rt-enable) runtime enable\n"
36 " heapusage(hu) runtime getHeapUsage\n"
37 " break(b) break with options\n"
38 " backtrack(bt) backtrace\n"
39 " continue(c) continue\n"
40 " delete(d) delete with options\n"
41 " disable disable\n"
42 " display display\n"
43 " enable debugger enable\n"
44 " finish(fin) finish\n"
45 " frame(f) frame\n"
46 " help(h) list available commands\n"
47 " ignore(ig) ignore\n"
48 " infobreakpoints(infob) info-breakpoints\n"
49 " infosource(infos) info-source\n"
50 " jump(j) jump\n"
51 " list(l) list\n"
52 " next(n) next\n"
53 " print(p) print with options\n"
54 " ptype ptype\n"
55 " quit(q) quit\n"
56 " run(r) run\n"
57 " setvar(sv) set value with options\n"
58 " step(s) step\n"
59 " undisplay undisplay\n"
60 " watch(wa) watch\n"
61 " resume resume\n"
62 " showstack(ss) showstack\n"
63 " step-into(si) step-into\n"
64 " step-out(so) step-out\n"
65 " step-over(sov) step-over\n"
66 " runtime-disable rt-disable\n"
67 " session-new add new session\n"
68 " session-remove del a session\n"
69 " session-list list all sessions\n"
70 " session switch session\n"
71 " forall command for all sessions\n"
72 " success test success\n"
73 " fail test fail\n";
74
75 const std::vector<std::string> cmdList = {
76 "allocationtrack",
77 "allocationtrack-stop",
78 "heapdump",
79 "heapprofiler-enable",
80 "heapprofiler-disable",
81 "sampling",
82 "sampling-stop",
83 "collectgarbage",
84 "cpuprofile",
85 "cpuprofile-stop",
86 "cpuprofile-enable",
87 "cpuprofile-disable",
88 "cpuprofile-show",
89 "cpuprofile-setSamplingInterval",
90 "runtime-enable",
91 "heapusage",
92 "break",
93 "backtrack",
94 "continue",
95 "delete",
96 "disable",
97 "display",
98 "enable",
99 "finish",
100 "frame",
101 "help",
102 "ignore",
103 "infobreakpoints",
104 "infosource",
105 "jump",
106 "list",
107 "next",
108 "print",
109 "ptype",
110 "run",
111 "setvar",
112 "step",
113 "undisplay",
114 "watch",
115 "resume",
116 "step-into",
117 "step-out",
118 "step-over",
119 "runtime-disable",
120 "session-new",
121 "session-remove",
122 "session-list",
123 "session",
124 "success",
125 "fail"
126 };
127
ExecCommand()128 ErrCode CliCommand::ExecCommand()
129 {
130 CreateCommandMap();
131
132 ErrCode result = OnCommand();
133 return result;
134 }
135
CreateCommandMap()136 void CliCommand::CreateCommandMap()
137 {
138 commandMap_ = {
139 {std::make_pair("allocationtrack", "at"), std::bind(&CliCommand::HeapProfilerCommand, this, "allocationtrack")},
140 {std::make_pair("allocationtrack-stop", "at-stop"),
141 std::bind(&CliCommand::HeapProfilerCommand, this, "allocationtrack-stop")},
142 {std::make_pair("heapdump", "hd"), std::bind(&CliCommand::HeapProfilerCommand, this, "heapdump")},
143 {std::make_pair("heapprofiler-enable", "hp-enable"),
144 std::bind(&CliCommand::HeapProfilerCommand, this, "heapprofiler-enable")},
145 {std::make_pair("heapprofiler-disable", "hp-disable"),
146 std::bind(&CliCommand::HeapProfilerCommand, this, "heapprofiler-disable")},
147 {std::make_pair("sampling", "sampling"), std::bind(&CliCommand::HeapProfilerCommand, this, "sampling")},
148 {std::make_pair("sampling-stop", "sampling-stop"),
149 std::bind(&CliCommand::HeapProfilerCommand, this, "sampling-stop")},
150 {std::make_pair("collectgarbage", "gc"), std::bind(&CliCommand::HeapProfilerCommand, this, "collectgarbage")},
151 {std::make_pair("cpuprofile", "cp"), std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile")},
152 {std::make_pair("cpuprofile-stop", "cp-stop"),
153 std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile-stop")},
154 {std::make_pair("cpuprofile-enable", "cp-enable"),
155 std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile-enable")},
156 {std::make_pair("cpuprofile-disable", "cp-disable"),
157 std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile-disable")},
158 {std::make_pair("cpuprofile-show", "cp-show"),
159 std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile-show")},
160 {std::make_pair("cpuprofile-setSamplingInterval", "cp-ssi"),
161 std::bind(&CliCommand::CpuProfileCommand, this, "cpuprofile-setSamplingInterval")},
162 {std::make_pair("runtime-enable", "rt-enable"), std::bind(&CliCommand::RuntimeCommand, this, "runtime-enable")},
163 {std::make_pair("heapusage", "hu"), std::bind(&CliCommand::RuntimeCommand, this, "heapusage")},
164 {std::make_pair("break", "b"), std::bind(&CliCommand::BreakCommand, this, "break")},
165 {std::make_pair("backtrack", "bt"), std::bind(&CliCommand::DebuggerCommand, this, "backtrack")},
166 {std::make_pair("continue", "c"), std::bind(&CliCommand::DebuggerCommand, this, "continue")},
167 {std::make_pair("delete", "d"), std::bind(&CliCommand::DeleteCommand, this, "delete")},
168 {std::make_pair("disable", "disable"), std::bind(&CliCommand::DebuggerCommand, this, "disable")},
169 {std::make_pair("display", "display"), std::bind(&CliCommand::DisplayCommand, this, "display")},
170 {std::make_pair("enable", "enable"), std::bind(&CliCommand::DebuggerCommand, this, "enable")},
171 {std::make_pair("finish", "fin"), std::bind(&CliCommand::DebuggerCommand, this, "finish")},
172 {std::make_pair("frame", "f"), std::bind(&CliCommand::DebuggerCommand, this, "frame")},
173 {std::make_pair("enable-launch-accelerate", "enable-acc"),
174 std::bind(&CliCommand::DebuggerCommand, this, "enable-launch-accelerate")},
175 {std::make_pair("saveAllPossibleBreakpoints", "b-new"),
176 std::bind(&CliCommand::SaveAllPossibleBreakpointsCommand, this, "saveAllPossibleBreakpoints")},
177 };
178 CreateOtherCommandMap();
179 }
CreateOtherCommandMap()180 void CliCommand::CreateOtherCommandMap()
181 {
182 commandMap_.insert({
183 {std::make_pair("help", "h"), std::bind(&CliCommand::ExecHelpCommand, this)},
184 {std::make_pair("ignore", "ig"), std::bind(&CliCommand::DebuggerCommand, this, "ignore")},
185 {std::make_pair("infobreakpoints", "infob"), std::bind(&CliCommand::DebuggerCommand, this, "infobreakpoints")},
186 {std::make_pair("infosource", "infos"), std::bind(&CliCommand::InfosourceCommand, this, "infosource")},
187 {std::make_pair("jump", "j"), std::bind(&CliCommand::DebuggerCommand, this, "jump")},
188 {std::make_pair("list", "l"), std::bind(&CliCommand::ListCommand, this, "list")},
189 {std::make_pair("next", "n"), std::bind(&CliCommand::DebuggerCommand, this, "next")},
190 {std::make_pair("print", "p"), std::bind(&CliCommand::PrintCommand, this, "print")},
191 {std::make_pair("ptype", "ptype"), std::bind(&CliCommand::DebuggerCommand, this, "ptype")},
192 {std::make_pair("run", "r"), std::bind(&CliCommand::RuntimeCommand, this, "run")},
193 {std::make_pair("setvar", "sv"), std::bind(&CliCommand::DebuggerCommand, this, "setvar")},
194 {std::make_pair("step", "s"), std::bind(&CliCommand::DebuggerCommand, this, "step")},
195 {std::make_pair("undisplay", "undisplay"), std::bind(&CliCommand::DebuggerCommand, this, "undisplay")},
196 {std::make_pair("watch", "wa"), std::bind(&CliCommand::WatchCommand, this, "watch")},
197 {std::make_pair("resume", "resume"), std::bind(&CliCommand::DebuggerCommand, this, "resume")},
198 {std::make_pair("showstack", "ss"), std::bind(&CliCommand::ShowstackCommand, this, "showstack")},
199 {std::make_pair("step-into", "si"), std::bind(&CliCommand::StepCommand, this, "step-into")},
200 {std::make_pair("step-out", "so"), std::bind(&CliCommand::StepCommand, this, "step-out")},
201 {std::make_pair("step-over", "sov"), std::bind(&CliCommand::StepCommand, this, "step-over")},
202 {std::make_pair("runtime-disable", "rt-disable"),
203 std::bind(&CliCommand::RuntimeCommand, this, "runtime-disable")},
204 {std::make_pair("session-new", "session-new"),
205 std::bind(&CliCommand::SessionAddCommand, this, "session-new")},
206 {std::make_pair("session-remove", "session-remove"),
207 std::bind(&CliCommand::SessionDelCommand, this, "session-remove")},
208 {std::make_pair("session-list", "session-list"),
209 std::bind(&CliCommand::SessionListCommand, this, "session-list")},
210 {std::make_pair("session", "session"),
211 std::bind(&CliCommand::SessionSwitchCommand, this, "session")},
212 {std::make_pair("success", "success"),
213 std::bind(&CliCommand::TestCommand, this, "success")},
214 {std::make_pair("fail", "fail"),
215 std::bind(&CliCommand::TestCommand, this, "fail")},
216 });
217 }
218
HeapProfilerCommand(const std::string & cmd)219 ErrCode CliCommand::HeapProfilerCommand(const std::string &cmd)
220 {
221 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
222 DomainManager &domainManager = session->GetDomainManager();
223 HeapProfilerClient &heapProfilerClient = domainManager.GetHeapProfilerClient();
224 VecStr argList = GetArgList();
225 if ((cmd == "allocationtrack" || cmd == "sampling") && !argList.empty()) {
226 std::cout << "This command does not need to follow a suffix" << std::endl;
227 return ErrCode::ERR_FAIL;
228 } else {
229 if (argList.empty()) {
230 argList.push_back("/data/");
231 std::cout << "exe success, cmd is " << cmd << std::endl;
232 } else {
233 const std::string &arg = argList[0];
234 std::string pathDump = arg;
235 std::ifstream fileExit(pathDump.c_str());
236 if (fileExit.good() && (pathDump[0] == pathDump[pathDump.size()-1]) && (GetArgList().size() == 1)) {
237 std::cout << "exe success, cmd is " << cmd << std::endl;
238 } else if (GetArgList().size() > 1) {
239 std::cout << "The folder path may contains spaces" << std::endl;
240 return ErrCode::ERR_FAIL;
241 } else if (pathDump[0] != pathDump[pathDump.size()-1]) {
242 std::cout << "The folder path format is incorrect :" << pathDump << std::endl;
243 std::cout << "Attention: Check for symbols /" << std::endl;
244 return ErrCode::ERR_FAIL;
245 } else {
246 std::cout << "The folder path does not exist :" << pathDump << std::endl;
247 return ErrCode::ERR_FAIL;
248 }
249 }
250 }
251
252 bool result = heapProfilerClient.DispatcherCmd(cmd, argList[0]);
253 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
254 }
255
CpuProfileCommand(const std::string & cmd)256 ErrCode CliCommand::CpuProfileCommand(const std::string &cmd)
257 {
258 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
259 DomainManager &domainManager = session->GetDomainManager();
260 ProfilerClient &profilerClient = domainManager.GetProfilerClient();
261 ProfilerSingleton &pro = session->GetProfilerSingleton();
262 VecStr argList = GetArgList();
263 if (cmd == "cpuprofile") {
264 if (argList.empty()) {
265 std::cout << "exe success, cmd is " << cmd << std::endl;
266 } else {
267 std::cout << "This command does not need to follow a suffix" << std::endl;
268 return ErrCode::ERR_FAIL;
269 }
270 }
271 if (cmd == "cpuprofile-show") {
272 std::cout << "exe success, cmd is " << cmd << std::endl;
273 pro.ShowCpuFile();
274 return ErrCode::ERR_OK;
275 }
276 if (cmd == "cpuprofile-setSamplingInterval") {
277 std::cout << "exe success, cmd is " << cmd << std::endl;
278 profilerClient.SetSamplingInterval(std::atoi(GetArgList()[0].c_str()));
279 }
280 if (cmd == "cpuprofile-stop" && GetArgList().size() == 1) {
281 pro.SetAddress(GetArgList()[0]);
282 const std::string &arg = argList[0];
283 std::string pathCpuPro = arg;
284 std::ifstream fileExit(pathCpuPro.c_str());
285 if (fileExit.good() && (pathCpuPro[0] == pathCpuPro[pathCpuPro.size()-1])) {
286 std::cout << "exe success, cmd is " << cmd << std::endl;
287 } else if (pathCpuPro[0] != pathCpuPro[pathCpuPro.size()-1]) {
288 std::cout << "The folder path format is incorrect :" << pathCpuPro << std::endl;
289 std::cout << "Attention: Check for symbols /" << std::endl;
290 return ErrCode::ERR_FAIL;
291 } else {
292 std::cout << "The folder path does not exist :" << pathCpuPro << std::endl;
293 return ErrCode::ERR_FAIL;
294 }
295 }
296 bool result = profilerClient.DispatcherCmd(cmd);
297 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
298 }
299
DebuggerCommand(const std::string & cmd)300 ErrCode CliCommand::DebuggerCommand(const std::string &cmd)
301 {
302 if (GetArgList().size()) {
303 OutputCommand(cmd, false);
304 return ErrCode::ERR_FAIL;
305 }
306
307 bool result = false;
308 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
309 DebuggerClient &debuggerCli = session->GetDomainManager().GetDebuggerClient();
310
311 result = debuggerCli.DispatcherCmd(cmd);
312 OutputCommand(cmd, true);
313 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
314 }
315
RuntimeCommand(const std::string & cmd)316 ErrCode CliCommand::RuntimeCommand(const std::string &cmd)
317 {
318 if (GetArgList().size()) {
319 OutputCommand(cmd, false);
320 return ErrCode::ERR_FAIL;
321 }
322 bool result = false;
323 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
324 RuntimeClient &runtimeClient = session->GetDomainManager().GetRuntimeClient();
325
326 result = runtimeClient.DispatcherCmd(cmd);
327 if (result) {
328 runtimeClient.SetObjectId("0");
329 } else {
330 return ErrCode::ERR_FAIL;
331 }
332 OutputCommand(cmd, true);
333 return ErrCode::ERR_OK;
334 }
335
BreakCommand(const std::string & cmd)336 ErrCode CliCommand::BreakCommand(const std::string &cmd)
337 {
338 bool result = false;
339 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
340 DebuggerClient &debuggerCli = session->GetDomainManager().GetDebuggerClient();
341 BreakPointManager &breakpointManager = session->GetBreakPointManager();
342 std::vector<Breaklocation> breaklist_ = breakpointManager.Getbreaklist();
343 if (GetArgList().size() == 2) { //2: two arguments
344 if (!Utils::IsNumber(GetArgList()[1])) {
345 OutputCommand(cmd, false);
346 return ErrCode::ERR_FAIL;
347 }
348 for (auto breakpoint : breaklist_) {
349 if (breakpoint.url == GetArgList()[0] &&
350 std::stoi(breakpoint.lineNumber) + 1 == std::stoi(GetArgList()[1])) {
351 std::cout << "the breakpoint is exist" << std::endl;
352 return ErrCode::ERR_FAIL;
353 }
354 }
355 debuggerCli.AddBreakPointInfo(GetArgList()[0], std::stoi(GetArgList()[1]));
356 } else {
357 OutputCommand(cmd, false);
358 return ErrCode::ERR_FAIL;
359 }
360
361 result = debuggerCli.DispatcherCmd(cmd);
362 OutputCommand(cmd, true);
363 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
364 }
365
DeleteCommand(const std::string & cmd)366 ErrCode CliCommand::DeleteCommand(const std::string &cmd)
367 {
368 bool result = false;
369 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
370 DebuggerClient &debuggerCli = session->GetDomainManager().GetDebuggerClient();
371 BreakPointManager &breakpoint = session->GetBreakPointManager();
372 if (GetArgList().size() == 1) {
373 if (!Utils::IsNumber(GetArgList()[0])) {
374 OutputCommand(cmd, false);
375 return ErrCode::ERR_FAIL;
376 }
377 int tmpNum = std::stoi(GetArgList()[0]);
378 size_t num = static_cast<size_t>(tmpNum);
379 if (breakpoint.Getbreaklist().size() >= num && num > 0) {
380 debuggerCli.AddBreakPointInfo(breakpoint.Getbreaklist()[num - 1].breakpointId, 0); // 1: breakpoinId
381 breakpoint.Deletebreaklist(num);
382 } else {
383 std::cout << "the breakpoint is not exist" << std::endl;
384 return ErrCode::ERR_FAIL;
385 }
386 } else {
387 OutputCommand(cmd, false);
388 return ErrCode::ERR_FAIL;
389 }
390 result = debuggerCli.DispatcherCmd(cmd);
391 OutputCommand(cmd, true);
392 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
393 }
394
DisplayCommand(const std::string & cmd)395 ErrCode CliCommand::DisplayCommand(const std::string &cmd)
396 {
397 if (GetArgList().size()) {
398 OutputCommand(cmd, false);
399 return ErrCode::ERR_FAIL;
400 }
401 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
402 BreakPointManager &breakpointManager = session->GetBreakPointManager();
403 breakpointManager.Show();
404 OutputCommand(cmd, true);
405 return ErrCode::ERR_OK;
406 }
407
InfosourceCommand(const std::string & cmd)408 ErrCode CliCommand::InfosourceCommand(const std::string &cmd)
409 {
410 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
411 SourceManager &sourceManager = session->GetSourceManager();
412 if (GetArgList().size() > 1) {
413 OutputCommand(cmd, false);
414 return ErrCode::ERR_FAIL;
415 } else if (GetArgList().size() == 1) {
416 if (!Utils::IsNumber(GetArgList()[0])) {
417 OutputCommand(cmd, false);
418 return ErrCode::ERR_FAIL;
419 }
420 sourceManager.GetFileSource(std::stoi(GetArgList()[0]));
421 } else {
422 sourceManager.GetFileName();
423 }
424 OutputCommand(cmd, true);
425 return ErrCode::ERR_OK;
426 }
427
ListCommand(const std::string & cmd)428 ErrCode CliCommand::ListCommand(const std::string &cmd)
429 {
430 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
431 SourceManager &sourceManager = session->GetSourceManager();
432 WatchManager &watchManager = session->GetWatchManager();
433 if (!watchManager.GetDebugState()) {
434 std::cout << "Start debugging your code before using the list command" << std::endl;
435 return ErrCode::ERR_FAIL;
436 }
437 if (GetArgList().size() > 2) { //2: two arguments
438 OutputCommand(cmd, false);
439 return ErrCode::ERR_FAIL;
440 } else if (GetArgList().size() == 2) { //2: two arguments
441 if (Utils::IsNumber(GetArgList()[0]) && Utils::IsNumber(GetArgList()[1])) {
442 sourceManager.GetListSource(GetArgList()[0], GetArgList()[1]);
443 } else {
444 OutputCommand(cmd, false);
445 return ErrCode::ERR_FAIL;
446 }
447 } else if (GetArgList().size() == 1) {
448 if (Utils::IsNumber(GetArgList()[0])) {
449 sourceManager.GetListSource(GetArgList()[0], "");
450 } else {
451 OutputCommand(cmd, false);
452 return ErrCode::ERR_FAIL;
453 }
454 } else {
455 sourceManager.GetListSource("", "");
456 }
457 OutputCommand(cmd, true);
458 return ErrCode::ERR_OK;
459 }
460
StepCommand(const std::string & cmd)461 ErrCode CliCommand::StepCommand(const std::string &cmd)
462 {
463 if (GetArgList().size()) {
464 OutputCommand(cmd, false);
465 return ErrCode::ERR_FAIL;
466 }
467 bool result = false;
468 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
469 DebuggerClient &debuggerCli = session->GetDomainManager().GetDebuggerClient();
470 RuntimeClient &runtimeClient = session->GetDomainManager().GetRuntimeClient();
471 runtimeClient.SetIsInitializeTree(true);
472 result = debuggerCli.DispatcherCmd(cmd);
473 OutputCommand(cmd, true);
474 return result ? ErrCode::ERR_OK : ErrCode::ERR_FAIL;
475 }
476
ShowstackCommand(const std::string & cmd)477 ErrCode CliCommand::ShowstackCommand(const std::string &cmd)
478 {
479 if (GetArgList().size()) {
480 OutputCommand(cmd, false);
481 return ErrCode::ERR_FAIL;
482 }
483 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
484 StackManager &stackManager = session->GetStackManager();
485 stackManager.ShowCallFrames();
486 OutputCommand(cmd, true);
487 return ErrCode::ERR_OK;
488 }
489
PrintCommand(const std::string & cmd)490 ErrCode CliCommand::PrintCommand(const std::string &cmd)
491 {
492 if (GetArgList().size() > 1) {
493 OutputCommand(cmd, false);
494 return ErrCode::ERR_FAIL;
495 }
496 bool result = false;
497 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
498 RuntimeClient &runtimeClient = session->GetDomainManager().GetRuntimeClient();
499 if (GetArgList().size() == 1) {
500 if (!Utils::IsNumber(GetArgList()[0])) {
501 return ErrCode::ERR_FAIL;
502 }
503 runtimeClient.SetIsInitializeTree(false);
504 VariableManager &variableManager = session->GetVariableManager();
505 int32_t objectId = variableManager.FindObjectIdWithIndex(std::stoi(GetArgList()[0]));
506 runtimeClient.SetObjectId(std::to_string(objectId));
507 }
508 result = runtimeClient.DispatcherCmd(cmd);
509 if (result) {
510 runtimeClient.SetObjectId("0");
511 } else {
512 return ErrCode::ERR_FAIL;
513 }
514 OutputCommand(cmd, true);
515 return ErrCode::ERR_OK;
516 }
517
WatchCommand(const std::string & cmd)518 ErrCode CliCommand::WatchCommand(const std::string &cmd)
519 {
520 if (GetArgList().size() != 1) {
521 OutputCommand(cmd, false);
522 return ErrCode::ERR_FAIL;
523 }
524 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
525 WatchManager &watchManager = session->GetWatchManager();
526 watchManager.AddWatchInfo(GetArgList()[0]);
527 if (watchManager.GetDebugState()) {
528 watchManager.SendRequestWatch(watchManager.GetWatchInfoSize() - 1, watchManager.GetCallFrameId());
529 } else {
530 std::cout << "Start debugging your code before using the watch command" << std::endl;
531 return ErrCode::ERR_FAIL;
532 }
533 OutputCommand(cmd, true);
534 return ErrCode::ERR_OK;
535 }
536
SessionAddCommand(const std::string & cmd)537 ErrCode CliCommand::SessionAddCommand([[maybe_unused]] const std::string &cmd)
538 {
539 VecStr argList = GetArgList();
540 if (argList.size() == 1) {
541 if (!SessionManager::getInstance().CreateNewSession(argList[0])) {
542 OutputCommand(cmd, true);
543 return ErrCode::ERR_OK;
544 } else {
545 std::cout << "session add failed" << std::endl;
546 }
547 } else {
548 OutputCommand(cmd, false);
549 }
550
551 return ErrCode::ERR_FAIL;
552 }
553
SessionDelCommand(const std::string & cmd)554 ErrCode CliCommand::SessionDelCommand([[maybe_unused]] const std::string &cmd)
555 {
556 VecStr argList = GetArgList();
557 if (argList.size() == 1 && Utils::IsNumber(argList[0])) {
558 uint32_t sessionId = 0;
559 if (Utils::StrToUInt(argList[0].c_str(), &sessionId)) {
560 if (sessionId == 0) {
561 std::cout << "cannot remove default session 0" << std::endl;
562 return ErrCode::ERR_OK;
563 }
564 if (SessionManager::getInstance().DelSessionById(sessionId) == 0) {
565 std::cout << "session remove success" << std::endl;
566 return ErrCode::ERR_OK;
567 } else {
568 std::cout << "sessionId is not exist" << std::endl;
569 }
570 }
571 } else {
572 OutputCommand(cmd, false);
573 }
574
575 return ErrCode::ERR_FAIL;
576 }
577
SessionListCommand(const std::string & cmd)578 ErrCode CliCommand::SessionListCommand([[maybe_unused]] const std::string &cmd)
579 {
580 if (GetArgList().size()) {
581 OutputCommand(cmd, false);
582 return ErrCode::ERR_FAIL;
583 }
584 SessionManager::getInstance().SessionList();
585 OutputCommand(cmd, true);
586 return ErrCode::ERR_OK;
587 }
588
SessionSwitchCommand(const std::string & cmd)589 ErrCode CliCommand::SessionSwitchCommand([[maybe_unused]] const std::string &cmd)
590 {
591 VecStr argList = GetArgList();
592 if (argList.size() == 1 && Utils::IsNumber(argList[0])) {
593 uint32_t sessionId = 0;
594 if (Utils::StrToUInt(argList[0].c_str(), &sessionId)) {
595 if (SessionManager::getInstance().SessionSwitch(sessionId) == 0) {
596 OutputCommand(cmd, true);
597 return ErrCode::ERR_OK;
598 } else {
599 std::cout << "sessionId is not exist" << std::endl;
600 }
601 }
602 } else {
603 OutputCommand(cmd, false);
604 }
605 return ErrCode::ERR_FAIL;
606 }
607
TestCommand(const std::string & cmd)608 ErrCode CliCommand::TestCommand(const std::string &cmd)
609 {
610 if (cmd == "success" || cmd == "fail") {
611 Session *session = SessionManager::getInstance().GetSessionById(sessionId_);
612 TestClient &testClient = session->GetDomainManager().GetTestClient();
613 testClient.DispatcherCmd(cmd);
614 } else {
615 return ErrCode::ERR_FAIL;
616 }
617 return ErrCode::ERR_OK;
618 }
619
SaveAllPossibleBreakpointsCommand(const std::string & cmd)620 ErrCode CliCommand::SaveAllPossibleBreakpointsCommand(const std::string &cmd)
621 {
622 return BreakCommand(cmd);
623 }
624
ExecHelpCommand()625 ErrCode CliCommand::ExecHelpCommand()
626 {
627 std::cout << HELP_MSG;
628 return ErrCode::ERR_OK;
629 }
630
OutputCommand(const std::string & cmd,bool flag)631 void CliCommand::OutputCommand(const std::string &cmd, bool flag)
632 {
633 if (flag) {
634 std::cout << "exe success, cmd is " << cmd << std::endl;
635 } else {
636 std::cout << cmd << " parameters is incorrect" << std::endl;
637 }
638 }
639
OnCommand()640 ErrCode CliCommand::OnCommand()
641 {
642 std::map<StrPair, std::function<ErrCode()>>::iterator it;
643 StrPair cmdPair;
644 bool haveCmdFlag = false;
645
646 for (it = commandMap_.begin(); it != commandMap_.end(); it++) {
647 cmdPair = it->first;
648 if (!strcmp(cmdPair.first.c_str(), cmd_.c_str())
649 ||!strcmp(cmdPair.second.c_str(), cmd_.c_str())) {
650 auto respond = it->second;
651 return respond();
652 }
653 }
654
655 for (unsigned int i = 0; i < cmdList.size(); i++) {
656 if (!strncmp(cmdList[i].c_str(), cmd_.c_str(), std::strlen(cmd_.c_str()))) {
657 haveCmdFlag = true;
658 std::cout << cmdList[i] << " ";
659 }
660 }
661
662 if (haveCmdFlag) {
663 std::cout << std::endl;
664 } else {
665 ExecHelpCommand();
666 }
667
668 return ErrCode::ERR_FAIL;
669 }
670 } // namespace OHOS::ArkCompiler::Toolchain
671