增加PROCESS_KILL打点参数

Signed-off-by: acdemicJava <yinhuiling@h-partners.com>
This commit is contained in:
acdemicJava
2026-03-03 09:26:06 +08:00
parent 43f6ac7a12
commit bec27e4398
7 changed files with 120 additions and 61 deletions
+1 -1
View File
@@ -2146,7 +2146,7 @@ void MainThread::ProcessExit(const ProcessExitInfo& info)
TAG_LOGE(AAFwkTag::APPKIT, "\n%{public}s is about to exit due to RuntimeError\nError type:%{public}s\n"
"%{public}s", info.bundleName.c_str(), info.errorObjectName.c_str(), info.summary.c_str());
AAFwk::ExitReasonCompability exitReason = { REASON_JS_ERROR, info.errorObjectName };
exitReason.killId = HiviewDFX::ProcessKillReason::REASON_JS_ERROR;
exitReason.killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_JS_ERROR;
AbilityManagerClient::GetInstance()->RecordAppWithReason(info.pid, getuid(), exitReason);
_exit(JS_ERROR_EXIT);
}
@@ -14287,7 +14287,7 @@ void AbilityManagerService::RecordRecoveryExitReason(bool isAppRecovery, int32_t
if (!isAppRecovery) {
return;
}
int32_t killId = HiviewDFX::ProcessKillReason::REASON_RESTART;
int32_t killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_RESTART;
std::string killReason = HiviewDFX::ProcessKillReason::GetKillReason(killId);
AAFwk::ExitReasonCompability exitReason = {REASON_JS_ERROR, "Kill Reason:" + killReason};
exitReason.killId = killId;
+9 -2
View File
@@ -83,6 +83,13 @@ public:
bool isRepeatKilledThread = false;
};
struct ProcessKillInfo {
std::string killReason;
std::string killMsg;
int adj = 0;
int64_t timestamp = 0;
};
AppfreezeManager();
~AppfreezeManager();
@@ -109,8 +116,8 @@ public:
void InsertKillThread(int32_t state, int32_t pid, int32_t uid, const std::string& bundleName);
bool IsSkipDetect(int32_t pid, int32_t uid, const std::string& bundleName,
const std::string& eventName);
std::string GetExitReasonByKillId(int32_t killId);
std::string GetExitKernelReason(int32_t pid);
AppfreezeManager::ProcessKillInfo GetProcessKillReason(int32_t killId, int32_t pid, const std::string& killMsg);
void GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo);
int GetFreezeExitReason(const std::string& eventName);
private:
+37 -11
View File
@@ -79,6 +79,10 @@ static constexpr const char *const SPAN_ID = "span_id: ";
static constexpr const char *const PARENT_SPAN_ID = "parent_span_id: ";
static constexpr const char *const TRACE_FLAG = "trace_flag: ";
static constexpr const char *const DEV_SYSLOAD = "/dev/sysload";
// kill resaon
constexpr int32_t INVALID_KILL_ID = -2;
constexpr const char* INVALID_KILL_REASON = "InvalidKillId";
static constexpr int SYSLOAD_GET_KILL_INFO_MAGIC = 0xE5AC02;
#define KILL_LOG_BASE 'S'
@@ -87,7 +91,7 @@ static constexpr int SYSLOAD_GET_KILL_INFO_MAGIC = 0xE5AC02;
#define SYSLOAD_GET_KILL_INFO_MAGIC 0xE5AC02
struct KillEventInfo {
enum HiviewDFX::ProcessKillReason::KillEventId id;
int id;
int adj;
bool processed;
bool foreground;
@@ -1261,17 +1265,33 @@ bool AppfreezeManager::IsSkipDetect(int32_t pid, int32_t uid, const std::string&
return false;
}
std::string AppfreezeManager::GetExitReasonByKillId(int32_t killId)
AppfreezeManager::ProcessKillInfo AppfreezeManager::GetProcessKillReason(
int32_t killId, int32_t pid, const std::string& killMsg)
{
return HiviewDFX::ProcessKillReason::GetKillReason(killId);
AppfreezeManager::ProcessKillInfo killInfo = {
.killReason = "",
.killMsg = "",
.adj = 0,
.timestamp = 0,
};
if (killId == INVALID_KILL_ID) {
killInfo.killReason = INVALID_KILL_REASON;
killInfo.killMsg = killMsg + " " + std::string(INVALID_KILL_REASON) + ":" + std::to_string(killId);
} else if (killId < 0) {
GetExitKernelReason(pid, killInfo);
} else {
killInfo.killReason = HiviewDFX::ProcessKillReason::GetKillReason(killId);
killInfo.killMsg = killMsg;
}
return killInfo;
}
std::string AppfreezeManager::GetExitKernelReason(int32_t pid)
void AppfreezeManager::GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo)
{
int sysloadFd = open(DEV_SYSLOAD, O_RDWR);
if (sysloadFd < 0) {
TAG_LOGW(AAFwkTag::APPDFR, "open failed, errno:%{public}d", errno);
return "";
return;
}
fdsan_exchange_owner_tag(sysloadFd, 0, FREEZE_DOMAIN);
KillInfo info = {0};
@@ -1286,28 +1306,34 @@ std::string AppfreezeManager::GetExitKernelReason(int32_t pid)
int killId = -1;
if (res == 0) {
killId = static_cast<int>(info.data.id);
killInfo.adj = static_cast<int>(info.data.adj);
killInfo.timestamp = static_cast<int64_t>(info.data.timestamp);
killInfo.killReason = HiviewDFX::ProcessKillReason::GetKillReason(killId);
int kernelPid = static_cast<int>(info.data.pid);
TAG_LOGI(AAFwkTag::APPDFR, "ioctl success, killId:%{public}d, adj:%{public}d, "
"timestamp:%{public}" PRId64 ", killReason:%{public}s, ioctlPid:%{public}d, pid:%{public}d",
killId, killInfo.adj, killInfo.timestamp, killInfo.killReason.c_str(), kernelPid, pid);
} else {
TAG_LOGW(AAFwkTag::APPDFR, "ioctl failed, errno:%{public}d", errno);
}
return GetExitReasonByKillId(killId);
}
int AppfreezeManager::GetFreezeExitReason(const std::string& eventName)
{
if (eventName == AppFreezeType::THREAD_BLOCK_6S) {
return HiviewDFX::ProcessKillReason::REASON_THREAD_BLOCK_6S;
return HiviewDFX::ProcessKillReason::KillEventId::REASON_THREAD_BLOCK_6S;
}
if (eventName == AppFreezeType::LIFECYCLE_TIMEOUT) {
return HiviewDFX::ProcessKillReason::REASON_LIFECYCLE_TIMEOUT;
return HiviewDFX::ProcessKillReason::KillEventId::REASON_LIFECYCLE_TIMEOUT;
}
if (eventName == AppFreezeType::APP_INPUT_BLOCK) {
return HiviewDFX::ProcessKillReason::REASON_APP_INPUT_BLOCK;
return HiviewDFX::ProcessKillReason::KillEventId::REASON_APP_INPUT_BLOCK;
}
if (eventName == AppFreezeType::BUSSINESS_THREAD_BLOCK_6S) {
return HiviewDFX::ProcessKillReason::REASON_BUSINESS_THREAD_BLOCK_6S;
return HiviewDFX::ProcessKillReason::KillEventId::REASON_BUSINESS_THREAD_BLOCK_6S;
}
if (eventName == AppFreezeType::BUSINESS_INPUT_BLOCK) {
return HiviewDFX::ProcessKillReason::REASON_BUSINESS_INPUT_BLOCK;
return HiviewDFX::ProcessKillReason::KillEventId::REASON_BUSINESS_INPUT_BLOCK;
}
return UNKNOWN_FREEZE_REASON;
}
+24 -21
View File
@@ -269,6 +269,13 @@ constexpr const char* EVENT_KEY_BUNDLE_NAME = "BUNDLE_NAME";
constexpr const char* EVENT_KEY_MESSAGE = "MSG";
constexpr const char* EVENT_KEY_REASON = "REASON";
constexpr const char* EVENT_KEY_FOREGROUND = "FOREGROUND";
constexpr const char* EVENT_KEY_APP_RUNNING_UNIQUE_ID = "APP_RUNNING_UNIQUE_ID";
constexpr const char* EVENT_KEY_VERSIONCODE = "VERSIONCODE";
constexpr const char* EVENT_KEY_VERSIONNAME = "VERSIONNAME";
constexpr const char* EVENT_KEY_INNER_MSG = "INNER_MSG";
constexpr const char* EVENT_KEY_PROCESS_KILL_ID = "PROCESS_KILL_ID";
constexpr const char* EVENT_KEY_ADJ = "ADJ";
constexpr const char* EVENT_KEY_TIMESTAMP = "TIMESTAMP";
// Developer mode param
constexpr const char* DEVELOPER_MODE_STATE = "const.security.developermode.state";
@@ -341,9 +348,7 @@ constexpr int32_t MAX_EXTENSION_CHILD_PROCESS = 1;
constexpr int32_t MAX_EXTENSION_CHILD_PROCESS_DEV_MODE = 3;
// kill resaon
constexpr int32_t INVALID_KILL_ID = -2;
constexpr const char* INVALID_KILL_REASON = "InvalidKillId";
constexpr int32_t PROCESS_KILL_PARAM = 9;
constexpr int32_t PROCESS_KILL_PARAM = 14; // PROCESS_KILL params
int32_t GetUserIdByUid(int32_t uid)
{
@@ -5114,19 +5119,12 @@ void AppMgrServiceInner::SendProcessKillEvent(std::shared_ptr<AppRunningRecord>
TAG_LOGE(AAFwkTag::APPMGR, "no appInfo");
return;
}
std::string versionCode = appInfo->versionName;
int32_t killId = appRecord->GetKillId();
std::string killMsg = appRecord->GetKillMsg();
std::string innerMsg = appRecord->GetInnerMsg();
std::string killReason;
if (killId == INVALID_KILL_ID) {
killReason = INVALID_KILL_REASON;
killMsg += " " + std::string(INVALID_KILL_REASON) + ":" + std::to_string(killId);
} else if (killId < 0) {
killReason = AppExecFwk::AppfreezeManager::GetInstance()->GetExitKernelReason(appRecord->GetPid());
} else {
killReason = AppExecFwk::AppfreezeManager::GetInstance()->GetExitReasonByKillId(killId);
}
int32_t pid = appRecord->GetPid();
AppfreezeManager::ProcessKillInfo killInfo =
AppExecFwk::AppfreezeManager::GetInstance()->GetProcessKillReason(killId, pid, killMsg);
bool foreground = appRecord->GetState() == ApplicationState::APP_STATE_FOREGROUND ||
appRecord->GetState() == ApplicationState::APP_STATE_FOCUS;
std::string appRunningUniqueId = std::to_string(appRecord->GetAppRunningUniqueId());
@@ -5134,20 +5132,25 @@ void AppMgrServiceInner::SendProcessKillEvent(std::shared_ptr<AppRunningRecord>
SetKilledEventInfo(appRecord, eventInfo);
AAFwk::EventReport::SendAppEvent(AAFwk::EventName::APP_TERMINATE, HISYSEVENT_BEHAVIOR, eventInfo);
auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(PROCESS_KILL_PARAM);
hisyseventReport->InsertParam(EVENT_KEY_PID, eventInfo.pid);
hisyseventReport->InsertParam(EVENT_KEY_PID, pid);
hisyseventReport->InsertParam(EVENT_KEY_UID, appRecord->GetUid());
hisyseventReport->InsertParam(EVENT_KEY_PROCESS_NAME, eventInfo.processName);
hisyseventReport->InsertParam(EVENT_KEY_BUNDLE_NAME, appRecord->GetBundleName());
hisyseventReport->InsertParam(EVENT_KEY_MESSAGE, killMsg);
hisyseventReport->InsertParam(EVENT_KEY_REASON, killReason);
hisyseventReport->InsertParam(EVENT_KEY_MESSAGE, killInfo.killMsg);
hisyseventReport->InsertParam(EVENT_KEY_REASON, killInfo.killReason);
hisyseventReport->InsertParam(EVENT_KEY_FOREGROUND, foreground);
hisyseventReport->InsertParam("APP_RUNNING_UNIQUE_ID", appRunningUniqueId);
hisyseventReport->InsertParam("VERSION", versionCode);
hisyseventReport->InsertParam(EVENT_KEY_APP_RUNNING_UNIQUE_ID, appRunningUniqueId);
hisyseventReport->InsertParam(EVENT_KEY_VERSIONCODE, std::to_string(appInfo->versionCode));
hisyseventReport->InsertParam(EVENT_KEY_VERSIONNAME, appInfo->versionName);
hisyseventReport->InsertParam(EVENT_KEY_INNER_MSG, innerMsg);
hisyseventReport->InsertParam(EVENT_KEY_PROCESS_KILL_ID, killId);
hisyseventReport->InsertParam(EVENT_KEY_ADJ, killInfo.adj);
hisyseventReport->InsertParam(EVENT_KEY_TIMESTAMP, killInfo.timestamp);
int result = hisyseventReport->Report("FRAMEWORK", "PROCESS_KILL", HISYSEVENT_FAULT);
TAG_LOGW(AAFwkTag::APPMGR, "hisysevent write result=%{public}d, send event [FRAMEWORK,PROCESS_KILL], pid="
"%{public}d, processName=%{public}s, msg=%{public}s, reason=%{public}s, FOREGROUND=%{public}d,"
" appRunningUniqueId=%{public}s", result, eventInfo.pid, eventInfo.processName.c_str(), killMsg.c_str(),
killReason.c_str(), foreground, appRunningUniqueId.c_str());
" appRunningUniqueId=%{public}s", result, pid, eventInfo.processName.c_str(), killInfo.killMsg.c_str(),
killInfo.killReason.c_str(), foreground, appRunningUniqueId.c_str());
}
void AppMgrServiceInner::OnRemoteDied(const wptr<IRemoteObject> &remote, bool isRenderProcess, bool isChildProcess)
@@ -7987,7 +7990,7 @@ int32_t AppMgrServiceInner::TransformedNotifyAppFault(const AppFaultDataBySA &fa
transformedFaultData.timeoutMarkers = "notifyFault:" + transformedFaultData.errorObject.name +
std::to_string(pid) + "-" + std::to_string(SystemTimeMillisecond());
}
const int64_t timeout = 3000; // ipc tiomeout 3000ms
const int64_t timeout = 3000; // ipc timeout 3000ms
if (faultData.faultType == FaultDataType::APP_FREEZE) {
if (!AppExecFwk::AppfreezeManager::GetInstance()->IsHandleAppfreeze(bundleName) || record->IsDebugging()) {
return ERR_OK;
@@ -896,6 +896,8 @@ HWTEST_F(AppMgrServiceInnerTest, SendProcessKillEvent_003, TestSize.Level1)
killId = -2;
appMgrServiceInner->NotifyAppMgrRecordExitReasonCompability(pid, killId, reason, reason);
appMgrServiceInner->SendProcessKillEvent(appRecord);
killId = 1;
appMgrServiceInner->SendProcessKillEvent(appRecord);
TAG_LOGI(AAFwkTag::TEST, "SendProcessKillEvent_003 end");
}
@@ -733,27 +733,40 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_CheckAppfreezeHappend_Test00
}
/**
* @tc.number: AppfreezeManagerTest GetExitReasonByKillId Test
* @tc.number: AppfreezeManagerTest GetProcessKillReason Test
* @tc.desc: add testcase
* @tc.type: FUNC
*/
HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetExitReasonByKillId_Test001, TestSize.Level1)
HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetProcessKillReason_Test001, TestSize.Level1)
{
int32_t killId = HiviewDFX::ProcessKillReason::REASON_THREAD_BLOCK_6S;
std::string result = appfreezeManager->GetExitReasonByKillId(killId);
EXPECT_EQ(result, "THREAD_BLOCK_6S");
killId = HiviewDFX::ProcessKillReason::REASON_CPP_CRASH;
result = appfreezeManager->GetExitReasonByKillId(killId);
EXPECT_EQ(result, "Cpp Crash");
killId = HiviewDFX::ProcessKillReason::REASON_JS_ERROR;
result = appfreezeManager->GetExitReasonByKillId(killId);
EXPECT_EQ(result, "Js Error");
killId = HiviewDFX::ProcessKillReason::REASON_LIFECYCLE_TIMEOUT;
result = appfreezeManager->GetExitReasonByKillId(killId);
EXPECT_EQ(result, "LIFECYCLE_TIMEOUT");
killId = HiviewDFX::ProcessKillReason::REASON_APP_INPUT_BLOCK;
result = appfreezeManager->GetExitReasonByKillId(killId);
EXPECT_EQ(result, "APP_INPUT_BLOCK");
AppfreezeManager::ProcessKillInfo killInfo = {
.killReason = "",
.killMsg = "",
.adj = 0,
.timestamp = 0,
};
int32_t killId = -2;
int32_t pid = getpid();
std::string killMsg = "AppfreezeManagerTest_GetProcessKillReason_Test001";
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "InvalidKillId");
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_THREAD_BLOCK_6S;
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "THREAD_BLOCK_6S");
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_CPP_CRASH;
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "Cpp Crash");
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_JS_ERROR;
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "Js Error");
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_LIFECYCLE_TIMEOUT;
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "LIFECYCLE_TIMEOUT");
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_APP_INPUT_BLOCK;
killInfo = appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
EXPECT_EQ(killInfo.killReason, "APP_INPUT_BLOCK");
killId = -1;
appfreezeManager->GetProcessKillReason(killId, pid, killMsg);
}
/**
@@ -765,11 +778,19 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetExitKernelReason_Test001,
{
EXPECT_NE(appfreezeManager, nullptr);
int32_t pid = getpid();
std::string result = appfreezeManager->GetExitKernelReason(pid);
AppfreezeManager::ProcessKillInfo killInfo = {
.killReason = "",
.killMsg = "",
.adj = 0,
.timestamp = 0,
};
appfreezeManager->GetExitKernelReason(pid, killInfo);
pid = 0;
result = appfreezeManager->GetExitKernelReason(pid);
appfreezeManager->GetExitKernelReason(pid, killInfo);
pid = 1;
result = appfreezeManager->GetExitKernelReason(pid);
appfreezeManager->GetExitKernelReason(pid, killInfo);
pid = -1;
appfreezeManager->GetExitKernelReason(pid, killInfo);
}
/**
@@ -779,24 +800,24 @@ HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetExitKernelReason_Test001,
*/
HWTEST_F(AppfreezeManagerTest, AppfreezeManagerTest_GetFreezeExitReason_Test001, TestSize.Level1)
{
int32_t killId = HiviewDFX::ProcessKillReason::REASON_THREAD_BLOCK_6S;
int32_t killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_THREAD_BLOCK_6S;
std::string eventName = AppFreezeType::THREAD_BLOCK_6S;
int32_t result = appfreezeManager->GetFreezeExitReason(eventName);
EXPECT_EQ(result, killId);
eventName = AppFreezeType::LIFECYCLE_TIMEOUT;
killId = HiviewDFX::ProcessKillReason::REASON_LIFECYCLE_TIMEOUT;
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_LIFECYCLE_TIMEOUT;
result = appfreezeManager->GetFreezeExitReason(eventName);
EXPECT_EQ(result, killId);
eventName = AppFreezeType::APP_INPUT_BLOCK;
killId = HiviewDFX::ProcessKillReason::REASON_APP_INPUT_BLOCK;
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_APP_INPUT_BLOCK;
result = appfreezeManager->GetFreezeExitReason(eventName);
EXPECT_EQ(result, killId);
eventName = AppFreezeType::BUSSINESS_THREAD_BLOCK_6S;
killId = HiviewDFX::ProcessKillReason::REASON_BUSINESS_THREAD_BLOCK_6S;
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_BUSINESS_THREAD_BLOCK_6S;
result = appfreezeManager->GetFreezeExitReason(eventName);
EXPECT_EQ(result, killId);
eventName = AppFreezeType::BUSINESS_INPUT_BLOCK;
killId = HiviewDFX::ProcessKillReason::REASON_BUSINESS_INPUT_BLOCK;
killId = HiviewDFX::ProcessKillReason::KillEventId::REASON_BUSINESS_INPUT_BLOCK;
result = appfreezeManager->GetFreezeExitReason(eventName);
EXPECT_EQ(result, killId);
eventName = "test";