Skip to content

Commit 2961f06

Browse files
Trottevanlucas
authored andcommitted
debugger: fix --debug-brk interaction with -e
The command line flag `--debug-brk` was ignored when the `-e` flag was also present. This change allows the flags to both be honored when they are used in a single command line. PR-URL: #7089 Fixes: #3589 Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 75d6875 commit 2961f06

10 files changed

+81
-46
lines changed

lib/internal/bootstrap_node.js

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,9 @@
109109

110110
const internalModule = NativeModule.require('internal/module');
111111
internalModule.addBuiltinLibsToObject(global);
112-
evalScript('[eval]');
112+
run(() => {
113+
evalScript('[eval]');
114+
});
113115
} else if (process.argv[1]) {
114116
// make process.argv[1] into a full path
115117
var path = NativeModule.require('path');
@@ -135,31 +137,7 @@
135137
}
136138

137139
preloadModules();
138-
139-
if (process._debugWaitConnect &&
140-
process.execArgv.some(function(arg) {
141-
return arg.match(/^--debug-brk(=[0-9]*)?$/);
142-
})) {
143-
144-
// XXX Fix this terrible hack!
145-
//
146-
// Give the client program a few ticks to connect.
147-
// Otherwise, there's a race condition where `node debug foo.js`
148-
// will not be able to connect in time to catch the first
149-
// breakpoint message on line 1.
150-
//
151-
// A better fix would be to somehow get a message from the
152-
// V8 debug object about a connection, and runMain when
153-
// that occurs. --isaacs
154-
155-
var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
156-
setTimeout(Module.runMain, debugTimeout);
157-
158-
} else {
159-
// Main entry point into most programs:
160-
Module.runMain();
161-
}
162-
140+
run(Module.runMain);
163141
} else {
164142
preloadModules();
165143
// If -i or --interactive were passed, or stdin is a TTY.
@@ -342,6 +320,35 @@
342320
}
343321
}
344322

323+
function isDebugBreak() {
324+
return process.execArgv.some((arg) => {
325+
return arg.match(/^--debug-brk(=[0-9]*)?$/);
326+
});
327+
}
328+
329+
function run(entryFunction) {
330+
if (process._debugWaitConnect && isDebugBreak()) {
331+
332+
// XXX Fix this terrible hack!
333+
//
334+
// Give the client program a few ticks to connect.
335+
// Otherwise, there's a race condition where `node debug foo.js`
336+
// will not be able to connect in time to catch the first
337+
// breakpoint message on line 1.
338+
//
339+
// A better fix would be to somehow get a message from the
340+
// V8 debug object about a connection, and runMain when
341+
// that occurs. --isaacs
342+
343+
var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
344+
setTimeout(entryFunction, debugTimeout);
345+
346+
} else {
347+
// Main entry point into most programs:
348+
entryFunction();
349+
}
350+
}
351+
345352
// Below you find a minimal module system, which is used to load the node
346353
// core modules found in lib/*.js. All core modules are compiled into the
347354
// node binary, so they can be loaded faster.

test/message/core_line_numbers.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ RangeError: Invalid input
1111
at Module.load (module.js:*:*)
1212
at tryModuleLoad (module.js:*:*)
1313
at Function.Module._load (module.js:*:*)
14-
at Function.Module.runMain (module.js:*:*)
15-
at startup (node.js:*:*)
14+
at Module.runMain (module.js:*:*)
15+
at run (node.js:*:*)

test/message/error_exit.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ AssertionError: 1 == 2
1010
at Module.load (module.js:*:*)
1111
at tryModuleLoad (module.js:*:*)
1212
at Function.Module._load (module.js:*:*)
13-
at Function.Module.runMain (module.js:*:*)
13+
at Module.runMain (module.js:*:*)
14+
at run (node.js:*:*)
1415
at startup (node.js:*:*)
1516
at node.js:*:*

test/message/nexttick_throw.out

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ ReferenceError: undefined_reference_error_maker is not defined
66
at *test*message*nexttick_throw.js:*:*
77
at _combinedTickCallback (internal/process/next_tick.js:*:*)
88
at process._tickCallback (internal/process/next_tick.js:*:*)
9-
at Function.Module.runMain (module.js:*:*)
9+
at Module.runMain (module.js:*:*)
10+
at run (node.js:*:*)
1011
at startup (node.js:*:*)
1112
at node.js:*:*

