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

debugger: fix --debug-brk interaction with -e #7089

Closed
wants to merge 2 commits into from
Closed
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
59 changes: 33 additions & 26 deletions lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@

const internalModule = NativeModule.require('internal/module');
internalModule.addBuiltinLibsToObject(global);
evalScript('[eval]');
run(() => {
evalScript('[eval]');
});
} else if (process.argv[1]) {
// make process.argv[1] into a full path
var path = NativeModule.require('path');
Expand All @@ -127,31 +129,7 @@
}

preloadModules();

if (process._debugWaitConnect &&
process.execArgv.some(function(arg) {
return arg.match(/^--debug-brk(=[0-9]*)?$/);
})) {

// XXX Fix this terrible hack!
//
// Give the client program a few ticks to connect.
// Otherwise, there's a race condition where `node debug foo.js`
// will not be able to connect in time to catch the first
// breakpoint message on line 1.
//
// A better fix would be to somehow get a message from the
// V8 debug object about a connection, and runMain when
// that occurs. --isaacs

var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
setTimeout(Module.runMain, debugTimeout);

} else {
// Main entry point into most programs:
Module.runMain();
}

run(Module.runMain);
} else {
preloadModules();
// If -i or --interactive were passed, or stdin is a TTY.
Expand Down Expand Up @@ -334,6 +312,35 @@
}
}

function isDebugBreak() {
return process.execArgv.some((arg) => {
return arg.match(/^--debug-brk(=[0-9]*)?$/);
});
}

function run(entryFunction) {
if (process._debugWaitConnect && isDebugBreak()) {

// XXX Fix this terrible hack!
//
// Give the client program a few ticks to connect.
// Otherwise, there's a race condition where `node debug foo.js`
// will not be able to connect in time to catch the first
// breakpoint message on line 1.
//
// A better fix would be to somehow get a message from the
// V8 debug object about a connection, and runMain when
// that occurs. --isaacs

var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
setTimeout(entryFunction, debugTimeout);

} else {
// Main entry point into most programs:
entryFunction();
}
}

// Below you find a minimal module system, which is used to load the node
// core modules found in lib/*.js. All core modules are compiled into the
// node binary, so they can be loaded faster.
Expand Down
4 changes: 2 additions & 2 deletions test/message/core_line_numbers.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ RangeError: Invalid input
at Module.load (module.js:*:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*:*)
at Function.Module.runMain (module.js:*:*)
at startup (node.js:*:*)
at Module.runMain (module.js:*:*)
at run (node.js:*:*)
3 changes: 2 additions & 1 deletion test/message/error_exit.out
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ AssertionError: 1 == 2
at Module.load (module.js:*:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*:*)
at Function.Module.runMain (module.js:*:*)
at Module.runMain (module.js:*:*)
at run (node.js:*:*)
at startup (node.js:*:*)
at node.js:*:*
3 changes: 2 additions & 1 deletion test/message/nexttick_throw.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ ReferenceError: undefined_reference_error_maker is not defined
at *test*message*nexttick_throw.js:*:*
at _combinedTickCallback (internal/process/next_tick.js:*:*)
at process._tickCallback (internal/process/next_tick.js:*:*)
at Function.Module.runMain (module.js:*:*)
at Module.runMain (module.js:*:*)
at run (node.js:*:*)
at startup (node.js:*:*)
at node.js:*:*
2 changes: 1 addition & 1 deletion test/message/undefined_reference_in_new_context.out
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ ReferenceError: foo is not defined
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*:*)
at Function.Module.runMain (module.js:*:*)
at Module.runMain (module.js:*:*)
4 changes: 2 additions & 2 deletions test/message/vm_display_runtime_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Error: boo!
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*)
at Function.Module.runMain (module.js:*)
at startup (node.js:*)
at Module.runMain (module.js:*)
at run (node.js:*)
8 changes: 4 additions & 4 deletions test/message/vm_display_syntax_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ SyntaxError: Unexpected number
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*)
at Function.Module.runMain (module.js:*)
at Module.runMain (module.js:*)
at run (node.js:*)
at startup (node.js:*)
at node.js:*
test.vm:1
var 5;
^
Expand All @@ -25,6 +25,6 @@ SyntaxError: Unexpected number
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*)
at Function.Module.runMain (module.js:*)
at Module.runMain (module.js:*)
at run (node.js:*)
at startup (node.js:*)
at node.js:*
4 changes: 2 additions & 2 deletions test/message/vm_dont_display_runtime_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ Error: boo!
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*)
at Function.Module.runMain (module.js:*)
at startup (node.js:*)
at Module.runMain (module.js:*)
at run (node.js:*)
4 changes: 2 additions & 2 deletions test/message/vm_dont_display_syntax_error.out
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ SyntaxError: Unexpected number
at Module.load (module.js:*)
at tryModuleLoad (module.js:*:*)
at Function.Module._load (module.js:*)
at Function.Module.runMain (module.js:*)
at Module.runMain (module.js:*)
at run (node.js:*)
at startup (node.js:*)
at node.js:*
36 changes: 31 additions & 5 deletions test/parallel/test-debug-brk.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
'use strict';

const common = require('../common');
const assert = require('assert');
const spawnSync = require('child_process').spawnSync;
const spawn = require('child_process').spawn;

const args = [`--debug-brk=${common.PORT}`, '-e', '0'];
const proc = spawnSync(process.execPath, args, {encoding: 'utf8'});
assert(/Debugger listening on/.test(proc.stderr));
var procStderr = '';
var agentStdout = '';
var needToSpawnAgent = true;
var needToExit = true;

const procArgs = [`--debug-brk=${common.PORT}`, '-e', '0'];
const proc = spawn(process.execPath, procArgs);
proc.stderr.setEncoding('utf8');

const exitAll = common.mustCall((processes) => {
processes.forEach((myProcess) => { myProcess.kill(); });
});

proc.stderr.on('data', (chunk) => {
procStderr += chunk;
if (/Debugger listening on/.test(procStderr) && needToSpawnAgent) {
needToSpawnAgent = false;
const agentArgs = ['debug', `localhost:${common.PORT}`];
const agent = spawn(process.execPath, agentArgs);
agent.stdout.setEncoding('utf8');

agent.stdout.on('data', (chunk) => {
agentStdout += chunk;
if (/connecting to .+ ok/.test(agentStdout) && needToExit) {
needToExit = false;
exitAll([proc, agent]);
}
});
}
});