From 8290ccf9a8fd67ad218cf0fe9cd0786bc5bae34d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E4=B9=8B=E5=B8=86?= Date: Thu, 4 Sep 2025 16:45:47 +0800 Subject: [PATCH 1/2] add freeze hisysevent record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 尹之帆 --- .../native/appkit/dfr/appfreeze_inner.cpp | 16 ++- frameworks/native/appkit/dfr/watchdog.cpp | 2 +- .../app_manager/include/appmgr/fault_data.h | 16 +++ .../app_manager/src/appmgr/fault_data.cpp | 60 +++++++++++ .../kits/native/appkit/dfr/appfreeze_inner.h | 3 +- services/appdfr/include/appfreeze_manager.h | 24 ++++- services/appdfr/src/appfreeze_manager.cpp | 99 ++++++++++++++++++- .../appdfr/src/application_anr_listener.cpp | 2 + services/appmgr/src/app_mgr_service_inner.cpp | 28 ++++-- 9 files changed, 232 insertions(+), 18 deletions(-) diff --git a/frameworks/native/appkit/dfr/appfreeze_inner.cpp b/frameworks/native/appkit/dfr/appfreeze_inner.cpp index 4b66b4a2109..350ea9e7156 100644 --- a/frameworks/native/appkit/dfr/appfreeze_inner.cpp +++ b/frameworks/native/appkit/dfr/appfreeze_inner.cpp @@ -240,12 +240,20 @@ int AppfreezeInner::AcquireStack(const FaultData& info, bool onlyMainThread) faultData.appfreezeInfo = it->appfreezeInfo; faultData.appRunningUniqueId = it->appRunningUniqueId; faultData.procStatm = it->procStatm; + faultData.schedTime = it->schedTime; + faultData.detectTime = it->detectTime; + faultData.appStatus = it->appStatus; + faultData.samplerStartTime = it->samplerStartTime; + faultData.samplerFinishTime = it->samplerFinishTime; + faultData.samplerCount = it->samplerCount; + faultData.pid = it->pid; ChangeFaultDateInfo(faultData, msgContent); } return 0; } -void AppfreezeInner::ThreadBlock(std::atomic_bool& isSixSecondEvent) +void AppfreezeInner::ThreadBlock(std::atomic_bool& isSixSecondEvent, uint64_t schedTime, + uint64_t now, bool isInBackground) { FaultData faultData; faultData.errorObject.message = @@ -254,10 +262,16 @@ void AppfreezeInner::ThreadBlock(std::atomic_bool& isSixSecondEvent) faultData.faultType = FaultDataType::APP_FREEZE; bool onlyMainThread = false; int32_t pid = static_cast(getpid()); + faultData.pid = pid; + faultData.schedTime = schedTime; + faultData.detectTime = now; + faultData.appStatus = isInBackground ? AppStatus::APP_STATUS_BACKGROUND : AppStatus::APP_STATUS_FOREGROUND; if (isSixSecondEvent) { faultData.errorObject.name = AppFreezeType::THREAD_BLOCK_6S; onlyMainThread = true; + OHOS::HiviewDFX::Watchdog::GetInstance().GetSamplerResult(faultData.samplerStartTime, + faultData.samplerFinishTime, faultData.samplerCount); #ifdef APP_NO_RESPONSE_DIALOG isSixSecondEvent.store(false); #endif diff --git a/frameworks/native/appkit/dfr/watchdog.cpp b/frameworks/native/appkit/dfr/watchdog.cpp index 5e6a810d735..c7ad0af7351 100644 --- a/frameworks/native/appkit/dfr/watchdog.cpp +++ b/frameworks/native/appkit/dfr/watchdog.cpp @@ -302,7 +302,7 @@ void Watchdog::ReportEvent() SetHiTraceChainId(); } #endif - AppExecFwk::AppfreezeInner::GetInstance()->ThreadBlock(isSixSecondEvent_); + AppExecFwk::AppfreezeInner::GetInstance()->ThreadBlock(isSixSecondEvent_, 0, now, isInBackground_); } } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/include/appmgr/fault_data.h b/interfaces/inner_api/app_manager/include/appmgr/fault_data.h index a9427a266c5..e66abd5cc61 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/fault_data.h +++ b/interfaces/inner_api/app_manager/include/appmgr/fault_data.h @@ -40,6 +40,12 @@ enum class FaultDataType { RESOURCE_CONTROL }; +enum AppStatus { + APP_STATUS_UNKNOW = -1, + APP_STATUS_FOREGROUND = 0, + APP_STATUS_BACKGROUND = 1 +}; + class AppFreezeType { public: static constexpr char LIFECYCLE_HALF_TIMEOUT[] = "LIFECYCLE_HALF_TIMEOUT"; @@ -73,8 +79,15 @@ struct FaultData : public Parcelable { bool needKillProcess = true; uint32_t state = 0; int32_t eventId = -1; + uint64_t schedTime = 0; + uint64_t detectTime = 0; + int32_t pid = -1; int32_t tid = -1; uint32_t stuckTimeout = 0; + int32_t appStatus = -1; + uint64_t samplerStartTime = 0; + uint64_t samplerFinishTime = 0; + int32_t samplerCount = -1; sptr token = nullptr; std::string appfreezeInfo; std::string appRunningUniqueId; @@ -102,6 +115,9 @@ struct AppFaultDataBySA : public Parcelable { int32_t pid = -1; uint32_t state = 0; int32_t eventId = -1; + uint64_t schedTime = 0; + uint64_t detectTime = 0; + int32_t appStatus = -1; sptr token = nullptr; std::string timeoutMarkers; std::string appfreezeInfo; diff --git a/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp b/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp index 4f4129e1f62..b3a21c555a7 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp @@ -61,6 +61,13 @@ bool FaultData::ReadFromParcel(Parcel &parcel) needKillProcess = parcel.ReadBool(); state = parcel.ReadUint32(); eventId = parcel.ReadInt32(); + schedTime = parcel.ReadUint64(); + detectTime = parcel.ReadUint64(); + appStatus = parcel.ReadInt32(); + samplerStartTime = parcel.ReadUint64(); + samplerFinishTime = parcel.ReadUint64(); + samplerCount = parcel.ReadInt32(); + pid = parcel.ReadInt32(); tid = parcel.ReadInt32(); stuckTimeout = parcel.ReadUint32(); if (parcel.ReadBool()) { @@ -194,6 +201,41 @@ bool FaultData::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteUint64(schedTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); + return false; + } + + if (!parcel.WriteUint64(detectTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); + return false; + } + + if (!parcel.WriteInt32(appStatus)) { + TAG_LOGE(AAFwkTag::APPMGR, "AppStatus [%{public}d] write int32 failed.", appStatus); + return false; + } + + if (!parcel.WriteUint64(samplerStartTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "SamplerStartTime [%{public}" PRIu64 "] write uint64 failed.", samplerStartTime); + return false; + } + + if (!parcel.WriteUint64(samplerFinishTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "SamplerFinishTime [%{public}" PRIu64"] write uint64 failed.", samplerFinishTime); + return false; + } + + if (!parcel.WriteInt32(samplerCount)) { + TAG_LOGE(AAFwkTag::APPMGR, "SamplerCount [%{public}d] write int32 failed.", samplerCount); + return false; + } + + if (!parcel.WriteInt32(pid)) { + TAG_LOGE(AAFwkTag::APPMGR, "Pid [%{public}u] write int32 failed.", pid); + return false; + } + if (!parcel.WriteInt32(tid)) { TAG_LOGE(AAFwkTag::APPMGR, "Tid [%{public}u] write int32 failed.", tid); return false; @@ -247,6 +289,9 @@ bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel) needKillProcess = parcel.ReadBool(); state = parcel.ReadUint32(); eventId = parcel.ReadInt32(); + schedTime = parcel.ReadUint64(); + detectTime = parcel.ReadUint64(); + appStatus = parcel.ReadInt32(); if (parcel.ReadBool()) { token = (static_cast(&parcel))->ReadRemoteObject(); } @@ -369,6 +414,21 @@ bool AppFaultDataBySA::Marshalling(Parcel &parcel) const return false; } + if (!parcel.WriteUint64(schedTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); + return false; + } + + if (!parcel.WriteUint64(detectTime)) { + TAG_LOGE(AAFwkTag::APPMGR, "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); + return false; + } + + if (!parcel.WriteInt32(appStatus)) { + TAG_LOGE(AAFwkTag::APPMGR, "AppStatus [%{public}d] write int32 failed.", appStatus); + return false; + } + return WriteContent(parcel); } diff --git a/interfaces/kits/native/appkit/dfr/appfreeze_inner.h b/interfaces/kits/native/appkit/dfr/appfreeze_inner.h index 8b4c04da7c6..701f3008319 100644 --- a/interfaces/kits/native/appkit/dfr/appfreeze_inner.h +++ b/interfaces/kits/native/appkit/dfr/appfreeze_inner.h @@ -40,7 +40,8 @@ public: static void DestroyInstance(); static void SetMainHandler(const std::shared_ptr& eventHandler); void SetApplicationInfo(const std::shared_ptr& applicationInfo); - void ThreadBlock(std::atomic_bool& isSixSecondEvent); + void ThreadBlock(std::atomic_bool& isSixSecondEvent, uint64_t schedTime = 0, + uint64_t now = 0, bool isInBackground = false); void ChangeFaultDateInfo(FaultData& faultData, const std::string& msgContent); void AppfreezeHandleOverReportCount(bool isSixSecondEvent); void GetMainHandlerDump(std::string& msgContent); diff --git a/services/appdfr/include/appfreeze_manager.h b/services/appdfr/include/appfreeze_manager.h index c7e6c10c033..8b2c983a173 100644 --- a/services/appdfr/include/appfreeze_manager.h +++ b/services/appdfr/include/appfreeze_manager.h @@ -33,6 +33,14 @@ namespace OHOS { using AbilityRuntime::FreezeUtil; namespace AppExecFwk { +static const std::vector APP_FREEZE_EVENT_NAME = { + "THREAD_BLOCK_3S", + "THREAD_BLOCK_6S", + "APP_INPUUT_BLOCK", + "LIFECYCLE_HALF_TIMEOUT", + "LIFECYCLE_TIMEOUT", +}; + class AppfreezeManager : public std::enable_shared_from_this { public: struct AppInfo { @@ -71,6 +79,15 @@ public: std::string msg; }; + struct AppfreezeEventRecord { + uint64_t schedTime = 0; + uint64_t detectTime = 0; + uint64_t dumpStartTime = 0; + uint64_t dumpFinishTime = 0; + std::string dumpResult; + int32_t appStatus = -1; + }; + AppfreezeManager(); ~AppfreezeManager(); @@ -88,6 +105,8 @@ public: void RemoveDeathProcess(std::string bundleName); void ResetAppfreezeState(int32_t pid, const std::string& bundleName); bool IsValidFreezeFilter(int32_t pid, const std::string& bundleName); + void ReportAppFreezeSysEvents(int32_t pid); + void RegisterAppKillTime(int32_t pid, uint64_t killTime); private: struct PeerBinderInfo { @@ -131,7 +150,8 @@ private: std::set GetBinderPeerPids(std::string& stack, AppfreezeManager::ParseBinderParam params, std::set& asyncPids, AppfreezeManager::TerminalBinder& terminalBinder) const; void FindStackByPid(std::string& msg, int pid) const; - std::string CatchJsonStacktrace(int pid, const std::string& faultType, const std::string& stack) const; + std::pair CatchJsonStacktrace( + int pid, const std::string& faultType, const std::string& stack) const; std::string CatcherStacktrace(int pid, const std::string& stack) const; FaultData GetFaultNotifyData(const FaultData& faultData, int pid); int AcquireStack(const FaultData& faultData, const AppInfo& appInfo, const std::string& memoryContent); @@ -145,6 +165,7 @@ private: void ClearOldInfo(); void CollectFreezeSysMemory(std::string& memoryContent); int MergeNotifyInfo(FaultData& faultNotifyData, const AppfreezeManager::AppInfo& appInfo); + void RecordAppFreezeBehavior(FaultData& faultData); std::string ParseDecToHex(uint64_t id); bool GetHitraceId(HitraceInfo& info); void PerfStart(std::string eventName); @@ -163,6 +184,7 @@ private: int64_t perfTime = 0; static ffrt::mutex freezeInfoMutex_; static std::string appfreezeInfoPath_; + std::map> freezeEventMap_; }; } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appdfr/src/appfreeze_manager.cpp b/services/appdfr/src/appfreeze_manager.cpp index a01dcd8a638..64132ad74cd 100644 --- a/services/appdfr/src/appfreeze_manager.cpp +++ b/services/appdfr/src/appfreeze_manager.cpp @@ -57,6 +57,7 @@ constexpr char BINDER_INFO[] = "BINDER_INFO"; constexpr char APP_RUNNING_UNIQUE_ID[] = "APP_RUNNING_UNIQUE_ID"; constexpr char FREEZE_MEMORY[] = "FREEZE_MEMORY"; constexpr char FREEZE_INFO_PATH[] = "FREEZE_INFO_PATH"; +constexpr char KILL_EVENT_NAME[] = "APP_KILL"; #ifdef ABILITY_RUNTIME_HITRACE_ENABLE constexpr char EVENT_TRACE_ID[] = "HITRACE_ID"; constexpr char EVENT_SPAN_ID[] = "SPAN_ID"; @@ -70,6 +71,7 @@ constexpr int32_t CHARACTER_WIDTH = 2; constexpr int MAX_LAYER = 8; constexpr int FREEZEMAP_SIZE_MAX = 20; constexpr int FREEZE_TIME_LIMIT = 60000; +constexpr int FREEZE_EVENT_MAX_SIZE = 200; static constexpr uint8_t ARR_SIZE = 7; static constexpr uint8_t DECIMAL = 10; static constexpr uint8_t FREE_ASYNC_INDEX = 6; @@ -183,13 +185,18 @@ int AppfreezeManager::MergeNotifyInfo(FaultData& faultNotifyData, const Appfreez std::string catcherStack; faultNotifyData.errorObject.message += "\nCatche stack trace start time: " + AbilityRuntime::TimeUtil::DefaultCurrentTimeStr() + "\n"; + uint64_t dumpStartTime = AbilityRuntime::TimeUtil::CurrentTimeMillis(); + std::string resultMsg; if (faultNotifyData.errorObject.name == AppFreezeType::LIFECYCLE_HALF_TIMEOUT || faultNotifyData.errorObject.name == AppFreezeType::LIFECYCLE_HALF_TIMEOUT_WARNING) { catcherStack += CatcherStacktrace(appInfo.pid, faultNotifyData.errorObject.stack); } else { - catcherStack += CatchJsonStacktrace(appInfo.pid, faultNotifyData.errorObject.name, - faultNotifyData.errorObject.stack); + std::pair catchResult = + CatchJsonStacktrace(appInfo.pid, faultNotifyData.errorObject.name, faultNotifyData.errorObject.stack); + catcherStack += catchResult.first; + resultMsg += catchResult.second; } + uint64_t dumpFinishTime = AbilityRuntime::TimeUtil::CurrentTimeMillis(); std::string timeStamp = "Catche stack trace end time: " + AbilityRuntime::TimeUtil::DefaultCurrentTimeStr(); faultNotifyData.errorObject.stack = WriteToFile(fileName, catcherStack); if (appInfo.isOccurException) { @@ -207,6 +214,26 @@ int AppfreezeManager::MergeNotifyInfo(FaultData& faultNotifyData, const Appfreez return 0; } +void AppfreezeManager::RecordAppFreezeBehavior(FaultData& faultData) +{ + std::string eventName = faultData.errorObject.name; + if (freezeEventMap_.size() > FREEZE_EVENT_MAX_SIZE) { + freezeEventMap_.clear(); + } + auto it = std::find(APP_FREEZE_EVENT_NAME.begin(), APP_FREEZE_EVENT_NAME.end(), eventName); + if (it != APP_FREEZE_EVENT_NAME.end()) { + freezeEventMap_[faultData.pid][eventName].schedTime = faultData.schedTime; + freezeEventMap_[faultData.pid][eventName].detectTime = faultData.detectTime; + freezeEventMap_[faultData.pid][eventName].dumpStartTime = dumpStartTime; + freezeEventMap_[faultData.pid][eventName].dumpFinishTime = dumpFinishTime; + freezeEventMap_[faultData.pid][eventName].dumpResult = resultMsg; + freezeEventMap_[faultData.pid][eventName].appStatus = faultData.appStatus; + freezeEventMap_[faultData.pid][KILL_EVENT_NAME].dumpStartTime = faultData.samplerStartTime; + freezeEventMap_[faultData.pid][KILL_EVENT_NAME].dumpFinishTime = faultData.samplerFinishTime; + freezeEventMap_[faultData.pid][KILL_EVENT_NAME].dumpResult = std::to_string(faultData.samplerCount); + } +} + int AppfreezeManager::AppfreezeHandleWithStack(const FaultData& faultData, const AppfreezeManager::AppInfo& appInfo) { TAG_LOGW(AAFwkTag::APPDFR, "NotifyAppFaultTask called, eventName:%{public}s, bundleName:%{public}s, " @@ -221,6 +248,13 @@ int AppfreezeManager::AppfreezeHandleWithStack(const FaultData& faultData, const faultNotifyData.errorObject.stack = faultData.errorObject.stack; faultNotifyData.faultType = FaultDataType::APP_FREEZE; faultNotifyData.eventId = faultData.eventId; + faultNotifyData.schedTime = faultData.schedTime; + faultNotifyData.detectTime = faultData.detectTime; + faultNotifyData.appStatus = faultData.appStatus; + faultNotifyData.samplerStartTime = faultData.samplerStartTime; + faultNotifyData.samplerFinishTime = faultData.samplerFinishTime; + faultNotifyData.samplerCount = faultData.samplerCount; + faultNotifyData.pid = faultData.pid; faultNotifyData.tid = faultData.tid; faultNotifyData.appfreezeInfo = faultData.appfreezeInfo; faultNotifyData.appRunningUniqueId = faultData.appRunningUniqueId; @@ -309,6 +343,7 @@ int AppfreezeManager::LifecycleTimeoutHandle(const ParamInfo& info, FreezeUtil:: faultDataSA.token = flow.token; faultDataSA.state = static_cast(flow.state); } + faultDataSA.detectTime = AbilityRuntime::TimeUtil::CurrentTimeMillis(); DelayedSingleton::GetInstance()->NotifyAppFaultBySA(faultDataSA); return 0; } @@ -656,7 +691,7 @@ void AppfreezeManager::FindStackByPid(std::string& msg, int pid) const } } -std::string AppfreezeManager::CatchJsonStacktrace(int pid, const std::string& faultType, +std::pair AppfreezeManager::CatchJsonStacktrace(int pid, const std::string& faultType, const std::string& stack) const { HITRACE_METER_FMT(HITRACE_TAG_APP, "CatchJsonStacktrace pid:%{public}d", pid); @@ -682,7 +717,7 @@ std::string AppfreezeManager::CatchJsonStacktrace(int pid, const std::string& fa catchStackMap_[pid] = msg; } } - return msg; + return std::make_pair(msg, dumpResult.second); } std::string AppfreezeManager::CatcherStacktrace(int pid, const std::string& stack) const @@ -869,6 +904,62 @@ bool AppfreezeManager::IsValidFreezeFilter(int32_t pid, const std::string& bundl "pid:%{public}d", ret, bundleName.c_str(), pid); return ret; } + +void AppfreezeManager::ReportAppFreezeSysEvents(int32_t pid) +{ + if (freezeEventMap_.find(pid) == freezeEventMap_.end()) { + return; + } + + uint64_t now = AbilityRuntime::TimeUtil::CurrentTimeMillis(); + int ret = HiSysEventWrite(OHOS::HiviewDFX::HiSysEvent::Domain::RELIABILITY, "APP_FREEZE_BEHAVIOR", + OHOS::HiviewDFX::HiSysEvent::EventType::BEHAVIOR, + "THREAD_HALF_SCHED", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].schedTime, + "THREAD_HALF_DETECT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].detectTime, + "THREAD_HALF_DUMP_START", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].dumpStartTime, + "THREAD_HALF_DUMP_FINISH", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].dumpFinishTime, + "THREAD_HALF_DUMP_RESULT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].dumpResult, + "THREAD_HALF_APP_STATUS", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[0]].appStatus, + "THREAD_TIMEOUT_SCHED", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].schedTime, + "THREAD_TIMEOUT_DETECT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].detectTime, + "THREAD_TIMEOUT_DUMP_START", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].dumpStartTime, + "THREAD_TIMEOUT_DUMP_FINISH", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].dumpFinishTime, + "THREAD_TIMEOUT_DUMP_RESULT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].dumpResult, + "THREAD_TIMEOUT_APP_STATUS", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[1]].appStatus, + "INPUT_SCHED", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].schedTime, + "INPUT_DETECT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].detectTime, + "INPUT_DUMP_START", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].dumpStartTime, + "INPUT_DUMP_FINISH", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].dumpFinishTime, + "INPUT_DUMP_RESULT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].dumpResult, + "INPUT_APP_STATUS", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[2]].appStatus, + "LIFECYCLE_HALF_SCHED", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].schedTime, + "LIFECYCLE_HALF_DETECT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].detectTime, + "LIFECYCLE_HALF_DUMP_START", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].dumpStartTime, + "LIFECYCLE_HALF_DUMP_FINISH", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].dumpFinishTime, + "LIFECYCLE_HALF_DUMP_RESULT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].dumpResult, + "LIFECYCLE_HALF_APP_STATUS", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[3]].appStatus, + "LIFECYCLE_TIMEOUT_SCHED", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].schedTime, + "LIFECYCLE_TIMEOUT_DETECT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].detectTime, + "LIFECYCLE_TIMEOUT_DUMP_START", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].dumpStartTime, + "LIFECYCLE_TIMEOUT_DUMP_FINISH", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].dumpFinishTime, + "LIFECYCLE_TIMEOUT_DUMP_RESULT", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].dumpResult, + "LIFECYCLE_TIMEOUT_APP_STATUS", freezeEventMap_[pid][APP_FREEZE_EVENT_NAME[4]].appStatus, + "APP_KILL_TIME", freezeEventMap_[pid][KILL_EVENT_NAME].schedTime, + "APP_TERMINATED_TIME", now, + "SAMPLER_START", freezeEventMap_[pid][KILL_EVENT_NAME].dumpStartTime, + "SAMPLER_FINISH", freezeEventMap_[pid][KILL_EVENT_NAME].dumpFinishTime, + "SAMPLER_COUNT", freezeEventMap_[pid][KILL_EVENT_NAME].dumpResult); + freezeEventMap_.erase(pid); +} + +void AppfreezeManager::RegisterAppKillTime(int32_t pid, uint64_t time) +{ + if (freezeEventMap_.find(pid) == freezeEventMap_.end()) { + return; + } + freezeEventMap_[pid][KILL_EVENT_NAME].schedTime = time; +} + void AppfreezeManager::PerfStart(std::string eventName) { if (OHOS::system::GetParameter("const.dfx.sub_health_recovery.enable", "") != "true") { diff --git a/services/appdfr/src/application_anr_listener.cpp b/services/appdfr/src/application_anr_listener.cpp index 2a227ac69e6..026190aea22 100644 --- a/services/appdfr/src/application_anr_listener.cpp +++ b/services/appdfr/src/application_anr_listener.cpp @@ -68,6 +68,8 @@ void ApplicationAnrListener::OnAnr(int32_t pid, int32_t eventId) const faultData.notifyApp = false; faultData.forceExit = false; faultData.eventId = eventId; + faultData.schedTime = 0; + faultData.detectTime = AbilityRuntime::TimeUtil::CurrentTimeMillis(); DelayedSingleton::GetInstance()->NotifyAppFaultBySA(faultData); } } // namespace AAFwk diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 9a5c544c456..04595f5bdad 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -4825,6 +4825,7 @@ void AppMgrServiceInner::OnRemoteDied(const wptr &remote, bool is TAG_LOGI(AAFwkTag::APPMGR, "null appRecord"); return; } + AppExecFwk::AppfreezeManager::GetInstance()->ReportAppFreezeSysEvents(appRecord->GetPid()); AppExecFwk::AppfreezeManager::GetInstance()->RemoveDeathProcess(appRecord->GetBundleName()); std::vector> abilityTokens; for (const auto &token : appRecord->GetAbilities()) { @@ -7370,20 +7371,24 @@ bool AppMgrServiceInner::CheckAppFault(const std::shared_ptr & int32_t AppMgrServiceInner::KillFaultApp(int32_t pid, const std::string &bundleName, const FaultData &faultData, bool isNeedExit) { - auto killAppTask = [pid, bundleName, faultData, isNeedExit, innerServiceWeak = weak_from_this()]() { - auto innerService = innerServiceWeak.lock(); - CHECK_POINTER_AND_RETURN_LOG(innerService, "get appMgrServiceInner fail"); - if (isNeedExit || (faultData.forceExit && !faultData.waitSaveState)) { + if (isNeedExit || (faultData.forceExit && !faultData.waitSaveState)) { + auto killAppTask = [pid, bundleName, faultData, isNeedExit, innerServiceWeak = weak_from_this()]() { + auto innerService = innerServiceWeak.lock(); + CHECK_POINTER_AND_RETURN_LOG(innerService, "get appMgrServiceInner fail"); TAG_LOGW(AAFwkTag::APPMGR, "faultData: %{public}s,pid: %{public}d will exit because %{public}s", bundleName.c_str(), pid, innerService->FaultTypeToString(faultData.faultType).c_str()); + uint64_t now = std::chrono::duration_cast( + std::chrono::steady_clock::now().time_since_epoch()).count(); + AppExecFwk::AppfreezeManager::GetInstance()->RegisterAppKillTime(now, pid); innerService->KillProcessByPid(pid, faultData.errorObject.name); return; - } - }; - constexpr int32_t waitTime = 3500; - // wait 3.5s before kill application - CHECK_POINTER_AND_RETURN_VALUE(taskHandler_, ERR_NO_INIT); - taskHandler_->SubmitTaskJust(killAppTask, "killAppTask", waitTime); + }; + constexpr int32_t waitTime = 3500; + // wait 3.5s before kill application + CHECK_POINTER_AND_RETURN_VALUE(taskHandler_, ERR_NO_INIT); + taskHandler_->SubmitTaskJust(killAppTask, "killAppTask", waitTime); + } + return ERR_OK; } @@ -7525,6 +7530,9 @@ FaultData AppMgrServiceInner::ConvertDataTypes(const AppFaultDataBySA &faultData newfaultData.token = faultData.token; newfaultData.state = faultData.state; newfaultData.eventId = faultData.eventId; + newfaultData.pid = faultData.pid; + newfaultData.schedTime = faultData.schedTime; + newfaultData.detectTime = faultData.detectTime; newfaultData.needKillProcess = faultData.needKillProcess; newfaultData.appfreezeInfo = faultData.appfreezeInfo; newfaultData.procStatm = faultData.procStatm; -- Gitee From c47dfe4c4be1723e58f87aef5fcf19f336af351b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=B9=E4=B9=8B=E5=B8=86?= Date: Fri, 5 Sep 2025 09:58:37 +0800 Subject: [PATCH 2/2] use macro to fold excessive expression MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 尹之帆 --- .../app_manager/src/appmgr/fault_data.cpp | 364 ++++++------------ services/appdfr/include/appfreeze_manager.h | 3 +- services/appdfr/src/appfreeze_manager.cpp | 4 +- 3 files changed, 126 insertions(+), 245 deletions(-) diff --git a/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp b/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp index b3a21c555a7..d3f71b83bfc 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/fault_data.cpp @@ -21,38 +21,39 @@ namespace OHOS { namespace AppExecFwk { +#define RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(expr, log) \ + do { \ + if ((expr)) { \ + TAG_LOGE(AAFwkTag::APPMGR, log); \ + return false; \ + } \ + } while (0) + +#define RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(expr, log, arg) \ + do { \ + if ((expr)) { \ + TAG_LOGE(AAFwkTag::APPMGR, log, arg); \ + return false; \ + } \ + } while (0) + bool FaultData::ReadFromParcel(Parcel &parcel) { std::string strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Name read string failed."); errorObject.name = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Message read string failed."); errorObject.message = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Stack read string failed."); errorObject.stack = strValue; int type = 0; - if (!parcel.ReadInt32(type)) { - TAG_LOGE(AAFwkTag::APPMGR, "FaultType read int32 failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadInt32(type), "FaultType read int32 failed."); faultType = static_cast(type); - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "TimeoutMarkers read string failed."); timeoutMarkers = strValue; waitSaveState = parcel.ReadBool(); @@ -73,10 +74,8 @@ bool FaultData::ReadFromParcel(Parcel &parcel) if (parcel.ReadBool()) { token = (static_cast(&parcel))->ReadRemoteObject(); } - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo read string failed."); - return false; - } + + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "AppfreezeInfo read string failed."); appfreezeInfo = strValue; return ReadContent(parcel); } @@ -84,15 +83,10 @@ bool FaultData::ReadFromParcel(Parcel &parcel) bool FaultData::ReadContent(Parcel &parcel) { std::string strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "AppRunningUniqueId read string failed."); appRunningUniqueId = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed."); - return false; - } + + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "ProcStatm read string failed."); procStatm = strValue; return true; } @@ -109,16 +103,11 @@ FaultData *FaultData::Unmarshalling(Parcel &parcel) bool FaultData::WriteContent(Parcel &parcel) const { - if (!parcel.WriteUint32(stuckTimeout)) { - TAG_LOGE(AAFwkTag::APPMGR, "stuckTimeout [%{public}u] write uint32 failed.", stuckTimeout); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint32(stuckTimeout), + "stuckTimeout [%{public}u] write uint32 failed.", stuckTimeout); if (token == nullptr) { - if (!parcel.WriteBool(false)) { - TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.WriteBool(false), "Token falge [false] write bool failed."); } else { if (!parcel.WriteBool(true) || !(static_cast(&parcel))->WriteRemoteObject(token)) { TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed."); @@ -126,120 +115,76 @@ bool FaultData::WriteContent(Parcel &parcel) const } } - if (!parcel.WriteString(appfreezeInfo)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(appfreezeInfo), + "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str()); - if (!parcel.WriteString(appRunningUniqueId)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(appRunningUniqueId), + "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str()); + + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(procStatm), + "ProcStatm [%{public}s] write string failed.", procStatm.c_str()); - if (!parcel.WriteString(procStatm)) { - TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str()); - return false; - } return true; } bool FaultData::Marshalling(Parcel &parcel) const { - if (!parcel.WriteString(errorObject.name)) { - TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.name), + "Name [%{public}s] write string failed.", errorObject.name.c_str()); - if (!parcel.WriteString(errorObject.message)) { - TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.message), + "Message [%{public}s] write string failed.", errorObject.message.c_str()); - if (!parcel.WriteString(errorObject.stack)) { - TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.stack), + "Stack [%{public}s] write string failed.", errorObject.stack.c_str()); - if (!parcel.WriteInt32(static_cast(faultType))) { - TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast(faultType)); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(static_cast(faultType)), + "FaultType [%{public}d] write int32 failed.", static_cast(faultType)); - if (!parcel.WriteString(timeoutMarkers)) { - TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(timeoutMarkers), + "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str()); - if (!parcel.WriteBool(waitSaveState)) { - TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(waitSaveState), + "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false"); - if (!parcel.WriteBool(notifyApp)) { - TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(notifyApp), + "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false"); - if (!parcel.WriteBool(forceExit)) { - TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(forceExit), + "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false"); - if (!parcel.WriteBool(needKillProcess)) { - TAG_LOGE(AAFwkTag::APPMGR, "needKillProcess [%{public}s] write bool failed.", - needKillProcess ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(needKillProcess), + "needKillProcess [%{public}s] write bool failed.", needKillProcess ? "true" : "false"); - if (!parcel.WriteUint32(state)) { - TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint32(state), + "State [%{public}u] write uint32 failed.", state); - if (!parcel.WriteInt32(eventId)) { - TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(eventId), + "EventId [%{public}u] write int32 failed.", eventId); - if (!parcel.WriteUint64(schedTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(schedTime), + "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); - if (!parcel.WriteUint64(detectTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(detectTime), + "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); - if (!parcel.WriteInt32(appStatus)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppStatus [%{public}d] write int32 failed.", appStatus); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(appStatus), + "AppStatus [%{public}d] write int32 failed.", appStatus); - if (!parcel.WriteUint64(samplerStartTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "SamplerStartTime [%{public}" PRIu64 "] write uint64 failed.", samplerStartTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(samplerStartTime), + "SamplerStartTime [%{public}" PRIu64 "] write uint64 failed.", samplerStartTime); - if (!parcel.WriteUint64(samplerFinishTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "SamplerFinishTime [%{public}" PRIu64"] write uint64 failed.", samplerFinishTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(samplerFinishTime), + "SamplerFinishTime [%{public}" PRIu64"] write uint64 failed.", samplerFinishTime); - if (!parcel.WriteInt32(samplerCount)) { - TAG_LOGE(AAFwkTag::APPMGR, "SamplerCount [%{public}d] write int32 failed.", samplerCount); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(samplerCount), + "SamplerCount [%{public}d] write int32 failed.", samplerCount); - if (!parcel.WriteInt32(pid)) { - TAG_LOGE(AAFwkTag::APPMGR, "Pid [%{public}u] write int32 failed.", pid); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(pid), + "Pid [%{public}u] write int32 failed.", pid); - if (!parcel.WriteInt32(tid)) { - TAG_LOGE(AAFwkTag::APPMGR, "Tid [%{public}u] write int32 failed.", tid); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(tid), + "Tid [%{public}u] write int32 failed.", tid); return WriteContent(parcel); } @@ -247,40 +192,22 @@ bool FaultData::Marshalling(Parcel &parcel) const bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel) { std::string strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Name read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Name read string failed."); errorObject.name = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Message read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Message read string failed."); errorObject.message = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "Stack read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "Stack read string failed."); errorObject.stack = strValue; int type = 0; - if (!parcel.ReadInt32(type)) { - TAG_LOGE(AAFwkTag::APPMGR, "Type read int32 failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadInt32(type), "Type read int32 failed."); faultType = static_cast(type); - if (!parcel.ReadInt32(pid)) { - TAG_LOGE(AAFwkTag::APPMGR, "Pid read int32 failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadInt32(pid), "Pid read int32 failed."); - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "TimeoutMarkers read string failed."); timeoutMarkers = strValue; waitSaveState = parcel.ReadBool(); @@ -296,10 +223,7 @@ bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel) token = (static_cast(&parcel))->ReadRemoteObject(); } - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "AppfreezeInfo read string failed."); appfreezeInfo = strValue; return ReadContent(parcel); } @@ -307,16 +231,10 @@ bool AppFaultDataBySA::ReadFromParcel(Parcel &parcel) bool AppFaultDataBySA::ReadContent(Parcel &parcel) { std::string strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "AppRunningUniqueId read string failed."); appRunningUniqueId = strValue; - if (!parcel.ReadString(strValue)) { - TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm read string failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.ReadString(strValue), "ProcStatm read string failed."); procStatm = strValue; return true; } @@ -334,10 +252,7 @@ AppFaultDataBySA *AppFaultDataBySA::Unmarshalling(Parcel &parcel) bool AppFaultDataBySA::WriteContent(Parcel &parcel) const { if (token == nullptr) { - if (!parcel.WriteBool(false)) { - TAG_LOGE(AAFwkTag::APPMGR, "Token falge [false] write bool failed."); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_IF_TRUE(!parcel.WriteBool(false), "Token falge [false] write bool failed."); } else { if (!parcel.WriteBool(true) || !(static_cast(&parcel))->WriteRemoteObject(token)) { TAG_LOGE(AAFwkTag::APPMGR, "Token falge [true] write bool failed."); @@ -345,20 +260,14 @@ bool AppFaultDataBySA::WriteContent(Parcel &parcel) const } } - if (!parcel.WriteString(appfreezeInfo)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(appfreezeInfo), + "AppfreezeInfo [%{public}s] write string failed.", appfreezeInfo.c_str()); - if (!parcel.WriteString(appRunningUniqueId)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(appRunningUniqueId), + "AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str()); - if (!parcel.WriteString(procStatm)) { - TAG_LOGE(AAFwkTag::APPMGR, "ProcStatm [%{public}s] write string failed.", procStatm.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(procStatm), + "ProcStatm [%{public}s] write string failed.", procStatm.c_str()); return true; } @@ -368,86 +277,55 @@ bool AppFaultDataBySA::Marshalling(Parcel &parcel) const return false; } - if (!parcel.WriteInt32(static_cast(faultType))) { - TAG_LOGE(AAFwkTag::APPMGR, "FaultType [%{public}d] write int32 failed.", static_cast(faultType)); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(static_cast(faultType)), + "FaultType [%{public}d] write int32 failed.", static_cast(faultType)); - if (!parcel.WriteInt32(pid)) { - TAG_LOGE(AAFwkTag::APPMGR, "Pid [%{public}d] write int32 failed.", static_cast(pid)); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(pid), + "Pid [%{public}d] write int32 failed.", static_cast(pid)); - if (!parcel.WriteString(timeoutMarkers)) { - TAG_LOGE(AAFwkTag::APPMGR, "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(timeoutMarkers), + "TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str()); - if (!parcel.WriteBool(waitSaveState)) { - TAG_LOGE(AAFwkTag::APPMGR, "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(waitSaveState), + "WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false"); - if (!parcel.WriteBool(notifyApp)) { - TAG_LOGE(AAFwkTag::APPMGR, "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(notifyApp), + "NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false"); - if (!parcel.WriteBool(forceExit)) { - TAG_LOGE(AAFwkTag::APPMGR, "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(forceExit), + "ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false"); - if (!parcel.WriteBool(needKillProcess)) { - TAG_LOGE(AAFwkTag::APPMGR, "needKillProcess [%{public}s] write bool failed.", - needKillProcess ? "true" : "false"); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteBool(needKillProcess), + "needKillProcess [%{public}s] write bool failed.", needKillProcess ? "true" : "false"); - if (!parcel.WriteUint32(state)) { - TAG_LOGE(AAFwkTag::APPMGR, "State [%{public}u] write uint32 failed.", state); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint32(state), + "State [%{public}u] write uint32 failed.", state); - if (!parcel.WriteInt32(eventId)) { - TAG_LOGE(AAFwkTag::APPMGR, "EventId [%{public}u] write int32 failed.", eventId); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(eventId), + "EventId [%{public}u] write int32 failed.", eventId); - if (!parcel.WriteUint64(schedTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(schedTime), + "SchedTime [%{public}" PRIu64 "] write uint64 failed.", schedTime); - if (!parcel.WriteUint64(detectTime)) { - TAG_LOGE(AAFwkTag::APPMGR, "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteUint64(detectTime), + "DetectTime [%{public}" PRIu64 "] write uint64 failed.", detectTime); - if (!parcel.WriteInt32(appStatus)) { - TAG_LOGE(AAFwkTag::APPMGR, "AppStatus [%{public}d] write int32 failed.", appStatus); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteInt32(appStatus), + "AppStatus [%{public}d] write int32 failed.", appStatus); return WriteContent(parcel); } bool AppFaultDataBySA::WriteErrorObject(Parcel &parcel) const { - if (!parcel.WriteString(errorObject.name)) { - TAG_LOGE(AAFwkTag::APPMGR, "Name [%{public}s] write string failed.", errorObject.name.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.name), + "Name [%{public}s] write string failed.", errorObject.name.c_str()); - if (!parcel.WriteString(errorObject.message)) { - TAG_LOGE(AAFwkTag::APPMGR, "Message [%{public}s] write string failed.", errorObject.message.c_str()); - return false; - } - - if (!parcel.WriteString(errorObject.stack)) { - TAG_LOGE(AAFwkTag::APPMGR, "Stack [%{public}s] write string failed.", errorObject.stack.c_str()); - return false; - } + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.message), + "Message [%{public}s] write string failed.", errorObject.message.c_str()); + + RETURN_FALSE_AND_WRITE_LOG_WITH_ONE_ARG_IF_TRUE(!parcel.WriteString(errorObject.stack), + "Stack [%{public}s] write string failed.", errorObject.stack.c_str()); return true; } } // namespace AppExecFwk diff --git a/services/appdfr/include/appfreeze_manager.h b/services/appdfr/include/appfreeze_manager.h index 8b2c983a173..037c0373cec 100644 --- a/services/appdfr/include/appfreeze_manager.h +++ b/services/appdfr/include/appfreeze_manager.h @@ -165,7 +165,8 @@ private: void ClearOldInfo(); void CollectFreezeSysMemory(std::string& memoryContent); int MergeNotifyInfo(FaultData& faultNotifyData, const AppfreezeManager::AppInfo& appInfo); - void RecordAppFreezeBehavior(FaultData& faultData); + void RecordAppFreezeBehavior(FaultData& faultData, uint64_t dumpStartTime, + uint64_t dumpFinishTime, const std::string& dumpResult); std::string ParseDecToHex(uint64_t id); bool GetHitraceId(HitraceInfo& info); void PerfStart(std::string eventName); diff --git a/services/appdfr/src/appfreeze_manager.cpp b/services/appdfr/src/appfreeze_manager.cpp index 64132ad74cd..43fbf055f49 100644 --- a/services/appdfr/src/appfreeze_manager.cpp +++ b/services/appdfr/src/appfreeze_manager.cpp @@ -211,10 +211,12 @@ int AppfreezeManager::MergeNotifyInfo(FaultData& faultNotifyData, const Appfreez } else { NotifyANR(faultNotifyData, appInfo, "", memoryContent); } + RecordAppFreezeBehavior(faultNotifyData, dumpStartTime, dumpFinishTime, resultMsg); return 0; } -void AppfreezeManager::RecordAppFreezeBehavior(FaultData& faultData) +void AppfreezeManager::RecordAppFreezeBehavior(FaultData& faultData, uint64_t dumpStartTime, + uint64_t dumpFinishTime, const std::string& resultMsg) { std::string eventName = faultData.errorObject.name; if (freezeEventMap_.size() > FREEZE_EVENT_MAX_SIZE) { -- Gitee