diff --git a/inspector/inspector.cpp b/inspector/inspector.cpp index f965277364f4c45c2b026a06afe330bc33f87fd4..d54facc60d843a428d179f37755700dd52f7e9d3 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 387f48722f272231ecda2f08e72216d6a6aaf498..826789146943cd5f2ce95a8a76cf2b70f13343b8 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;