blob: 735de1583c45b1782367ffeb0739454d6aa6ee4f [file] [log] [blame]
Greg Claytonb43767a2011-03-22 01:34:44 +00001//===-- 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 Malea93a64302012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Greg Claytond314e812011-03-23 00:09:55 +000012// C Includes
Greg Claytonb43767a2011-03-22 01:34:44 +000013#include <errno.h>
Zachary Turner98688922014-08-06 18:16:26 +000014#if defined(__APPLE__)
15#include <netinet/in.h>
16#endif
Greg Claytonb43767a2011-03-22 01:34:44 +000017#include <signal.h>
18#include <stdint.h>
19#include <stdio.h>
20#include <stdlib.h>
21#include <string.h>
Robert Flacka0e70cd2015-03-25 12:51:31 +000022#include <sys/wait.h>
Greg Claytonb43767a2011-03-22 01:34:44 +000023
Greg Claytond314e812011-03-23 00:09:55 +000024// C++ Includes
25
26// Other libraries and framework includes
27#include "lldb/Core/Error.h"
Greg Clayton9b1e1cd2011-04-04 18:18:57 +000028#include "lldb/Core/ConnectionMachPort.h"
Greg Clayton1cb64962011-03-24 04:28:38 +000029#include "lldb/Core/Debugger.h"
30#include "lldb/Core/StreamFile.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000031#include "lldb/Host/ConnectionFileDescriptor.h"
Zachary Turner98688922014-08-06 18:16:26 +000032#include "lldb/Host/HostGetOpt.h"
Greg Claytonfb909312013-11-23 01:58:15 +000033#include "lldb/Host/OptionParser.h"
Robert Flacka0e70cd2015-03-25 12:51:31 +000034#include "lldb/Host/Socket.h"
Greg Clayton5fb8f792013-12-02 19:35:49 +000035#include "lldb/Interpreter/CommandInterpreter.h"
36#include "lldb/Interpreter/CommandReturnObject.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000037#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h"
Greg Clayton1cb64962011-03-24 04:28:38 +000038#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000039
Greg Claytond314e812011-03-23 00:09:55 +000040using namespace lldb;
41using namespace lldb_private;
42
Greg Claytonb43767a2011-03-22 01:34:44 +000043//----------------------------------------------------------------------
Greg Claytonb7ad58a2013-04-04 20:35:24 +000044// option descriptors for getopt_long_only()
Greg Claytonb43767a2011-03-22 01:34:44 +000045//----------------------------------------------------------------------
46
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000047static int g_debug = 0;
48static int g_verbose = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +000049static int g_server = 0;
Greg Claytonb43767a2011-03-22 01:34:44 +000050
51static struct option g_long_options[] =
52{
53 { "debug", no_argument, &g_debug, 1 },
54 { "verbose", no_argument, &g_verbose, 1 },
Greg Claytond314e812011-03-23 00:09:55 +000055 { "listen", required_argument, NULL, 'L' },
Greg Claytond4724cf2013-11-22 18:55:04 +000056 { "port-offset", required_argument, NULL, 'p' },
57 { "gdbserver-port", required_argument, NULL, 'P' },
Greg Clayton29b8fc42013-11-21 01:44:58 +000058 { "min-gdbserver-port", required_argument, NULL, 'm' },
59 { "max-gdbserver-port", required_argument, NULL, 'M' },
Greg Clayton5fb8f792013-12-02 19:35:49 +000060 { "lldb-command", required_argument, NULL, 'c' },
Robert Flacka0e70cd2015-03-25 12:51:31 +000061 { "server", no_argument, &g_server, 1 },
Greg Claytonb43767a2011-03-22 01:34:44 +000062 { NULL, 0, NULL, 0 }
63};
64
Greg Clayton29b8fc42013-11-21 01:44:58 +000065#if defined (__APPLE__)
66#define LOW_PORT (IPPORT_RESERVED)
67#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
68#else
69#define LOW_PORT (1024u)
70#define HIGH_PORT (49151u)
71#endif
72
73
Greg Claytonb43767a2011-03-22 01:34:44 +000074//----------------------------------------------------------------------
75// Watch for signals
76//----------------------------------------------------------------------
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000077static void
Greg Claytonb43767a2011-03-22 01:34:44 +000078signal_handler(int signo)
79{
80 switch (signo)
81 {
Daniel Maleae0f8f572013-08-26 23:57:52 +000082 case SIGHUP:
83 // Use SIGINT first, if that does not work, use SIGHUP as a last resort.
84 // And we should not call exit() here because it results in the global destructors
85 // to be invoked and wreaking havoc on the threads still running.
Robert Flack8cc4cf12015-03-06 14:36:33 +000086 Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-server...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +000087 abort();
88 break;
Greg Claytonb43767a2011-03-22 01:34:44 +000089 }
90}
91
Daniel Maleae0f8f572013-08-26 23:57:52 +000092static void
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000093display_usage (const char *progname, const char *subcommand)
Daniel Maleae0f8f572013-08-26 23:57:52 +000094{
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000095 fprintf(stderr, "Usage:\n %s %s [--log-file log-file-path] [--log-flags flags] --listen port\n", progname, subcommand);
Daniel Maleae0f8f572013-08-26 23:57:52 +000096 exit(0);
97}
98
Greg Claytonb43767a2011-03-22 01:34:44 +000099//----------------------------------------------------------------------
100// main
101//----------------------------------------------------------------------
102int
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000103main_platform (int argc, char *argv[])
Greg Claytonb43767a2011-03-22 01:34:44 +0000104{
Daniel Maleae0f8f572013-08-26 23:57:52 +0000105 const char *progname = argv[0];
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000106 const char *subcommand = argv[1];
107 argc--;
108 argv++;
Greg Claytonfb909312013-11-23 01:58:15 +0000109 signal (SIGPIPE, SIG_IGN);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000110 signal (SIGHUP, signal_handler);
Greg Claytonb43767a2011-03-22 01:34:44 +0000111 int long_option_index = 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000112 Error error;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000113 std::string listen_host_port;
Greg Clayton9d3d6882011-10-31 23:51:19 +0000114 int ch;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000115
116 lldb::DebuggerSP debugger_sp = Debugger::CreateInstance ();
117
118 debugger_sp->SetInputFileHandle(stdin, false);
119 debugger_sp->SetOutputFileHandle(stdout, false);
120 debugger_sp->SetErrorFileHandle(stderr, false);
Greg Clayton1cb64962011-03-24 04:28:38 +0000121
Tamas Berghammere13c2732015-02-11 10:29:30 +0000122 GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000123 int min_gdbserver_port = 0;
124 int max_gdbserver_port = 0;
Greg Claytond4724cf2013-11-22 18:55:04 +0000125 uint16_t port_offset = 0;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000126
Greg Clayton5fb8f792013-12-02 19:35:49 +0000127 std::vector<std::string> lldb_commands;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000128 bool show_usage = false;
129 int option_error = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +0000130 int socket_error = -1;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000131
Greg Claytonfb909312013-11-23 01:58:15 +0000132 std::string short_options(OptionParser::GetShortOptionString(g_long_options));
133
Greg Claytond4724cf2013-11-22 18:55:04 +0000134#if __GLIBC__
135 optind = 0;
136#else
137 optreset = 1;
138 optind = 1;
139#endif
140
Greg Claytonfb909312013-11-23 01:58:15 +0000141 while ((ch = getopt_long_only(argc, argv, short_options.c_str(), g_long_options, &long_option_index)) != -1)
Greg Claytonb43767a2011-03-22 01:34:44 +0000142 {
Greg Claytonb43767a2011-03-22 01:34:44 +0000143 switch (ch)
144 {
145 case 0: // Any optional that auto set themselves will return 0
146 break;
147
Greg Claytond314e812011-03-23 00:09:55 +0000148 case 'L':
Greg Clayton73bf5db2011-06-17 01:22:15 +0000149 listen_host_port.append (optarg);
Greg Claytond314e812011-03-23 00:09:55 +0000150 break;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000151
Greg Clayton29b8fc42013-11-21 01:44:58 +0000152 case 'p':
Greg Claytond4724cf2013-11-22 18:55:04 +0000153 {
154 char *end = NULL;
155 long tmp_port_offset = strtoul(optarg, &end, 0);
156 if (end && *end == '\0')
157 {
158 if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT)
159 {
160 port_offset = (uint16_t)tmp_port_offset;
161 }
162 else
163 {
164 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);
165 option_error = 5;
166 }
167 }
168 else
169 {
170 fprintf (stderr, "error: invalid port offset string %s\n", optarg);
171 option_error = 4;
172 }
173 }
174 break;
175
176 case 'P':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000177 case 'm':
178 case 'M':
179 {
180 char *end = NULL;
181 long portnum = strtoul(optarg, &end, 0);
182 if (end && *end == '\0')
183 {
184 if (LOW_PORT <= portnum && portnum <= HIGH_PORT)
185 {
Greg Claytond4724cf2013-11-22 18:55:04 +0000186 if (ch == 'P')
Greg Clayton29b8fc42013-11-21 01:44:58 +0000187 gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID;
188 else if (ch == 'm')
189 min_gdbserver_port = portnum;
190 else
191 max_gdbserver_port = portnum;
192 }
193 else
194 {
195 fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT);
196 option_error = 1;
197 }
198 }
199 else
200 {
201 fprintf (stderr, "error: invalid port number string %s\n", optarg);
202 option_error = 2;
203 }
204 }
205 break;
206
Greg Clayton5fb8f792013-12-02 19:35:49 +0000207 case 'c':
208 lldb_commands.push_back(optarg);
209 break;
210
Daniel Maleae0f8f572013-08-26 23:57:52 +0000211 case 'h': /* fall-through is intentional */
212 case '?':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000213 show_usage = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000214 break;
Greg Claytonb43767a2011-03-22 01:34:44 +0000215 }
216 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000217
Greg Clayton29b8fc42013-11-21 01:44:58 +0000218 // Make a port map for a port range that was specified.
219 if (min_gdbserver_port < max_gdbserver_port)
220 {
221 for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port)
222 gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID;
223 }
224 else if (min_gdbserver_port != max_gdbserver_port)
225 {
226 fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port);
227 option_error = 3;
228
229 }
230
Daniel Maleae0f8f572013-08-26 23:57:52 +0000231 // Print usage and exit if no listening port is specified.
232 if (listen_host_port.empty())
Greg Clayton29b8fc42013-11-21 01:44:58 +0000233 show_usage = true;
234
235 if (show_usage || option_error)
236 {
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000237 display_usage(progname, subcommand);
Greg Clayton29b8fc42013-11-21 01:44:58 +0000238 exit(option_error);
239 }
Greg Claytonb43767a2011-03-22 01:34:44 +0000240
Greg Clayton5fb8f792013-12-02 19:35:49 +0000241 // Execute any LLDB commands that we were asked to evaluate.
242 for (const auto &lldb_command : lldb_commands)
243 {
244 lldb_private::CommandReturnObject result;
245 printf("(lldb) %s\n", lldb_command.c_str());
246 debugger_sp->GetCommandInterpreter().HandleCommand(lldb_command.c_str(), eLazyBoolNo, result);
247 const char *output = result.GetOutputData();
248 if (output && output[0])
249 puts(output);
250 }
251
Robert Flacka0e70cd2015-03-25 12:51:31 +0000252 std::unique_ptr<Socket> listening_socket_up;
253 Socket *socket = nullptr;
254 printf ("Listening for a connection from %s...\n", listen_host_port.c_str());
255 const bool children_inherit_listen_socket = false;
256 error = Socket::TcpListen(listen_host_port.c_str(), children_inherit_listen_socket, socket, NULL);
257 if (error.Fail())
258 {
259 printf("error: %s\n", error.AsCString());
260 exit(socket_error);
261 }
262 listening_socket_up.reset(socket);
Greg Claytonb43767a2011-03-22 01:34:44 +0000263
Daniel Maleae0f8f572013-08-26 23:57:52 +0000264 do {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000265 GDBRemoteCommunicationServerPlatform platform;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000266
Greg Claytond4724cf2013-11-22 18:55:04 +0000267 if (port_offset > 0)
Tamas Berghammere13c2732015-02-11 10:29:30 +0000268 platform.SetPortOffset(port_offset);
Greg Claytond4724cf2013-11-22 18:55:04 +0000269
Greg Clayton29b8fc42013-11-21 01:44:58 +0000270 if (!gdbserver_portmap.empty())
271 {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000272 platform.SetPortMap(std::move(gdbserver_portmap));
Greg Clayton29b8fc42013-11-21 01:44:58 +0000273 }
274
Robert Flacka0e70cd2015-03-25 12:51:31 +0000275 const bool children_inherit_accept_socket = true;
276 socket = nullptr;
277 error = listening_socket_up->BlockingAccept(listen_host_port.c_str(), children_inherit_accept_socket, socket);
278 if (error.Fail())
Greg Claytond314e812011-03-23 00:09:55 +0000279 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000280 printf ("error: %s\n", error.AsCString());
281 exit(socket_error);
282 }
283 printf ("Connection established.\n");
284 if (g_server)
285 {
286 // Collect child zombie processes.
287 while (waitpid(-1, nullptr, WNOHANG) > 0);
288 if (fork())
Greg Claytond314e812011-03-23 00:09:55 +0000289 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000290 // Parent will continue to listen for new connections.
291 continue;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000292 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000293 else
Daniel Maleae0f8f572013-08-26 23:57:52 +0000294 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000295 // Child process will handle the connection and exit.
296 g_server = 0;
297 // Listening socket is owned by parent process.
298 listening_socket_up.release();
Greg Claytond314e812011-03-23 00:09:55 +0000299 }
300 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000301 else
302 {
303 // If not running as a server, this process will not accept
304 // connections while a connection is active.
305 listening_socket_up.reset();
306 }
307 platform.SetConnection (new ConnectionFileDescriptor(socket));
308
309 if (platform.IsConnected())
310 {
311 // After we connected, we need to get an initial ack from...
312 if (platform.HandshakeWithClient(&error))
313 {
314 bool interrupt = false;
315 bool done = false;
316 while (!interrupt && !done)
317 {
318 if (platform.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success)
319 break;
320 }
321
322 if (error.Fail())
323 {
324 fprintf(stderr, "error: %s\n", error.AsCString());
325 }
326 }
327 else
328 {
329 fprintf(stderr, "error: handshake with client failed\n");
330 }
331 }
332 } while (g_server);
Greg Claytonb43767a2011-03-22 01:34:44 +0000333
Robert Flack8cc4cf12015-03-06 14:36:33 +0000334 fprintf(stderr, "lldb-server exiting...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +0000335
Greg Claytonb43767a2011-03-22 01:34:44 +0000336 return 0;
337}