GCC Code Coverage Report
Directory: ../ Exec Total Coverage
File: /home/iojs/build/workspace/node-test-commit-linux-coverage/nodes/benchmark/out/../src/node_debug_options.cc Lines: 61 66 92.4 %
Date: 2017-06-14 Branches: 43 56 76.8 %

Line Branch Exec Source
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
20
inline std::string remove_brackets(const std::string& host) {
14


20
  if (!host.empty() && host.front() == '[' && host.back() == ']')
15
    return host.substr(1, host.size() - 2);
16
  else
17
20
    return host;
18
}
19
20
16
int parse_and_validate_port(const std::string& port) {
21
  char* endptr;
22
16
  errno = 0;
23
16
  const long result = strtol(port.c_str(), &endptr, 10);  // NOLINT(runtime/int)
24

16
  if (errno != 0 || *endptr != '\0'||
25

9
      (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
16
  return static_cast<int>(result);
30
}
31
32
16
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
16
  std::string host = remove_brackets(arg);
36
16
  if (host.length() < arg.length())
37
    return {host, -1};
38
39
16
  size_t colon = arg.rfind(':');
40
16
  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
60
    for (char c : arg) {
44

48
      if (c < '0' || c > '9') {
45
        return {arg, -1};
46
      }
47
    }
48
12
    return {"", parse_and_validate_port(arg)};
49
  }
50
  // host and port found
51
  return std::make_pair(remove_brackets(arg.substr(0, colon)),
52
4
                        parse_and_validate_port(arg.substr(colon + 1)));
53
}
54
55
}  // namespace
56
57
4847
DebugOptions::DebugOptions() :
58
                               inspector_enabled_(false),
59
                               deprecated_debug_(false),
60
                               break_first_line_(false),
61
4847
                               host_name_("127.0.0.1"), port_(-1) { }
62
63
444
bool DebugOptions::ParseOption(const char* argv0, const std::string& option) {
64
444
  bool has_argument = false;
65
444
  std::string option_name;
66
884
  std::string argument;
67
68
444
  auto pos = option.find("=");
69
444
  if (pos == std::string::npos) {
70
407
    option_name = option;
71
  } else {
72
37
    option_name = option.substr(0, pos);
73
37
    argument = option.substr(pos + 1);
74
75
37
    if (argument.length() > 0)
76
35
      has_argument = true;
77
    else
78
2
      argument.clear();
79
  }
80
81
  // Note that --debug-port and --debug-brk in conjuction 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
444
  if (option_name == "--inspect") {
87
14
    inspector_enabled_ = true;
88
430
  } else if (option_name == "--debug") {
89
1
    deprecated_debug_ = true;
90
429
  } else if (option_name == "--inspect-brk") {
91
3
    inspector_enabled_ = true;
92
3
    break_first_line_ = true;
93
426
  } else if (option_name == "--debug-brk") {
94
2
    break_first_line_ = true;
95
2
    deprecated_debug_ = true;
96

845
  } else if (option_name == "--debug-port" ||
97
421
             option_name == "--inspect-port") {
98
10
    if (!has_argument) {
99
      fprintf(stderr, "%s: %s requires an argument\n",
100
4
              argv0, option.c_str());
101
4
      exit(9);
102
    }
103
  } else {
104
414
    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
26
  if (has_argument) {
118
16
    std::pair<std::string, int> host_port = split_host_port(argument);
119
16
    if (!host_port.first.empty()) {
120
4
      host_name_ = host_port.first;
121
    }
122
16
    if (host_port.second >= 0) {
123
16
      port_ = host_port.second;
124
16
    }
125
  }
126
127
466
  return true;
128
}
129
130
51
int DebugOptions::port() const {
131
51
  int port = port_;
132
51
  if (port < 0) {
133
17
    port = default_inspector_port;
134
  }
135
51
  return port;
136
}
137
138
}  // namespace node