diff --git a/bundle.json b/bundle.json index b92f68b5fa26fb23354753275562258c1ce13c84..dcbbec80a3da2a75ad442f6995f0820fbd6336f1 100644 --- a/bundle.json +++ b/bundle.json @@ -39,7 +39,8 @@ "init", "hiview", "storage_service", - "samgr" + "samgr", + "ability_runtime" ], "third_party": [] }, diff --git a/interfaces/ndk/BUILD.gn b/interfaces/ndk/BUILD.gn index 50a57317db8792dc208879d9c43db7968a4526fd..8a884f65191fb1fd0e7edfe3db8b84ff7ce75ee4 100644 --- a/interfaces/ndk/BUILD.gn +++ b/interfaces/ndk/BUILD.gn @@ -19,14 +19,12 @@ ohos_shared_library("ohhicollie") { "../native/innerkits/include/xcollie", "../../frameworks/native", ] - sources = [ - "hicollie.cpp", - "report_data.cpp", - ] + sources = [ "hicollie.cpp" ] deps = [ "../native/innerkits:libhicollie" ] external_deps = [ + "ability_runtime:app_manager", "c_utils:utils", "hilog:libhilog", "ipc:ipc_core", diff --git a/interfaces/ndk/hicollie.cpp b/interfaces/ndk/hicollie.cpp index d4a97d4a5dcc46262136fe020b22edcc2941ef65..63fa0fe9dcb967541e743c3431678651e2da4d53 100644 --- a/interfaces/ndk/hicollie.cpp +++ b/interfaces/ndk/hicollie.cpp @@ -20,17 +20,15 @@ #include #include #include "watchdog.h" -#include "report_data.h" #include "xcollie.h" #include "xcollie_define.h" #include "xcollie_utils.h" -#include "iservice_registry.h" -#include "iremote_object.h" +#include "fault_data.h" +#include "app_mgr_client.h" namespace OHOS { namespace HiviewDFX { namespace { -DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.AppMgr"); #ifdef SUPPORT_ASAN constexpr uint32_t CHECK_INTERVAL_TIME = 45000; #else @@ -39,8 +37,6 @@ DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.AppMgr"); constexpr uint32_t MAX_TIMEOUT = 15000; #endif constexpr uint32_t INI_TIMER_FIRST_SECOND = 10000; - constexpr uint32_t NOTIFY_APP_FAULT = 38; - constexpr uint32_t APP_MGR_SERVICE_ID = 501; constexpr uint32_t RATIO = 2; constexpr int32_t BACKGROUND_REPORT_COUNT_MAX = 5; } @@ -60,38 +56,6 @@ bool IsAppMainThread() return false; } -int32_t NotifyAppFault(const OHOS::HiviewDFX::ReportData &reportData) -{ - XCOLLIE_LOGD("called."); - auto systemAbilityMgr = OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); - if (systemAbilityMgr == nullptr) { - XCOLLIE_LOGE("ReportData failed to get system ability manager."); - return -1; - } - auto appMgrService = systemAbilityMgr->GetSystemAbility(APP_MGR_SERVICE_ID); - if (appMgrService == nullptr) { - XCOLLIE_LOGE("ReportData failed to find APP_MGR_SERVICE_ID."); - return -1; - } - OHOS::MessageParcel data; - OHOS::MessageParcel reply; - OHOS::MessageOption option; - if (!data.WriteInterfaceToken(GetDescriptor())) { - XCOLLIE_LOGE("ReportData failed to WriteInterfaceToken."); - return -1; - } - if (!data.WriteParcelable(&reportData)) { - XCOLLIE_LOGE("ReportData write reportData failed."); - return -1; - } - auto ret = appMgrService->SendRequest(NOTIFY_APP_FAULT, data, reply, option); - if (ret != OHOS::NO_ERROR) { - XCOLLIE_LOGE("ReportData Send request failed with error code: %{public}d", ret); - return -1; - } - return reply.ReadInt32(); -} - bool CheckInBackGround(bool* isSixSecond) { int64_t now = std::chrono::duration_cast(std::chrono:: @@ -121,12 +85,12 @@ int Report(bool* isSixSecond) } g_backgroundReportCount++; - OHOS::HiviewDFX::ReportData reportData; - reportData.faultType = OHOS::HiviewDFX::FaultDataType::APP_FREEZE; + AppExecFwk::FaultData faultData; + faultData.faultType = OHOS::AppExecFwk::FaultDataType::APP_FREEZE; int stuckTimeout = g_stuckTimeout; if (*isSixSecond) { - reportData.errorObject.name = "BUSSINESS_THREAD_BLOCK_6S"; - reportData.forceExit = true; + faultData.errorObject.name = "BUSSINESS_THREAD_BLOCK_6S"; + faultData.forceExit = true; *isSixSecond = false; stuckTimeout = g_stuckTimeout * RATIO; std::ifstream statmStream("/proc/" + std::to_string(g_pid) + "/statm"); @@ -134,25 +98,25 @@ int Report(bool* isSixSecond) std::string procStatm; std::getline(statmStream, procStatm); statmStream.close(); - reportData.procStatm = procStatm; + faultData.procStatm = procStatm; } } else { - reportData.errorObject.name = "BUSSINESS_THREAD_BLOCK_3S"; - reportData.forceExit = false; + faultData.errorObject.name = "BUSSINESS_THREAD_BLOCK_3S"; + faultData.forceExit = false; *isSixSecond = true; } std::string timeStamp = "\nFaultTime:" + FormatTime("%Y-%m-%d %H:%M:%S") + "\n"; - reportData.errorObject.message = timeStamp + + faultData.errorObject.message = timeStamp + "Bussiness thread is not response, timeout " + std::to_string(stuckTimeout) + "ms.\n"; - reportData.timeoutMarkers = ""; - reportData.errorObject.stack = ""; - reportData.notifyApp = false; - reportData.waitSaveState = false; - reportData.tid = g_bussinessTid > 0 ? g_bussinessTid : g_pid; - reportData.stuckTimeout = g_stuckTimeout; - auto result = NotifyAppFault(reportData); + faultData.timeoutMarkers = ""; + faultData.errorObject.stack = ""; + faultData.notifyApp = false; + faultData.waitSaveState = false; + faultData.tid = g_bussinessTid > 0 ? g_bussinessTid : g_pid; + faultData.stuckTimeout = g_stuckTimeout; + auto result = DelayedSingleton::GetInstance()->NotifyAppFault(faultData); XCOLLIE_LOGI("OH_HiCollie_Report result: %{public}d, current tid: %{public}d, timeout: %{public}u", - result, reportData.tid, reportData.stuckTimeout); + result, faultData.tid, faultData.stuckTimeout); int64_t now = std::chrono::duration_cast(std::chrono:: system_clock::now().time_since_epoch()).count(); if ((now - g_lastWatchTime) < 0 || (now - g_lastWatchTime) >= (g_stuckTimeout / RATIO)) { diff --git a/interfaces/ndk/include/parcel.h b/interfaces/ndk/include/parcel.h deleted file mode 100644 index 57a22bd81055b3e9e16685b4a302035f8f88ebea..0000000000000000000000000000000000000000 --- a/interfaces/ndk/include/parcel.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef HIVIEWDFX_HICOLLIE_PARCEL_H -#define HIVIEWDFX_HICOLLIE_PARCEL_H - -class Parcel final {}; - -class Parcelable { -public: - virtual ~Parcelable() = default; - virtual bool Marshalling(Parcel &parcel) const = 0; -}; - -#endif // HIVIEWDFX_HICOLLIE_PARCEL_H diff --git a/interfaces/ndk/include/report_data.h b/interfaces/ndk/include/report_data.h deleted file mode 100644 index ab4da04d7e1d7dceea2cdc90bcb63f3c4513aa71..0000000000000000000000000000000000000000 --- a/interfaces/ndk/include/report_data.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef HIVIEWDFX_HICOLLIE_FAULT_DATA_H -#define HIVIEWDFX_HICOLLIE_FAULT_DATA_H - -#include -#include "parcel.h" -#include "iremote_object.h" - -namespace OHOS { -namespace HiviewDFX { -struct ErrorObject { - std::string name = ""; - std::string message = ""; - std::string stack = ""; -}; - -enum class FaultDataType { - UNKNOWN = -1, - APP_FREEZE = 3 -}; - -/** - * @struct ReportData - * ReportData is used to save information about report data. - */ -struct ReportData : public Parcelable { - virtual bool Marshalling(Parcel &parcel) const override; - bool WriteContent(Parcel &parcel) const; - // error object - ErrorObject errorObject; - FaultDataType faultType = FaultDataType::UNKNOWN; - std::string timeoutMarkers = ""; - bool waitSaveState = false; - bool notifyApp = false; - bool forceExit = false; - bool needKillProcess = true; - uint32_t state = 0; - int32_t eventId = -1; - int32_t tid = 0; - uint32_t stuckTimeout = 0; - sptr token = nullptr; - std::string freezeInfo = ""; - std::string appRunningUniqueId; - std::string procStatm; -}; -} // end of HiviewDFX -} // end of OHOS -#endif // HIVIEWDFX_HICOLLIE_FAULT_DATA_H diff --git a/interfaces/ndk/report_data.cpp b/interfaces/ndk/report_data.cpp deleted file mode 100644 index d0ad9f7f8aeb5b8216ea0725b640c3e42804f2bb..0000000000000000000000000000000000000000 --- a/interfaces/ndk/report_data.cpp +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright (c) 2024 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "string_ex.h" -#include "report_data.h" -#include "xcollie_utils.h" - -namespace OHOS { -namespace HiviewDFX { -bool ReportData::WriteContent(Parcel &parcel) const -{ - if (!parcel.WriteInt32(tid)) { - XCOLLIE_LOGE("Tid [%{public}u] write int32 failed.", tid); - return false; - } - - if (!parcel.WriteUint32(stuckTimeout)) { - XCOLLIE_LOGE("StuckTimeout [%{public}u] write uint32 failed.", stuckTimeout); - return false; - } - - if (token == nullptr) { - if (!parcel.WriteBool(false)) { - XCOLLIE_LOGE("Token falge [false] write bool failed."); - return false; - } - } else { - if (!parcel.WriteBool(true) || !(static_cast(&parcel))->WriteRemoteObject(token)) { - XCOLLIE_LOGE("Token falge [true] write bool failed."); - return false; - } - } - - if (!parcel.WriteString(freezeInfo)) { - XCOLLIE_LOGE("FreezeInfo [%{public}s] write string failed.", freezeInfo.c_str()); - return false; - } - - if (!parcel.WriteString(appRunningUniqueId)) { - XCOLLIE_LOGE("AppRunningUniqueId [%{public}s] write string failed.", appRunningUniqueId.c_str()); - return false; - } - - if (!parcel.WriteString(procStatm)) { - XCOLLIE_LOGE("ProcStatm [%{public}s] write string failed.", procStatm.c_str()); - return false; - } - return true; -} - -bool ReportData::Marshalling(Parcel &parcel) const -{ - if (!parcel.WriteString(errorObject.name)) { - XCOLLIE_LOGE("Name [%{public}s] write string failed.", errorObject.name.c_str()); - return false; - } - - if (!parcel.WriteString(errorObject.message)) { - XCOLLIE_LOGE("Message [%{public}s] write string failed.", errorObject.message.c_str()); - return false; - } - - if (!parcel.WriteString(errorObject.stack)) { - XCOLLIE_LOGE("Stack [%{public}s] write string failed.", errorObject.stack.c_str()); - return false; - } - - if (!parcel.WriteInt32(static_cast(faultType))) { - XCOLLIE_LOGE("FaultType [%{public}d] write int32 failed.", static_cast(faultType)); - return false; - } - - if (!parcel.WriteString(timeoutMarkers)) { - XCOLLIE_LOGE("TimeoutMarkers [%{public}s] write string failed.", timeoutMarkers.c_str()); - return false; - } - - if (!parcel.WriteBool(waitSaveState)) { - XCOLLIE_LOGE("WaitSaveState [%{public}s] write bool failed.", waitSaveState ? "true" : "false"); - return false; - } - - if (!parcel.WriteBool(notifyApp)) { - XCOLLIE_LOGE("NotifyApp [%{public}s] write bool failed.", notifyApp ? "true" : "false"); - return false; - } - - if (!parcel.WriteBool(forceExit)) { - XCOLLIE_LOGE("ForceExit [%{public}s] write bool failed.", forceExit ? "true" : "false"); - return false; - } - - if (!parcel.WriteBool(needKillProcess)) { - XCOLLIE_LOGE("needKillProcess [%{public}s] write bool failed.", needKillProcess ? "true" : "false"); - return false; - } - - if (!parcel.WriteUint32(state)) { - XCOLLIE_LOGE("State [%{public}u] write uint32 failed.", state); - return false; - } - - if (!parcel.WriteInt32(eventId)) { - XCOLLIE_LOGE("EventId [%{public}u] write int32 failed.", eventId); - return false; - } - - return WriteContent(parcel); -} -} // end of HiviewDFX -} // end of OHOS