diff --git a/frameworks/kits/appkit/native/app/src/main_thread.cpp b/frameworks/kits/appkit/native/app/src/main_thread.cpp index faace598f7c8a2a8391ce4ec743166f30ef01461..b4a2555276482b204a5ae972c1d828e35a12252c 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) {