1/*
2 * Copyright (c) 2021-2024 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 OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
17#define OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
18
19#include <regex>
20
21#include "ability_manager_interface.h"
22#include "shell_command.h"
23
24namespace OHOS {
25namespace AAFwk {
26namespace {
27using ParametersInteger = std::map<std::string, int>;
28using ParametersString = std::map<std::string, std::string>;
29using ParametersBool = std::map<std::string, bool>;
30
31const std::string TOOL_NAME = "aa";
32
33// not show in command
34// process -- debug ability with options
35const std::string HELP_MSG = "usage: aa <command> <options>\n"
36    "These are common aa commands list:\n"
37    "  help                        list available commands\n"
38    "  start                       start ability with options\n"
39    "  stop-service                stop service with options\n"
40    "  dump                        dump the ability info\n"
41    "  force-stop <bundle-name>    force stop the process with bundle name\n"
42    "  attach                      attach application to enter debug mode\n"
43    "  detach                      detach application to exit debug mode\n"
44#ifdef ABILITY_COMMAND_FOR_TEST
45    "  test                        start the test framework with options\n"
46    "  ApplicationNotResponding     Pass in pid with options\n"
47#else
48    "  test                        start the test framework with options\n"
49    "  appdebug                    set / cancel / get waiting debug status\n"
50    "  process                     debug and tune\n";
51#endif
52
53const std::string HELP_ApplicationNotResponding =
54    "usage: aa ApplicationNotResponding <options>\n"
55    "options list:\n"
56    "  -h, --help                   list available commands\n"
57    "  -p, --pid                    Pass in pid with option\n";
58
59// not show in command
60// [-N] [-p <perf-cmd>]
61const std::string HELP_MSG_START =
62    "usage: aa start <options>\n"
63    "options list:\n"
64    "  -h, --help                                                   list available commands\n"
65    "  [-d <device-id>] [-a <ability-name> -b <bundle-name>] [-m <module-name>] [-p <perf-cmd>] [-D] [-E] [-S] [-N]"
66    "  [-R] [--ps <key> <string-value>] "
67    "  [--pi <key> <integer-value>] "
68    "  [--pb <key> <boolean-value>] "
69    "  [--psn <key>] "
70    "  [-A <action-name>] "
71    "  [-U <URI>] "
72    "  [-e <entity>] "
73    "  [-t <mime-type>] "
74    "  [--wl <window-left>] "
75    "  [--wt <window-top>] "
76    "  [--wh <window-height>] "
77    "  [--ww <window-width>] "
78    "  start ability with an element name\n";
79
80const std::string HELP_MSG_STOP_SERVICE =
81    "usage: aa stop-service <options>\n"
82    "options list:\n"
83    "  -h, --help                                                   list available commands\n"
84    "  [-d <device-id>] -a <ability-name> -b <bundle-name> [-m <module-name>] "
85    "  stop service with an element name\n";
86
87const std::string HELP_MSG_DUMPSYS = "usage: aa dump <options>\n"
88    "options list:\n"
89    "  -h, --help                   list available commands\n"
90    "  -a, --all                    dump all abilities\n"
91    "  -l, --mission-list           dump mission list\n"
92    "  -i, --ability                dump abilityRecordId\n"
93    "  -e, --extension              dump elementName (FA: serviceAbilityRecords,"
94    "Stage: ExtensionRecords)\n"
95    "  -p, --pending                dump pendingWantRecordId\n"
96    "  -r, --process                dump process\n"
97    "  -d, --data                   dump the data abilities\n"
98    "  -u, --userId                 userId\n"
99    "  -c, --client                 client\n"
100    "  -c, -u are auxiliary parameters and cannot be used alone\n"
101    "  The original -s parameter is invalid\n"
102    "  The original -m parameter is invalid\n";
103
104const std::string HELP_MSG_PROCESS = "usage: aa process <options>\n"
105    "options list:\n"
106    "  -h, --help                   list available commands\n"
107    "  -a <ability-name> -b <bundle-name> [-m <module-name>]\n"
108    "  -p <perf-cmd>                performance optimization command. Either -p or -D must be selected, "
109    "-p takes precedence.\n"
110    "  -D <debug-cmd>               debug command. Either -p or -D must be selected, -p takes precedence.\n"
111    "  [-S]\n"
112    "  debug ability with an element name\n";
113
114const std::string HELP_MSG_TEST =
115    "usage: aa test <options>\n"
116    "options list:\n"
117    "  -h, --help                                             list available commands\n"
118    "  -b <bundle-name> -s unittest <test-runner>             start the test framework with options\n"
119    "                  [-p <package-name>]                    the name of package with test-runner, "
120    "required for the FA model\n"
121    "                  [-m <module-name>]                     the name of module with test-runner, "
122    "required for the STAGE model\n"
123    "                  [-s class <test-class>]\n"
124    "                  [-s level <test-level>]\n"
125    "                  [-s size <test-size>]\n"
126    "                  [-s testType <test-testType>]\n"
127    "                  [-s timeout <test-timeout>]\n"
128    "                  [-s <any-key> <any-value>]\n"
129    "                  [-w <wait-time>]\n"
130    "                  [-D]\n";
131
132const std::string HELP_MSG_ATTACH_APP_DEBUG =
133    "usage: aa attach <options>\n"
134    "options list:\n"
135    "  -h, --help                                             list available commands\n"
136    "  -b <bundle-name>                                       let application enter debug mode by bundle name\n";
137const std::string HELP_MSG_DETACH_APP_DEBUG =
138    "usage: aa detach <options>\n"
139    "options list:\n"
140    "  -h, --help                                             list available commands\n"
141    "  -b <bundle-name>                                       let application exit debug mode by bundle name\n";
142
143const std::string HELP_MSG_APPDEBUG_APP_DEBUG =
144    "usage: aa appdebug <options>\n"
145    "options list:\n"
146    "  -h, --help                                  list available commands\n"
147    "  -b, --bundlename <bundle-name>              let application set wait debug mode by bundle name with options\n"
148    "                  [-p, --persist]             option: persist flag\n"
149    "  -c, --cancel                                let application cancel wait debug\n"
150    "  -g, --get                                   get wait debug mode application bundle name and persist flag\n";
151
152const std::string HELP_MSG_FORCE_STOP = "usage: aa force-stop <bundle-name> [-p pid] [-r kill-reason]\n";
153const std::string HELP_MSG_FORCE_TIMEOUT =
154    "usage: aa force-timeout <ability-name> <INITIAL|INACTIVE|COMMAND|FOREGROUND|BACKGROUND|TERMINATING>\n"
155    "usage: aa force-timeout clean.";
156const std::string HELP_MSG_FORCE_TIMEOUT_CLEAN = "clean";
157
158const std::string HELP_MSG_NO_ABILITY_NAME_OPTION = "error: -a <ability-name> is expected";
159const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: -b <bundle-name> is expected";
160
161const std::string STRING_START_ABILITY_OK = "start ability successfully.";
162const std::string STRING_START_ABILITY_NG = "error: failed to start ability.";
163
164const std::string STRING_STOP_SERVICE_ABILITY_OK = "stop service ability successfully.";
165const std::string STRING_STOP_SERVICE_ABILITY_NG = "error: failed to stop service ability.";
166
167const std::string STRING_FORCE_STOP_OK = "force stop process successfully.";
168const std::string STRING_FORCE_STOP_NG = "error: failed to force stop process.";
169
170const std::string STRING_ATTACH_APP_DEBUG_OK = "attach app debug successfully.";
171const std::string STRING_ATTACH_APP_DEBUG_NG = "error: failed to attach app debug.";
172
173const std::string STRING_DETACH_APP_DEBUG_OK = "detach app debug successfully.";
174const std::string STRING_DETACH_APP_DEBUG_NG = "error: failed to detach app debug.";
175
176const std::string STRING_START_USER_TEST_NG = "error: failed to start user test.";
177const std::string STRING_USER_TEST_STARTED = "user test started.";
178const std::string STRING_USER_TEST_FINISHED = "user test finished.";
179
180const std::string STRING_BLOCK_AMS_SERVICE_OK = "block ams service successfully.";
181const std::string STRING_BLOCK_AMS_SERVICE_NG = "error: failed to block ams service.";
182
183const std::string STRING_APP_DEBUG_OK = "app debug successfully.";
184const std::string STRING_APP_DEBUG_NG = "error: failed to app debug.";
185
186const std::string STRING_START_NATIVE_PROCESS_OK = "start native process successfully.";
187const std::string STRING_START_NATIVE_PROCESS_NG = "error: failed to start native process.";
188
189const int USER_TEST_COMMAND_START_INDEX = 2;
190const int USER_TEST_COMMAND_PARAMS_NUM = 2;
191const int TIME_RATE_MS = 1000;
192const std::string STRING_FORCE_TIMEOUT_OK = "force ability timeout successfully.";
193const std::string STRING_FORCE_TIMEOUT_NG = "error: failed to force ability timeout.";
194
195const int NUMBER_TWO = 2;
196const int NUMBER_ONE = 1;
197
198const std::string DEBUG_VALUE = "true";
199
200const std::string PERFCMD_FIRST_PROFILE = "profile";
201const std::string PERFCMD_FIRST_DUMPHEAP = "dumpheap";
202
203const std::string STRING_TEST_REGEX_INTEGER_NUMBERS = "^(0|[1-9][0-9]*|-[1-9][0-9]*)$";
204const std::string STRING_REGEX_ALL_NUMBERS = "^(-)?([0-9]|[1-9][0-9]+)([\\.][0-9]+)?$";
205}  // namespace
206
207class AbilityManagerShellCommand : public ShellCommand {
208public:
209    AbilityManagerShellCommand(int argc, char* argv[]);
210    ~AbilityManagerShellCommand() override
211    {}
212
213    ErrCode CreateMessageMap() override;
214    bool IsTestCommandIntegrity(const std::map<std::string, std::string>& params);
215    ErrCode StartUserTest(const std::map<std::string, std::string>& params);
216
217private:
218    ErrCode CreateCommandMap() override;
219    ErrCode init() override;
220
221    ErrCode RunAsHelpCommand();
222    ErrCode RunAsStartAbility();
223    ErrCode RunAsStopService();
224    ErrCode RunAsDumpsysCommand();
225    ErrCode RunAsForceStop();
226    bool SwitchOptionForAppDebug(int32_t option, std::string &bundleName, bool &isPersist, bool &isCancel, bool &isGet);
227    bool ParseAppDebugParameter(std::string &bundleName, bool &isPersist, bool &isCancel, bool &isGet);
228    ErrCode RunAsAppDebugDebugCommand();
229    ErrCode RunAsProcessCommand();
230    ErrCode RunAsAttachDebugCommand();
231    ErrCode RunAsDetachDebugCommand();
232    bool CheckParameters(int target);
233    ErrCode ParseParam(ParametersInteger& pi);
234    ErrCode ParseParam(ParametersString& ps, bool isNull);
235    ErrCode ParseParam(ParametersBool& pb);
236    void SetParams(const ParametersInteger& pi, Want& want);
237    void SetParams(const ParametersString& ps, Want& want);
238    void SetParams(const ParametersBool& pb, Want& want);
239    Reason CovertExitReason(std::string& reasonStr);
240    pid_t ConvertPid(std::string& inputPid);
241
242#ifdef ABILITY_COMMAND_FOR_TEST
243    ErrCode RunForceTimeoutForTest();
244    ErrCode RunAsSendAppNotRespondingProcessID();
245    ErrCode RunAsSendAppNotRespondingWithUnknownOption();
246    ErrCode RunAsSendAppNotRespondingWithOption(int32_t option, std::string& pid);
247#endif
248#ifdef ABILITY_FAULT_AND_EXIT_TEST
249    ErrCode RunAsForceExitAppCommand();
250    ErrCode RunAsNotifyAppFaultCommand();
251#endif
252    sptr<IAbilityManager> GetAbilityManagerService();
253
254    ErrCode MakeWantFromCmd(Want& want, std::string& windowMode);
255    ErrCode MakeWantForProcess(Want& want);
256    ErrCode RunAsTestCommand();
257    ErrCode TestCommandError(const std::string& info);
258    bool MatchOrderString(const std::regex &r, const std::string &orderCmd);
259    bool CheckPerfCmdString(const char* optarg, const size_t paramLength, std::string &perfCmd);
260    void ParseBundleName(std::string &bundleName);
261};
262}  // namespace AAFwk
263}  // namespace OHOS
264
265#endif  // OHOS_ABILITY_RUNTIME_ABILITY_COMMAND_H
266