From 6fefa1ecbd04fc4000c4c6448547f73eb4530890 Mon Sep 17 00:00:00 2001 From: Gymee Date: Tue, 18 Oct 2022 20:40:35 +0800 Subject: [PATCH] resort signal as last way to dispatch message Signed-off-by: Gymee Change-Id: I0c73bf270fe8a49c2b6550442b40ad3bb7758071 --- inspector/inspector.cpp | 28 +++++++++++++++++++++++++++- inspector/inspector.h | 1 + 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/inspector/inspector.cpp b/inspector/inspector.cpp index f9652773..d54facc6 100644 --- a/inspector/inspector.cpp +++ b/inspector/inspector.cpp @@ -20,6 +20,8 @@ #include #include +#include + #include "log_wrapper.h" #include "library_loader.h" @@ -89,6 +91,13 @@ void* GetArkDynFunction(const char* symbol) return ResolveSymbol(g_handle, symbol); } +#if !defined(WINDOWS_PLATFORM) +void DispatchMessage(int) +{ + g_processMessage(g_vm); +} +#endif + void SendReply(const void* vm, const std::string& message) { std::shared_lock lock(g_mutex); @@ -213,9 +222,18 @@ void Inspector::OnMessage(std::string&& msg) return; } - // the debugger thread maybe in idle status, so try to post a task to wake it up + // otherwise, the debugger thread maybe in idle status or busy for executing native code +#if !defined(WINDOWS_PLATFORM) + // NOTE: this way maybe unsafe + std::this_thread::sleep_for(std::chrono::milliseconds(DELAY_RECHECK_DISPATCH_STATUS)); + if (g_getDispatchStatus(vm_) == DispatchStatus::UNKNOWN) { + LOGW("Resort to pthread_kill"); + pthread_kill(tid_, SIGALRM); + } +#else if (debuggerPostTask_ != nullptr) { debuggerPostTask_([tid = tid_, vm = vm_] { + LOGI("Resort to DebuggerPostTask"); if (tid != pthread_self()) { LOGE("Task not in debugger thread"); return; @@ -225,6 +243,7 @@ void Inspector::OnMessage(std::string&& msg) } else { LOGW("No debuggerPostTask provided"); } +#endif } bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode, int32_t instanceId, @@ -246,9 +265,16 @@ bool StartDebug(const std::string& componentName, void* vm, bool isDebugMode, in return false; } +#if !defined(WINDOWS_PLATFORM) + if (signal(SIGALRM, &DispatchMessage) == SIG_ERR) { + LOGW("Install signal handler failed"); + } +#endif + if (isDebugMode) { g_waitForDebugger(vm); } + LOGI("StartDebug end"); return true; } diff --git a/inspector/inspector.h b/inspector/inspector.h index 387f4872..82678914 100644 --- a/inspector/inspector.h +++ b/inspector/inspector.h @@ -51,6 +51,7 @@ public: void OnMessage(std::string&& msg); static constexpr int32_t DELAY_CHECK_DISPATCH_STATUS = 100; + static constexpr int32_t DELAY_RECHECK_DISPATCH_STATUS = 50; pthread_t tid_ = 0; void* vm_ = nullptr; -- Gitee