Skip to content

Commit 7954d2a

Browse files
author
Eugene Ostroukhov
committed
inspector: use inspector API for "break on start"
This change removes a need for using deprecated debug context for breaking at the start of the main module. PR-URL: #12076 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 4ddd23f commit 7954d2a

File tree

4 files changed

+73
-6
lines changed

4 files changed

+73
-6
lines changed

lib/internal/bootstrap_node.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,9 @@
262262
if (process.inspector) {
263263
inspectorConsole = global.console;
264264
wrapConsoleCall = process.inspector.wrapConsoleCall;
265-
delete process.inspector;
265+
delete process.inspector.wrapConsoleCall;
266+
if (Object.keys(process.inspector).length === 0)
267+
delete process.inspector;
266268
}
267269
var console;
268270
Object.defineProperty(global, 'console', {

lib/module.js

+27-4
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,19 @@ function tryModuleLoad(module, filename) {
472472
}
473473
}
474474

475+
function getInspectorCallWrapper() {
476+
var inspector = process.inspector;
477+
if (!inspector || !inspector.callAndPauseOnStart) {
478+
return null;
479+
}
480+
var wrapper = inspector.callAndPauseOnStart.bind(inspector);
481+
delete inspector.callAndPauseOnStart;
482+
if (Object.keys(process.inspector).length === 0) {
483+
delete process.inspector;
484+
}
485+
return wrapper;
486+
}
487+
475488
Module._resolveFilename = function(request, parent, isMain) {
476489
if (NativeModule.nonInternalExists(request)) {
477490
return request;
@@ -561,6 +574,7 @@ Module.prototype._compile = function(content, filename) {
561574
displayErrors: true
562575
});
563576

577+
var inspectorWrapper = null;
564578
if (process._debugWaitConnect && process._eval == null) {
565579
if (!resolvedArgv) {
566580
// we enter the repl if we're not given a filename argument.
@@ -574,16 +588,25 @@ Module.prototype._compile = function(content, filename) {
574588
// Set breakpoint on module start
575589
if (filename === resolvedArgv) {
576590
delete process._debugWaitConnect;
577-
const Debug = vm.runInDebugContext('Debug');
578-
Debug.setBreakPoint(compiledWrapper, 0, 0);
591+
inspectorWrapper = getInspectorCallWrapper();
592+
if (!inspectorWrapper) {
593+
const Debug = vm.runInDebugContext('Debug');
594+
Debug.setBreakPoint(compiledWrapper, 0, 0);
595+
}
579596
}
580597
}
581598
var dirname = path.dirname(filename);
582599
var require = internalModule.makeRequireFunction(this);
583600
var depth = internalModule.requireDepth;
584601
if (depth === 0) stat.cache = new Map();
585-
var result = compiledWrapper.call(this.exports, this.exports, require, this,
586-
filename, dirname);
602+
var result;
603+
if (inspectorWrapper) {
604+
result = inspectorWrapper(compiledWrapper, this.exports, this.exports,
605+
require, this, filename, dirname);
606+
} else {
607+
result = compiledWrapper.call(this.exports, this.exports, require, this,
608+
filename, dirname);
609+
}
587610
if (depth === 0) stat.cache = null;
588611
return result;
589612
};

src/inspector_agent.cc

+42-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ std::unique_ptr<StringBuffer> Utf8ToStringView(const std::string& message) {
9494
utf16.length());
9595
return StringBuffer::create(view);
9696
}
97-
9897
} // namespace
9998

10099
class V8NodeInspector;
@@ -145,6 +144,8 @@ class AgentImpl {
145144
void FatalException(v8::Local<v8::Value> error,
146145
v8::Local<v8::Message> message);
147146

147+
void SchedulePauseOnNextStatement(const std::string& reason);
148+
148149
void PostIncomingMessage(InspectorAction action, int session_id,
149150
const std::string& message);
150151
void ResumeStartup() {
@@ -160,6 +161,8 @@ class AgentImpl {
160161
static void ThreadCbIO(void* agent);
161162
static void WriteCbIO(uv_async_t* async);
162163
static void MainThreadAsyncCb(uv_async_t* req);
164+
static void CallAndPauseOnStart(
165+
const v8::FunctionCallbackInfo<v8::Value>& args);
163166

164167
void InstallInspectorOnProcess();
165168

@@ -310,6 +313,14 @@ class V8NodeInspector : public v8_inspector::V8InspectorClient {
310313
session_->dispatchProtocolMessage(message);
311314
}
312315

316+
void schedulePauseOnNextStatement(const std::string& reason) {
317+
if (session_ != nullptr) {
318+
std::unique_ptr<StringBuffer> buffer = Utf8ToStringView(reason);
319+
session_->schedulePauseOnNextStatement(buffer->string(),
320+
buffer->string());
321+
}
322+
}
323+
313324
v8::Local<v8::Context> ensureDefaultContextInGroup(int contextGroupId)
314325
override {
315326
return env_->context();
@@ -477,6 +488,28 @@ void AgentImpl::InstallInspectorOnProcess() {
477488
v8::Local<v8::Object> inspector = v8::Object::New(env->isolate());
478489
READONLY_PROPERTY(process, "inspector", inspector);
479490
env->SetMethod(inspector, "wrapConsoleCall", InspectorWrapConsoleCall);
491+
if (options_.wait_for_connect()) {
492+
env->SetMethod(inspector, "callAndPauseOnStart", CallAndPauseOnStart);
493+
}
494+
}
495+
496+
// static
497+
void AgentImpl::CallAndPauseOnStart(
498+
const v8::FunctionCallbackInfo<v8::Value>& args) {
499+
Environment* env = Environment::GetCurrent(args);
500+
CHECK_GT(args.Length(), 1);
501+
CHECK(args[0]->IsFunction());
502+
std::vector<v8::Local<v8::Value>> call_args;
503+
for (int i = 2; i < args.Length(); i++) {
504+
call_args.push_back(args[i]);
505+
}
506+
507+
env->inspector_agent()->SchedulePauseOnNextStatement("Break on start");
508+
509+
v8::MaybeLocal<v8::Value> retval =
510+
args[0].As<v8::Function>()->Call(env->context(), args[1],
511+
call_args.size(), call_args.data());
512+
args.GetReturnValue().Set(retval.ToLocalChecked());
480513
}
481514

482515
std::unique_ptr<StringBuffer> ToProtocolString(v8::Local<v8::Value> value) {
@@ -682,6 +715,10 @@ void AgentImpl::Write(TransportAction action, int session_id,
682715
CHECK_EQ(0, err);
683716
}
684717

718+
void AgentImpl::SchedulePauseOnNextStatement(const std::string& reason) {
719+
inspector_->schedulePauseOnNextStatement(reason);
720+
}
721+
685722
// Exported class Agent
686723
Agent::Agent(node::Environment* env) : impl(new AgentImpl(env)) {}
687724

@@ -715,6 +752,10 @@ void Agent::FatalException(v8::Local<v8::Value> error,
715752
impl->FatalException(error, message);
716753
}
717754

755+
void Agent::SchedulePauseOnNextStatement(const std::string& reason) {
756+
impl->SchedulePauseOnNextStatement(reason);
757+
}
758+
718759
InspectorAgentDelegate::InspectorAgentDelegate(AgentImpl* agent,
719760
const std::string& script_path,
720761
const std::string& script_name,

src/inspector_agent.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Agent {
4343
void WaitForDisconnect();
4444
void FatalException(v8::Local<v8::Value> error,
4545
v8::Local<v8::Message> message);
46+
void SchedulePauseOnNextStatement(const std::string& reason);
4647
private:
4748
AgentImpl* impl;
4849
};

0 commit comments

Comments
 (0)