test/message/undefined_reference_in_new_context.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ ReferenceError: foo is not defined
1313
at Module.load (module.js:*)
1414
at tryModuleLoad (module.js:*:*)
1515
at Function.Module._load (module.js:*:*)
16-
at Function.Module.runMain (module.js:*:*)
16+
at Module.runMain (module.js:*:*)

test/message/vm_display_runtime_error.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Error: boo!
1212
at Module.load (module.js:*)
1313
at tryModuleLoad (module.js:*:*)
1414
at Function.Module._load (module.js:*)
15-
at Function.Module.runMain (module.js:*)
16-
at startup (node.js:*)
15+
at Module.runMain (module.js:*)
16+
at run (node.js:*)

test/message/vm_display_syntax_error.out

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ SyntaxError: Unexpected number
1111
at Module.load (module.js:*)
1212
at tryModuleLoad (module.js:*:*)
1313
at Function.Module._load (module.js:*)
14-
at Function.Module.runMain (module.js:*)
14+
at Module.runMain (module.js:*)
15+
at run (node.js:*)
1516
at startup (node.js:*)
16-
at node.js:*
1717
test.vm:1
1818
var 5;
1919
^
@@ -25,6 +25,6 @@ SyntaxError: Unexpected number
2525
at Module.load (module.js:*)
2626
at tryModuleLoad (module.js:*:*)
2727
at Function.Module._load (module.js:*)
28-
at Function.Module.runMain (module.js:*)
28+
at Module.runMain (module.js:*)
29+
at run (node.js:*)
2930
at startup (node.js:*)
30-
at node.js:*

test/message/vm_dont_display_runtime_error.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Error: boo!
1212
at Module.load (module.js:*)
1313
at tryModuleLoad (module.js:*:*)
1414
at Function.Module._load (module.js:*)
15-
at Function.Module.runMain (module.js:*)
16-
at startup (node.js:*)
15+
at Module.runMain (module.js:*)
16+
at run (node.js:*)

test/message/vm_dont_display_syntax_error.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ SyntaxError: Unexpected number
1111
at Module.load (module.js:*)
1212
at tryModuleLoad (module.js:*:*)
1313
at Function.Module._load (module.js:*)
14-
at Function.Module.runMain (module.js:*)
14+
at Module.runMain (module.js:*)
15+
at run (node.js:*)
1516
at startup (node.js:*)
16-
at node.js:*

test/parallel/test-debug-brk.js

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,35 @@
11
'use strict';
22

33
const common = require('../common');
4-
const assert = require('assert');
5-
const spawnSync = require('child_process').spawnSync;
4+
const spawn = require('child_process').spawn;
65

7-
const args = [`--debug-brk=${common.PORT}`, '-e', '0'];
8-
const proc = spawnSync(process.execPath, args, {encoding: 'utf8'});
9-
assert(/Debugger listening on/.test(proc.stderr));
6+
var procStderr = '';
7+
var agentStdout = '';
8+
var needToSpawnAgent = true;
9+
var needToExit = true;
10+
11+
const procArgs = [`--debug-brk=${common.PORT}`, '-e', '0'];
12+
const proc = spawn(process.execPath, procArgs);
13+
proc.stderr.setEncoding('utf8');
14+
15+
const exitAll = common.mustCall((processes) => {
16+
processes.forEach((myProcess) => { myProcess.kill(); });
17+
});
18+
19+
proc.stderr.on('data', (chunk) => {
20+
procStderr += chunk;
21+
if (/Debugger listening on/.test(procStderr) && needToSpawnAgent) {
22+
needToSpawnAgent = false;
23+
const agentArgs = ['debug', `localhost:${common.PORT}`];
24+
const agent = spawn(process.execPath, agentArgs);
25+
agent.stdout.setEncoding('utf8');
26+
27+
agent.stdout.on('data', (chunk) => {
28+
agentStdout += chunk;
29+
if (/connecting to .+ ok/.test(agentStdout) && needToExit) {
30+
needToExit = false;
31+
exitAll([proc, agent]);
32+
}
33+
});
34+
}
35+
});

0 commit comments

Comments
 (0)