Skip to content

Commit 2e4b318

Browse files
committed
inspector: add contexts when using vm module
Adds all Contexts created by the vm module to the inspector. This does permanent tracking of Contexts and syncing the Contexts whenever a new inspector is created. Fixes: nodejs#7593
1 parent 1e4fafc commit 2e4b318

File tree

6 files changed

+75
-2
lines changed

6 files changed

+75
-2
lines changed

src/env-inl.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,25 @@ inline IsolateData* Environment::isolate_data() const {
356356
return isolate_data_;
357357
}
358358

359+
inline void Environment::context_created(v8_inspector::V8ContextInfo info) {
360+
contexts()->push_back(info);
361+
if (inspector_agent()->IsStarted()) {
362+
inspector_agent()->ContextCreated(info);
363+
}
364+
}
365+
inline void Environment::context_destroyed(v8::Local<v8::Context> context) {
366+
for (auto i = std::begin(*contexts()); i != std::end(*contexts()); ++i) {
367+
auto it = *i;
368+
if (it.context == context) {
369+
contexts()->erase(i);
370+
if (inspector_agent()->IsStarted()) {
371+
inspector_agent()->ContextDestroyed(context);
372+
}
373+
return;
374+
}
375+
}
376+
}
377+
359378
inline void Environment::ThrowError(const char* errmsg) {
360379
ThrowError(v8::Exception::Error, errmsg);
361380
}

src/env.cc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ void Environment::Start(int argc,
3030
HandleScope handle_scope(isolate());
3131
Context::Scope context_scope(context());
3232

33+
context_created(
34+
v8_inspector::V8ContextInfo(context(), 1, "NodeJS Main Context"));
35+
36+
isolate()->SetAutorunMicrotasks(false);
37+
3338
uv_check_init(event_loop(), immediate_check_handle());
3439
uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));
3540

src/env.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "debug-agent.h"
88
#if HAVE_INSPECTOR
99
#include "inspector_agent.h"
10+
#include <vector>
1011
#endif
1112
#include "handle_wrap.h"
1213
#include "req-wrap.h"
@@ -528,6 +529,12 @@ class Environment {
528529
inline inspector::Agent* inspector_agent() {
529530
return &inspector_agent_;
530531
}
532+
533+
inline void context_created(v8_inspector::V8ContextInfo context);
534+
inline void context_destroyed(v8::Local<v8::Context> context);
535+
inline std::vector<v8_inspector::V8ContextInfo>* contexts() {
536+
return &contexts_;
537+
}
531538
#endif
532539

533540
typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
@@ -564,6 +571,7 @@ class Environment {
564571
debugger::Agent debugger_agent_;
565572
#if HAVE_INSPECTOR
566573
inspector::Agent inspector_agent_;
574+
std::vector<v8_inspector::V8ContextInfo> contexts_;
567575
#endif
568576

569577
HandleWrapQueue handle_wrap_queue_;

src/inspector_agent.cc

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ class AgentImpl {
191191
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
192192
// Stop the inspector agent
193193
void Stop();
194+
void ContextCreated(const v8_inspector::V8ContextInfo& info);
195+
void ContextDestroyed(v8::Local<v8::Context> context);
194196

195197
bool IsStarted();
196198
bool IsConnected() { return state_ == State::kConnected; }
@@ -323,8 +325,17 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
323325
terminated_(false),
324326
running_nested_loop_(false),
325327
inspector_(V8Inspector::create(env->isolate(), this)) {
326-
inspector_->contextCreated(
327-
v8_inspector::V8ContextInfo(env->context(), 1, "NodeJS Main Context"));
328+
v8::HandleScope handles(env_->isolate());
329+
for (auto it : *(env->contexts())) {
330+
contextCreated(it);
331+
}
332+
}
333+
334+
void contextCreated(const v8_inspector::V8ContextInfo& info) {
335+
inspector()->contextCreated(info);
336+
}
337+
void contextDestroyed(v8::Local<v8::Context> context) {
338+
inspector()->contextDestroyed(context);
328339
}
329340

330341
void runMessageLoopOnPause(int context_group_id) override {
@@ -510,6 +521,13 @@ void AgentImpl::Stop() {
510521
delete inspector_;
511522
}
512523

524+
void AgentImpl::ContextCreated(const v8_inspector::V8ContextInfo& info) {
525+
inspector_->contextCreated(info);
526+
}
527+
void AgentImpl::ContextDestroyed(v8::Local<v8::Context> context) {
528+
inspector_->contextDestroyed(context);
529+
}
530+
513531
bool AgentImpl::IsStarted() {
514532
return !!platform_;
515533
}
@@ -865,6 +883,13 @@ void Agent::Stop() {
865883
impl->Stop();
866884
}
867885

886+
void Agent::ContextCreated(const v8_inspector::V8ContextInfo& info) {
887+
impl->ContextCreated(info);
888+
}
889+
void Agent::ContextDestroyed(v8::Local<v8::Context> context) {
890+
impl->ContextDestroyed(context);
891+
}
892+
868893
bool Agent::IsStarted() {
869894
return impl->IsStarted();
870895
}

src/inspector_agent.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#error("This header can only be used when inspector is enabled")
66
#endif
77

8+
#include "platform/v8_inspector/public/V8Inspector.h"
9+
810
// Forward declaration to break recursive dependency chain with src/env.h.
911
namespace node {
1012
class Environment;
@@ -33,6 +35,9 @@ class Agent {
3335
// Stop the inspector agent
3436
void Stop();
3537

38+
void ContextCreated(const v8_inspector::V8ContextInfo& info);
39+
void ContextDestroyed(v8::Local<v8::Context> context);
40+
3641
bool IsStarted();
3742
bool IsConnected();
3843
void WaitForDisconnect();

src/node_contextify.cc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
#include "util-inl.h"
1010
#include "v8-debug.h"
1111

12+
#if HAVE_INSPECTOR
13+
#include "platform/v8_inspector/public/V8Inspector.h"
14+
#endif
15+
1216
namespace node {
1317

1418
using v8::Array;
@@ -207,6 +211,10 @@ class ContextifyContext {
207211
object_template->SetHandler(config);
208212

209213
Local<Context> ctx = Context::New(env->isolate(), nullptr, object_template);
214+
#if HAVE_INSPECTOR
215+
env->context_created(
216+
v8_inspector::V8ContextInfo(ctx, 1, "vm Module Context"));
217+
#endif
210218

211219
if (ctx.IsEmpty()) {
212220
env->ThrowError("Could not instantiate context");
@@ -323,6 +331,9 @@ class ContextifyContext {
323331

324332
static void WeakCallback(const WeakCallbackInfo<ContextifyContext>& data) {
325333
ContextifyContext* context = data.GetParameter();
334+
#if HAVE_INSPECTOR
335+
context->env()->context_destroyed(context->context());
336+
#endif
326337
delete context;
327338
}
328339

0 commit comments

Comments
 (0)