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

inspector: support for worker inspection in chrome devtools #56759

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
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
11 changes: 11 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -1136,6 +1136,17 @@ added: v22.4.0

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

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

<!-- YAML
added:
- v22.13.1
-->

> Stability: 1 - Experimental

Enable experimental support for the worker inspection with Chrome DevTools.

### `--expose-gc`

<!-- YAML
Expand Down
5 changes: 5 additions & 0 deletions src/inspector/main_thread_interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ 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 +69,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 @@ -30,6 +30,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 @@ -45,6 +48,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/Forward_h.template',
Expand Down
25 changes: 25 additions & 0 deletions src/inspector/node_protocol.pdl
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,28 @@ 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
command setAutoAttach
parameters
boolean autoAttach
boolean waitForDebuggerOnStart
134 changes: 134 additions & 0 deletions src/inspector/target_agent.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
#include "target_agent.h"
#include "crdtp/dispatch.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>>();
int TargetAgent::next_session_id_ = 1;
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 {
target_agent_->createAndAttachIfNecessary(worker, 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::createAndAttachIfNecessary(
std::shared_ptr<MainThreadHandle> worker,
const std::string& title,
const std::string& url) {
std::string target_id = std::to_string(getNextTargetId());
std::string type = "worker";

targetCreated(target_id, type, title, url);
bool attached = false;
if (auto_attach_) {
attached = true;
attachedToTarget(worker, target_id, type, title, url);
}
targets_.push_back({target_id, type, title, url, worker, attached});
}

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);
}

// TODO(islandryu): Currently, setAutoAttach applies the main thread's value to
// all threads. Modify it to be managed per worker thread.
crdtp::DispatchResponse TargetAgent::setAutoAttach(
bool auto_attach, bool wait_for_debugger_on_start) {
auto_attach_ = auto_attach;
wait_for_debugger_on_start_ = wait_for_debugger_on_start;

if (auto_attach) {
for (auto& target : targets_) {
if (!target.attached) {
target.attached = true;
attachedToTarget(target.worker,
target.target_id,
target.type,
target.title,
target.url);
}
}
}
Comment on lines +109 to +127
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setAutoAttach is implemented.
However, I am not able to determine autoAttach for each related Target and waitForDebuggerOnStart.
I have tried several way, but it was difficult to make the changes without affecting the existing nodeWorker, so I would like to make a TODO.


return DispatchResponse::Success();
}

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

#include <unordered_map>
#include <vector>
#include "inspector/worker_inspector.h"
#include "node/inspector/protocol/Target.h"

namespace node {

namespace inspector {
class TargetInspector;

namespace protocol {

struct TargetInfo {
std::string target_id;
std::string type;
std::string title;
std::string url;
std::shared_ptr<MainThreadHandle> worker;
bool attached;
};

class TargetAgent : public Target::Backend,
public std::enable_shared_from_this<TargetAgent> {
public:
void Wire(UberDispatcher* dispatcher);

void createAndAttachIfNecessary(std::shared_ptr<MainThreadHandle> worker,
const std::string& title,
const std::string& url);

DispatchResponse setAutoAttach(bool auto_attach,
bool wait_for_debugger_on_start) override;

void listenWorker(std::weak_ptr<WorkerManager> worker_manager);
void reset();
static std::unordered_map<int, std::shared_ptr<MainThreadHandle>>
target_session_id_worker_map_;

bool isThisThread(MainThreadHandle* worker) { return worker == main_thread_; }

private:
int getNextTargetId();
int getNextSessionId();
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);
std::shared_ptr<Target::Frontend> frontend_;
std::weak_ptr<WorkerManager> worker_manager_;
static int next_session_id_;
int next_target_id_ = 1;
std::unique_ptr<WorkerManagerEventHandle> worker_event_handle_ = nullptr;
bool auto_attach_ = false;
// TODO(islandryu): If false, implement it so that each thread does not wait
// for the worker to execute.
bool wait_for_debugger_on_start_ = true;
std::vector<TargetInfo> targets_;
MainThreadHandle* main_thread_;
};

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

#endif // SRC_INSPECTOR_TARGET_AGENT_H_
Loading
Loading