From 2184e772d272dba5621014bda9f00d4c974430ce Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 21 Oct 2015 13:52:04 +0200 Subject: [PATCH] debugger: propagate --debug-port= to debuggee Before this commit `node --debug-port=1234 debug t.js` ignored the --debug-port= argument, binding to the default port 5858 instead, making it impossible to debug more than one process on the same machine that way. This commit also reduces the number of places where the default port is hard-coded by one. Fixes: https://github.com/nodejs/node/issues/3345 PR-URL: https://github.com/nodejs/node/pull/3470 Reviewed-By: Colin Ihrig Reviewed-By: Fedor Indutny Reviewed-By: James M Snell --- lib/_debugger.js | 4 +-- test/parallel/test-debug-port-numbers.js | 41 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-debug-port-numbers.js diff --git a/lib/_debugger.js b/lib/_debugger.js index e872d77ea55962..7da2f70bcaf1e9 100644 --- a/lib/_debugger.js +++ b/lib/_debugger.js @@ -26,7 +26,7 @@ exports.start = function(argv, stdin, stdout) { stdin = stdin || process.stdin; stdout = stdout || process.stdout; - const args = ['--debug-brk'].concat(argv); + const args = [`--debug-brk=${exports.port}`].concat(argv); const interface_ = new Interface(stdin, stdout, args); stdin.resume(); @@ -41,7 +41,7 @@ exports.start = function(argv, stdin, stdout) { }); }; -exports.port = 5858; +exports.port = process.debugPort; // diff --git a/test/parallel/test-debug-port-numbers.js b/test/parallel/test-debug-port-numbers.js new file mode 100644 index 00000000000000..e21cb72f0a0622 --- /dev/null +++ b/test/parallel/test-debug-port-numbers.js @@ -0,0 +1,41 @@ +'use strict'; + +const common = require('../common'); +const assert = require('assert'); +const path = require('path'); +const spawn = require('child_process').spawn; + +const children = []; +for (let i = 0; i < 4; i += 1) { + const port = common.PORT + i; + const args = [`--debug-port=${port}`, '--interactive', 'debug', __filename]; + const child = spawn(process.execPath, args, { stdio: 'pipe' }); + child.test = { port: port, stdout: '' }; + child.stdout.setEncoding('utf8'); + child.stdout.on('data', function(s) { child.test.stdout += s; update(); }); + child.stdout.pipe(process.stdout); + child.stderr.pipe(process.stderr); + children.push(child); +} + +function update() { + // Debugger prints relative paths except on Windows. + const filename = path.basename(__filename); + + let ready = 0; + for (const child of children) + ready += RegExp(`break in .*?${filename}:1`).test(child.test.stdout); + + if (ready === children.length) + for (const child of children) + child.kill(); +} + +process.on('exit', function() { + for (const child of children) { + const one = RegExp(`Debugger listening on port ${child.test.port}`); + const two = RegExp(`connecting to 127.0.0.1:${child.test.port}`); + assert(one.test(child.test.stdout)); + assert(two.test(child.test.stdout)); + } +});