diff --git a/tools/aa/BUILD.gn b/tools/aa/BUILD.gn index 625017bad2..7b55a42e6b 100644 --- a/tools/aa/BUILD.gn +++ b/tools/aa/BUILD.gn @@ -19,7 +19,9 @@ config("ability_command_config") { "include", "//foundation/distributedschedule/dmsfwk/services/dtbschedmgr/include", "//foundation/aafwk/standard/interfaces/innerkits/app_manager/include/appmgr", - "//utils/system/safwk/native/include", + "//foundation/aafwk/standard/services/abilitymgr/include", + "//foundation/appexecfwk/standard/interfaces/innerkits/appexecfwk_base/include", + "//foundation/distributedschedule/samgr/interfaces/innerkits/samgr_proxy/include", "//third_party/jsoncpp/include", ] } @@ -73,12 +75,7 @@ ohos_executable("aa") { cflags += [ "-DBINDER_IPC_32BIT" ] } - deps = [ - ":tools_aa_source_set", - "${appexecfwk_path}/interfaces/innerkits/appexecfwk_base:appexecfwk_base", - "${innerkits_path}/want:want", - "//utils/native/base:utils", - ] + deps = [ ":tools_aa_source_set" ] external_deps = [ "hiviewdfx_hilog_native:libhilog", diff --git a/tools/aa/include/ability_command.h b/tools/aa/include/ability_command.h index b6ad7dd126..ce7f301c3d 100644 --- a/tools/aa/include/ability_command.h +++ b/tools/aa/include/ability_command.h @@ -31,7 +31,8 @@ const std::string HELP_MSG = "usage: aa \n" " stop-service stop service with options\n" " dump dump the ability info\n" " force-stop force stop the process with bundle name\n" - " test start the test framework with options\n"; + " test start the test framework with options\n" + " anr Pass in pid with options\n"; const std::string HELP_MSG_SCREEN = "usage: aa screen \n" @@ -39,6 +40,12 @@ const std::string HELP_MSG_SCREEN = " -h, --help list available commands\n" " -p, --power power on or off with a state name\n"; +const std::string HELP_ANR = + "usage: aa anr \n" + "options list:\n" + " -h, --help list available commands\n" + " -p, --pid anr SendANRProcessID\n"; + const std::string HELP_MSG_START = "usage: aa start \n" "options list:\n" @@ -136,6 +143,8 @@ private: ErrCode RunAsDumpCommand(); ErrCode RunAsDumpsysCommand(); ErrCode RunAsForceStop(); + ErrCode RunAsSendANRProcessID(); + sptr GetAbilityManagerService(); ErrCode RunAsDumpCommandOptopt(); ErrCode MakeWantFromCmd(Want &want, std::string &windowMode); diff --git a/tools/aa/src/ability_command.cpp b/tools/aa/src/ability_command.cpp index 849f595f4c..f61e17a256 100644 --- a/tools/aa/src/ability_command.cpp +++ b/tools/aa/src/ability_command.cpp @@ -17,10 +17,14 @@ #include #include "ability_manager_client.h" +#include "iservice_registry.h" #include "mission_snapshot.h" #include "hilog_wrapper.h" #include "ohos/aafwk/base/bool_wrapper.h" #include "test_observer.h" +#include "sa_mgr_client.h" +#include "system_ability_definition.h" +#include using namespace OHOS::AppExecFwk; @@ -38,7 +42,12 @@ const struct option LONG_OPTIONS[] = { {"debug", no_argument, nullptr, 'D'}, {nullptr, 0, nullptr, 0}, }; - +const std::string SHORT_OPTIONS_ANR = "hp:"; +const struct option LONG_OPTIONS_ANR[] = { + {"help", no_argument, nullptr, 'h'}, + {"pid", required_argument, nullptr, 'p'}, + {nullptr, 0, nullptr, 0}, +}; const std::string SHORT_OPTIONS_DUMP = "has:m:lud::e::LS"; const struct option LONG_OPTIONS_DUMP[] = { {"help", no_argument, nullptr, 'h'}, @@ -87,6 +96,7 @@ ErrCode AbilityManagerShellCommand::CreateCommandMap() {"dump", std::bind(&AbilityManagerShellCommand::RunAsDumpsysCommand, this)}, {"force-stop", std::bind(&AbilityManagerShellCommand::RunAsForceStop, this)}, {"test", std::bind(&AbilityManagerShellCommand::RunAsTestCommand, this)}, + {"anr", std::bind(&AbilityManagerShellCommand::RunAsSendANRProcessID, this)}, }; return OHOS::ERR_OK; @@ -1287,5 +1297,107 @@ ErrCode AbilityManagerShellCommand::StartUserTest(const std::map AbilityManagerShellCommand::GetAbilityManagerService() +{ + sptr systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (systemManager == nullptr) { + HILOG_ERROR("Fail to get registry."); + return nullptr; + } + sptr remoteObject = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID); + return iface_cast(remoteObject); +} + +ErrCode AbilityManagerShellCommand::RunAsSendANRProcessID() +{ + static sptr abilityMs_; + std::string pid = ""; + int option = -1; + ErrCode result = OHOS::ERR_OK; + option = getopt_long(argc_, argv_, SHORT_OPTIONS_ANR.c_str(), LONG_OPTIONS_ANR, nullptr); + HILOG_INFO("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + if ( strcmp(argv_[optind], cmd_.c_str()) == 0) { + HILOG_INFO("'aa %{public}s' %{public}s", HELP_ANR.c_str(), cmd_.c_str()); + result = OHOS::ERR_INVALID_VALUE; + } + } + else if (option == '?') { + switch (optopt) { + case 'h': { + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'p': { + HILOG_INFO("'aa anr -p' with no argument."); + resultReceiver_.append("error: option -p "); + resultReceiver_.append("' requires a value.\n"); + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 0: { + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + + HILOG_INFO("'aa anr' with an unknown option."); + + resultReceiver_.append(unknownOptionMsg); + result = OHOS::ERR_INVALID_VALUE; + break; + } + default: { + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + + HILOG_INFO("'aa anr' with an unknown option."); + + resultReceiver_.append(unknownOptionMsg); + result = OHOS::ERR_INVALID_VALUE; + break; + } + } + } + else { + switch (option) { + case 'h': { + result = OHOS::ERR_INVALID_VALUE; + break; + } + case 'p':{ + HILOG_INFO("aa anr 'aa %{public}s' -p process.", cmd_.c_str()); + HILOG_INFO("aa anr 'aa optarg = %{public}s'.", optarg); + pid = optarg; + HILOG_INFO("aa anr 'aa pid = %{public}s'.", pid.c_str()); + break; + } + case 0: { + HILOG_INFO("'aa %{public}s' with an unknown option.", cmd_.c_str()); + result = OHOS::ERR_INVALID_VALUE; + break; + } + default: { + HILOG_INFO("'aa %{public}s' with an unknown option.", cmd_.c_str()); + result = OHOS::ERR_INVALID_VALUE; + break; + } + } + } + + if (result == OHOS::ERR_OK) { + HILOG_INFO("'aa pid = %{public}d'.", atoi(pid.c_str())); + abilityMs_ = GetAbilityManagerService(); + if(abilityMs_ == nullptr){ + std::cout << "abilityMsObj is nullptr"; + } + abilityMs_->SendANRProcessID(atoi(pid.c_str())); + }else{ + resultReceiver_.append(HELP_ANR+ "\n"); + result = OHOS::ERR_INVALID_VALUE; + } + return result; +} } // namespace AAFwk } // namespace OHOS