blob: 00e46199fcc3900b60a1ded972af16952d5db54c [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;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000042using namespace lldb_private::process_gdb_remote;
Greg Claytond314e812011-03-23 00:09:55 +000043
Greg Claytonb43767a2011-03-22 01:34:44 +000044//----------------------------------------------------------------------
Greg Claytonb7ad58a2013-04-04 20:35:24 +000045// option descriptors for getopt_long_only()
Greg Claytonb43767a2011-03-22 01:34:44 +000046//----------------------------------------------------------------------
47
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000048static int g_debug = 0;
49static int g_verbose = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +000050static int g_server = 0;
Greg Claytonb43767a2011-03-22 01:34:44 +000051
52static struct option g_long_options[] =
53{
54 { "debug", no_argument, &g_debug, 1 },
55 { "verbose", no_argument, &g_verbose, 1 },
Greg Claytond314e812011-03-23 00:09:55 +000056 { "listen", required_argument, NULL, 'L' },
Greg Claytond4724cf2013-11-22 18:55:04 +000057 { "port-offset", required_argument, NULL, 'p' },
58 { "gdbserver-port", required_argument, NULL, 'P' },
Greg Clayton29b8fc42013-11-21 01:44:58 +000059 { "min-gdbserver-port", required_argument, NULL, 'm' },
60 { "max-gdbserver-port", required_argument, NULL, 'M' },
Greg Clayton5fb8f792013-12-02 19:35:49 +000061 { "lldb-command", required_argument, NULL, 'c' },
Robert Flacka0e70cd2015-03-25 12:51:31 +000062 { "server", no_argument, &g_server, 1 },
Greg Claytonb43767a2011-03-22 01:34:44 +000063 { NULL, 0, NULL, 0 }
64};
65
Greg Clayton29b8fc42013-11-21 01:44:58 +000066#if defined (__APPLE__)
67#define LOW_PORT (IPPORT_RESERVED)
68#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
69#else
70#define LOW_PORT (1024u)
71#define HIGH_PORT (49151u)
72#endif
73
74
Greg Claytonb43767a2011-03-22 01:34:44 +000075//----------------------------------------------------------------------
76// Watch for signals
77//----------------------------------------------------------------------
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000078static void
Greg Claytonb43767a2011-03-22 01:34:44 +000079signal_handler(int signo)
80{
81 switch (signo)
82 {
Daniel Maleae0f8f572013-08-26 23:57:52 +000083 case SIGHUP:
84 // Use SIGINT first, if that does not work, use SIGHUP as a last resort.
85 // And we should not call exit() here because it results in the global destructors
86 // to be invoked and wreaking havoc on the threads still running.
Robert Flack8cc4cf12015-03-06 14:36:33 +000087 Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-server...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +000088 abort();
89 break;
Greg Claytonb43767a2011-03-22 01:34:44 +000090 }
91}
92
Daniel Maleae0f8f572013-08-26 23:57:52 +000093static void
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000094display_usage (const char *progname, const char *subcommand)
Daniel Maleae0f8f572013-08-26 23:57:52 +000095{
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000096 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 +000097 exit(0);
98}
99
Greg Claytonb43767a2011-03-22 01:34:44 +0000100//----------------------------------------------------------------------
101// main
102//----------------------------------------------------------------------
103int
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000104main_platform (int argc, char *argv[])
Greg Claytonb43767a2011-03-22 01:34:44 +0000105{
Daniel Maleae0f8f572013-08-26 23:57:52 +0000106 const char *progname = argv[0];
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000107 const char *subcommand = argv[1];
108 argc--;
109 argv++;
Greg Claytonfb909312013-11-23 01:58:15 +0000110 signal (SIGPIPE, SIG_IGN);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000111 signal (SIGHUP, signal_handler);
Greg Claytonb43767a2011-03-22 01:34:44 +0000112 int long_option_index = 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000113 Error error;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000114 std::string listen_host_port;
Greg Clayton9d3d6882011-10-31 23:51:19 +0000115 int ch;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000116
117 lldb::DebuggerSP debugger_sp = Debugger::CreateInstance ();
118
119 debugger_sp->SetInputFileHandle(stdin, false);
120 debugger_sp->SetOutputFileHandle(stdout, false);
121 debugger_sp->SetErrorFileHandle(stderr, false);
Greg Clayton1cb64962011-03-24 04:28:38 +0000122
Tamas Berghammere13c2732015-02-11 10:29:30 +0000123 GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000124 int min_gdbserver_port = 0;
125 int max_gdbserver_port = 0;
Greg Claytond4724cf2013-11-22 18:55:04 +0000126 uint16_t port_offset = 0;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000127
Greg Clayton5fb8f792013-12-02 19:35:49 +0000128 std::vector<std::string> lldb_commands;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000129 bool show_usage = false;
130 int option_error = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +0000131 int socket_error = -1;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000132
Greg Claytonfb909312013-11-23 01:58:15 +0000133 std::string short_options(OptionParser::GetShortOptionString(g_long_options));
134
Greg Claytond4724cf2013-11-22 18:55:04 +0000135#if __GLIBC__
136 optind = 0;
137#else
138 optreset = 1;
139 optind = 1;
140#endif
141
Greg Claytonfb909312013-11-23 01:58:15 +0000142 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 +0000143 {
Greg Claytonb43767a2011-03-22 01:34:44 +0000144 switch (ch)
145 {
146 case 0: // Any optional that auto set themselves will return 0
147 break;
148
Greg Claytond314e812011-03-23 00:09:55 +0000149 case 'L':
Greg Clayton73bf5db2011-06-17 01:22:15 +0000150 listen_host_port.append (optarg);
Greg Claytond314e812011-03-23 00:09:55 +0000151 break;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000152
Greg Clayton29b8fc42013-11-21 01:44:58 +0000153 case 'p':
Greg Claytond4724cf2013-11-22 18:55:04 +0000154 {
155 char *end = NULL;
156 long tmp_port_offset = strtoul(optarg, &end, 0);
157 if (end && *end == '\0')
158 {
159 if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT)
160 {
161 port_offset = (uint16_t)tmp_port_offset;
162 }
163 else
164 {
165 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);
166 option_error = 5;
167 }
168 }
169 else
170 {
171 fprintf (stderr, "error: invalid port offset string %s\n", optarg);
172 option_error = 4;
173 }
174 }
175 break;
176
177 case 'P':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000178 case 'm':
179 case 'M':
180 {
181 char *end = NULL;
182 long portnum = strtoul(optarg, &end, 0);
183 if (end && *end == '\0')
184 {
185 if (LOW_PORT <= portnum && portnum <= HIGH_PORT)
186 {
Greg Claytond4724cf2013-11-22 18:55:04 +0000187 if (ch == 'P')
Greg Clayton29b8fc42013-11-21 01:44:58 +0000188 gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID;
189 else if (ch == 'm')
190 min_gdbserver_port = portnum;
191 else
192 max_gdbserver_port = portnum;
193 }
194 else
195 {
196 fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT);
197 option_error = 1;
198 }
199 }
200 else
201 {
202 fprintf (stderr, "error: invalid port number string %s\n", optarg);
203 option_error = 2;
204 }
205 }
206 break;
207
Greg Clayton5fb8f792013-12-02 19:35:49 +0000208 case 'c':
209 lldb_commands.push_back(optarg);
210 break;
211
Daniel Maleae0f8f572013-08-26 23:57:52 +0000212 case 'h': /* fall-through is intentional */
213 case '?':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000214 show_usage = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000215 break;
Greg Claytonb43767a2011-03-22 01:34:44 +0000216 }
217 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000218
Greg Clayton29b8fc42013-11-21 01:44:58 +0000219 // Make a port map for a port range that was specified.
220 if (min_gdbserver_port < max_gdbserver_port)
221 {
222 for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port)
223 gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID;
224 }
225 else if (min_gdbserver_port != max_gdbserver_port)
226 {
227 fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port);
228 option_error = 3;
229
230 }
231
Daniel Maleae0f8f572013-08-26 23:57:52 +0000232 // Print usage and exit if no listening port is specified.
233 if (listen_host_port.empty())
Greg Clayton29b8fc42013-11-21 01:44:58 +0000234 show_usage = true;
235
236 if (show_usage || option_error)
237 {
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000238 display_usage(progname, subcommand);
Greg Clayton29b8fc42013-11-21 01:44:58 +0000239 exit(option_error);
240 }
Greg Claytonb43767a2011-03-22 01:34:44 +0000241
Greg Clayton5fb8f792013-12-02 19:35:49 +0000242 // Execute any LLDB commands that we were asked to evaluate.
243 for (const auto &lldb_command : lldb_commands)
244 {
245 lldb_private::CommandReturnObject result;
246 printf("(lldb) %s\n", lldb_command.c_str());
247 debugger_sp->GetCommandInterpreter().HandleCommand(lldb_command.c_str(), eLazyBoolNo, result);
248 const char *output = result.GetOutputData();
249 if (output && output[0])
250 puts(output);
251 }
252
Robert Flacka0e70cd2015-03-25 12:51:31 +0000253 std::unique_ptr<Socket> listening_socket_up;
254 Socket *socket = nullptr;
255 printf ("Listening for a connection from %s...\n", listen_host_port.c_str());
256 const bool children_inherit_listen_socket = false;
Vince Harron33aea902015-03-31 00:27:10 +0000257
258 // the test suite makes many connections in parallel, let's not miss any.
259 // The highest this should get reasonably is a function of the number
260 // of target CPUs. For now, let's just use 100
261 const int backlog = 100;
262 error = Socket::TcpListen(listen_host_port.c_str(), children_inherit_listen_socket, socket, NULL, backlog);
Robert Flacka0e70cd2015-03-25 12:51:31 +0000263 if (error.Fail())
264 {
265 printf("error: %s\n", error.AsCString());
266 exit(socket_error);
267 }
268 listening_socket_up.reset(socket);
Greg Claytonb43767a2011-03-22 01:34:44 +0000269
Daniel Maleae0f8f572013-08-26 23:57:52 +0000270 do {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000271 GDBRemoteCommunicationServerPlatform platform;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000272
Greg Claytond4724cf2013-11-22 18:55:04 +0000273 if (port_offset > 0)
Tamas Berghammere13c2732015-02-11 10:29:30 +0000274 platform.SetPortOffset(port_offset);
Greg Claytond4724cf2013-11-22 18:55:04 +0000275
Greg Clayton29b8fc42013-11-21 01:44:58 +0000276 if (!gdbserver_portmap.empty())
277 {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000278 platform.SetPortMap(std::move(gdbserver_portmap));
Greg Clayton29b8fc42013-11-21 01:44:58 +0000279 }
280
Robert Flacka0e70cd2015-03-25 12:51:31 +0000281 const bool children_inherit_accept_socket = true;
282 socket = nullptr;
283 error = listening_socket_up->BlockingAccept(listen_host_port.c_str(), children_inherit_accept_socket, socket);
284 if (error.Fail())
Greg Claytond314e812011-03-23 00:09:55 +0000285 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000286 printf ("error: %s\n", error.AsCString());
287 exit(socket_error);
288 }
289 printf ("Connection established.\n");
290 if (g_server)
291 {
292 // Collect child zombie processes.
293 while (waitpid(-1, nullptr, WNOHANG) > 0);
294 if (fork())
Greg Claytond314e812011-03-23 00:09:55 +0000295 {
Vince Harronf1e69992015-03-31 00:24:51 +0000296 // Parent doesn't need a connection to the lldb client
297 delete socket;
298 socket = nullptr;
299
Robert Flacka0e70cd2015-03-25 12:51:31 +0000300 // Parent will continue to listen for new connections.
301 continue;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000302 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000303 else
Daniel Maleae0f8f572013-08-26 23:57:52 +0000304 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000305 // Child process will handle the connection and exit.
306 g_server = 0;
307 // Listening socket is owned by parent process.
308 listening_socket_up.release();
Greg Claytond314e812011-03-23 00:09:55 +0000309 }
310 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000311 else
312 {
313 // If not running as a server, this process will not accept
314 // connections while a connection is active.
315 listening_socket_up.reset();
316 }
317 platform.SetConnection (new ConnectionFileDescriptor(socket));
318
319 if (platform.IsConnected())
320 {
321 // After we connected, we need to get an initial ack from...
322 if (platform.HandshakeWithClient(&error))
323 {
324 bool interrupt = false;
325 bool done = false;
326 while (!interrupt && !done)
327 {
328 if (platform.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success)
329 break;
330 }
331
332 if (error.Fail())
333 {
334 fprintf(stderr, "error: %s\n", error.AsCString());
335 }
336 }
337 else
338 {
339 fprintf(stderr, "error: handshake with client failed\n");
340 }
341 }
342 } while (g_server);
Greg Claytonb43767a2011-03-22 01:34:44 +0000343
Robert Flack8cc4cf12015-03-06 14:36:33 +0000344 fprintf(stderr, "lldb-server exiting...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +0000345
Greg Claytonb43767a2011-03-22 01:34:44 +0000346 return 0;
347}