Skip to content

Commit

Permalink
inspector: add contexts when using vm module
Browse files Browse the repository at this point in the history
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: #7593
  • Loading branch information
bmeck committed Oct 25, 2016
1 parent 1e4fafc commit 2e4b318
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/env-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,25 @@ inline IsolateData* Environment::isolate_data() const {
return isolate_data_;
}

inline void Environment::context_created(v8_inspector::V8ContextInfo info) {
contexts()->push_back(info);
if (inspector_agent()->IsStarted()) {
inspector_agent()->ContextCreated(info);
}
}
inline void Environment::context_destroyed(v8::Local<v8::Context> context) {
for (auto i = std::begin(*contexts()); i != std::end(*contexts()); ++i) {
auto it = *i;
if (it.context == context) {
contexts()->erase(i);
if (inspector_agent()->IsStarted()) {
inspector_agent()->ContextDestroyed(context);
}
return;
}
}
}

inline void Environment::ThrowError(const char* errmsg) {
ThrowError(v8::Exception::Error, errmsg);
}
Expand Down
5 changes: 5 additions & 0 deletions src/env.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ void Environment::Start(int argc,
HandleScope handle_scope(isolate());
Context::Scope context_scope(context());

context_created(
v8_inspector::V8ContextInfo(context(), 1, "NodeJS Main Context"));

isolate()->SetAutorunMicrotasks(false);

uv_check_init(event_loop(), immediate_check_handle());
uv_unref(reinterpret_cast<uv_handle_t*>(immediate_check_handle()));

Expand Down
8 changes: 8 additions & 0 deletions src/env.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "debug-agent.h"
#if HAVE_INSPECTOR
#include "inspector_agent.h"
#include <vector>
#endif
#include "handle_wrap.h"
#include "req-wrap.h"
Expand Down Expand Up @@ -528,6 +529,12 @@ class Environment {
inline inspector::Agent* inspector_agent() {
return &inspector_agent_;
}

inline void context_created(v8_inspector::V8ContextInfo context);
inline void context_destroyed(v8::Local<v8::Context> context);
inline std::vector<v8_inspector::V8ContextInfo>* contexts() {
return &contexts_;
}
#endif

typedef ListHead<HandleWrap, &HandleWrap::handle_wrap_queue_> HandleWrapQueue;
Expand Down Expand Up @@ -564,6 +571,7 @@ class Environment {
debugger::Agent debugger_agent_;
#if HAVE_INSPECTOR
inspector::Agent inspector_agent_;
std::vector<v8_inspector::V8ContextInfo> contexts_;
#endif

HandleWrapQueue handle_wrap_queue_;
Expand Down
29 changes: 27 additions & 2 deletions src/inspector_agent.cc
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ class AgentImpl {
bool Start(v8::Platform* platform, const char* path, int port, bool wait);
// Stop the inspector agent
void Stop();
void ContextCreated(const v8_inspector::V8ContextInfo& info);
void ContextDestroyed(v8::Local<v8::Context> context);

bool IsStarted();
bool IsConnected() { return state_ == State::kConnected; }
Expand Down Expand Up @@ -323,8 +325,17 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
terminated_(false),
running_nested_loop_(false),
inspector_(V8Inspector::create(env->isolate(), this)) {
inspector_->contextCreated(
v8_inspector::V8ContextInfo(env->context(), 1, "NodeJS Main Context"));
v8::HandleScope handles(env_->isolate());
for (auto it : *(env->contexts())) {
contextCreated(it);
}
}

void contextCreated(const v8_inspector::V8ContextInfo& info) {
inspector()->contextCreated(info);
}
void contextDestroyed(v8::Local<v8::Context> context) {
inspector()->contextDestroyed(context);
}

void runMessageLoopOnPause(int context_group_id) override {
Expand Down Expand Up @@ -510,6 +521,13 @@ void AgentImpl::Stop() {
delete inspector_;
}

void AgentImpl::ContextCreated(const v8_inspector::V8ContextInfo& info) {
inspector_->contextCreated(info);
}
void AgentImpl::ContextDestroyed(v8::Local<v8::Context> context) {
inspector_->contextDestroyed(context);
}

bool AgentImpl::IsStarted() {
return !!platform_;
}
Expand Down Expand Up @@ -865,6 +883,13 @@ void Agent::Stop() {
impl->Stop();
}

void Agent::ContextCreated(const v8_inspector::V8ContextInfo& info) {
impl->ContextCreated(info);
}
void Agent::ContextDestroyed(v8::Local<v8::Context> context) {
impl->ContextDestroyed(context);
}

bool Agent::IsStarted() {
return impl->IsStarted();
}
Expand Down
5 changes: 5 additions & 0 deletions src/inspector_agent.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#error("This header can only be used when inspector is enabled")
#endif

#include "platform/v8_inspector/public/V8Inspector.h"

// Forward declaration to break recursive dependency chain with src/env.h.
namespace node {
class Environment;
Expand Down Expand Up @@ -33,6 +35,9 @@ class Agent {
// Stop the inspector agent
void Stop();

void ContextCreated(const v8_inspector::V8ContextInfo& info);
void ContextDestroyed(v8::Local<v8::Context> context);

bool IsStarted();
bool IsConnected();
void WaitForDisconnect();
Expand Down
11 changes: 11 additions & 0 deletions src/node_contextify.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#include "util-inl.h"
#include "v8-debug.h"

#if HAVE_INSPECTOR
#include "platform/v8_inspector/public/V8Inspector.h"
#endif

namespace node {

using v8::Array;
Expand Down Expand Up @@ -207,6 +211,10 @@ class ContextifyContext {
object_template->SetHandler(config);

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

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

static void WeakCallback(const WeakCallbackInfo<ContextifyContext>& data) {
ContextifyContext* context = data.GetParameter();
#if HAVE_INSPECTOR
context->env()->context_destroyed(context->context());
#endif
delete context;
}

Expand Down

0 comments on commit 2e4b318

Please sign in to comment.