diff --git a/inspector/inspector.cpp b/inspector/inspector.cpp index 3769358e806b5e797278b4afaf2a65296e1a3655..59a64ce7e043d62e349526cb14adf856ec0fa98e 100644 --- a/inspector/inspector.cpp +++ b/inspector/inspector.cpp @@ -20,6 +20,8 @@ #include #include +#include + #include "log_wrapper.h" #include "library_loader.h" @@ -99,6 +101,13 @@ void* GetArkDynFunction(const char* symbol) } #endif +#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); @@ -271,6 +280,12 @@ 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); } diff --git a/tooling/agent/debugger_impl.cpp b/tooling/agent/debugger_impl.cpp index 284cdfb78e67f8998fdc0d18c4da3c86117fd4e3..4982d21f80523ab055019c866bfc8450b47bb93a 100644 --- a/tooling/agent/debugger_impl.cpp +++ b/tooling/agent/debugger_impl.cpp @@ -294,7 +294,8 @@ void DebuggerImpl::DispatcherImpl::Dispatch(const DispatchRequest &request) { "stepOver", &DebuggerImpl::DispatcherImpl::StepOver }, { "setMixedDebugEnabled", &DebuggerImpl::DispatcherImpl::SetMixedDebugEnabled }, { "setBlackboxPatterns", &DebuggerImpl::DispatcherImpl::SetBlackboxPatterns }, - { "replyNativeCalling", &DebuggerImpl::DispatcherImpl::ReplyNativeCalling } + { "replyNativeCalling", &DebuggerImpl::DispatcherImpl::ReplyNativeCalling }, + { "setFastRequest", &DebuggerImpl::DispatcherImpl::SetFastRequest } }; const std::string &method = request.GetMethod(); @@ -483,6 +484,17 @@ void DebuggerImpl::DispatcherImpl::ReplyNativeCalling(const DispatchRequest &req SendResponse(request, response); } +void DebuggerImpl::DispatcherImpl::SetFastRequest(const DispatchRequest &request) +{ + std::unique_ptr params = SetFastRequestParams::Create(request.GetParams()); + if (params == nullptr) { + SendResponse(request, DispatchResponse::Fail("wrong params")); + return; + } + DispatchResponse response = debugger_->SetFastRequest(*params); + SendResponse(request, response); +} + void DebuggerImpl::DispatcherImpl::SetBlackboxPatterns(const DispatchRequest &request) { DispatchResponse response = debugger_->SetBlackboxPatterns(); @@ -522,6 +534,15 @@ void DebuggerImpl::Frontend::NativeCalling(const EcmaVM *vm, const tooling::Nati channel_->SendNotification(nativeCalling); } +// void DebuggerImpl::Frontend::FastRequest(const EcmaVM *vm, const tooling::NativeCalling &fastRe) +// { +// if (!AllowNotify(vm)) { +// return; +// } + +// channel_->SendNotification(nativeCalling); +// } + void DebuggerImpl::Frontend::Resumed(const EcmaVM *vm) { if (!AllowNotify(vm)) { @@ -862,6 +883,12 @@ DispatchResponse DebuggerImpl::ReplyNativeCalling([[maybe_unused]] const ReplyNa return DispatchResponse::Ok(); } +DispatchResponse DebuggerImpl::SetFastRequest([[maybe_unused]] const SetFastRequestParams ¶ms) +{ + frontend_.Resumed(vm_); + return DispatchResponse::Ok(); +} + void DebuggerImpl::CleanUpOnPaused() { runtime_->curObjectId_ = 0; diff --git a/tooling/agent/debugger_impl.h b/tooling/agent/debugger_impl.h index c49f56ea65fd90d08da01bd0298f81d0ba56787d..6da3fb0f2fe32605a321153f26413a31d5668400 100644 --- a/tooling/agent/debugger_impl.h +++ b/tooling/agent/debugger_impl.h @@ -64,6 +64,7 @@ public: DispatchResponse SetBlackboxPatterns(); DispatchResponse SetMixedDebugEnabled(const SetMixedDebugParams ¶ms); DispatchResponse ReplyNativeCalling(const ReplyNativeCallingParams ¶ms); + DispatchResponse SetFastRequest(const SetFastRequestParams ¶ms); /** * @brief: match first script and callback @@ -124,7 +125,7 @@ public: void SetMixedDebugEnabled(const DispatchRequest &request); void SetBlackboxPatterns(const DispatchRequest &request); void ReplyNativeCalling(const DispatchRequest &request); - + void SetFastRequest(const DispatchRequest &request); private: NO_COPY_SEMANTIC(DispatcherImpl); NO_MOVE_SEMANTIC(DispatcherImpl); diff --git a/tooling/base/pt_params.cpp b/tooling/base/pt_params.cpp index 7b7194714554d24b5276457f398b34b9efdd0649..4b90bc60fa3160fbc4587ea080fe422467950f49 100644 --- a/tooling/base/pt_params.cpp +++ b/tooling/base/pt_params.cpp @@ -471,6 +471,28 @@ std::unique_ptr ReplyNativeCallingParams::Create(const return paramsObject; } +std::unique_ptr SetFastRequestParams::Create(const PtJson ¶ms) +{ + auto paramsObject = std::make_unique(); + std::string error; + Result ret; + + bool isFastDispatch = false; + ret = params.GetBool("isFastDispatch", &isFastDispatch); + if (ret == Result::SUCCESS) { + paramsObject->isFastDispatch_ = isFastDispatch; + } else if (ret == Result::TYPE_ERROR) { + error += "Unknown 'isFastDispatch';"; + } + + if (!error.empty()) { + LOG_DEBUGGER(ERROR) << "SetFastRequestParams::Create " << error; + return nullptr; + } + + return paramsObject; +} + std::unique_ptr GetPropertiesParams::Create(const PtJson ¶ms) { auto paramsObject = std::make_unique(); diff --git a/tooling/base/pt_params.h b/tooling/base/pt_params.h index 56bd93be98f1d8b1ff39169a677e6014e3d920e1..e0a62da2fda1021fbfbebd1c27f67a7148dd0418 100644 --- a/tooling/base/pt_params.h +++ b/tooling/base/pt_params.h @@ -447,6 +447,24 @@ private: bool userCode_ { false }; }; +class SetFastRequestParams : public PtBaseParams { +public: + SetFastRequestParams() = default; + ~SetFastRequestParams() override = default; + static std::unique_ptr Create(const PtJson ¶ms); + + bool GetIsFastDispatch() const + { + return isFastDispatch_; + } + +private: + NO_COPY_SEMANTIC(SetFastRequestParams); + NO_MOVE_SEMANTIC(SetFastRequestParams); + + bool isFastDispatch_ { false }; +}; + class GetPropertiesParams : public PtBaseParams { public: GetPropertiesParams() = default; diff --git a/tooling/protocol_handler.cpp b/tooling/protocol_handler.cpp index d685a4e79f2fc3c7f9699a38e3aed68933d90ff1..a8def09175d119ff190093808883010eaa0e2dbb 100644 --- a/tooling/protocol_handler.cpp +++ b/tooling/protocol_handler.cpp @@ -17,6 +17,8 @@ #include "agent/debugger_impl.h" +#include + namespace panda::ecmascript::tooling { void ProtocolHandler::WaitForDebugger() { @@ -33,8 +35,31 @@ void ProtocolHandler::DispatchCommand(std::string &&msg) { LOG_DEBUGGER(DEBUG) << "ProtocolHandler::DispatchCommand: " << msg; std::unique_lock queueLock(requestLock_); - requestQueue_.push(std::move(msg)); - requestQueueCond_.notify_one(); + requestQueue_.emplace(std::move(msg)); +#if !defined(WINDOWS_PLATFORM) + if (CheckNeedFastDispatch(requestQueue_.front())) { + LOG_DEBUGGER(INFO) << "ProtocolHandler::CheckNeedFastDispatch"; + queueLock.unlock(); + pthread_kill(tid_, SIGALRM); + } else { + requestQueueCond_.notify_one(); + } +#endif +} + +bool ProtocolHandler::CheckNeedFastDispatch(const DispatchRequest &request) +{ + if (request.GetDomain() == "Debugger" && request.GetMethod() == "setFastRequest") { + bool isFastDispatch = false; + if (request.GetParams().GetBool("isFastDispatch", &isFastDispatch) == Result::SUCCESS) { + isFastDispatch_ = isFastDispatch; + // should not dispatch setFastRequest + requestQueue_.pop(); + } + return false; + } + + return isFastDispatch_; } // called after DispatchCommand @@ -52,7 +77,7 @@ int32_t ProtocolHandler::GetDispatchStatus() void ProtocolHandler::ProcessCommand() { - std::queue dispatchingQueue; + std::queue dispatchingQueue; do { { std::unique_lock queueLock(requestLock_); @@ -67,12 +92,10 @@ void ProtocolHandler::ProcessCommand() isDispatchingMessage_ = true; while (!dispatchingQueue.empty()) { - std::string msg = std::move(dispatchingQueue.front()); - dispatchingQueue.pop(); - [[maybe_unused]] LocalScope scope(vm_); auto exception = DebuggerApi::GetAndClearException(vm_); - dispatcher_.Dispatch(DispatchRequest(msg)); + dispatcher_.Dispatch(dispatchingQueue.front()); + dispatchingQueue.pop(); DebuggerApi::SetException(vm_, exception); } isDispatchingMessage_ = false; diff --git a/tooling/protocol_handler.h b/tooling/protocol_handler.h index ee7a046aad54c41270672a43f02e0032a56775f3..c8decb90424b63f38f642837d5c25d5a1e55385e 100644 --- a/tooling/protocol_handler.h +++ b/tooling/protocol_handler.h @@ -33,7 +33,7 @@ public: }; ProtocolHandler(std::function callback, const EcmaVM *vm) - : callback_(std::move(callback)), dispatcher_(vm, this), vm_(vm) {} + : callback_(std::move(callback)), dispatcher_(vm, this), vm_(vm), tid_(pthread_self()) {} ~ProtocolHandler() override = default; void WaitForDebugger() override; @@ -51,15 +51,18 @@ private: NO_COPY_SEMANTIC(ProtocolHandler); std::unique_ptr CreateErrorReply(const DispatchResponse &response); void SendReply(const PtJson &reply); + bool CheckNeedFastDispatch(const DispatchRequest &request); std::function callback_; Dispatcher dispatcher_; bool waitingForDebugger_ {false}; const EcmaVM *vm_ {nullptr}; + pthread_t tid_ {0}; + bool isFastDispatch_ {false}; std::condition_variable requestQueueCond_; - std::queue requestQueue_; + std::queue requestQueue_; std::mutex requestLock_; std::atomic isDispatchingMessage_ {false}; };