diff --git a/frameworks/native/appkit/app/mix_stack_dumper.cpp b/frameworks/native/appkit/app/mix_stack_dumper.cpp index 353dd4f7fa..7a59ddd152 100644 --- a/frameworks/native/appkit/app/mix_stack_dumper.cpp +++ b/frameworks/native/appkit/app/mix_stack_dumper.cpp @@ -207,18 +207,25 @@ bool MixStackDumper::DumpMixFrame(int fd, pid_t nstid, pid_t tid) std::vector nativeFrames; bool hasNativeFrame = true; + bool isVmSuspended = false; + auto application = application_.lock(); + if (application != nullptr && application->GetRuntime() != nullptr) { + isVmSuspended = application->GetRuntime()->SuspendVM(nstid); + } if (!catcher_->CatchFrame(nstid, nativeFrames)) { hasNativeFrame = false; } bool hasJsFrame = true; std::vector jsFrames; - auto application = application_.lock(); // if we failed to get native frame, target thread may not be seized - if (application != nullptr && application->GetRuntime() != nullptr && hasNativeFrame) { + if (isVmSuspended) { hasJsFrame = application->GetRuntime()->BuildJsStackInfoList(nstid, jsFrames); } catcher_->ReleaseThread(nstid); + if (isVmSuspended) { + application->GetRuntime()->ResumeVM(nstid); + } if (jsFrames.size() == 0) { hasJsFrame = false; diff --git a/frameworks/native/runtime/js_runtime.cpp b/frameworks/native/runtime/js_runtime.cpp index 3ff487fd86..9e7af19c1b 100644 --- a/frameworks/native/runtime/js_runtime.cpp +++ b/frameworks/native/runtime/js_runtime.cpp @@ -955,6 +955,30 @@ void JsRuntime::NotifyApplicationState(bool isBackground) HILOG_INFO("NotifyApplicationState, isBackground %{public}d.", isBackground); } +bool JsRuntime::SuspendVM(uint32_t tid) +{ + auto nativeEngine = GetNativeEnginePointer(); + CHECK_POINTER_AND_RETURN(nativeEngine, false); + auto arkNativeEngine = nativeEngine->GetWorkerEngine(tid); + if (arkNativeEngine == nullptr) { + HILOG_ERROR("SuspendVM arkNativeEngine is nullptr"); + return false; + } + return arkNativeEngine->SuspendVM(); +} + +void JsRuntime::ResumeVM(uint32_t tid) +{ + auto nativeEngine = GetNativeEnginePointer(); + CHECK_POINTER(nativeEngine); + auto arkNativeEngine = nativeEngine->GetWorkerEngine(tid); + if (arkNativeEngine == nullptr) { + HILOG_ERROR("ResumeVM arkNativeEngine is nullptr"); + return; + } + arkNativeEngine->ResumeVM(); +} + void JsRuntime::PreloadSystemModule(const std::string& moduleName) { HandleScope handleScope(*this); diff --git a/interfaces/inner_api/runtime/include/js_runtime.h b/interfaces/inner_api/runtime/include/js_runtime.h index 31883566a0..4d2be4764c 100644 --- a/interfaces/inner_api/runtime/include/js_runtime.h +++ b/interfaces/inner_api/runtime/include/js_runtime.h @@ -85,6 +85,8 @@ public: void DumpHeapSnapshot(bool isPrivate) override; bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) override; void NotifyApplicationState(bool isBackground) override; + bool SuspendVM(uint32_t tid) override; + void ResumeVM(uint32_t tid) override; bool RunSandboxScript(const std::string& path, const std::string& hapPath); bool RunScript(const std::string& path, const std::string& hapPath, bool useCommonChunk = false); diff --git a/interfaces/inner_api/runtime/include/runtime.h b/interfaces/inner_api/runtime/include/runtime.h index beeef5b895..96fc0d2cb8 100644 --- a/interfaces/inner_api/runtime/include/runtime.h +++ b/interfaces/inner_api/runtime/include/runtime.h @@ -79,6 +79,8 @@ public: virtual bool BuildJsStackInfoList(uint32_t tid, std::vector& jsFrames) = 0; virtual void DumpHeapSnapshot(bool isPrivate) = 0; virtual void NotifyApplicationState(bool isBackground) = 0; + virtual bool SuspendVM(uint32_t tid) = 0; + virtual void ResumeVM(uint32_t tid) = 0; virtual void PreloadSystemModule(const std::string& moduleName) = 0; virtual void FinishPreload() = 0; virtual bool LoadRepairPatch(const std::string& patchFile, const std::string& baseFile) = 0; diff --git a/test/mock/frameworks_kits_runtime_test/mock_runtime.h b/test/mock/frameworks_kits_runtime_test/mock_runtime.h index bafd1d77cb..d6eceaf6ac 100644 --- a/test/mock/frameworks_kits_runtime_test/mock_runtime.h +++ b/test/mock/frameworks_kits_runtime_test/mock_runtime.h @@ -47,6 +47,11 @@ public: { return true; } + bool SuspendVM(uint32_t tid) override + { + return true; + } + void ResumeVM(uint32_t tid) override {} bool UnLoadRepairPatch(const std::string& patchFile) override { return true; diff --git a/test/unittest/ability_runtime_error_util_test/mock_native_engine.h b/test/unittest/ability_runtime_error_util_test/mock_native_engine.h index ccfa90acec..6c84f2cbde 100644 --- a/test/unittest/ability_runtime_error_util_test/mock_native_engine.h +++ b/test/unittest/ability_runtime_error_util_test/mock_native_engine.h @@ -310,12 +310,12 @@ public: return true; } - bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override + bool DeleteWorker(NativeEngine* workerEngine) override { return true; } - NativeEngine* GetWorkerVm(NativeEngine* hostEngine, uint32_t tid) override + NativeEngine* GetWorkerEngine(uint32_t tid) override { return nullptr; } diff --git a/test/unittest/runtime_test/mock_js_native_engine.h b/test/unittest/runtime_test/mock_js_native_engine.h index 680b17d9dd..14bafa8f67 100644 --- a/test/unittest/runtime_test/mock_js_native_engine.h +++ b/test/unittest/runtime_test/mock_js_native_engine.h @@ -309,12 +309,12 @@ public: return true; } - bool DeleteWorker(NativeEngine* hostEngine, NativeEngine* workerEngine) override + bool DeleteWorker(NativeEngine* workerEngine) override { return true; } - NativeEngine* GetWorkerVm(NativeEngine* hostEngine, uint32_t tid) override + NativeEngine* GetWorkerEngine(uint32_t tid) override { return nullptr; }