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> |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 14 | #if defined(__APPLE__) |
| 15 | #include <netinet/in.h> |
| 16 | #endif |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 17 | #include <signal.h> |
| 18 | #include <stdint.h> |
| 19 | #include <stdio.h> |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 23 | // C++ Includes |
| 24 | |
| 25 | // Other libraries and framework includes |
Greg Clayton | fbb7634 | 2013-11-20 21:07:01 +0000 | [diff] [blame] | 26 | #include "lldb/lldb-private-log.h" |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 27 | #include "lldb/Core/Error.h" |
| 28 | #include "lldb/Core/ConnectionFileDescriptor.h" |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 29 | #include "lldb/Core/ConnectionMachPort.h" |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 30 | #include "lldb/Core/Debugger.h" |
| 31 | #include "lldb/Core/StreamFile.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 32 | #include "lldb/Host/HostGetOpt.h" |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 33 | #include "lldb/Host/OptionParser.h" |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 34 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 35 | #include "lldb/Interpreter/CommandReturnObject.h" |
Greg Clayton | 59b4fa1 | 2012-03-29 17:46:11 +0000 | [diff] [blame] | 36 | #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h" |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 37 | #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h" |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 38 | using namespace lldb; |
| 39 | using namespace lldb_private; |
| 40 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 41 | //---------------------------------------------------------------------- |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 42 | // option descriptors for getopt_long_only() |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 43 | //---------------------------------------------------------------------- |
| 44 | |
| 45 | int g_debug = 0; |
| 46 | int g_verbose = 0; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 47 | int g_stay_alive = 0; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 48 | |
| 49 | static struct option g_long_options[] = |
| 50 | { |
| 51 | { "debug", no_argument, &g_debug, 1 }, |
| 52 | { "verbose", no_argument, &g_verbose, 1 }, |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 53 | { "stay-alive", no_argument, &g_stay_alive, 1 }, |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 54 | { "listen", required_argument, NULL, 'L' }, |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 55 | { "port-offset", required_argument, NULL, 'p' }, |
| 56 | { "gdbserver-port", required_argument, NULL, 'P' }, |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 57 | { "min-gdbserver-port", required_argument, NULL, 'm' }, |
| 58 | { "max-gdbserver-port", required_argument, NULL, 'M' }, |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 59 | { "lldb-command", required_argument, NULL, 'c' }, |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 60 | { NULL, 0, NULL, 0 } |
| 61 | }; |
| 62 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 63 | #if defined (__APPLE__) |
| 64 | #define LOW_PORT (IPPORT_RESERVED) |
| 65 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 66 | #else |
| 67 | #define LOW_PORT (1024u) |
| 68 | #define HIGH_PORT (49151u) |
| 69 | #endif |
| 70 | |
| 71 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 72 | //---------------------------------------------------------------------- |
| 73 | // Watch for signals |
| 74 | //---------------------------------------------------------------------- |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 75 | void |
| 76 | signal_handler(int signo) |
| 77 | { |
| 78 | switch (signo) |
| 79 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 80 | case SIGHUP: |
| 81 | // Use SIGINT first, if that does not work, use SIGHUP as a last resort. |
| 82 | // And we should not call exit() here because it results in the global destructors |
| 83 | // to be invoked and wreaking havoc on the threads still running. |
| 84 | Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-platform...\n"); |
| 85 | abort(); |
| 86 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 90 | static void |
| 91 | display_usage (const char *progname) |
| 92 | { |
| 93 | fprintf(stderr, "Usage:\n %s [--log-file log-file-path] [--log-flags flags] --listen port\n", progname); |
| 94 | exit(0); |
| 95 | } |
| 96 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 97 | //---------------------------------------------------------------------- |
| 98 | // main |
| 99 | //---------------------------------------------------------------------- |
| 100 | int |
| 101 | main (int argc, char *argv[]) |
| 102 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 103 | const char *progname = argv[0]; |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 104 | signal (SIGPIPE, SIG_IGN); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 105 | signal (SIGHUP, signal_handler); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 106 | int long_option_index = 0; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 107 | Error error; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 108 | std::string listen_host_port; |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 109 | int ch; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 110 | Debugger::Initialize(NULL); |
| 111 | |
| 112 | lldb::DebuggerSP debugger_sp = Debugger::CreateInstance (); |
| 113 | |
| 114 | debugger_sp->SetInputFileHandle(stdin, false); |
| 115 | debugger_sp->SetOutputFileHandle(stdout, false); |
| 116 | debugger_sp->SetErrorFileHandle(stderr, false); |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 117 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 118 | GDBRemoteCommunicationServer::PortMap gdbserver_portmap; |
| 119 | int min_gdbserver_port = 0; |
| 120 | int max_gdbserver_port = 0; |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 121 | uint16_t port_offset = 0; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 122 | |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 123 | std::vector<std::string> lldb_commands; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 124 | bool show_usage = false; |
| 125 | int option_error = 0; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 126 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 127 | std::string short_options(OptionParser::GetShortOptionString(g_long_options)); |
| 128 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 129 | #if __GLIBC__ |
| 130 | optind = 0; |
| 131 | #else |
| 132 | optreset = 1; |
| 133 | optind = 1; |
| 134 | #endif |
| 135 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 136 | while ((ch = getopt_long_only(argc, argv, short_options.c_str(), g_long_options, &long_option_index)) != -1) |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 137 | { |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 138 | switch (ch) |
| 139 | { |
| 140 | case 0: // Any optional that auto set themselves will return 0 |
| 141 | break; |
| 142 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 143 | case 'L': |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 144 | listen_host_port.append (optarg); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 145 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 146 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 147 | case 'p': |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 148 | { |
| 149 | char *end = NULL; |
| 150 | long tmp_port_offset = strtoul(optarg, &end, 0); |
| 151 | if (end && *end == '\0') |
| 152 | { |
| 153 | if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT) |
| 154 | { |
| 155 | port_offset = (uint16_t)tmp_port_offset; |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | 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); |
| 160 | option_error = 5; |
| 161 | } |
| 162 | } |
| 163 | else |
| 164 | { |
| 165 | fprintf (stderr, "error: invalid port offset string %s\n", optarg); |
| 166 | option_error = 4; |
| 167 | } |
| 168 | } |
| 169 | break; |
| 170 | |
| 171 | case 'P': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 172 | case 'm': |
| 173 | case 'M': |
| 174 | { |
| 175 | char *end = NULL; |
| 176 | long portnum = strtoul(optarg, &end, 0); |
| 177 | if (end && *end == '\0') |
| 178 | { |
| 179 | if (LOW_PORT <= portnum && portnum <= HIGH_PORT) |
| 180 | { |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 181 | if (ch == 'P') |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 182 | gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID; |
| 183 | else if (ch == 'm') |
| 184 | min_gdbserver_port = portnum; |
| 185 | else |
| 186 | max_gdbserver_port = portnum; |
| 187 | } |
| 188 | else |
| 189 | { |
| 190 | fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT); |
| 191 | option_error = 1; |
| 192 | } |
| 193 | } |
| 194 | else |
| 195 | { |
| 196 | fprintf (stderr, "error: invalid port number string %s\n", optarg); |
| 197 | option_error = 2; |
| 198 | } |
| 199 | } |
| 200 | break; |
| 201 | |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 202 | case 'c': |
| 203 | lldb_commands.push_back(optarg); |
| 204 | break; |
| 205 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 206 | case 'h': /* fall-through is intentional */ |
| 207 | case '?': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 208 | show_usage = true; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 209 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 210 | } |
| 211 | } |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 212 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 213 | // Make a port map for a port range that was specified. |
| 214 | if (min_gdbserver_port < max_gdbserver_port) |
| 215 | { |
| 216 | for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port) |
| 217 | gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID; |
| 218 | } |
| 219 | else if (min_gdbserver_port != max_gdbserver_port) |
| 220 | { |
| 221 | fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port); |
| 222 | option_error = 3; |
| 223 | |
| 224 | } |
| 225 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 226 | // Print usage and exit if no listening port is specified. |
| 227 | if (listen_host_port.empty()) |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 228 | show_usage = true; |
| 229 | |
| 230 | if (show_usage || option_error) |
| 231 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 232 | display_usage(progname); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 233 | exit(option_error); |
| 234 | } |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 235 | |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 236 | // Skip any options we consumed with getopt_long_only |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 237 | argc -= optind; |
| 238 | argv += optind; |
| 239 | |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 240 | // Execute any LLDB commands that we were asked to evaluate. |
| 241 | for (const auto &lldb_command : lldb_commands) |
| 242 | { |
| 243 | lldb_private::CommandReturnObject result; |
| 244 | printf("(lldb) %s\n", lldb_command.c_str()); |
| 245 | debugger_sp->GetCommandInterpreter().HandleCommand(lldb_command.c_str(), eLazyBoolNo, result); |
| 246 | const char *output = result.GetOutputData(); |
| 247 | if (output && output[0]) |
| 248 | puts(output); |
| 249 | } |
| 250 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 251 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 252 | do { |
| 253 | GDBRemoteCommunicationServer gdb_server (true); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 254 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 255 | if (port_offset > 0) |
| 256 | gdb_server.SetPortOffset(port_offset); |
| 257 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 258 | if (!gdbserver_portmap.empty()) |
| 259 | { |
| 260 | gdb_server.SetPortMap(std::move(gdbserver_portmap)); |
| 261 | } |
| 262 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 263 | if (!listen_host_port.empty()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 264 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 265 | std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor()); |
| 266 | if (conn_ap.get()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 267 | { |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 268 | std::string connect_url ("listen://"); |
| 269 | connect_url.append(listen_host_port.c_str()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 270 | |
Greg Clayton | 00fe87b | 2013-12-05 22:58:22 +0000 | [diff] [blame] | 271 | printf ("Listening for a connection from %s...\n", listen_host_port.c_str()); |
| 272 | if (conn_ap->Connect(connect_url.c_str(), &error) == eConnectionStatusSuccess) |
| 273 | { |
| 274 | printf ("Connection established.\n"); |
| 275 | gdb_server.SetConnection (conn_ap.release()); |
| 276 | } |
| 277 | else |
| 278 | { |
| 279 | printf ("error: %s\n", error.AsCString()); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | |
| 283 | if (gdb_server.IsConnected()) |
| 284 | { |
| 285 | // After we connected, we need to get an initial ack from... |
| 286 | if (gdb_server.HandshakeWithClient(&error)) |
| 287 | { |
| 288 | bool interrupt = false; |
| 289 | bool done = false; |
| 290 | while (!interrupt && !done) |
| 291 | { |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 292 | if (gdb_server.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 293 | break; |
| 294 | } |
| 295 | |
| 296 | if (error.Fail()) |
| 297 | { |
| 298 | fprintf(stderr, "error: %s\n", error.AsCString()); |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | fprintf(stderr, "error: handshake with client failed\n"); |
| 304 | } |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 305 | } |
| 306 | } |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 307 | } while (g_stay_alive); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 308 | |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 309 | Debugger::Terminate(); |
| 310 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 311 | fprintf(stderr, "lldb-platform exiting...\n"); |
| 312 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 313 | return 0; |
| 314 | } |