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