| @@ -128,9 +128,10 @@ public: | |||
| 128 | void InsertKillThread(int32_t state, int32_t pid, int32_t uid, const std::string& bundleName); | 128 | void InsertKillThread(int32_t state, int32_t pid, int32_t uid, const std::string& bundleName); |
| 129 | bool IsSkipDetect(int32_t pid, int32_t uid, const std::string& bundleName, | 129 | bool IsSkipDetect(int32_t pid, int32_t uid, const std::string& bundleName, |
| 130 | const std::string& eventName); | 130 | const std::string& eventName); |
| 131 | - AppfreezeManager::ProcessKillInfo GetProcessKillReason(int32_t killId, int32_t pid, | 131 | + bool GetProcessKillReason(AppfreezeManager::ProcessKillInfo &killInfo, |
| 132 | - const std::string& killMsg, bool foreground); | 132 | + int32_t killId, int32_t pid, const std::string& killMsg); |
| 133 | - void GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo); | 133 | + bool GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo); |
| 134 | + void SetExitKernelReason(int32_t pid); | ||
| 134 | int GetFreezeExitReason(const std::string& eventName); | 135 | int GetFreezeExitReason(const std::string& eventName); |
| 135 | void UpdateFreezeExcludedPid(bool isAdd, int32_t targetPid, int32_t profilerPid); | 136 | void UpdateFreezeExcludedPid(bool isAdd, int32_t targetPid, int32_t profilerPid); |
| 136 | bool IsFreezeExcludedPid(int32_t targetPid); | 137 | bool IsFreezeExcludedPid(int32_t targetPid); |
| @@ -83,12 +83,12 @@ constexpr const char* PRELOAD_UIEXTENSION = "PreloadUIExtension"; | |||
| 83 | constexpr int32_t INVALID_KILL_ID = -2; | 83 | constexpr int32_t INVALID_KILL_ID = -2; |
| 84 | constexpr const char* INVALID_KILL_REASON = "InvalidKillId"; | 84 | constexpr const char* INVALID_KILL_REASON = "InvalidKillId"; |
| 85 | 85 | ||
| 86 | -static constexpr int SYSLOAD_GET_KILL_INFO_MAGIC = 0xE5AC02; | ||
| 87 | - | ||
| 88 | 86 | ||
| 89 | 87 | ||
| 88 | + | ||
| 90 | 89 | ||
| 91 | 90 | ||
| 91 | + | ||
| 92 | 92 | ||
| 93 | struct KillEventInfo { | 93 | struct KillEventInfo { |
| 94 | int id = 0; | 94 | int id = 0; |
| @@ -1361,42 +1361,31 @@ bool AppfreezeManager::IsSkipDetect(int32_t pid, int32_t uid, const std::string& | |||
| 1361 | return false; | 1361 | return false; |
| 1362 | } | 1362 | } |
| 1363 | 1363 | ||
| 1364 | -AppfreezeManager::ProcessKillInfo AppfreezeManager::GetProcessKillReason( | 1364 | +bool AppfreezeManager::GetProcessKillReason( |
| 1365 | - int32_t killId, int32_t pid, const std::string& killMsg, bool foreground) | 1365 | + AppfreezeManager::ProcessKillInfo &killInfo, int32_t killId, int32_t pid, const std::string& killMsg) |
| 1366 | { | 1366 | { |
| 1367 | - AppfreezeManager::ProcessKillInfo killInfo = { | ||
| 1368 | - .killReason = "", | ||
| 1369 | - .killMsg = "", | ||
| 1370 | - .adj = 0, | ||
| 1371 | - .foreground = foreground, | ||
| 1372 | - .timestamp = 0, | ||
| 1373 | - .killId = killId, | ||
| 1374 | - .eventParamFirst = 0, | ||
| 1375 | - .eventParamSecond = 0, | ||
| 1376 | - .eventParamThird = 0, | ||
| 1377 | - .eventParamFourth = 0, | ||
| 1378 | - .eventParamFifth = 0, | ||
| 1379 | - .eventParamSixth = 0, | ||
| 1380 | - .eventParamSeventh = 0, | ||
| 1381 | - }; | ||
| 1382 | if (killId == INVALID_KILL_ID) { | 1367 | if (killId == INVALID_KILL_ID) { |
| 1383 | killInfo.killReason = INVALID_KILL_REASON; | 1368 | killInfo.killReason = INVALID_KILL_REASON; |
| 1384 | killInfo.killMsg = killMsg + " " + std::string(INVALID_KILL_REASON) + ":" + std::to_string(killId); | 1369 | killInfo.killMsg = killMsg + " " + std::string(INVALID_KILL_REASON) + ":" + std::to_string(killId); |
| 1385 | } else if (killId < 0) { | 1370 | } else if (killId < 0) { |
H | |||
| 1386 | - GetExitKernelReason(pid, killInfo); | 1371 | + bool res = GetExitKernelReason(pid, killInfo); |
| 1372 | + if (res || killInfo.killReason == INVALID_KILL_REASON) { | ||
| 1373 | + return false; | ||
| 1374 | + } | ||
| 1387 | } else { | 1375 | } else { |
| 1388 | killInfo.killReason = HiviewDFX::ProcessKillReason::GetKillReason(killId); | 1376 | killInfo.killReason = HiviewDFX::ProcessKillReason::GetKillReason(killId); |
| 1389 | killInfo.killMsg = killMsg; | 1377 | killInfo.killMsg = killMsg; |
| 1378 | + SetExitKernelReason(pid); | ||
| 1390 | } | 1379 | } |
| 1391 | - return killInfo; | 1380 | + return true; |
| 1392 | } | 1381 | } |
| 1393 | 1382 | ||
| 1394 | -void AppfreezeManager::GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo) | 1383 | +bool AppfreezeManager::GetExitKernelReason(int32_t pid, ProcessKillInfo& killInfo) |
| 1395 | { | 1384 | { |
| 1396 | int sysloadFd = open(DEV_SYSLOAD, O_RDWR); | 1385 | int sysloadFd = open(DEV_SYSLOAD, O_RDWR); |
| 1397 | if (sysloadFd < 0) { | 1386 | if (sysloadFd < 0) { |
| 1398 | TAG_LOGW(AAFwkTag::APPDFR, "open failed, errno:%{public}d", errno); | 1387 | TAG_LOGW(AAFwkTag::APPDFR, "open failed, errno:%{public}d", errno); |
| 1399 | - return; | 1388 | + return false; |
| 1400 | } | 1389 | } |
| 1401 | fdsan_exchange_owner_tag(sysloadFd, 0, FREEZE_DOMAIN); | 1390 | fdsan_exchange_owner_tag(sysloadFd, 0, FREEZE_DOMAIN); |
| 1402 | KillInfo info = {0}; | 1391 | KillInfo info = {0}; |
| @@ -1432,10 +1421,32 @@ void AppfreezeManager::GetExitKernelReason(int32_t pid, ProcessKillInfo& killInf | |||
| 1432 | killId, killInfo.adj, killInfo.foreground, killInfo.timestamp, killInfo.killReason.c_str(), | 1421 | killId, killInfo.adj, killInfo.foreground, killInfo.timestamp, killInfo.killReason.c_str(), |
| 1433 | kernelPid, pid, killInfo.eventParamFirst, killInfo.eventParamSecond, killInfo.eventParamThird, | 1422 | kernelPid, pid, killInfo.eventParamFirst, killInfo.eventParamSecond, killInfo.eventParamThird, |
| 1434 | killInfo.eventParamFourth, killInfo.eventParamFifth, killInfo.eventParamSixth, killInfo.eventParamSeventh); | 1423 | killInfo.eventParamFourth, killInfo.eventParamFifth, killInfo.eventParamSixth, killInfo.eventParamSeventh); |
| 1424 | + return true; | ||
| 1435 | } else { | 1425 | } else { |
| 1436 | killInfo.killReason = INVALID_KILL_REASON; | 1426 | killInfo.killReason = INVALID_KILL_REASON; |
| 1437 | TAG_LOGW(AAFwkTag::APPDFR, "Get killReason ioctl failed, errno:%{public}d", errno); | 1427 | TAG_LOGW(AAFwkTag::APPDFR, "Get killReason ioctl failed, errno:%{public}d", errno); |
| 1438 | } | 1428 | } |
| 1429 | + return false; | ||
| 1430 | +} | ||
| 1431 | + | ||
| 1432 | +void AppfreezeManager::SetExitKernelReason(int32_t pid) | ||
| 1433 | +{ | ||
| 1434 | + int sysloadFd = open(DEV_SYSLOAD, O_RDWR); | ||
| 1435 | + if (sysloadFd < 0) { | ||
| 1436 | + TAG_LOGW(AAFwkTag::APPDFR, "open failed, errno:%{public}d", errno); | ||
| 1437 | + return; | ||
| 1438 | + } | ||
| 1439 | + fdsan_exchange_owner_tag(sysloadFd, 0, FREEZE_DOMAIN); | ||
| 1440 | + KillInfo info = {0}; | ||
H [次要] SetExitKernelReason 中 int res = ioctl(...) 前有一行仅含空格的行,存在多余空白字符,建议清理。 ![]() ![]() | |||
| 1441 | + info.magic = SYSLOAD_SET_KILL_INFO_MAGIC; | ||
| 1442 | + info.pid = pid; | ||
| 1443 | + info.data.processed = true; | ||
| 1444 | + info.structSize = sizeof(struct KillInfo); | ||
| 1445 | + | ||
| 1446 | + int res = ioctl(sysloadFd, SET_KILL_INFO, &info); | ||
| 1447 | + fdsan_close_with_tag(sysloadFd, FREEZE_DOMAIN); | ||
| 1448 | + TAG_LOGI(AAFwkTag::APPDFR, "SetExitKernelReason pid:%{public}d, processed:%{public}d, res:%{public}d", | ||
| 1449 | + pid, info.data.processed, res); | ||
| 1439 | } | 1450 | } |
| 1440 | 1451 | ||
| 1441 | int AppfreezeManager::GetFreezeExitReason(const std::string& eventName) | 1452 | int AppfreezeManager::GetFreezeExitReason(const std::string& eventName) |
| @@ -36,6 +36,7 @@ | |||
| 36 | 36 | ||
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | + | ||
| 39 | 40 | ||
| 40 | 41 | ||
| 41 | 42 | ||
| @@ -2426,7 +2427,7 @@ private: | |||
| 2426 | std::vector<std::string> &instanceKeys, int32_t userId); | 2427 | std::vector<std::string> &instanceKeys, int32_t userId); |
| 2427 | int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason, | 2428 | int32_t KillProcessByPidInner(const pid_t pid, const std::string& reason, |
| 2428 | const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord, bool isKillPrecedeStart); | 2429 | const std::string& killReason, std::shared_ptr<AppRunningRecord> appRecord, bool isKillPrecedeStart); |
| 2429 | - void SetKilledEventInfo(std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo); | 2430 | + static void SetKilledEventInfo(std::shared_ptr<AppRunningRecord> appRecord, AAFwk::EventInfo &eventInfo); |
| 2430 | void AddToKillProcessMap(const std::string &processName); | 2431 | void AddToKillProcessMap(const std::string &processName); |
| 2431 | bool IsAllowedNWebPreload(const std::string &processName); | 2432 | bool IsAllowedNWebPreload(const std::string &processName); |
| 2432 | void ParseInfoToAppfreeze(const FaultData &faultData, int32_t pid, int32_t uid, const std::string &bundleName, | 2433 | void ParseInfoToAppfreeze(const FaultData &faultData, int32_t pid, int32_t uid, const std::string &bundleName, |
| @@ -2466,6 +2467,10 @@ private: | |||
| 2466 | void RemoveUninstallOrUpgradeUidSet(int32_t uid); | 2467 | void RemoveUninstallOrUpgradeUidSet(int32_t uid); |
| 2467 | bool IsUninstallingOrUpgrading(int32_t uid); | 2468 | bool IsUninstallingOrUpgrading(int32_t uid); |
| 2468 | void SendProcessKillEvent(std::shared_ptr<AppRunningRecord> appRecord); | 2469 | void SendProcessKillEvent(std::shared_ptr<AppRunningRecord> appRecord); |
| 2470 | + | ||
| 2471 | +private: | ||
| 2472 | + static void ReportProcessKillEvent(int32_t pid, std::string versionCode, std::string versionName, | ||
| 2473 | + std::shared_ptr<AppRunningRecord> appRecord, const AppfreezeManager::ProcessKillInfo& killInfo); | ||
| 2469 | void HandleForegroundAbilityDied(const std::vector<sptr<IRemoteObject>>& abilityTokens, | 2474 | void HandleForegroundAbilityDied(const std::vector<sptr<IRemoteObject>>& abilityTokens, |
| 2470 | ApplicationState state); | 2475 | ApplicationState state); |
| 2471 | 2476 | ||
| @@ -221,6 +221,7 @@ constexpr const int64_t PRELOAD_FREEZE_TIMEOUT = 60000; | |||
| 221 | constexpr size_t MAX_PROCESS_NAME_LENGTH = 64; | 221 | constexpr size_t MAX_PROCESS_NAME_LENGTH = 64; |
| 222 | constexpr const int32_t DEFAULT_APPFREEZE_KILL_WAIT_TIME = 3500; // 3.5s | 222 | constexpr const int32_t DEFAULT_APPFREEZE_KILL_WAIT_TIME = 3500; // 3.5s |
| 223 | constexpr const int32_t BETA_APPFREEZE_KILL_WAIT_TIME = 6000; // 6s | 223 | constexpr const int32_t BETA_APPFREEZE_KILL_WAIT_TIME = 6000; // 6s |
| 224 | +constexpr const int32_t PROCESS_KILLDELAY_TIME = 2000 * 1000; // 2000ms in microseconds | ||
| 224 | 225 | ||
| 225 | 226 | ||
| 226 | constexpr const char* DLP_PARAMS_SECURITY_FLAG = "ohos.dlp.params.securityFlag"; | 227 | constexpr const char* DLP_PARAMS_SECURITY_FLAG = "ohos.dlp.params.securityFlag"; |
| @@ -6557,6 +6558,30 @@ void AppMgrServiceInner::SendProcessKillEvent(std::shared_ptr<AppRunningRecord> | |||
| 6557 | return; | 6558 | return; |
| 6558 | } | 6559 | } |
| 6559 | int32_t pid = appRecord->GetPid(); | 6560 | int32_t pid = appRecord->GetPid(); |
| 6561 | + AppfreezeManager::ProcessKillInfo killInfo = {}; | ||
| 6562 | + bool res = AppExecFwk::AppfreezeManager::GetInstance()->GetProcessKillReason(killInfo, | ||
| 6563 | + appRecord->GetKillId(), pid, appRecord->GetKillMsg()); | ||
| 6564 | + std::string versionCode = std::to_string(appInfo->versionCode); | ||
| 6565 | + std::string versionName = appInfo->versionName; | ||
| 6566 | + AAFwk::EventInfo eventInfo; | ||
| 6567 | + SetKilledEventInfo(appRecord, eventInfo); | ||
| 6568 | + AAFwk::EventReport::SendAppEvent(AAFwk::EventName::APP_TERMINATE, HISYSEVENT_BEHAVIOR, eventInfo); | ||
| 6569 | + if (!res) { | ||
| 6570 | + ffrt::submit([appRecord, pid, versionCode, versionName] () { | ||
HH [阻断] lambda 中声明了变量 delayInfo,但调用 GetProcessKillReason 和 ReportProcessKillEvent 时使用了未定义的 delayKillInfo,编译错误。应统一为 delayInfo。 ![]() ![]() [阻断] lambda 中使用 AAFwk::AppfreezeManager,但上方代码使用的是 AppExecFwk::AppfreezeManager。AppfreezeManager 应属于 AppExecFwk 命名空间,此处命名空间错误会导致编译失败。 ![]() ![]() | |||
| 6571 | + AppfreezeManager::ProcessKillInfo delayInfo = {}; | ||
| 6572 | + AAFwk::AppfreezeManager::GetInstance()->GetProcessKillReason(delayKillInfo, | ||
| 6573 | + appRecord->GetKillId(), pid, appRecord->GetKillMsg()); | ||
| 6574 | + ReportProcessKillEvent(pid, versionCode, versionName, appRecord, delayKillInfo); | ||
| 6575 | + }, ffrt::task_attr().name("delayKillUIAbility").delay(PROCESS_KILL_DELAY_TIME) | ||
| 6576 | + .timeout(AbilityRuntime::GlobalConstant::DEFAULT_FFRT_TASK_TIMEOUT)); | ||
| 6577 | + } else { | ||
| 6578 | + ReportProcessKillEvent(pid, versionCode, versionName, appRecord, killInfo); | ||
| 6579 | + } | ||
| 6580 | +} | ||
| 6581 | + | ||
| 6582 | +void AppMgrServiceInner::ReportProcessKillEvent(int32_t pid, std::string versionCode, std::string versionName, | ||
| 6583 | + std::shared_ptr<AppRunningRecord> appRecord, const AppfreezeManager::ProcessKillInfo& killInfo) | ||
H [重要] ReportProcessKillEvent 中保留了 bool foreground = ... 计算但不再使用(旧代码将其传给 GetProcessKillReason 的第4参数,现已移除)。同时新流程中 killInfo 的 foreground 字段不再被设置(零初始化为 false),而后续 hisyseventReport->InsertParam(EVENT_KEY_FOREGROUND, killInfo.foreground) 仍依赖该字段。应在 GetProcessKillReason 中或调用方设置 killInfo.foreground。 ![]() ![]() | |||
| 6584 | +{ | ||
| 6560 | bool foreground = appRecord->GetState() == ApplicationState::APP_STATE_FOREGROUND || | 6585 | bool foreground = appRecord->GetState() == ApplicationState::APP_STATE_FOREGROUND || |
HH [阻断] ReportProcessKillEvent 函数参数名为 killInfo(const ref),但函数体中保留了原有代码 AppfreezeManager::ProcessKillInfo killInfo = ...GetProcessKillReason(...),导致变量重定义。同时该处仍使用旧的 GetProcessKillReason 签名(返回 ProcessKillInfo、4参数),与新签名(返回 bool、引用+3参数)不匹配。应删除这行旧代码,直接使用传入的 killInfo 参数。 ![]() ![]() [次要] ReportProcessKillEvent 中保留了 AAFwk::EventInfo eventInfo; SetKilledEventInfo(appRecord, eventInfo); 但 SendAppEvent 已移至 SendProcessKillEvent 中调用,此处 eventInfo 创建后未使用,属于死代码,建议清理。 ![]() ![]() | |||
| 6561 | appRecord->GetState() == ApplicationState::APP_STATE_FOCUS; | 6586 | appRecord->GetState() == ApplicationState::APP_STATE_FOCUS; |
| 6562 | AppfreezeManager::ProcessKillInfo killInfo = AppExecFwk::AppfreezeManager::GetInstance()->GetProcessKillReason( | 6587 | AppfreezeManager::ProcessKillInfo killInfo = AppExecFwk::AppfreezeManager::GetInstance()->GetProcessKillReason( |
| @@ -6566,7 +6591,6 @@ void AppMgrServiceInner::SendProcessKillEvent(std::shared_ptr<AppRunningRecord> | |||
| 6566 | std::chrono::system_clock::now().time_since_epoch()).count(); | 6591 | std::chrono::system_clock::now().time_since_epoch()).count(); |
| 6567 | AAFwk::EventInfo eventInfo; | 6592 | AAFwk::EventInfo eventInfo; |
| 6568 | SetKilledEventInfo(appRecord, eventInfo); | 6593 | SetKilledEventInfo(appRecord, eventInfo); |
| 6569 | - AAFwk::EventReport::SendAppEvent(AAFwk::EventName::APP_TERMINATE, HISYSEVENT_BEHAVIOR, eventInfo); | ||
| 6570 | auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(PROCESS_KILL_PARAM); | 6594 | auto hisyseventReport = std::make_shared<AAFwk::HisyseventReport>(PROCESS_KILL_PARAM); |
| 6571 | hisyseventReport->InsertParam(EVENT_KEY_PID, pid); | 6595 | hisyseventReport->InsertParam(EVENT_KEY_PID, pid); |
| 6572 | hisyseventReport->InsertParam(EVENT_KEY_UID, appRecord->GetUid()); | 6596 | hisyseventReport->InsertParam(EVENT_KEY_UID, appRecord->GetUid()); |
| @@ -6576,8 +6600,8 @@ void AppMgrServiceInner::SendProcessKillEvent(std::shared_ptr<AppRunningRecord> | |||
| 6576 | hisyseventReport->InsertParam(EVENT_KEY_REASON, killInfo.killReason); | 6600 | hisyseventReport->InsertParam(EVENT_KEY_REASON, killInfo.killReason); |
| 6577 | hisyseventReport->InsertParam(EVENT_KEY_FOREGROUND, killInfo.foreground); | 6601 | hisyseventReport->InsertParam(EVENT_KEY_FOREGROUND, killInfo.foreground); |
| 6578 | hisyseventReport->InsertParam(EVENT_KEY_APP_RUNNING_UNIQUE_ID, appRunningUniqueId); | 6602 | hisyseventReport->InsertParam(EVENT_KEY_APP_RUNNING_UNIQUE_ID, appRunningUniqueId); |
| 6579 | - hisyseventReport->InsertParam(EVENT_KEY_VERSIONCODE, std::to_string(appInfo->versionCode)); | 6603 | + hisyseventReport->InsertParam(EVENT_KEY_VERSIONCODE, versionCode); |
| 6580 | - hisyseventReport->InsertParam(EVENT_KEY_VERSIONNAME, appInfo->versionName); | 6604 | + hisyseventReport->InsertParam(EVENT_KEY_VERSIONNAME, versionName); |
| 6581 | hisyseventReport->InsertParam(EVENT_KEY_INNER_MSG, appRecord->GetInnerMsg()); | 6605 | hisyseventReport->InsertParam(EVENT_KEY_INNER_MSG, appRecord->GetInnerMsg()); |
| 6582 | hisyseventReport->InsertParam(EVENT_KEY_PROCESS_KILL_ID, killInfo.killId); | 6606 | hisyseventReport->InsertParam(EVENT_KEY_PROCESS_KILL_ID, killInfo.killId); |
| 6583 | hisyseventReport->InsertParam(EVENT_KEY_ADJ, killInfo.adj); | 6607 | hisyseventReport->InsertParam(EVENT_KEY_ADJ, killInfo.adj); |


[重要] GetProcessKillReason 中条件 if (res || killInfo.killReason == INVALID_KILL_REASON) 逻辑反转。当 GetExitKernelReason 成功(res==true)时返回 false 触发重试,但实际上成功获取到原因时应返回 true 直接上报。应改为 if (!res || killInfo.killReason == INVALID_KILL_REASON)。