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 #ifndef ECMASCRIPT_TOOLING_CLIENT_ARK_CLI_CLI_COMMAND_H
17 #define ECMASCRIPT_TOOLING_CLIENT_ARK_CLI_CLI_COMMAND_H
18 
19 #include <cstdlib>
20 #include <functional>
21 #include <map>
22 #include <vector>
23 
24 #include "common/log_wrapper.h"
25 #include "tooling/client/domain/heapprofiler_client.h"
26 #include "tooling/client/domain/profiler_client.h"
27 #include "tooling/client/manager/domain_manager.h"
28 #include "tooling/utils/utils.h"
29 #include "tooling/client/session/session.h"
30 
31 namespace OHOS::ArkCompiler::Toolchain {
32 using StrPair = std::pair<std::string, std::string>;
33 using VecStr = std::vector<std::string>;
34 
35 enum class ErrCode : uint8_t {
36     ERR_OK   = 0,
37     ERR_FAIL = 1
38 };
39 
40 class CliCommand {
41 public:
CliCommand(std::vector<std::string> cliCmdStr, uint32_t sessionId)42     CliCommand(std::vector<std::string> cliCmdStr, uint32_t sessionId)
43         : cmd_(cliCmdStr[0]), sessionId_(sessionId)
44     {
45         for (size_t i = 1u; i < cliCmdStr.size(); i++) {
46             argList_.push_back(cliCmdStr[i]);
47         }
48     }
49 
50     ~CliCommand() = default;
51 
52     ErrCode OnCommand();
53     ErrCode ExecCommand();
54     void CreateCommandMap();
55     void CreateOtherCommandMap();
56     ErrCode HeapProfilerCommand(const std::string &cmd);
57     ErrCode DebuggerCommand(const std::string &cmd);
58     ErrCode CpuProfileCommand(const std::string &cmd);
59     ErrCode RuntimeCommand(const std::string &cmd);
60     ErrCode BreakCommand(const std::string &cmd);
61     ErrCode DeleteCommand(const std::string &cmd);
62     ErrCode StepCommand(const std::string &cmd);
63     ErrCode ShowstackCommand(const std::string &cmd);
64     ErrCode DisplayCommand(const std::string &cmd);
65     ErrCode InfosourceCommand(const std::string &cmd);
66     ErrCode ListCommand(const std::string &cmd);
67     ErrCode PrintCommand(const std::string &cmd);
68     ErrCode WatchCommand(const std::string &cmd);
69     ErrCode SessionAddCommand(const std::string &cmd);
70     ErrCode SessionDelCommand(const std::string &cmd);
71     ErrCode SessionListCommand(const std::string &cmd);
72     ErrCode SessionSwitchCommand(const std::string &cmd);
73     ErrCode TestCommand(const std::string &cmd);
74     ErrCode ExecHelpCommand();
75     void OutputCommand(const std::string &cmd, bool flag);
76 
GetArgList()77     VecStr GetArgList()
78     {
79         return argList_;
80     }
81 
82 private:
83     std::string cmd_ ;
84     VecStr argList_ {};
85     std::map<StrPair, std::function<ErrCode()>> commandMap_;
86     std::string resultReceiver_ = "";
87     uint32_t sessionId_;
88 };
89 } // namespace OHOS::ArkCompiler::Toolchain
90 
91 #endif // ECMASCRIPT_TOOLING_CLIENT_ARK_CLI_CLI_COMMAND_H