Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] inspector: support for worker inspection in chrome devtools #56759

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,18 @@ added: v22.4.0

Enable experimental [`Web Storage`][] support.

### `--experimental-worker-inspection`

<!-- YAML
added:
- v22.14.0
- v20.19.0
-->

> Stability: 1 - Experimental

Enable experimental support for the worker inspection with Chrome DevTools.

### `--expose-gc`

<!-- YAML
Expand Down
8 changes: 8 additions & 0 deletions src/inspector/main_thread_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,13 @@ class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> {
std::unique_ptr<InspectorSessionDelegate> MakeDelegateThreadSafe(
std::unique_ptr<InspectorSessionDelegate> delegate);
bool Expired();
void SetTargetSessionId(int target_session_id) {
target_session_id_ = target_session_id;
}
std::optional<int> GetTargetSessionId() {
return target_session_id_;
}


private:
void Reset();
Expand All @@ -65,6 +72,7 @@ class MainThreadHandle : public std::enable_shared_from_this<MainThreadHandle> {
Mutex block_lock_;
int next_session_id_ = 0;
std::atomic_int next_object_id_ = {1};
std::optional<int> target_session_id_ = std::nullopt;

friend class MainThreadInterface;
};
Expand Down
5 changes: 5 additions & 0 deletions src/inspector/node_inspector.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
'src/inspector/network_inspector.h',
'src/inspector/network_agent.cc',
'src/inspector/network_agent.h',
'src/inspector/network_agent.h',
'src/inspector/target_agent.cc',
'src/inspector/target_agent.h',
'src/inspector/worker_inspector.cc',
'src/inspector/worker_inspector.h',
],
Expand All @@ -42,6 +45,8 @@
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/NodeRuntime.h',
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Network.cpp',
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Network.h',
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Target.cpp',
'<(SHARED_INTERMEDIATE_DIR)/src/node/inspector/protocol/Target.h',
],
'node_protocol_files': [
'<(protocol_tool_path)/lib/Allocator_h.template',
Expand Down
22 changes: 22 additions & 0 deletions src/inspector/node_protocol.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,25 @@ experimental domain NodeRuntime
# This event is fired when the runtime is waiting for the debugger. For
# example, when inspector.waitingForDebugger is called
event waitingForDebugger

# https://chromedevtools.github.io/devtools-protocol/1-3/Target/
experimental domain Target
type SessionID extends string
type TargetID extends string
type TargetInfo extends object
properties
TargetID targetId
string type
string title
string url
boolean attached
boolean canAccessOpener
event targetCreated
parameters
TargetInfo targetInfo
event attachedToTarget
parameters
SessionID sessionId
TargetInfo targetInfo
boolean waitingForDebugger

96 changes: 96 additions & 0 deletions src/inspector/target_agent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#include "target_agent.h"
#include <iostream>
#include <memory>
#include <set>
#include <string>
#include <tuple>
#include <utility>
#include "inspector/node_string.h"
#include "inspector/worker_inspector.h"
#include "main_thread_interface.h"

namespace node {
namespace inspector {
namespace protocol {

std::unordered_map<int,std::shared_ptr<MainThreadHandle>> TargetAgent::target_session_id_worker_map_ = std::unordered_map<int, std::shared_ptr<MainThreadHandle>>();


class WorkerTargetDelegate : public WorkerDelegate {
public:
explicit WorkerTargetDelegate(std::shared_ptr<TargetAgent> target_agent)
: target_agent_(target_agent) {}


void WorkerCreated(const std::string& title,
const std::string& url,
bool waiting,
std::shared_ptr<MainThreadHandle> worker) override {
std::string target_id = std::to_string(target_agent_->getNextTargetId());
std::string type = "worker";

target_agent_->targetCreated(target_id, type, title, url);
target_agent_->attachedToTarget(worker,target_id, type, title, url);
}

private:
const std::shared_ptr<TargetAgent> target_agent_;
};

std::unique_ptr<Target::TargetInfo> createTargetInfo(
const String& target_id,
const String& type,
const String& title,
const String& url) {
return Target::TargetInfo::create()
.setTargetId(target_id)
.setType(type)
.setTitle(title)
.setUrl(url)
.setAttached(false)
.setCanAccessOpener(true)
.build();
}

void TargetAgent::Wire(UberDispatcher* dispatcher) {
frontend_ = std::make_unique<Target::Frontend>(dispatcher->channel());
Target::Dispatcher::wire(dispatcher, this);
}

void TargetAgent::listenWorker(std::weak_ptr<WorkerManager> worker_manager) {
auto manager = worker_manager.lock();
if (!manager) {
return;
}
std::unique_ptr<WorkerDelegate> delegate(new WorkerTargetDelegate(shared_from_this()));
worker_event_handle_ = manager->SetAutoAttach(std::move(delegate));
}

void TargetAgent::reset() {
if (worker_event_handle_) {
worker_event_handle_.reset();
}
}

void TargetAgent::targetCreated(const std::string &target_id,const std::string &type,const std::string &title,const std::string &url) {
frontend_->targetCreated(createTargetInfo(target_id, type, title, url));
}

int TargetAgent::getNextSessionId() {
return next_session_id_++;
}

int TargetAgent::getNextTargetId() {
return next_target_id_++;
}

void TargetAgent::attachedToTarget(std::shared_ptr<MainThreadHandle> worker,const std::string &target_id, const std::string &type,const std::string &title,const std::string &url) {
int session_id = getNextSessionId();
target_session_id_worker_map_[session_id] = worker;
worker->SetTargetSessionId(session_id);
frontend_->attachedToTarget(std::to_string(session_id), createTargetInfo(target_id, type, title, url), true);
}

} // namespace protocol
} // namespace inspector
} // namespace node
49 changes: 49 additions & 0 deletions src/inspector/target_agent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#ifndef SRC_INSPECTOR_TARGET_AGENT_H_
#define SRC_INSPECTOR_TARGET_AGENT_H_

#include "inspector/worker_inspector.h"
#include "node/inspector/protocol/Target.h"

#include <memory>
#include <string>
#include <thread>
#include <unordered_map>

namespace node {

namespace inspector {
class TargetInspector;

namespace protocol {

class TargetAgent : public Target::Backend, public std::enable_shared_from_this<TargetAgent>
{
public:
explicit TargetAgent() {}

void Wire(UberDispatcher* dispatcher);


void targetCreated(const std::string &target_id, const std::string &type,const std::string &title,const std::string &url);
void attachedToTarget(std::shared_ptr<MainThreadHandle> worker,const std::string &target_id, const std::string &type,const std::string &title,const std::string &url);

int getNextTargetId();

void listenWorker(std::weak_ptr<WorkerManager> worker_manager);
void reset();
static std::unordered_map<int,std::shared_ptr<MainThreadHandle>> target_session_id_worker_map_;
private:
int getNextSessionId();
std::shared_ptr<Target::Frontend> frontend_;
std::weak_ptr<WorkerManager> worker_manager_;
int next_session_id_ = 1;
int next_target_id_ = 1;
std::unique_ptr<WorkerManagerEventHandle> worker_event_handle_ = nullptr;
};

} // namespace protocol
} // namespace inspector
} // namespace node

#endif // SRC_INSPECTOR_TARGET_AGENT_H_

29 changes: 27 additions & 2 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "inspector_agent.h"
#include <string>

#include "env-inl.h"
#include "inspector/main_thread_interface.h"
#include "inspector/network_inspector.h"
#include "inspector/node_string.h"
#include "inspector/runtime_agent.h"
#include "inspector/target_agent.h"
#include "inspector/tracing_agent.h"
#include "inspector/worker_agent.h"
#include "inspector/worker_inspector.h"
Expand All @@ -18,6 +20,7 @@
#include "permission/permission.h"
#include "timer_wrap-inl.h"
#include "util-inl.h"
#include "util.h"
#include "v8-inspector.h"
#include "v8-platform.h"

Expand Down Expand Up @@ -216,8 +219,9 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
std::unique_ptr<InspectorSessionDelegate> delegate,
std::shared_ptr<MainThreadHandle> main_thread_,
bool prevent_shutdown)
: delegate_(std::move(delegate)), prevent_shutdown_(prevent_shutdown),
retaining_context_(false) {
: delegate_(std::move(delegate)), main_thread_(main_thread_),
prevent_shutdown_(prevent_shutdown), retaining_context_(false)
{
session_ = inspector->connect(CONTEXT_GROUP_ID,
this,
StringView(),
Expand All @@ -234,6 +238,11 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
runtime_agent_->Wire(node_dispatcher_.get());
network_inspector_ = std::make_unique<NetworkInspector>(env);
network_inspector_->Wire(node_dispatcher_.get());
if(env->options()->experimental_worker_inspection) {
target_agent_ = std::make_shared<protocol::TargetAgent>();
target_agent_->Wire(node_dispatcher_.get());
target_agent_->listenWorker(worker_manager);
}
}

~ChannelImpl() override {
Expand All @@ -247,6 +256,9 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
runtime_agent_.reset(); // Dispose before the dispatchers
network_inspector_->Disable();
network_inspector_.reset(); // Dispose before the dispatchers
if(target_agent_) {
target_agent_->reset();
}
}

void emitNotificationFromBackend(const StringView& event,
Expand Down Expand Up @@ -329,6 +341,17 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
"[inspector send] %s\n",
raw_message);
}
std::optional<int> target_session_id = main_thread_->GetTargetSessionId();
if (target_session_id.has_value()) {
std::string raw_message = protocol::StringUtil::StringViewToUtf8(message);
std::unique_ptr<protocol::DictionaryValue> value =
protocol::DictionaryValue::cast(
protocol::StringUtil::parseJSON(raw_message));
std::string target_session_id_str = std::to_string(*target_session_id);
value->setString("sessionId", target_session_id_str);
delegate_->SendMessageToFrontend(Utf8ToStringView(value->serializeToJSON())->string());
}

delegate_->SendMessageToFrontend(message);
}

Expand Down Expand Up @@ -356,10 +379,12 @@ class ChannelImpl final : public v8_inspector::V8Inspector::Channel,
std::unique_ptr<protocol::RuntimeAgent> runtime_agent_;
std::unique_ptr<protocol::TracingAgent> tracing_agent_;
std::unique_ptr<protocol::WorkerAgent> worker_agent_;
std::shared_ptr<protocol::TargetAgent> target_agent_;
std::unique_ptr<NetworkInspector> network_inspector_;
std::unique_ptr<InspectorSessionDelegate> delegate_;
std::unique_ptr<v8_inspector::V8InspectorSession> session_;
std::unique_ptr<protocol::UberDispatcher> node_dispatcher_;
std::shared_ptr<MainThreadHandle> main_thread_;
bool prevent_shutdown_;
bool retaining_context_;
};
Expand Down
Loading
Loading