Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 1 | //===-- lldb-platform.cpp ---------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Daniel Malea | 93a6430 | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 12 | // C Includes |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 13 | #include <errno.h> |
| 14 | #include <getopt.h> |
| 15 | #include <signal.h> |
| 16 | #include <stdint.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 21 | // C++ Includes |
| 22 | |
| 23 | // Other libraries and framework includes |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 24 | #include "lldb/lldb-private-log.h" |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 25 | #include "lldb/Core/Error.h" |
| 26 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 27 | #include "lldb/Core/ConnectionMachPort.h" |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 28 | #include "lldb/Core/Debugger.h" |
| 29 | #include "lldb/Core/StreamFile.h" |
Greg Clayton | 59b4fa1 | 2012-03-29 17:46:11 +0000 | [diff] [blame] | 30 | #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h" |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 31 | #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h" |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 32 | using namespace lldb; |
| 33 | using namespace lldb_private; |
| 34 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 35 | //---------------------------------------------------------------------- |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 36 | // option descriptors for getopt_long_only() |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 37 | //---------------------------------------------------------------------- |
| 38 | |
| 39 | int g_debug = 0; |
| 40 | int g_verbose = 0; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 41 | int g_stay_alive = 0; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 42 | |
| 43 | static struct option g_long_options[] = |
| 44 | { |
| 45 | { "debug", no_argument, &g_debug, 1 }, |
| 46 | { "verbose", no_argument, &g_verbose, 1 }, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 47 | { "stay-alive", no_argument, &g_stay_alive, 1 }, |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 48 | { "log-file", required_argument, NULL, 'l' }, |
| 49 | { "log-flags", required_argument, NULL, 'f' }, |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 50 | { "listen", required_argument, NULL, 'L' }, |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 51 | { "port-offset", required_argument, NULL, 'p' }, |
| 52 | { "gdbserver-port", required_argument, NULL, 'P' }, |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 53 | { "min-gdbserver-port", required_argument, NULL, 'm' }, |
| 54 | { "max-gdbserver-port", required_argument, NULL, 'M' }, |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 55 | { NULL, 0, NULL, 0 } |
| 56 | }; |
| 57 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 58 | #if defined (__APPLE__) |
| 59 | #define LOW_PORT (IPPORT_RESERVED) |
| 60 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 61 | #else |
| 62 | #define LOW_PORT (1024u) |
| 63 | #define HIGH_PORT (49151u) |
| 64 | #endif |
| 65 | |
| 66 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 67 | //---------------------------------------------------------------------- |
| 68 | // Watch for signals |
| 69 | //---------------------------------------------------------------------- |
| 70 | int g_sigpipe_received = 0; |
| 71 | void |
| 72 | signal_handler(int signo) |
| 73 | { |
| 74 | switch (signo) |
| 75 | { |
| 76 | case SIGPIPE: |
| 77 | g_sigpipe_received = 1; |
| 78 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 79 | case SIGHUP: |
| 80 | // Use SIGINT first, if that does not work, use SIGHUP as a last resort. |
| 81 | // And we should not call exit() here because it results in the global destructors |
| 82 | // to be invoked and wreaking havoc on the threads still running. |
| 83 | Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-platform...\n"); |
| 84 | abort(); |
| 85 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 89 | static void |
| 90 | display_usage (const char *progname) |
| 91 | { |
| 92 | fprintf(stderr, "Usage:\n %s [--log-file log-file-path] [--log-flags flags] --listen port\n", progname); |
| 93 | exit(0); |
| 94 | } |
| 95 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 96 | //---------------------------------------------------------------------- |
| 97 | // main |
| 98 | //---------------------------------------------------------------------- |
| 99 | int |
| 100 | main (int argc, char *argv[]) |
| 101 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 102 | const char *progname = argv[0]; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 103 | signal (SIGPIPE, signal_handler); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 104 | signal (SIGHUP, signal_handler); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 105 | int long_option_index = 0; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 106 | StreamSP log_stream_sp; |
| 107 | Args log_args; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 108 | Error error; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 109 | std::string listen_host_port; |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 110 | int ch; |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 111 | Debugger::Initialize(); |
| 112 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 113 | GDBRemoteCommunicationServer::PortMap gdbserver_portmap; |
| 114 | int min_gdbserver_port = 0; |
| 115 | int max_gdbserver_port = 0; |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 116 | uint16_t port_offset = 0; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 117 | |
| 118 | bool show_usage = false; |
| 119 | int option_error = 0; |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 120 | // Enable LLDB log channels... |
| 121 | StreamSP stream_sp (new StreamFile(stdout, false)); |
| 122 | const char *log_channels[] = { "platform", "host", "process", NULL }; |
| 123 | EnableLog (stream_sp, 0, log_channels, NULL); |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 124 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 125 | #if __GLIBC__ |
| 126 | optind = 0; |
| 127 | #else |
| 128 | optreset = 1; |
| 129 | optind = 1; |
| 130 | #endif |
| 131 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 132 | while ((ch = getopt_long_only(argc, argv, "l:f:L:p:m:M:", g_long_options, &long_option_index)) != -1) |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 133 | { |
| 134 | // DNBLogDebug("option: ch == %c (0x%2.2x) --%s%c%s\n", |
| 135 | // ch, (uint8_t)ch, |
| 136 | // g_long_options[long_option_index].name, |
| 137 | // g_long_options[long_option_index].has_arg ? '=' : ' ', |
| 138 | // optarg ? optarg : ""); |
| 139 | switch (ch) |
| 140 | { |
| 141 | case 0: // Any optional that auto set themselves will return 0 |
| 142 | break; |
| 143 | |
| 144 | case 'l': // Set Log File |
| 145 | if (optarg && optarg[0]) |
| 146 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 147 | if ((strcasecmp(optarg, "stdout") == 0) || (strcmp(optarg, "/dev/stdout") == 0)) |
| 148 | { |
| 149 | log_stream_sp.reset (new StreamFile (stdout, false)); |
| 150 | } |
| 151 | else if ((strcasecmp(optarg, "stderr") == 0) || (strcmp(optarg, "/dev/stderr") == 0)) |
| 152 | { |
| 153 | log_stream_sp.reset (new StreamFile (stderr, false)); |
| 154 | } |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 155 | else |
| 156 | { |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 157 | FILE *log_file = fopen(optarg, "w"); |
| 158 | if (log_file) |
| 159 | { |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 160 | setlinebuf(log_file); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 161 | log_stream_sp.reset (new StreamFile (log_file, true)); |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | const char *errno_str = strerror(errno); |
| 166 | fprintf (stderr, "Failed to open log file '%s' for writing: errno = %i (%s)", optarg, errno, errno_str ? errno_str : "unknown error"); |
| 167 | } |
| 168 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 171 | } |
| 172 | break; |
| 173 | |
| 174 | case 'f': // Log Flags |
| 175 | if (optarg && optarg[0]) |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 176 | log_args.AppendArgument(optarg); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 177 | break; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 178 | |
| 179 | case 'L': |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 180 | listen_host_port.append (optarg); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 181 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 182 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 183 | case 'p': |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 184 | { |
| 185 | char *end = NULL; |
| 186 | long tmp_port_offset = strtoul(optarg, &end, 0); |
| 187 | if (end && *end == '\0') |
| 188 | { |
| 189 | if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT) |
| 190 | { |
| 191 | port_offset = (uint16_t)tmp_port_offset; |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | fprintf (stderr, "error: port offset %li is not in the valid user port range of %u - %u\n", tmp_port_offset, LOW_PORT, HIGH_PORT); |
| 196 | option_error = 5; |
| 197 | } |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | fprintf (stderr, "error: invalid port offset string %s\n", optarg); |
| 202 | option_error = 4; |
| 203 | } |
| 204 | } |
| 205 | break; |
| 206 | |
| 207 | case 'P': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 208 | case 'm': |
| 209 | case 'M': |
| 210 | { |
| 211 | char *end = NULL; |
| 212 | long portnum = strtoul(optarg, &end, 0); |
| 213 | if (end && *end == '\0') |
| 214 | { |
| 215 | if (LOW_PORT <= portnum && portnum <= HIGH_PORT) |
| 216 | { |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 217 | if (ch == 'P') |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 218 | gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID; |
| 219 | else if (ch == 'm') |
| 220 | min_gdbserver_port = portnum; |
| 221 | else |
| 222 | max_gdbserver_port = portnum; |
| 223 | } |
| 224 | else |
| 225 | { |
| 226 | fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT); |
| 227 | option_error = 1; |
| 228 | } |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | fprintf (stderr, "error: invalid port number string %s\n", optarg); |
| 233 | option_error = 2; |
| 234 | } |
| 235 | } |
| 236 | break; |
| 237 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 238 | case 'h': /* fall-through is intentional */ |
| 239 | case '?': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 240 | show_usage = true; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 241 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 242 | } |
| 243 | } |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 244 | |
| 245 | // Make a port map for a port range that was specified. |
| 246 | if (min_gdbserver_port < max_gdbserver_port) |
| 247 | { |
| 248 | for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port) |
| 249 | gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID; |
| 250 | } |
| 251 | else if (min_gdbserver_port != max_gdbserver_port) |
| 252 | { |
| 253 | fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port); |
| 254 | option_error = 3; |
| 255 | |
| 256 | } |
| 257 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 258 | // Print usage and exit if no listening port is specified. |
| 259 | if (listen_host_port.empty()) |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 260 | show_usage = true; |
| 261 | |
| 262 | if (show_usage || option_error) |
| 263 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 264 | display_usage(progname); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 265 | exit(option_error); |
| 266 | } |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 267 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 268 | if (log_stream_sp) |
| 269 | { |
| 270 | if (log_args.GetArgumentCount() == 0) |
| 271 | log_args.AppendArgument("default"); |
Jim Ingham | 228063c | 2012-02-21 02:23:08 +0000 | [diff] [blame] | 272 | ProcessGDBRemoteLog::EnableLog (log_stream_sp, 0,log_args.GetConstArgumentVector(), log_stream_sp.get()); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 275 | // Skip any options we consumed with getopt_long_only |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 276 | argc -= optind; |
| 277 | argv += optind; |
| 278 | |
| 279 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 280 | do { |
| 281 | GDBRemoteCommunicationServer gdb_server (true); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 282 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 283 | if (port_offset > 0) |
| 284 | gdb_server.SetPortOffset(port_offset); |
| 285 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 286 | if (!gdbserver_portmap.empty()) |
| 287 | { |
| 288 | gdb_server.SetPortMap(std::move(gdbserver_portmap)); |
| 289 | } |
| 290 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 291 | if (!listen_host_port.empty()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 292 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 293 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 294 | if (conn_ap.get()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 295 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 296 | for (int j = 0; j < listen_host_port.size(); j++) |
| 297 | { |
| 298 | char c = listen_host_port[j]; |
| 299 | if (c > '9' || c < '0') |
| 300 | printf("WARNING: passing anything but a number as argument to --listen will most probably make connecting impossible.\n"); |
| 301 | } |
| 302 | std::auto_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 303 | if (conn_ap.get()) |
| 304 | { |
| 305 | std::string connect_url ("listen://"); |
| 306 | connect_url.append(listen_host_port.c_str()); |
| 307 | |
| 308 | printf ("Listening for a connection on %s...\n", listen_host_port.c_str()); |
| 309 | if (conn_ap->Connect(connect_url.c_str(), &error) == eConnectionStatusSuccess) |
| 310 | { |
| 311 | printf ("Connection established.\n"); |
| 312 | gdb_server.SetConnection (conn_ap.release()); |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | if (gdb_server.IsConnected()) |
| 318 | { |
| 319 | // After we connected, we need to get an initial ack from... |
| 320 | if (gdb_server.HandshakeWithClient(&error)) |
| 321 | { |
| 322 | bool interrupt = false; |
| 323 | bool done = false; |
| 324 | while (!interrupt && !done) |
| 325 | { |
| 326 | if (!gdb_server.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done)) |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | if (error.Fail()) |
| 331 | { |
| 332 | fprintf(stderr, "error: %s\n", error.AsCString()); |
| 333 | } |
| 334 | } |
| 335 | else |
| 336 | { |
| 337 | fprintf(stderr, "error: handshake with client failed\n"); |
| 338 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 341 | } while (g_stay_alive); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 342 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 343 | Debugger::Terminate(); |
| 344 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 345 | fprintf(stderr, "lldb-platform exiting...\n"); |
| 346 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 347 | return 0; |
| 348 | } |