Node.js  v8.x
Node.js is a JavaScript runtime built on Chrome's V8 JavaScript engine
node_debug_options.cc
Go to the documentation of this file.
1 #include "node_debug_options.h"
2 
3 #include <errno.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include "util.h"
7 
8 namespace node {
9 
10 namespace {
11 const int default_inspector_port = 9229;
12 
13 inline std::string remove_brackets(const std::string& host) {
14  if (!host.empty() && host.front() == '[' && host.back() == ']')
15  return host.substr(1, host.size() - 2);
16  else
17  return host;
18 }
19 
20 int parse_and_validate_port(const std::string& port) {
21  char* endptr;
22  errno = 0;
23  const long result = strtol(port.c_str(), &endptr, 10); // NOLINT(runtime/int)
24  if (errno != 0 || *endptr != '\0'||
25  (result != 0 && result < 1024) || result > 65535) {
26  fprintf(stderr, "Debug port must be 0 or in range 1024 to 65535.\n");
27  exit(12);
28  }
29  return static_cast<int>(result);
30 }
31 
32 std::pair<std::string, int> split_host_port(const std::string& arg) {
33  // remove_brackets only works if no port is specified
34  // so if it has an effect only an IPv6 address was specified
35  std::string host = remove_brackets(arg);
36  if (host.length() < arg.length())
37  return {host, -1};
38 
39  size_t colon = arg.rfind(':');
40  if (colon == std::string::npos) {
41  // Either a port number or a host name. Assume that
42  // if it's not all decimal digits, it's a host name.
43  for (char c : arg) {
44  if (c < '0' || c > '9') {
45  return {arg, -1};
46  }
47  }
48  return {"", parse_and_validate_port(arg)};
49  }
50  // host and port found
51  return std::make_pair(remove_brackets(arg.substr(0, colon)),
52  parse_and_validate_port(arg.substr(colon + 1)));
53 }
54 
55 } // namespace
56 
58  inspector_enabled_(false),
59  deprecated_debug_(false),
60  break_first_line_(false),
61  host_name_("127.0.0.1"), port_(-1) { }
62 
63 bool DebugOptions::ParseOption(const char* argv0, const std::string& option) {
64  bool has_argument = false;
65  std::string option_name;
66  std::string argument;
67 
68  auto pos = option.find("=");
69  if (pos == std::string::npos) {
70  option_name = option;
71  } else {
72  option_name = option.substr(0, pos);
73  argument = option.substr(pos + 1);
74 
75  if (argument.length() > 0)
76  has_argument = true;
77  else
78  argument.clear();
79  }
80 
81  // Note that --debug-port and --debug-brk in conjunction with --inspect
82  // work but are undocumented.
83  // --debug is no longer valid.
84  // Ref: https://github.com/nodejs/node/issues/12630
85  // Ref: https://github.com/nodejs/node/pull/12949
86  if (option_name == "--inspect") {
87  inspector_enabled_ = true;
88  } else if (option_name == "--debug") {
89  deprecated_debug_ = true;
90  } else if (option_name == "--inspect-brk") {
91  inspector_enabled_ = true;
92  break_first_line_ = true;
93  } else if (option_name == "--debug-brk") {
94  break_first_line_ = true;
95  deprecated_debug_ = true;
96  } else if (option_name == "--debug-port" ||
97  option_name == "--inspect-port") {
98  if (!has_argument) {
99  fprintf(stderr, "%s: %s requires an argument\n",
100  argv0, option.c_str());
101  exit(9);
102  }
103  } else {
104  return false;
105  }
106 
107 #if !HAVE_INSPECTOR
108  if (inspector_enabled_) {
109  fprintf(stderr,
110  "Inspector support is not available with this Node.js build\n");
111  }
112  inspector_enabled_ = false;
113  return false;
114 #endif
115 
116  // argument can be specified for *any* option to specify host:port
117  if (has_argument) {
118  std::pair<std::string, int> host_port = split_host_port(argument);
119  if (!host_port.first.empty()) {
120  host_name_ = host_port.first;
121  }
122  if (host_port.second >= 0) {
123  port_ = host_port.second;
124  }
125  }
126 
127  return true;
128 }
129 
130 int DebugOptions::port() const {
131  int port = port_;
132  if (port < 0) {
133  port = default_inspector_port;
134  }
135  return port;
136 }
137 
138 } // namespace node
bool ParseOption(const char *argv0, const std::string &option)
hostent * host
Definition: cares_wrap.cc:482