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‎

Lines changed: 3 additions & 1 deletion
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-
deleteprocess.inspector;
265+
deleteprocess.inspector.wrapConsoleCall;
266+
if(Object.keys(process.inspector).length===0)
267+
deleteprocess.inspector;
266268
}
267269
varconsole;
268270
Object.defineProperty(global,'console',{

‎lib/module.js‎

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

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

577+
varinspectorWrapper=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
deleteprocess._debugWaitConnect;
577-
constDebug=vm.runInDebugContext('Debug');
578-
Debug.setBreakPoint(compiledWrapper,0,0);
591+
inspectorWrapper=getInspectorCallWrapper();
592+
if(!inspectorWrapper){
593+
constDebug=vm.runInDebugContext('Debug');
594+
Debug.setBreakPoint(compiledWrapper,0,0);
595+
}
579596
}
580597
}
581598
vardirname=path.dirname(filename);
582599
varrequire=internalModule.makeRequireFunction(this);
583600
vardepth=internalModule.requireDepth;
584601
if(depth===0)stat.cache=newMap();
585-
varresult=compiledWrapper.call(this.exports,this.exports,require,this,
586-
filename,dirname);
602+
varresult;
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
returnresult;
589612
};

‎src/inspector_agent.cc‎

Lines changed: 42 additions & 1 deletion
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
returnStringBuffer::create(view);
9696
}
97-
9897
} // namespace
9998

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

147+
voidSchedulePauseOnNextStatement(const std::string& reason);
148+
148149
voidPostIncomingMessage(InspectorAction action, int session_id,
149150
const std::string& message);
150151
voidResumeStartup(){
@@ -160,6 +161,8 @@ class AgentImpl{
160161
staticvoidThreadCbIO(void* agent);
161162
staticvoidWriteCbIO(uv_async_t* async);
162163
staticvoidMainThreadAsyncCb(uv_async_t* req);
164+
staticvoidCallAndPauseOnStart(
165+
const v8::FunctionCallbackInfo<v8::Value>& args);
163166

164167
voidInstallInspectorOnProcess();
165168

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

316+
voidschedulePauseOnNextStatement(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+
voidAgentImpl::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+
voidAgentImpl::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+
voidAgent::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‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class Agent{
4343
voidWaitForDisconnect();
4444
voidFatalException(v8::Local<v8::Value> error,
4545
v8::Local<v8::Message> message);
46+
voidSchedulePauseOnNextStatement(const std::string& reason);
4647
private:
4748
AgentImpl* impl;
4849
};

0 commit comments

Comments
(0)