Skip to content

Commit 1a21524

Browse files
committed
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 edbb092 commit 1a21524

10 files changed

+81
-46
lines changed

lib/internal/bootstrap_node.js

+33-26
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@
101101

102102
const internalModule = NativeModule.require('internal/module');
103103
internalModule.addBuiltinLibsToObject(global);
104-
evalScript('[eval]');
104+
run(() => {
105+
evalScript('[eval]');
106+
});
105107
} else if (process.argv[1]) {
106108
// make process.argv[1] into a full path
107109
var path = NativeModule.require('path');
@@ -127,31 +129,7 @@
127129
}
128130

129131
preloadModules();
130-
131-
if (process._debugWaitConnect &&
132-
process.execArgv.some(function(arg) {
133-
return arg.match(/^--debug-brk(=[0-9]*)?$/);
134-
})) {
135-
136-
// XXX Fix this terrible hack!
137-
//
138-
// Give the client program a few ticks to connect.
139-
// Otherwise, there's a race condition where `node debug foo.js`
140-
// will not be able to connect in time to catch the first
141-
// breakpoint message on line 1.
142-
//
143-
// A better fix would be to somehow get a message from the
144-
// V8 debug object about a connection, and runMain when
145-
// that occurs. --isaacs
146-
147-
var debugTimeout = +process.env.NODE_DEBUG_TIMEOUT || 50;
148-
setTimeout(Module.runMain, debugTimeout);
149-
150-
} else {
151-
// Main entry point into most programs:
152-
Module.runMain();
153-
}
154-
132+
run(Module.runMain);
155133
} else {
156134
preloadModules();
157135
// If -i or --interactive were passed, or stdin is a TTY.
@@ -334,6 +312,35 @@
334312
}
335313
}
336314

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

test/message/core_line_numbers.out

+2-2
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

+2-1
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

+2-1
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

+1-1
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

+2-2
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

+4-4
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

+2-2
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

+2-2
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

+31-5
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)