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