• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2023 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 #include "application_anr_listener.h"
17 
18 #include <sys/time.h>
19 #include <fstream>
20 #include "singleton.h"
21 
22 #include "app_mgr_client.h"
23 #include "backtrace_local.h"
24 #include "fault_data.h"
25 #include "hilog_tag_wrapper.h"
26 #include "hisysevent.h"
27 #include "time_util.h"
28 #include "parameters.h"
29 
30 namespace OHOS {
31 namespace AAFwk {
32 namespace {
33 const bool BETA_VERSION = OHOS::system::GetParameter("const.logsystem.versiontype", "unknown") == "beta";
34 }
ApplicationAnrListener()35 ApplicationAnrListener::ApplicationAnrListener() {}
36 
~ApplicationAnrListener()37 ApplicationAnrListener::~ApplicationAnrListener() {}
38 
OnAnr(int32_t pid,int32_t eventId) const39 void ApplicationAnrListener::OnAnr(int32_t pid, int32_t eventId) const
40 {
41     if (!BETA_VERSION) {
42         int32_t ret = HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::AAFWK, "HIVIEW_HALF_FREEZE_LOG",
43             HiviewDFX::HiSysEvent::EventType::FAULT, "PID", pid, "PACKAGE_NAME", "");
44         TAG_LOGW(AAFwkTag::APPDFR, "hisysevent write HIVIEW_HALF_FREEZE_LOG, pid:%{public}d, packageName:,"
45             " ret:%{public}d", pid, ret);
46     }
47     AppExecFwk::AppFaultDataBySA faultData;
48     std::ifstream statmStream("/proc/" + std::to_string(pid) + "/statm");
49     if (statmStream) {
50         std::string procStatm;
51         std::getline(statmStream, procStatm);
52         statmStream.close();
53         faultData.procStatm = procStatm;
54     }
55     faultData.faultType = AppExecFwk::FaultDataType::APP_FREEZE;
56     faultData.pid = pid;
57     faultData.errorObject.message = "User input does not respond!";
58     faultData.errorObject.stack =  "\nDump tid stack start time: " +
59         AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
60     std::string stack;
61     if (!HiviewDFX::GetBacktraceStringByTidWithMix(stack, pid, 0, true)) {
62         stack = "Failed to dump stacktrace for " + std::to_string(pid) + "\n" + stack;
63     }
64     faultData.errorObject.stack += stack + "\nDump tid stack end time: " +
65         AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n";
66     faultData.errorObject.name = AppExecFwk::AppFreezeType::APP_INPUT_BLOCK;
67     faultData.waitSaveState = false;
68     faultData.notifyApp = false;
69     faultData.forceExit = false;
70     faultData.eventId = eventId;
71     DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->NotifyAppFaultBySA(faultData);
72 }
73 }  // namespace AAFwk
74 }  // namespace OHOS
75