From 81598da1d09b6065c1c0bf0a97d5c0e4778416c3 Mon Sep 17 00:00:00 2001 From: wanchengzhen Date: Sat, 26 Feb 2022 16:04:53 +0800 Subject: [PATCH 1/2] Add JS exception interception Signed-off-by: wanchengzhen --- frameworks/kits/appkit/BUILD.gn | 1 + .../appkit/native/app/src/main_thread.cpp | 62 ++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/frameworks/kits/appkit/BUILD.gn b/frameworks/kits/appkit/BUILD.gn index 43cb6d0433d..969611ed98a 100644 --- a/frameworks/kits/appkit/BUILD.gn +++ b/frameworks/kits/appkit/BUILD.gn @@ -170,6 +170,7 @@ ohos_shared_library("appkit_native") { "bytrace_standard:bytrace_core", "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", + "hisysevent_native:libhisysevent", "ipc:ipc_core", "napi:ace_napi", ] diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index faace598f7c..b4a25552764 100644 --- a/frameworks/kits/appkit/native/app/src/main_thread.cpp +++ b/frameworks/kits/appkit/native/app/src/main_thread.cpp @@ -46,6 +46,8 @@ #include "task_handler_client.h" #include "faultloggerd_client.h" #include "dfx_dump_catcher.h" +#include "hisysevent.h" +#include "js_runtime_utils.h" #if defined(ABILITY_LIBRARY_LOADER) || defined(APPLICATION_LIBRARY_LOADER) #include @@ -60,6 +62,13 @@ namespace { constexpr int32_t DELIVERY_TIME = 200; constexpr int32_t DISTRIBUTE_TIME = 100; constexpr int32_t UNSPECIFIED_USERID = -2; + +constexpr char EVENT_KEY_UID[] = "UID"; +constexpr char EVENT_KEY_PID[] = "PID"; +constexpr char EVENT_KEY_MESSAGE[] = "MSG"; +constexpr char EVENT_KEY_PACKAGE_NAME[] = "PACKAGE_NAME"; +constexpr char EVENT_KEY_PROCESS_NAME[] = "PROCESS_NAME"; +constexpr uint8_t CAP_MAX_SIZE = 64; } #define ACEABILITY_LIBRARY_LOADER @@ -870,7 +879,58 @@ void MainThread::HandleLaunchApplication(const AppLaunchData &appLaunchData, con APP_LOGE("OHOSApplication::OHOSApplication: Failed to create runtime"); return; } - + auto& jsEngine = (static_cast(*runtime)).GetNativeEngine(); + auto bundleName = appInfo.bundleName; + auto uid = appInfo.uid; + auto processName = processInfo.GetProcessName(); + auto processPid = processInfo.GetPid(); + wptr weak = this; + auto uncaughtTask = [weak, uid, processPid, bundleName, processName](NativeValue* v) { + APP_LOGI("RegisterUncaughtExceptionHandler Begin"); + NativeObject* obj = AbilityRuntime::ConvertNativeValueTo(v); + NativeValue* message = obj->GetProperty("message"); + NativeString* messageStr = AbilityRuntime::ConvertNativeValueTo(message); + if (messageStr == nullptr) { + APP_LOGE("messageStr Convert failed"); + return; + } + size_t messagebufferLen = messageStr->GetLength(); + size_t messagestrLen = 0; + char messagecap[CAP_MAX_SIZE] = { 0 }; + messageStr->GetCString(messagecap, messagebufferLen + 1, &messagestrLen); + APP_LOGI("messagecap = %{public}s", messagecap); + NativeValue* stack = obj->GetProperty("stack"); + NativeString* stackStr = AbilityRuntime::ConvertNativeValueTo(stack); + if (stackStr == nullptr) { + APP_LOGE("stackStr Convert failed"); + return; + } + size_t stackbufferLen = stackStr->GetLength(); + size_t stackstrLen = 0; + char stackcap[CAP_MAX_SIZE] = { 0 }; + stackStr->GetCString(stackcap, stackbufferLen + 1, &stackstrLen); + APP_LOGI("stackcap = %{public}s", stackcap); + auto appThread = weak.promote(); + if (appThread == nullptr) { + APP_LOGE("appThread is nullptr, HandleLaunchApplication failed."); + return; + } + std::string eventType = "JS_EXCEPTION"; + std::string msgContent; + std::string tempMessageStr(messagecap); + std::string tempStackStr(stackcap); + msgContent = " message:" + tempMessageStr + " Stack:" + tempStackStr; + auto ret = OHOS::HiviewDFX::HiSysEvent::Write(OHOS::HiviewDFX::HiSysEvent::Domain::AAFWK, eventType, + OHOS::HiviewDFX::HiSysEvent::EventType::FAULT, + EVENT_KEY_UID, std::to_string(uid), + EVENT_KEY_PID, std::to_string(processPid), + EVENT_KEY_PACKAGE_NAME, bundleName, + EVENT_KEY_PROCESS_NAME, processName, + EVENT_KEY_MESSAGE, msgContent); + appThread->ScheduleProcessSecurityExit(); + APP_LOGI("RegisterUncaughtExceptionHandler End ret = %{public}d", ret); + }; + jsEngine.RegisterUncaughtExceptionHandler(uncaughtTask); application_->SetRuntime(std::move(runtime)); auto usertestInfo = appLaunchData.GetUserTestInfo(); if (usertestInfo.observer) { -- Gitee From ab6622ed4a83273c565963b0e772c26ab4eef4b4 Mon Sep 17 00:00:00 2001 From: wanchengzhen Date: Sat, 26 Feb 2022 16:31:38 +0800 Subject: [PATCH 2/2] Format gn Signed-off-by: wanchengzhen --- frameworks/kits/appkit/BUILD.gn | 1 - 1 file changed, 1 deletion(-) diff --git a/frameworks/kits/appkit/BUILD.gn b/frameworks/kits/appkit/BUILD.gn index 969611ed98a..43cb6d0433d 100644 --- a/frameworks/kits/appkit/BUILD.gn +++ b/frameworks/kits/appkit/BUILD.gn @@ -170,7 +170,6 @@ ohos_shared_library("appkit_native") { "bytrace_standard:bytrace_core", "hisysevent_native:libhisysevent", "hiviewdfx_hilog_native:libhilog", - "hisysevent_native:libhisysevent", "ipc:ipc_core", "napi:ace_napi", ] -- Gitee