blob: 4dfa7d23a4a85652828f4841884f30b92f3fef33 [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
Greg Claytond314e812011-03-23 00:09:55 +000010// C Includes
Greg Claytonb43767a2011-03-22 01:34:44 +000011#include <errno.h>
Zachary Turner98688922014-08-06 18:16:26 +000012#if defined(__APPLE__)
13#include <netinet/in.h>
14#endif
Greg Claytonb43767a2011-03-22 01:34:44 +000015#include <signal.h>
16#include <stdint.h>
17#include <stdio.h>
18#include <stdlib.h>
19#include <string.h>
Robert Flacka0e70cd2015-03-25 12:51:31 +000020#include <sys/wait.h>
Greg Claytonb43767a2011-03-22 01:34:44 +000021
Greg Claytond314e812011-03-23 00:09:55 +000022// C++ Includes
Oleksiy Vyalov298be462015-07-06 18:05:19 +000023#include <fstream>
Greg Claytond314e812011-03-23 00:09:55 +000024
25// Other libraries and framework includes
26#include "lldb/Core/Error.h"
Zachary Turner93a66fc2014-10-06 21:22:36 +000027#include "lldb/Host/ConnectionFileDescriptor.h"
Oleksiy Vyalov298be462015-07-06 18:05:19 +000028#include "lldb/Host/FileSpec.h"
29#include "lldb/Host/FileSystem.h"
Zachary Turner98688922014-08-06 18:16:26 +000030#include "lldb/Host/HostGetOpt.h"
Greg Claytonfb909312013-11-23 01:58:15 +000031#include "lldb/Host/OptionParser.h"
Robert Flacka0e70cd2015-03-25 12:51:31 +000032#include "lldb/Host/Socket.h"
Oleksiy Vyalov298be462015-07-06 18:05:19 +000033#include "llvm/Support/FileSystem.h"
34#include "llvm/Support/FileUtilities.h"
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000035#include "LLDBServerUtilities.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000036#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h"
Greg Clayton1cb64962011-03-24 04:28:38 +000037#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
Tamas Berghammere13c2732015-02-11 10:29:30 +000038
Greg Claytond314e812011-03-23 00:09:55 +000039using namespace lldb;
40using namespace lldb_private;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000041using namespace lldb_private::lldb_server;
Tamas Berghammerdb264a62015-03-31 09:52:22 +000042using namespace lldb_private::process_gdb_remote;
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000043using namespace llvm;
Greg Claytond314e812011-03-23 00:09:55 +000044
Greg Claytonb43767a2011-03-22 01:34:44 +000045//----------------------------------------------------------------------
Greg Claytonb7ad58a2013-04-04 20:35:24 +000046// option descriptors for getopt_long_only()
Greg Claytonb43767a2011-03-22 01:34:44 +000047//----------------------------------------------------------------------
48
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000049static int g_debug = 0;
50static int g_verbose = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +000051static int g_server = 0;
Greg Claytonb43767a2011-03-22 01:34:44 +000052
53static struct option g_long_options[] =
54{
55 { "debug", no_argument, &g_debug, 1 },
56 { "verbose", no_argument, &g_verbose, 1 },
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +000057 { "log-file", required_argument, NULL, 'l' },
58 { "log-channels", required_argument, NULL, 'c' },
Greg Claytond314e812011-03-23 00:09:55 +000059 { "listen", required_argument, NULL, 'L' },
Greg Claytond4724cf2013-11-22 18:55:04 +000060 { "port-offset", required_argument, NULL, 'p' },
61 { "gdbserver-port", required_argument, NULL, 'P' },
Greg Clayton29b8fc42013-11-21 01:44:58 +000062 { "min-gdbserver-port", required_argument, NULL, 'm' },
63 { "max-gdbserver-port", required_argument, NULL, 'M' },
Oleksiy Vyalov298be462015-07-06 18:05:19 +000064 { "port-file", required_argument, NULL, 'f' },
Robert Flacka0e70cd2015-03-25 12:51:31 +000065 { "server", no_argument, &g_server, 1 },
Greg Claytonb43767a2011-03-22 01:34:44 +000066 { NULL, 0, NULL, 0 }
67};
68
Greg Clayton29b8fc42013-11-21 01:44:58 +000069#if defined (__APPLE__)
70#define LOW_PORT (IPPORT_RESERVED)
71#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
72#else
73#define LOW_PORT (1024u)
74#define HIGH_PORT (49151u)
75#endif
76
Greg Claytonb43767a2011-03-22 01:34:44 +000077//----------------------------------------------------------------------
78// Watch for signals
79//----------------------------------------------------------------------
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000080static void
Greg Claytonb43767a2011-03-22 01:34:44 +000081signal_handler(int signo)
82{
83 switch (signo)
84 {
Daniel Maleae0f8f572013-08-26 23:57:52 +000085 case SIGHUP:
86 // Use SIGINT first, if that does not work, use SIGHUP as a last resort.
87 // And we should not call exit() here because it results in the global destructors
88 // to be invoked and wreaking havoc on the threads still running.
Robert Flack8cc4cf12015-03-06 14:36:33 +000089 Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-server...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +000090 abort();
91 break;
Greg Claytonb43767a2011-03-22 01:34:44 +000092 }
93}
94
Daniel Maleae0f8f572013-08-26 23:57:52 +000095static void
Tamas Berghammerc2c3d712015-02-18 15:39:41 +000096display_usage (const char *progname, const char *subcommand)
Daniel Maleae0f8f572013-08-26 23:57:52 +000097{
Oleksiy Vyalov298be462015-07-06 18:05:19 +000098 fprintf(stderr, "Usage:\n %s %s [--log-file log-file-name] [--log-channels log-channel-list] [--port-file port-file-path] --server --listen port\n", progname, subcommand);
Daniel Maleae0f8f572013-08-26 23:57:52 +000099 exit(0);
100}
101
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000102static Error
103save_port_to_file(const uint16_t port, const FileSpec &port_file_spec)
104{
Oleksiy Vyalovdf62ed32015-07-14 18:54:52 +0000105 FileSpec temp_file_spec(port_file_spec.GetDirectory().AsCString(), false);
106 auto error = FileSystem::MakeDirectory(temp_file_spec, eFilePermissionsDirectoryDefault);
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000107 if (error.Fail())
Oleksiy Vyalovdf62ed32015-07-14 18:54:52 +0000108 return Error("Failed to create directory %s: %s", temp_file_spec.GetCString(), error.AsCString());
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000109
110 llvm::SmallString<PATH_MAX> temp_file_path;
Oleksiy Vyalovdf62ed32015-07-14 18:54:52 +0000111 temp_file_spec.AppendPathComponent("port-file.%%%%%%");
112 auto err_code = llvm::sys::fs::createUniqueFile(temp_file_spec.GetCString(), temp_file_path);
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000113 if (err_code)
114 return Error("Failed to create temp file: %s", err_code.message().c_str());
115
116 llvm::FileRemover tmp_file_remover(temp_file_path.c_str());
117
118 {
119 std::ofstream temp_file(temp_file_path.c_str(), std::ios::out);
120 if (!temp_file.is_open())
121 return Error("Failed to open temp file %s", temp_file_path.c_str());
122 temp_file << port;
123 }
124
125 err_code = llvm::sys::fs::rename(temp_file_path.c_str(), port_file_spec.GetPath().c_str());
126 if (err_code)
127 return Error("Failed to rename file %s to %s: %s",
128 temp_file_path.c_str(), port_file_spec.GetPath().c_str(), err_code.message().c_str());
129
130 tmp_file_remover.releaseFile();
131 return Error();
132}
133
Greg Claytonb43767a2011-03-22 01:34:44 +0000134//----------------------------------------------------------------------
135// main
136//----------------------------------------------------------------------
137int
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000138main_platform (int argc, char *argv[])
Greg Claytonb43767a2011-03-22 01:34:44 +0000139{
Daniel Maleae0f8f572013-08-26 23:57:52 +0000140 const char *progname = argv[0];
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000141 const char *subcommand = argv[1];
142 argc--;
143 argv++;
Greg Claytonfb909312013-11-23 01:58:15 +0000144 signal (SIGPIPE, SIG_IGN);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000145 signal (SIGHUP, signal_handler);
Greg Claytonb43767a2011-03-22 01:34:44 +0000146 int long_option_index = 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000147 Error error;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000148 std::string listen_host_port;
Greg Clayton9d3d6882011-10-31 23:51:19 +0000149 int ch;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000150
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000151 std::string log_file;
152 StringRef log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000153
Tamas Berghammere13c2732015-02-11 10:29:30 +0000154 GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000155 int min_gdbserver_port = 0;
156 int max_gdbserver_port = 0;
Greg Claytond4724cf2013-11-22 18:55:04 +0000157 uint16_t port_offset = 0;
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000158
159 FileSpec port_file;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000160 bool show_usage = false;
161 int option_error = 0;
Robert Flacka0e70cd2015-03-25 12:51:31 +0000162 int socket_error = -1;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000163
Greg Claytonfb909312013-11-23 01:58:15 +0000164 std::string short_options(OptionParser::GetShortOptionString(g_long_options));
165
Greg Claytond4724cf2013-11-22 18:55:04 +0000166#if __GLIBC__
167 optind = 0;
168#else
169 optreset = 1;
170 optind = 1;
171#endif
172
Greg Claytonfb909312013-11-23 01:58:15 +0000173 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 +0000174 {
Greg Claytonb43767a2011-03-22 01:34:44 +0000175 switch (ch)
176 {
177 case 0: // Any optional that auto set themselves will return 0
178 break;
179
Greg Claytond314e812011-03-23 00:09:55 +0000180 case 'L':
Greg Clayton73bf5db2011-06-17 01:22:15 +0000181 listen_host_port.append (optarg);
Greg Claytond314e812011-03-23 00:09:55 +0000182 break;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000183
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000184 case 'l': // Set Log File
185 if (optarg && optarg[0])
186 log_file.assign(optarg);
187 break;
188
189 case 'c': // Log Channels
190 if (optarg && optarg[0])
191 log_channels = StringRef(optarg);
192 break;
193
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000194 case 'f': // Port file
195 if (optarg && optarg[0])
196 port_file.SetFile(optarg, false);
197 break;
198
Greg Clayton29b8fc42013-11-21 01:44:58 +0000199 case 'p':
Greg Claytond4724cf2013-11-22 18:55:04 +0000200 {
201 char *end = NULL;
202 long tmp_port_offset = strtoul(optarg, &end, 0);
203 if (end && *end == '\0')
204 {
205 if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT)
206 {
207 port_offset = (uint16_t)tmp_port_offset;
208 }
209 else
210 {
211 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);
212 option_error = 5;
213 }
214 }
215 else
216 {
217 fprintf (stderr, "error: invalid port offset string %s\n", optarg);
218 option_error = 4;
219 }
220 }
221 break;
222
223 case 'P':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000224 case 'm':
225 case 'M':
226 {
227 char *end = NULL;
228 long portnum = strtoul(optarg, &end, 0);
229 if (end && *end == '\0')
230 {
231 if (LOW_PORT <= portnum && portnum <= HIGH_PORT)
232 {
Greg Claytond4724cf2013-11-22 18:55:04 +0000233 if (ch == 'P')
Greg Clayton29b8fc42013-11-21 01:44:58 +0000234 gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID;
235 else if (ch == 'm')
236 min_gdbserver_port = portnum;
237 else
238 max_gdbserver_port = portnum;
239 }
240 else
241 {
242 fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT);
243 option_error = 1;
244 }
245 }
246 else
247 {
248 fprintf (stderr, "error: invalid port number string %s\n", optarg);
249 option_error = 2;
250 }
251 }
252 break;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000253
Daniel Maleae0f8f572013-08-26 23:57:52 +0000254 case 'h': /* fall-through is intentional */
255 case '?':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000256 show_usage = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000257 break;
Greg Claytonb43767a2011-03-22 01:34:44 +0000258 }
259 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000260
Tamas Berghammer9c9ecce2015-05-27 13:34:04 +0000261 if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0))
262 return -1;
263
Greg Clayton29b8fc42013-11-21 01:44:58 +0000264 // Make a port map for a port range that was specified.
265 if (min_gdbserver_port < max_gdbserver_port)
266 {
267 for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port)
268 gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID;
269 }
270 else if (min_gdbserver_port != max_gdbserver_port)
271 {
272 fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port);
273 option_error = 3;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000274 }
275
Daniel Maleae0f8f572013-08-26 23:57:52 +0000276 // Print usage and exit if no listening port is specified.
277 if (listen_host_port.empty())
Greg Clayton29b8fc42013-11-21 01:44:58 +0000278 show_usage = true;
279
280 if (show_usage || option_error)
281 {
Tamas Berghammerc2c3d712015-02-18 15:39:41 +0000282 display_usage(progname, subcommand);
Greg Clayton29b8fc42013-11-21 01:44:58 +0000283 exit(option_error);
284 }
Greg Claytonb43767a2011-03-22 01:34:44 +0000285
Robert Flacka0e70cd2015-03-25 12:51:31 +0000286 std::unique_ptr<Socket> listening_socket_up;
287 Socket *socket = nullptr;
Robert Flacka0e70cd2015-03-25 12:51:31 +0000288 const bool children_inherit_listen_socket = false;
Vince Harron33aea902015-03-31 00:27:10 +0000289
290 // the test suite makes many connections in parallel, let's not miss any.
291 // The highest this should get reasonably is a function of the number
292 // of target CPUs. For now, let's just use 100
293 const int backlog = 100;
294 error = Socket::TcpListen(listen_host_port.c_str(), children_inherit_listen_socket, socket, NULL, backlog);
Robert Flacka0e70cd2015-03-25 12:51:31 +0000295 if (error.Fail())
296 {
297 printf("error: %s\n", error.AsCString());
298 exit(socket_error);
299 }
300 listening_socket_up.reset(socket);
Vince Harron218353d2015-05-26 03:08:05 +0000301 printf ("Listening for a connection from %u...\n", listening_socket_up->GetLocalPortNumber());
Oleksiy Vyalov298be462015-07-06 18:05:19 +0000302 if (port_file)
303 {
304 error = save_port_to_file(listening_socket_up->GetLocalPortNumber(), port_file);
305 if (error.Fail())
306 {
307 fprintf(stderr, "failed to write port to %s: %s", port_file.GetPath().c_str(), error.AsCString());
308 return 1;
309 }
310 }
Greg Claytonb43767a2011-03-22 01:34:44 +0000311
Daniel Maleae0f8f572013-08-26 23:57:52 +0000312 do {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000313 GDBRemoteCommunicationServerPlatform platform;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000314
Greg Claytond4724cf2013-11-22 18:55:04 +0000315 if (port_offset > 0)
Tamas Berghammere13c2732015-02-11 10:29:30 +0000316 platform.SetPortOffset(port_offset);
Greg Claytond4724cf2013-11-22 18:55:04 +0000317
Greg Clayton29b8fc42013-11-21 01:44:58 +0000318 if (!gdbserver_portmap.empty())
319 {
Tamas Berghammere13c2732015-02-11 10:29:30 +0000320 platform.SetPortMap(std::move(gdbserver_portmap));
Greg Clayton29b8fc42013-11-21 01:44:58 +0000321 }
322
Robert Flacka0e70cd2015-03-25 12:51:31 +0000323 const bool children_inherit_accept_socket = true;
324 socket = nullptr;
325 error = listening_socket_up->BlockingAccept(listen_host_port.c_str(), children_inherit_accept_socket, socket);
326 if (error.Fail())
Greg Claytond314e812011-03-23 00:09:55 +0000327 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000328 printf ("error: %s\n", error.AsCString());
329 exit(socket_error);
330 }
331 printf ("Connection established.\n");
332 if (g_server)
333 {
334 // Collect child zombie processes.
335 while (waitpid(-1, nullptr, WNOHANG) > 0);
336 if (fork())
Greg Claytond314e812011-03-23 00:09:55 +0000337 {
Vince Harronf1e69992015-03-31 00:24:51 +0000338 // Parent doesn't need a connection to the lldb client
339 delete socket;
340 socket = nullptr;
341
Robert Flacka0e70cd2015-03-25 12:51:31 +0000342 // Parent will continue to listen for new connections.
343 continue;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000344 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000345 else
Daniel Maleae0f8f572013-08-26 23:57:52 +0000346 {
Robert Flacka0e70cd2015-03-25 12:51:31 +0000347 // Child process will handle the connection and exit.
348 g_server = 0;
349 // Listening socket is owned by parent process.
350 listening_socket_up.release();
Greg Claytond314e812011-03-23 00:09:55 +0000351 }
352 }
Robert Flacka0e70cd2015-03-25 12:51:31 +0000353 else
354 {
355 // If not running as a server, this process will not accept
356 // connections while a connection is active.
357 listening_socket_up.reset();
358 }
359 platform.SetConnection (new ConnectionFileDescriptor(socket));
360
361 if (platform.IsConnected())
362 {
363 // After we connected, we need to get an initial ack from...
Pavel Labath77dc9562015-07-13 10:44:55 +0000364 if (platform.HandshakeWithClient())
Robert Flacka0e70cd2015-03-25 12:51:31 +0000365 {
366 bool interrupt = false;
367 bool done = false;
368 while (!interrupt && !done)
369 {
370 if (platform.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success)
371 break;
372 }
373
374 if (error.Fail())
375 {
376 fprintf(stderr, "error: %s\n", error.AsCString());
377 }
378 }
379 else
380 {
381 fprintf(stderr, "error: handshake with client failed\n");
382 }
383 }
384 } while (g_server);
Greg Claytonb43767a2011-03-22 01:34:44 +0000385
Robert Flack8cc4cf12015-03-06 14:36:33 +0000386 fprintf(stderr, "lldb-server exiting...\n");
Daniel Maleae0f8f572013-08-26 23:57:52 +0000387
Greg Claytonb43767a2011-03-22 01:34:44 +0000388 return 0;
389}