ANR test code

Signed-off-by: wanchengzhen <wanchengzhen@huawei.com>
This commit is contained in:
wanchengzhen 2022-02-17 19:35:08 +00:00
parent fb55a34a2f
commit a746d1f267
3 changed files with 127 additions and 9 deletions

View File

@ -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",

View File

@ -31,7 +31,8 @@ const std::string HELP_MSG = "usage: aa <command> <options>\n"
" stop-service stop service with options\n"
" dump dump the ability info\n"
" force-stop <bundle-name> 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 <options>\n"
@ -39,6 +40,12 @@ const std::string HELP_MSG_SCREEN =
" -h, --help list available commands\n"
" -p, --power <state> power on or off with a state name\n";
const std::string HELP_ANR =
"usage: aa anr <options>\n"
"options list:\n"
" -h, --help list available commands\n"
" -p, --pid anr SendANRProcessID\n";
const std::string HELP_MSG_START =
"usage: aa start <options>\n"
"options list:\n"
@ -136,6 +143,8 @@ private:
ErrCode RunAsDumpCommand();
ErrCode RunAsDumpsysCommand();
ErrCode RunAsForceStop();
ErrCode RunAsSendANRProcessID();
sptr<IAbilityManager> GetAbilityManagerService();
ErrCode RunAsDumpCommandOptopt();
ErrCode MakeWantFromCmd(Want &want, std::string &windowMode);

View File

@ -17,10 +17,14 @@
#include <getopt.h>
#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 <stdlib.h>
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<std::string, st
return result;
}
sptr<IAbilityManager> AbilityManagerShellCommand::GetAbilityManagerService()
{
sptr<ISystemAbilityManager> systemManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (systemManager == nullptr) {
HILOG_ERROR("Fail to get registry.");
return nullptr;
}
sptr<IRemoteObject> remoteObject = systemManager->GetSystemAbility(ABILITY_MGR_SERVICE_ID);
return iface_cast<IAbilityManager>(remoteObject);
}
ErrCode AbilityManagerShellCommand::RunAsSendANRProcessID()
{
static sptr<IAbilityManager> 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