blob: 5c8057d3c292d19fc17d545b8e39d87020cf7a7d [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>
22
Greg Claytond314e812011-03-23 00:09:55 +000023// C++ Includes
24
25// Other libraries and framework includes
Greg Claytonfbb76342013-11-20 21:07:01 +000026#include "lldb/lldb-private-log.h"
Greg Claytond314e812011-03-23 00:09:55 +000027#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"
Greg Clayton5fb8f792013-12-02 19:35:49 +000034#include "lldb/Interpreter/CommandInterpreter.h"
35#include "lldb/Interpreter/CommandReturnObject.h"
Greg Clayton59b4fa12012-03-29 17:46:11 +000036#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServer.h"
Greg Clayton1cb64962011-03-24 04:28:38 +000037#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
Greg Claytond314e812011-03-23 00:09:55 +000038using namespace lldb;
39using namespace lldb_private;
40
Greg Claytonb43767a2011-03-22 01:34:44 +000041//----------------------------------------------------------------------
Greg Claytonb7ad58a2013-04-04 20:35:24 +000042// option descriptors for getopt_long_only()
Greg Claytonb43767a2011-03-22 01:34:44 +000043//----------------------------------------------------------------------
44
45int g_debug = 0;
46int g_verbose = 0;
Daniel Maleae0f8f572013-08-26 23:57:52 +000047int g_stay_alive = 0;
Greg Claytonb43767a2011-03-22 01:34:44 +000048
49static struct option g_long_options[] =
50{
51 { "debug", no_argument, &g_debug, 1 },
52 { "verbose", no_argument, &g_verbose, 1 },
Daniel Maleae0f8f572013-08-26 23:57:52 +000053 { "stay-alive", no_argument, &g_stay_alive, 1 },
Greg Claytond314e812011-03-23 00:09:55 +000054 { "listen", required_argument, NULL, 'L' },
Greg Claytond4724cf2013-11-22 18:55:04 +000055 { "port-offset", required_argument, NULL, 'p' },
56 { "gdbserver-port", required_argument, NULL, 'P' },
Greg Clayton29b8fc42013-11-21 01:44:58 +000057 { "min-gdbserver-port", required_argument, NULL, 'm' },
58 { "max-gdbserver-port", required_argument, NULL, 'M' },
Greg Clayton5fb8f792013-12-02 19:35:49 +000059 { "lldb-command", required_argument, NULL, 'c' },
Greg Claytonb43767a2011-03-22 01:34:44 +000060 { NULL, 0, NULL, 0 }
61};
62
Greg Clayton29b8fc42013-11-21 01:44:58 +000063#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 Claytonb43767a2011-03-22 01:34:44 +000072//----------------------------------------------------------------------
73// Watch for signals
74//----------------------------------------------------------------------
Greg Claytonb43767a2011-03-22 01:34:44 +000075void
76signal_handler(int signo)
77{
78 switch (signo)
79 {
Daniel Maleae0f8f572013-08-26 23:57:52 +000080 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 Claytonb43767a2011-03-22 01:34:44 +000087 }
88}
89
Daniel Maleae0f8f572013-08-26 23:57:52 +000090static void
91display_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 Claytonb43767a2011-03-22 01:34:44 +000097//----------------------------------------------------------------------
98// main
99//----------------------------------------------------------------------
100int
101main (int argc, char *argv[])
102{
Daniel Maleae0f8f572013-08-26 23:57:52 +0000103 const char *progname = argv[0];
Greg Claytonfb909312013-11-23 01:58:15 +0000104 signal (SIGPIPE, SIG_IGN);
Daniel Maleae0f8f572013-08-26 23:57:52 +0000105 signal (SIGHUP, signal_handler);
Greg Claytonb43767a2011-03-22 01:34:44 +0000106 int long_option_index = 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000107 Error error;
Greg Clayton73bf5db2011-06-17 01:22:15 +0000108 std::string listen_host_port;
Greg Clayton9d3d6882011-10-31 23:51:19 +0000109 int ch;
Greg Clayton5fb8f792013-12-02 19:35:49 +0000110 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 Clayton1cb64962011-03-24 04:28:38 +0000117
Greg Clayton29b8fc42013-11-21 01:44:58 +0000118 GDBRemoteCommunicationServer::PortMap gdbserver_portmap;
119 int min_gdbserver_port = 0;
120 int max_gdbserver_port = 0;
Greg Claytond4724cf2013-11-22 18:55:04 +0000121 uint16_t port_offset = 0;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000122
Greg Clayton5fb8f792013-12-02 19:35:49 +0000123 std::vector<std::string> lldb_commands;
Greg Clayton29b8fc42013-11-21 01:44:58 +0000124 bool show_usage = false;
125 int option_error = 0;
Greg Clayton9b1e1cd2011-04-04 18:18:57 +0000126
Greg Claytonfb909312013-11-23 01:58:15 +0000127 std::string short_options(OptionParser::GetShortOptionString(g_long_options));
128
Greg Claytond4724cf2013-11-22 18:55:04 +0000129#if __GLIBC__
130 optind = 0;
131#else
132 optreset = 1;
133 optind = 1;
134#endif
135
Greg Claytonfb909312013-11-23 01:58:15 +0000136 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 +0000137 {
Greg Claytonb43767a2011-03-22 01:34:44 +0000138 switch (ch)
139 {
140 case 0: // Any optional that auto set themselves will return 0
141 break;
142
Greg Claytond314e812011-03-23 00:09:55 +0000143 case 'L':
Greg Clayton73bf5db2011-06-17 01:22:15 +0000144 listen_host_port.append (optarg);
Greg Claytond314e812011-03-23 00:09:55 +0000145 break;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000146
Greg Clayton29b8fc42013-11-21 01:44:58 +0000147 case 'p':
Greg Claytond4724cf2013-11-22 18:55:04 +0000148 {
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 Clayton29b8fc42013-11-21 01:44:58 +0000172 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 Claytond4724cf2013-11-22 18:55:04 +0000181 if (ch == 'P')
Greg Clayton29b8fc42013-11-21 01:44:58 +0000182 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 Clayton5fb8f792013-12-02 19:35:49 +0000202 case 'c':
203 lldb_commands.push_back(optarg);
204 break;
205
Daniel Maleae0f8f572013-08-26 23:57:52 +0000206 case 'h': /* fall-through is intentional */
207 case '?':
Greg Clayton29b8fc42013-11-21 01:44:58 +0000208 show_usage = true;
Daniel Maleae0f8f572013-08-26 23:57:52 +0000209 break;
Greg Claytonb43767a2011-03-22 01:34:44 +0000210 }
211 }
Greg Clayton5fb8f792013-12-02 19:35:49 +0000212
Greg Clayton29b8fc42013-11-21 01:44:58 +0000213 // 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 Maleae0f8f572013-08-26 23:57:52 +0000226 // Print usage and exit if no listening port is specified.
227 if (listen_host_port.empty())
Greg Clayton29b8fc42013-11-21 01:44:58 +0000228 show_usage = true;
229
230 if (show_usage || option_error)
231 {
Daniel Maleae0f8f572013-08-26 23:57:52 +0000232 display_usage(progname);
Greg Clayton29b8fc42013-11-21 01:44:58 +0000233 exit(option_error);
234 }
Greg Claytonb43767a2011-03-22 01:34:44 +0000235
Greg Claytonb7ad58a2013-04-04 20:35:24 +0000236 // Skip any options we consumed with getopt_long_only
Greg Claytonb43767a2011-03-22 01:34:44 +0000237 argc -= optind;
238 argv += optind;
239
Greg Clayton5fb8f792013-12-02 19:35:49 +0000240 // 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 Claytonb43767a2011-03-22 01:34:44 +0000251
Daniel Maleae0f8f572013-08-26 23:57:52 +0000252 do {
253 GDBRemoteCommunicationServer gdb_server (true);
Greg Clayton29b8fc42013-11-21 01:44:58 +0000254
Greg Claytond4724cf2013-11-22 18:55:04 +0000255 if (port_offset > 0)
256 gdb_server.SetPortOffset(port_offset);
257
Greg Clayton29b8fc42013-11-21 01:44:58 +0000258 if (!gdbserver_portmap.empty())
259 {
260 gdb_server.SetPortMap(std::move(gdbserver_portmap));
261 }
262
Daniel Maleae0f8f572013-08-26 23:57:52 +0000263 if (!listen_host_port.empty())
Greg Claytond314e812011-03-23 00:09:55 +0000264 {
Daniel Maleae0f8f572013-08-26 23:57:52 +0000265 std::unique_ptr<ConnectionFileDescriptor> conn_ap(new ConnectionFileDescriptor());
266 if (conn_ap.get())
Greg Claytond314e812011-03-23 00:09:55 +0000267 {
Greg Clayton00fe87b2013-12-05 22:58:22 +0000268 std::string connect_url ("listen://");
269 connect_url.append(listen_host_port.c_str());
Daniel Maleae0f8f572013-08-26 23:57:52 +0000270
Greg Clayton00fe87b2013-12-05 22:58:22 +0000271 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 Maleae0f8f572013-08-26 23:57:52 +0000280 }
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 Fialaaf245d12014-06-30 21:05:18 +0000292 if (gdb_server.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success)
Daniel Maleae0f8f572013-08-26 23:57:52 +0000293 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 Claytond314e812011-03-23 00:09:55 +0000305 }
306 }
Daniel Maleae0f8f572013-08-26 23:57:52 +0000307 } while (g_stay_alive);
Greg Claytonb43767a2011-03-22 01:34:44 +0000308
Greg Clayton1cb64962011-03-24 04:28:38 +0000309 Debugger::Terminate();
310
Daniel Maleae0f8f572013-08-26 23:57:52 +0000311 fprintf(stderr, "lldb-platform exiting...\n");
312
Greg Claytonb43767a2011-03-22 01:34:44 +0000313 return 0;
314}