!760 ANR修复注册机制

Merge pull request !760 from wanchengzhen/update_master_0217_2
This commit is contained in:
openharmony_ci
2022-02-18 07:04:02 +00:00
committed by Gitee
22 changed files with 201 additions and 47 deletions
@@ -278,9 +278,9 @@ public:
return true;
}
bool SendANRProcessID(int pid) override
int SendANRProcessID(int pid) override
{
return true;
return 0;
}
int RegisterSnapshotHandler(const sptr<ISnapshotHandler>& handler) override
@@ -276,9 +276,9 @@ public:
return 0;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
@@ -1374,13 +1374,17 @@ void MainThread::HandleANRProcess(int sigMessage)
{
if (sigMessage == SIGUSR1) {
int rFD = -1;
std::string mainThreadStackInfo;
if ((rFD = RequestFileDescriptor(int32_t(FaultLoggerType::CPP_CRASH))) < 0) {
APP_LOGE("MainThread::HandleANRProcess request file eescriptor failed");
return;
}
auto jsRuntime = std::move((std::unique_ptr<AbilityRuntime::JsRuntime>&)applicationForAnr_->GetRuntime());
std::string mainThreadStackInfo = jsRuntime->BuildNativeAndJsBackStackTrace();
if (write(rFD, mainThreadStackInfo.c_str(), mainThreadStackInfo.size()) != mainThreadStackInfo.size()) {
APP_LOGE("MainThread::HandleANRProcess write main thread stack info failed");
if (jsRuntime != nullptr) {
mainThreadStackInfo= jsRuntime->BuildNativeAndJsBackStackTrace();
if (write(rFD, mainThreadStackInfo.c_str(), mainThreadStackInfo.size()) != mainThreadStackInfo.size()) {
APP_LOGE("MainThread::HandleANRProcess write main thread stack info failed");
}
}
OHOS::HiviewDFX::DfxDumpCatcher dumplog;
std::string proStackInfo;
@@ -1416,6 +1420,14 @@ void MainThread::Start()
return;
}
APP_LOGI("MainThread::main Register sig handle start");
struct sigaction sigAct;
sigemptyset(&sigAct.sa_mask);
sigAct.sa_flags = 0;
sigAct.sa_handler = &MainThread::HandleANRProcess;
sigaction(SIGUSR1, &sigAct, NULL);
APP_LOGI("MainThread::main Register sig handle end");
APP_LOGI("MainThread::main called start Init");
thread->Init(runner, runnerWatchDog);
APP_LOGI("MainThread::main called end Init");
@@ -239,9 +239,9 @@ public:
{
return true;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
@@ -260,9 +260,9 @@ public:
{
return true;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
@@ -772,6 +772,14 @@ public:
virtual int SetAbilityController(const sptr<AppExecFwk::IAbilityController> &abilityController,
bool imAStabilityTest);
/**
* Send not response process ID to ability manager service.
*
* @param pid The not response process ID.
* @return Returns ERR_OK on success, others on failure.
*/
ErrCode SendANRProcessID(int pid);
private:
static std::mutex mutex_;
static std::shared_ptr<AbilityManagerClient> instance_;
@@ -375,6 +375,11 @@ enum {
* Result() for start user test fail.
*/
START_USER_TEST_FAIL,
/**
* Result(2097220) for send usr1 sig to the process of not response fail.
*/
SEND_USR1_SIG_FAIL,
};
enum {
@@ -703,9 +703,11 @@ public:
/**
* Send not response process ID to ability manager service.
*
* @param pid The not response process ID.
* @return Returns ERR_OK on success, others on failure.
*/
virtual bool SendANRProcessID(int pid) = 0;
virtual int SendANRProcessID(int pid) = 0;
enum {
// ipc id 1-1000 for kit
@@ -665,9 +665,11 @@ public:
/**
* Send not response process ID to ability manager service.
*
* @param pid The not response process ID.
* @return Returns ERR_OK on success, others on failure.
*/
virtual bool SendANRProcessID(int pid) override;
virtual int SendANRProcessID(int pid) override;
private:
template <typename T>
@@ -860,8 +860,9 @@ public:
/**
* Send not response process ID to ability manager service.
* @param pid The not response process ID.
* @return Returns ERR_OK on success, others on failure.
*/
virtual bool SendANRProcessID(int pid) override;
virtual int SendANRProcessID(int pid) override;
// MSG 0 - 20 represents timeout message
static constexpr uint32_t LOAD_TIMEOUT_MSG = 0;
@@ -948,5 +948,13 @@ ErrCode AbilityManagerClient::SetAbilityController(const sptr<AppExecFwk::IAbili
sptr<IAbilityManager> abms = iface_cast<IAbilityManager>(remoteObject_);
return abms->SetAbilityController(abilityController, imAStabilityTest);
}
ErrCode AbilityManagerClient::SendANRProcessID(int pid)
{
CHECK_REMOTE_OBJECT_AND_RETURN(remoteObject_, ABILITY_SERVICE_NOT_CONNECTED);
sptr<IAbilityManager> abms = iface_cast<IAbilityManager>(remoteObject_);
return abms->SendANRProcessID(pid);
}
} // namespace AAFwk
} // namespace AAFwk
@@ -2592,25 +2592,24 @@ int AbilityManagerProxy::DoAbilityBackground(const sptr<IRemoteObject> &token, u
return reply.ReadInt32();
}
bool AbilityManagerProxy::SendANRProcessID(int pid)
int AbilityManagerProxy::SendANRProcessID(int pid)
{
int error;
MessageParcel data;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(data)) {
return false;
return INNER_ERR;
}
if (!data.WriteInt32(pid)) {
HILOG_ERROR("pid WriteInt32 fail.");
return false;
return ERR_INVALID_VALUE;
}
error = Remote()->SendRequest(IAbilityManager::SEND_APP_NOT_RESPONSE_PROCESS_ID, data, reply, option);
auto error = Remote()->SendRequest(IAbilityManager::SEND_APP_NOT_RESPONSE_PROCESS_ID, data, reply, option);
if (error != NO_ERROR) {
HILOG_ERROR("SendANRProcessID error: %d", error);
return false;
return error;
}
return reply.ReadBool();
return reply.ReadInt32();
}
} // namespace AAFwk
} // namespace OHOS
@@ -4005,7 +4005,7 @@ int AbilityManagerService::SetAbilityController(const sptr<IAbilityController> &
return ERR_OK;
}
bool AbilityManagerService::SendANRProcessID(int pid)
int AbilityManagerService::SendANRProcessID(int pid)
{
int anrTimeOut = amsConfigResolver_->GetANRTimeOutTime();
auto timeoutTask = [pid]() {
@@ -4013,11 +4013,12 @@ bool AbilityManagerService::SendANRProcessID(int pid)
HILOG_ERROR("Kill app not response process failed");
}
};
handler_->PostTask(timeoutTask, "TIME_OUT_TASK", anrTimeOut);
if (kill(pid, SIGUSR1) != ERR_OK) {
HILOG_ERROR("Send sig to app not response process failed");
return SEND_USR1_SIG_FAIL;
}
handler_->PostTask(timeoutTask, "TIME_OUT_TASK", anrTimeOut);
return true;
return ERR_OK;
}
bool AbilityManagerService::IsRunningInStabilityTest()
@@ -1505,8 +1505,8 @@ int AbilityManagerStub::DoAbilityBackgroundInner(MessageParcel &data, MessagePar
int AbilityManagerStub::SendANRProcessIDInner(MessageParcel &data, MessageParcel &reply)
{
int32_t pid = data.ReadInt32();
bool result = SendANRProcessID(pid);
if (!reply.WriteBool(result)) {
int32_t result = SendANRProcessID(pid);
if (!reply.WriteInt32(result)) {
HILOG_ERROR("reply write failed.");
return ERR_INVALID_VALUE;
}
@@ -375,9 +375,9 @@ public:
return true;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
MOCK_METHOD2(TerminateAbilityByCaller, int(const sptr<IRemoteObject> &callerToken, int requestCode));
@@ -426,9 +426,9 @@ public:
{
return true;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
@@ -865,9 +865,9 @@ public:
{
return 0;
}
virtual bool SendANRProcessID(int pid)
virtual int SendANRProcessID(int pid)
{
return true;
return 0;
}
virtual int StartAbilityByCall(
const Want &want, const sptr<IAbilityConnection> &connect, const sptr<IRemoteObject> &callerToken) override
@@ -192,9 +192,9 @@ public:
return true;
}
virtual bool SendANRProcessID(int pid) override
virtual int SendANRProcessID(int pid) override
{
return true;
return 0;
}
virtual int StartUserTest(const Want &want, const sptr<IRemoteObject> &observer) override
+4 -7
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",
+10 -1
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"
" ApplicationNotRespondin 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_ApplicationNotRespondin =
"usage: aa ApplicationNotRespondin <options>\n"
"options list:\n"
" -h, --help list available commands\n"
" -p, --pid Pass in pid with option\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 RunAsSendAppNotRespondinProcessID();
sptr<IAbilityManager> GetAbilityManagerService();
ErrCode RunAsDumpCommandOptopt();
ErrCode MakeWantFromCmd(Want &want, std::string &windowMode);
+113 -3
View File
@@ -12,14 +12,17 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ability_command.h"
#include <cstdlib>
#include <getopt.h>
#include "ability_manager_client.h"
#include "mission_snapshot.h"
#include "hilog_wrapper.h"
#include "iservice_registry.h"
#include "mission_snapshot.h"
#include "ohos/aafwk/base/bool_wrapper.h"
#include "sa_mgr_client.h"
#include "system_ability_definition.h"
#include "test_observer.h"
using namespace OHOS::AppExecFwk;
@@ -38,7 +41,12 @@ const struct option LONG_OPTIONS[] = {
{"debug", no_argument, nullptr, 'D'},
{nullptr, 0, nullptr, 0},
};
const std::string SHORT_OPTIONS_ApplicationNotRespondin = "hp:";
const struct option LONG_OPTIONS_ApplicationNotRespondin[] = {
{"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 +95,7 @@ ErrCode AbilityManagerShellCommand::CreateCommandMap()
{"dump", std::bind(&AbilityManagerShellCommand::RunAsDumpsysCommand, this)},
{"force-stop", std::bind(&AbilityManagerShellCommand::RunAsForceStop, this)},
{"test", std::bind(&AbilityManagerShellCommand::RunAsTestCommand, this)},
{"ApplicationNotRespondin", std::bind(&AbilityManagerShellCommand::RunAsSendAppNotRespondinProcessID, this)},
};
return OHOS::ERR_OK;
@@ -1287,5 +1296,106 @@ 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::RunAsSendAppNotRespondinProcessID()
{
static sptr<IAbilityManager> abilityMs_;
std::string pid = "";
int option = -1;
ErrCode result = OHOS::ERR_OK;
option = getopt_long(argc_, argv_, SHORT_OPTIONS_ApplicationNotRespondin.c_str(),
LONG_OPTIONS_ApplicationNotRespondin, 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_ApplicationNotRespondin.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 ApplicationNotRespondin -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 ApplicationNotRespondin' 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 ApplicationNotRespondin' 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 ApplicationNotRespondin 'aa %{public}s' -p process.", cmd_.c_str());
HILOG_INFO("aa ApplicationNotRespondin 'aa optarg = %{public}s'.", optarg);
pid = optarg;
HILOG_INFO("aa ApplicationNotRespondinr '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_ApplicationNotRespondin+ "\n");
result = OHOS::ERR_INVALID_VALUE;
}
return result;
}
} // namespace AAFwk
} // namespace OHOS
+2 -2
View File
@@ -207,9 +207,9 @@ public:
{
return 0;
}
virtual bool SendANRProcessID(int pid)
virtual int SendANRProcessID(int pid)
{
return true;
return 0;
}
virtual int SetAbilityController(const sptr<AppExecFwk::IAbilityController> &abilityController,
bool imAStabilityTest) override