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