Skip to content

Commit 748e424

Browse files
bnoordhuisMyles Borins
authored andcommitted
debugger: make listen address configurable
`--debug=1.2.3.4:5678` and `--debug=example.com:5678` are now accepted, likewise the `--debug-brk` and `--debug-port` switch. The latter is now something of a misnomer but it's undocumented and for internal use only so it shouldn't matter too much. `--inspect=1.2.3.4:5678` and `--inspect=example.com:5678` are also accepted but don't use the host name yet; they still bind to the default address. Fixes: #3306 PR-URL: #3316 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trevor Norris <[email protected]>
1 parent 9a02414 commit 748e424

10 files changed

+124
-28
lines changed

‎lib/_debug_agent.js‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,10 @@ exports.start = function start(){
1818
process._rawDebug(err.stack||err);
1919
});
2020

21-
agent.listen(process._debugAPI.port,function(){
22-
varaddr=this.address();
23-
process._rawDebug('Debugger listening on port %d',addr.port);
21+
agent.listen(process._debugAPI.port,process._debugAPI.host,function(){
22+
constaddr=this.address();
23+
consthost=net.isIPv6(addr.address) ? `[${addr.address}]` : addr.address;
24+
process._rawDebug('Debugger listening on %s:%d',host,addr.port);
2425
process._debugAPI.notifyListen();
2526
});
2627

‎src/debug-agent.cc‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ using v8::Integer;
4444
using v8::Isolate;
4545
using v8::Local;
4646
using v8::Locker;
47+
using v8::NewStringType;
4748
using v8::Object;
4849
using v8::String;
4950
using v8::Value;
@@ -69,7 +70,7 @@ Agent::~Agent(){
6970
}
7071

7172

72-
boolAgent::Start(int port, bool wait){
73+
boolAgent::Start(const std::string& host, int port, bool wait){
7374
int err;
7475

7576
if (state_ == kRunning)
@@ -85,6 +86,7 @@ bool Agent::Start(int port, bool wait){
8586
goto async_init_failed;
8687
uv_unref(reinterpret_cast<uv_handle_t*>(&child_signal_));
8788

89+
host_ = host;
8890
port_ = port;
8991
wait_ = wait;
9092

@@ -211,6 +213,10 @@ void Agent::InitAdaptor(Environment* env){
211213
Local<Object> api = t->GetFunction()->NewInstance();
212214
api->SetAlignedPointerInInternalField(0, this);
213215

216+
api->Set(String::NewFromUtf8(isolate, "host",
217+
NewStringType::kNormal).ToLocalChecked(),
218+
String::NewFromUtf8(isolate, host_.data(), NewStringType::kNormal,
219+
host_.size()).ToLocalChecked());
214220
api->Set(String::NewFromUtf8(isolate, "port"), Integer::New(isolate, port_));
215221

216222
env->process_object()->Set(String::NewFromUtf8(isolate, "_debugAPI"), api);

‎src/debug-agent.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include"v8-debug.h"
3131

3232
#include<string.h>
33+
#include<string>
3334

3435
// Forward declaration to break recursive dependency chain with src/env.h.
3536
namespacenode{
@@ -73,7 +74,7 @@ class Agent{
7374
typedefvoid (*DispatchHandler)(node::Environment* env);
7475

7576
// Start the debugger agent thread
76-
boolStart(int port, bool wait);
77+
boolStart(const std::string& host, int port, bool wait);
7778
// Listen for debug events
7879
voidEnable();
7980
// Stop the debugger agent
@@ -112,6 +113,7 @@ class Agent{
112113

113114
State state_;
114115

116+
std::string host_;
115117
int port_;
116118
bool wait_;
117119

‎src/node.cc‎

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@
5858
#include<stdlib.h>
5959
#include<string.h>
6060
#include<sys/types.h>
61+
62+
#include<string>
6163
#include<vector>
6264

6365
#if defined(NODE_HAVE_I18N_SUPPORT)
@@ -139,6 +141,7 @@ static unsigned int preload_module_count = 0;
139141
staticconstchar** preload_modules = nullptr;
140142
staticbool use_debug_agent = false;
141143
staticbool debug_wait_connect = false;
144+
static std::string debug_host; // NOLINT(runtime/string)
142145
staticint debug_port = 5858;
143146
staticbool prof_process = false;
144147
staticbool v8_is_profiling = false;
@@ -3288,20 +3291,55 @@ static bool ParseDebugOpt(const char* arg){
32883291
debug_wait_connect = true;
32893292
port = arg + sizeof("--debug-brk=") - 1;
32903293
} elseif (!strncmp(arg, "--debug-port=", sizeof("--debug-port=") - 1)){
3294+
// XXX(bnoordhuis) Misnomer, configures port and listen address.
32913295
port = arg + sizeof("--debug-port=") - 1;
32923296
} else{
32933297
returnfalse;
32943298
}
32953299

3296-
if (port != nullptr){
3297-
debug_port = atoi(port);
3298-
if (debug_port < 1024 || debug_port > 65535){
3299-
fprintf(stderr, "Debug port must be in range 1024 to 65535.\n");
3300-
PrintHelp();
3301-
exit(12);
3300+
if (port == nullptr){
3301+
returntrue;
3302+
}
3303+
3304+
std::string* const the_host = &debug_host;
3305+
int* const the_port = &debug_port;
3306+
3307+
// FIXME(bnoordhuis) Move IPv6 address parsing logic to lib/net.js.
3308+
// It seems reasonable to support [address]:port notation
3309+
// in net.Server#listen() and net.Socket#connect().
3310+
constsize_t port_len = strlen(port);
3311+
if (port[0] == '[' && port[port_len - 1] == ']'){
3312+
the_host->assign(port + 1, port_len - 2);
3313+
returntrue;
3314+
}
3315+
3316+
constchar* const colon = strrchr(port, ':');
3317+
if (colon == nullptr){
3318+
// Either a port number or a host name. Assume that
3319+
// if it's not all decimal digits, it's a host name.
3320+
for (size_t n = 0; port[n] != '\0'; n += 1){
3321+
if (port[n] < '0' || port[n] > '9'){
3322+
*the_host = port;
3323+
returntrue;
3324+
}
33023325
}
3326+
} else{
3327+
constbool skip = (colon > port && port[0] == '[' && colon[-1] == ']');
3328+
the_host->assign(port + skip, colon - skip);
33033329
}
33043330

3331+
char* endptr;
3332+
errno = 0;
3333+
constchar* const digits = colon != nullptr ? colon + 1 : port;
3334+
constlong result = strtol(digits, &endptr, 10); // NOLINT(runtime/int)
3335+
if (errno != 0 || *endptr != '\0' || result < 1024 || result > 65535){
3336+
fprintf(stderr, "Debug port must be in range 1024 to 65535.\n");
3337+
PrintHelp();
3338+
exit(12);
3339+
}
3340+
3341+
*the_port = static_cast<int>(result);
3342+
33053343
returntrue;
33063344
}
33073345

@@ -3539,9 +3577,11 @@ static void StartDebug(Environment* env, bool wait){
35393577

35403578
env->debugger_agent()->set_dispatch_handler(
35413579
DispatchMessagesDebugAgentCallback);
3542-
debugger_running = env->debugger_agent()->Start(debug_port, wait);
3580+
debugger_running =
3581+
env->debugger_agent()->Start(debug_host, debug_port, wait);
35433582
if (debugger_running == false){
3544-
fprintf(stderr, "Starting debugger on port %d failed\n", debug_port);
3583+
fprintf(stderr, "Starting debugger on %s:%d failed\n",
3584+
debug_host.c_str(), debug_port);
35453585
fflush(stderr);
35463586
return;
35473587
}

‎test/parallel/test-cluster-disconnect-handles.js‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,8 @@ cluster.schedulingPolicy = cluster.SCHED_RR;
2525
if(cluster.isMaster){
2626
letisKilling=false;
2727
consthandles=require('internal/cluster').handles;
28-
// FIXME(bnoordhuis) lib/cluster.js scans the execArgv arguments for
29-
// debugger flags and renumbers any port numbers it sees starting
30-
// from the default port 5858. Add a '.' that circumvents the
31-
// scanner but is ignored by atoi(3). Heinous hack.
32-
cluster.setupMaster({execArgv: [`--debug=${common.PORT}.`]});
28+
constaddress=common.hasIPv6 ? '[::1]' : common.localhostIPv4;
29+
cluster.setupMaster({execArgv: [`--debug=${address}:${common.PORT}`]});
3330
constworker=cluster.fork();
3431
worker.once('exit',common.mustCall((code,signal)=>{
3532
assert.strictEqual(code,0,'worker did not exit normally');

‎test/parallel/test-debug-port-cluster.js‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ child.stderr.setEncoding('utf8');
1616

1717
constcheckMessages=common.mustCall(()=>{
1818
for(letport=PORT_MIN;port<=PORT_MAX;port+=1){
19-
assert(stderr.includes(`Debugger listening on port ${port}`));
19+
constre=RegExp(`Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):${port}`);
20+
assert(re.test(stderr));
2021
}
2122
});
2223

‎test/parallel/test-debug-port-from-cmdline.js‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ function processStderrLine(line){
3939
functionassertOutputLines(){
4040
varexpectedLines=[
4141
'Starting debugger agent.',
42-
'Debugger listening on port '+debugPort
42+
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):'+debugPort,
4343
];
4444

4545
assert.equal(outputLines.length,expectedLines.length);
4646
for(vari=0;i<expectedLines.length;i++)
47-
assert.equal(outputLines[i],expectedLines[i]);
48-
47+
assert(RegExp(expectedLines[i]).test(outputLines[i]));
4948
}

‎test/parallel/test-debug-port-numbers.js‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ function kill(child){
5252

5353
process.on('exit',function(){
5454
for(constchildofchildren){
55-
constone=RegExp(`Debugger listening on port ${child.test.port}`);
56-
consttwo=RegExp(`connecting to 127.0.0.1:${child.test.port}`);
55+
constport=child.test.port;
56+
constone=RegExp(`Debugger listening on (\\[::\\]|0\.0\.0\.0):${port}`);
57+
consttwo=RegExp(`connecting to 127.0.0.1:${port}`);
5758
assert(one.test(child.test.stdout));
5859
assert(two.test(child.test.stdout));
5960
}

‎test/parallel/test-debug-signal-cluster.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ process.on('exit', function onExit(){
6565

6666
constexpectedLines=[
6767
'Starting debugger agent.',
68-
'Debugger listening on port '+(port+0),
68+
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):'+(port+0),
6969
'Starting debugger agent.',
70-
'Debugger listening on port '+(port+1),
70+
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):'+(port+1),
7171
'Starting debugger agent.',
72-
'Debugger listening on port '+(port+2),
72+
'Debugger listening on (\\[::\\]|0\\.0\\.0\\.0):'+(port+2),
7373
];
7474

7575
functionassertOutputLines(){
@@ -79,5 +79,7 @@ function assertOutputLines(){
7979
outputLines.sort();
8080
expectedLines.sort();
8181

82-
assert.deepStrictEqual(outputLines,expectedLines);
82+
assert.equal(outputLines.length,expectedLines.length);
83+
for(vari=0;i<expectedLines.length;i++)
84+
assert(RegExp(expectedLines[i]).test(outputLines[i]));
8385
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
constassert=require('assert');
5+
constspawn=require('child_process').spawn;
6+
7+
letrun=()=>{};
8+
functiontest(args,re){
9+
constnext=run;
10+
run=()=>{
11+
constoptions={encoding: 'utf8'};
12+
constproc=spawn(process.execPath,args.concat(['-e','0']),options);
13+
letstderr='';
14+
proc.stderr.setEncoding('utf8');
15+
proc.stderr.on('data',(data)=>{
16+
stderr+=data;
17+
if(re.test(stderr))proc.kill();
18+
});
19+
proc.on('exit',common.mustCall(()=>{
20+
assert(re.test(stderr));
21+
next();
22+
}));
23+
};
24+
}
25+
26+
test(['--debug-brk'],/Debuggerlisteningon(\[::\]|0\.0\.0\.0):5858/);
27+
test(['--debug-brk=1234'],/Debuggerlisteningon(\[::\]|0\.0\.0\.0):1234/);
28+
test(['--debug-brk=127.0.0.1'],/Debuggerlisteningon127\.0\.0\.1:5858/);
29+
test(['--debug-brk=127.0.0.1:1234'],/Debuggerlisteningon127\.0\.0\.1:1234/);
30+
test(['--debug-brk=localhost'],
31+
/Debuggerlisteningon(\[::\]|127\.0\.0\.1):5858/);
32+
test(['--debug-brk=localhost:1234'],
33+
/Debuggerlisteningon(\[::\]|127\.0\.0\.1):1234/);
34+
35+
if(common.hasIPv6){
36+
test(['--debug-brk=::'],/Debugportmustbeinrange1024to65535/);
37+
test(['--debug-brk=::0'],/Debugportmustbeinrange1024to65535/);
38+
test(['--debug-brk=::1'],/Debugportmustbeinrange1024to65535/);
39+
test(['--debug-brk=[::]'],/Debuggerlisteningon\[::\]:5858/);
40+
test(['--debug-brk=[::0]'],/Debuggerlisteningon\[::\]:5858/);
41+
test(['--debug-brk=[::]:1234'],/Debuggerlisteningon\[::\]:1234/);
42+
test(['--debug-brk=[::0]:1234'],/Debuggerlisteningon\[::\]:1234/);
43+
test(['--debug-brk=[::ffff:127.0.0.1]:1234'],
44+
/Debuggerlisteningon\[::ffff:127\.0\.0\.1\]:1234/);
45+
}
46+
47+
run();// Runs tests in reverse order.

0 commit comments

Comments
(0)