|
58 | 58 | #include <stdlib.h>
|
59 | 59 | #include <string.h>
|
60 | 60 | #include <sys/types.h>
|
| 61 | + |
| 62 | +#include <string> |
61 | 63 | #include <vector>
|
62 | 64 |
|
63 | 65 | #if defined(NODE_HAVE_I18N_SUPPORT)
|
@@ -139,6 +141,7 @@ static unsigned int preload_module_count = 0;
|
139 | 141 | static const char** preload_modules = nullptr;
|
140 | 142 | static bool use_debug_agent = false;
|
141 | 143 | static bool debug_wait_connect = false;
|
| 144 | +static std::string debug_host; // NOLINT(runtime/string) |
142 | 145 | static int debug_port = 5858;
|
143 | 146 | static bool prof_process = false;
|
144 | 147 | static bool v8_is_profiling = false;
|
@@ -3288,20 +3291,55 @@ static bool ParseDebugOpt(const char* arg) {
|
3288 | 3291 | debug_wait_connect = true;
|
3289 | 3292 | port = arg + sizeof("--debug-brk=") - 1;
|
3290 | 3293 | } else if (!strncmp(arg, "--debug-port=", sizeof("--debug-port=") - 1)) {
|
| 3294 | + // XXX(bnoordhuis) Misnomer, configures port and listen address. |
3291 | 3295 | port = arg + sizeof("--debug-port=") - 1;
|
3292 | 3296 | } else {
|
3293 | 3297 | return false;
|
3294 | 3298 | }
|
3295 | 3299 |
|
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 | + return true; |
| 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 | + const size_t port_len = strlen(port); |
| 3311 | + if (port[0] == '[' && port[port_len - 1] == ']') { |
| 3312 | + the_host->assign(port + 1, port_len - 2); |
| 3313 | + return true; |
| 3314 | + } |
| 3315 | + |
| 3316 | + const char* 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 | + return true; |
| 3324 | + } |
3302 | 3325 | }
|
| 3326 | + } else { |
| 3327 | + const bool skip = (colon > port && port[0] == '[' && colon[-1] == ']'); |
| 3328 | + the_host->assign(port + skip, colon - skip); |
3303 | 3329 | }
|
3304 | 3330 |
|
| 3331 | + char* endptr; |
| 3332 | + errno = 0; |
| 3333 | + const char* const digits = colon != nullptr ? colon + 1 : port; |
| 3334 | + const long 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 | + |
3305 | 3343 | return true;
|
3306 | 3344 | }
|
3307 | 3345 |
|
@@ -3539,9 +3577,11 @@ static void StartDebug(Environment* env, bool wait) {
|
3539 | 3577 |
|
3540 | 3578 | env->debugger_agent()->set_dispatch_handler(
|
3541 | 3579 | DispatchMessagesDebugAgentCallback);
|
3542 |
| - debugger_running = env->debugger_agent()->Start(debug_port, wait); |
| 3580 | + debugger_running = |
| 3581 | + env->debugger_agent()->Start(debug_host, debug_port, wait); |
3543 | 3582 | 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); |
3545 | 3585 | fflush(stderr);
|
3546 | 3586 | return;
|
3547 | 3587 | }
|
|
0 commit comments