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 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 10 | // C Includes |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 11 | #include <errno.h> |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 12 | #if defined(__APPLE__) |
| 13 | #include <netinet/in.h> |
| 14 | #endif |
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> |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 20 | #include <sys/wait.h> |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 21 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 22 | // C++ Includes |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 23 | #include <fstream> |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 24 | |
| 25 | // Other libraries and framework includes |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 26 | #include "llvm/Support/FileSystem.h" |
| 27 | #include "llvm/Support/FileUtilities.h" |
| 28 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 29 | #include "lldb/Core/Error.h" |
Zachary Turner | 93a66fc | 2014-10-06 21:22:36 +0000 | [diff] [blame] | 30 | #include "lldb/Host/ConnectionFileDescriptor.h" |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 31 | #include "lldb/Host/FileSpec.h" |
| 32 | #include "lldb/Host/FileSystem.h" |
Zachary Turner | 9868892 | 2014-08-06 18:16:26 +0000 | [diff] [blame] | 33 | #include "lldb/Host/HostGetOpt.h" |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 34 | #include "lldb/Host/OptionParser.h" |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame] | 35 | #include "lldb/Host/common/TCPSocket.h" |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 36 | #include "Acceptor.h" |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 37 | #include "LLDBServerUtilities.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 38 | #include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h" |
Greg Clayton | 1cb6496 | 2011-03-24 04:28:38 +0000 | [diff] [blame] | 39 | #include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h" |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 40 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 41 | using namespace lldb; |
| 42 | using namespace lldb_private; |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 43 | using namespace lldb_private::lldb_server; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 44 | using namespace lldb_private::process_gdb_remote; |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 45 | using namespace llvm; |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 46 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 47 | //---------------------------------------------------------------------- |
Greg Clayton | b7ad58a | 2013-04-04 20:35:24 +0000 | [diff] [blame] | 48 | // option descriptors for getopt_long_only() |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 49 | //---------------------------------------------------------------------- |
| 50 | |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 51 | static int g_debug = 0; |
| 52 | static int g_verbose = 0; |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 53 | static int g_server = 0; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 54 | |
| 55 | static struct option g_long_options[] = |
| 56 | { |
| 57 | { "debug", no_argument, &g_debug, 1 }, |
| 58 | { "verbose", no_argument, &g_verbose, 1 }, |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 59 | { "log-file", required_argument, NULL, 'l' }, |
| 60 | { "log-channels", required_argument, NULL, 'c' }, |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 61 | { "listen", required_argument, NULL, 'L' }, |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 62 | { "port-offset", required_argument, NULL, 'p' }, |
| 63 | { "gdbserver-port", required_argument, NULL, 'P' }, |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 64 | { "min-gdbserver-port", required_argument, NULL, 'm' }, |
| 65 | { "max-gdbserver-port", required_argument, NULL, 'M' }, |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 66 | { "socket-file", required_argument, NULL, 'f' }, |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 67 | { "server", no_argument, &g_server, 1 }, |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 68 | { NULL, 0, NULL, 0 } |
| 69 | }; |
| 70 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 71 | #if defined (__APPLE__) |
| 72 | #define LOW_PORT (IPPORT_RESERVED) |
| 73 | #define HIGH_PORT (IPPORT_HIFIRSTAUTO) |
| 74 | #else |
| 75 | #define LOW_PORT (1024u) |
| 76 | #define HIGH_PORT (49151u) |
| 77 | #endif |
| 78 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 79 | //---------------------------------------------------------------------- |
| 80 | // Watch for signals |
| 81 | //---------------------------------------------------------------------- |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 82 | static void |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 83 | signal_handler(int signo) |
| 84 | { |
| 85 | switch (signo) |
| 86 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 87 | case SIGHUP: |
| 88 | // Use SIGINT first, if that does not work, use SIGHUP as a last resort. |
| 89 | // And we should not call exit() here because it results in the global destructors |
| 90 | // to be invoked and wreaking havoc on the threads still running. |
Robert Flack | 8cc4cf1 | 2015-03-06 14:36:33 +0000 | [diff] [blame] | 91 | Host::SystemLog(Host::eSystemLogWarning, "SIGHUP received, exiting lldb-server...\n"); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 92 | abort(); |
| 93 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 94 | } |
| 95 | } |
| 96 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 97 | static void |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 98 | display_usage (const char *progname, const char *subcommand) |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 99 | { |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 100 | 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 Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 101 | exit(0); |
| 102 | } |
| 103 | |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 104 | static Error |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 105 | save_socket_id_to_file(const std::string &socket_id, const FileSpec &file_spec) |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 106 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 107 | FileSpec temp_file_spec(file_spec.GetDirectory().AsCString(), false); |
Oleksiy Vyalov | df62ed3 | 2015-07-14 18:54:52 +0000 | [diff] [blame] | 108 | auto error = FileSystem::MakeDirectory(temp_file_spec, eFilePermissionsDirectoryDefault); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 109 | if (error.Fail()) |
Oleksiy Vyalov | df62ed3 | 2015-07-14 18:54:52 +0000 | [diff] [blame] | 110 | return Error("Failed to create directory %s: %s", temp_file_spec.GetCString(), error.AsCString()); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 111 | |
| 112 | llvm::SmallString<PATH_MAX> temp_file_path; |
Oleksiy Vyalov | df62ed3 | 2015-07-14 18:54:52 +0000 | [diff] [blame] | 113 | temp_file_spec.AppendPathComponent("port-file.%%%%%%"); |
| 114 | auto err_code = llvm::sys::fs::createUniqueFile(temp_file_spec.GetCString(), temp_file_path); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 115 | if (err_code) |
| 116 | return Error("Failed to create temp file: %s", err_code.message().c_str()); |
| 117 | |
| 118 | llvm::FileRemover tmp_file_remover(temp_file_path.c_str()); |
| 119 | |
| 120 | { |
| 121 | std::ofstream temp_file(temp_file_path.c_str(), std::ios::out); |
| 122 | if (!temp_file.is_open()) |
| 123 | return Error("Failed to open temp file %s", temp_file_path.c_str()); |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 124 | temp_file << socket_id; |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 125 | } |
| 126 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 127 | err_code = llvm::sys::fs::rename(temp_file_path.c_str(), file_spec.GetPath().c_str()); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 128 | if (err_code) |
| 129 | return Error("Failed to rename file %s to %s: %s", |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 130 | temp_file_path.c_str(), file_spec.GetPath().c_str(), err_code.message().c_str()); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 131 | |
| 132 | tmp_file_remover.releaseFile(); |
| 133 | return Error(); |
| 134 | } |
| 135 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 136 | //---------------------------------------------------------------------- |
| 137 | // main |
| 138 | //---------------------------------------------------------------------- |
| 139 | int |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 140 | main_platform (int argc, char *argv[]) |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 141 | { |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 142 | const char *progname = argv[0]; |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 143 | const char *subcommand = argv[1]; |
| 144 | argc--; |
| 145 | argv++; |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 146 | signal (SIGPIPE, SIG_IGN); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 147 | signal (SIGHUP, signal_handler); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 148 | int long_option_index = 0; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 149 | Error error; |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 150 | std::string listen_host_port; |
Greg Clayton | 9d3d688 | 2011-10-31 23:51:19 +0000 | [diff] [blame] | 151 | int ch; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 152 | |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 153 | std::string log_file; |
| 154 | StringRef log_channels; // e.g. "lldb process threads:gdb-remote default:linux all" |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 155 | |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 156 | GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 157 | int min_gdbserver_port = 0; |
| 158 | int max_gdbserver_port = 0; |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 159 | uint16_t port_offset = 0; |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 160 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 161 | FileSpec socket_file; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 162 | bool show_usage = false; |
| 163 | int option_error = 0; |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 164 | int socket_error = -1; |
Greg Clayton | 9b1e1cd | 2011-04-04 18:18:57 +0000 | [diff] [blame] | 165 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 166 | std::string short_options(OptionParser::GetShortOptionString(g_long_options)); |
| 167 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 168 | #if __GLIBC__ |
| 169 | optind = 0; |
| 170 | #else |
| 171 | optreset = 1; |
| 172 | optind = 1; |
| 173 | #endif |
| 174 | |
Greg Clayton | fb90931 | 2013-11-23 01:58:15 +0000 | [diff] [blame] | 175 | 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] | 176 | { |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 177 | switch (ch) |
| 178 | { |
| 179 | case 0: // Any optional that auto set themselves will return 0 |
| 180 | break; |
| 181 | |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 182 | case 'L': |
Greg Clayton | 73bf5db | 2011-06-17 01:22:15 +0000 | [diff] [blame] | 183 | listen_host_port.append (optarg); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 184 | break; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 185 | |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 186 | case 'l': // Set Log File |
| 187 | if (optarg && optarg[0]) |
| 188 | log_file.assign(optarg); |
| 189 | break; |
| 190 | |
| 191 | case 'c': // Log Channels |
| 192 | if (optarg && optarg[0]) |
| 193 | log_channels = StringRef(optarg); |
| 194 | break; |
| 195 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 196 | case 'f': // Socket file |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 197 | if (optarg && optarg[0]) |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 198 | socket_file.SetFile(optarg, false); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 199 | break; |
| 200 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 201 | case 'p': |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 202 | { |
| 203 | char *end = NULL; |
| 204 | long tmp_port_offset = strtoul(optarg, &end, 0); |
| 205 | if (end && *end == '\0') |
| 206 | { |
| 207 | if (LOW_PORT <= tmp_port_offset && tmp_port_offset <= HIGH_PORT) |
| 208 | { |
| 209 | port_offset = (uint16_t)tmp_port_offset; |
| 210 | } |
| 211 | else |
| 212 | { |
| 213 | 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); |
| 214 | option_error = 5; |
| 215 | } |
| 216 | } |
| 217 | else |
| 218 | { |
| 219 | fprintf (stderr, "error: invalid port offset string %s\n", optarg); |
| 220 | option_error = 4; |
| 221 | } |
| 222 | } |
| 223 | break; |
| 224 | |
| 225 | case 'P': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 226 | case 'm': |
| 227 | case 'M': |
| 228 | { |
| 229 | char *end = NULL; |
| 230 | long portnum = strtoul(optarg, &end, 0); |
| 231 | if (end && *end == '\0') |
| 232 | { |
| 233 | if (LOW_PORT <= portnum && portnum <= HIGH_PORT) |
| 234 | { |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 235 | if (ch == 'P') |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 236 | gdbserver_portmap[(uint16_t)portnum] = LLDB_INVALID_PROCESS_ID; |
| 237 | else if (ch == 'm') |
| 238 | min_gdbserver_port = portnum; |
| 239 | else |
| 240 | max_gdbserver_port = portnum; |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | fprintf (stderr, "error: port number %li is not in the valid user port range of %u - %u\n", portnum, LOW_PORT, HIGH_PORT); |
| 245 | option_error = 1; |
| 246 | } |
| 247 | } |
| 248 | else |
| 249 | { |
| 250 | fprintf (stderr, "error: invalid port number string %s\n", optarg); |
| 251 | option_error = 2; |
| 252 | } |
| 253 | } |
| 254 | break; |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 255 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 256 | case 'h': /* fall-through is intentional */ |
| 257 | case '?': |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 258 | show_usage = true; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 259 | break; |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 260 | } |
| 261 | } |
Greg Clayton | 5fb8f79 | 2013-12-02 19:35:49 +0000 | [diff] [blame] | 262 | |
Tamas Berghammer | 9c9ecce | 2015-05-27 13:34:04 +0000 | [diff] [blame] | 263 | if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0)) |
| 264 | return -1; |
| 265 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 266 | // Make a port map for a port range that was specified. |
| 267 | if (min_gdbserver_port < max_gdbserver_port) |
| 268 | { |
| 269 | for (uint16_t port = min_gdbserver_port; port < max_gdbserver_port; ++port) |
| 270 | gdbserver_portmap[port] = LLDB_INVALID_PROCESS_ID; |
| 271 | } |
| 272 | else if (min_gdbserver_port != max_gdbserver_port) |
| 273 | { |
| 274 | fprintf (stderr, "error: --min-gdbserver-port (%u) is greater than --max-gdbserver-port (%u)\n", min_gdbserver_port, max_gdbserver_port); |
| 275 | option_error = 3; |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 276 | } |
| 277 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 278 | // Print usage and exit if no listening port is specified. |
| 279 | if (listen_host_port.empty()) |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 280 | show_usage = true; |
| 281 | |
| 282 | if (show_usage || option_error) |
| 283 | { |
Tamas Berghammer | c2c3d71 | 2015-02-18 15:39:41 +0000 | [diff] [blame] | 284 | display_usage(progname, subcommand); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 285 | exit(option_error); |
| 286 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 287 | |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 288 | const bool children_inherit_listen_socket = false; |
Vince Harron | 33aea90 | 2015-03-31 00:27:10 +0000 | [diff] [blame] | 289 | // the test suite makes many connections in parallel, let's not miss any. |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 290 | // The highest this should get reasonably is a function of the number |
| 291 | // of target CPUs. For now, let's just use 100. |
Vince Harron | 33aea90 | 2015-03-31 00:27:10 +0000 | [diff] [blame] | 292 | const int backlog = 100; |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 293 | |
| 294 | std::unique_ptr<Acceptor> acceptor_up(Acceptor::Create(listen_host_port, children_inherit_listen_socket, error)); |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame] | 295 | if (error.Fail()) |
| 296 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 297 | fprintf(stderr, "failed to create acceptor: %s", error.AsCString()); |
Oleksiy Vyalov | e98628c | 2015-10-15 23:54:09 +0000 | [diff] [blame] | 298 | exit(socket_error); |
| 299 | } |
| 300 | |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 301 | error = acceptor_up->Listen(backlog); |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 302 | if (error.Fail()) |
| 303 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 304 | printf("failed to listen: %s\n", error.AsCString()); |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 305 | exit(socket_error); |
| 306 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 307 | if (socket_file) |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 308 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 309 | error = save_socket_id_to_file(acceptor_up->GetLocalSocketId(), socket_file); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 310 | if (error.Fail()) |
| 311 | { |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 312 | fprintf(stderr, "failed to write socket id to %s: %s", socket_file.GetPath().c_str(), error.AsCString()); |
Oleksiy Vyalov | 298be46 | 2015-07-06 18:05:19 +0000 | [diff] [blame] | 313 | return 1; |
| 314 | } |
| 315 | } |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 316 | |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 317 | do { |
Oleksiy Vyalov | 1485712 | 2015-10-28 19:49:50 +0000 | [diff] [blame] | 318 | GDBRemoteCommunicationServerPlatform platform(acceptor_up->GetSocketProtocol(), |
| 319 | acceptor_up->GetSocketScheme()); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 320 | |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 321 | if (port_offset > 0) |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 322 | platform.SetPortOffset(port_offset); |
Greg Clayton | d4724cf | 2013-11-22 18:55:04 +0000 | [diff] [blame] | 323 | |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 324 | if (!gdbserver_portmap.empty()) |
| 325 | { |
Tamas Berghammer | e13c273 | 2015-02-11 10:29:30 +0000 | [diff] [blame] | 326 | platform.SetPortMap(std::move(gdbserver_portmap)); |
Greg Clayton | 29b8fc4 | 2013-11-21 01:44:58 +0000 | [diff] [blame] | 327 | } |
| 328 | |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 329 | const bool children_inherit_accept_socket = true; |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 330 | Connection* conn = nullptr; |
| 331 | error = acceptor_up->Accept(children_inherit_accept_socket, conn); |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 332 | if (error.Fail()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 333 | { |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 334 | printf ("error: %s\n", error.AsCString()); |
| 335 | exit(socket_error); |
| 336 | } |
| 337 | printf ("Connection established.\n"); |
| 338 | if (g_server) |
| 339 | { |
| 340 | // Collect child zombie processes. |
| 341 | while (waitpid(-1, nullptr, WNOHANG) > 0); |
| 342 | if (fork()) |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 343 | { |
Vince Harron | f1e6999 | 2015-03-31 00:24:51 +0000 | [diff] [blame] | 344 | // Parent doesn't need a connection to the lldb client |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 345 | delete conn; |
Vince Harron | f1e6999 | 2015-03-31 00:24:51 +0000 | [diff] [blame] | 346 | |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 347 | // Parent will continue to listen for new connections. |
| 348 | continue; |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 349 | } |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 350 | else |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 351 | { |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 352 | // Child process will handle the connection and exit. |
| 353 | g_server = 0; |
| 354 | // Listening socket is owned by parent process. |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 355 | acceptor_up.release(); |
Greg Clayton | d314e81 | 2011-03-23 00:09:55 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 358 | else |
| 359 | { |
| 360 | // If not running as a server, this process will not accept |
| 361 | // connections while a connection is active. |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 362 | acceptor_up.reset(); |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 363 | } |
Oleksiy Vyalov | 9fe526c | 2015-10-21 19:34:26 +0000 | [diff] [blame] | 364 | platform.SetConnection (conn); |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 365 | |
| 366 | if (platform.IsConnected()) |
| 367 | { |
| 368 | // After we connected, we need to get an initial ack from... |
Pavel Labath | 77dc956 | 2015-07-13 10:44:55 +0000 | [diff] [blame] | 369 | if (platform.HandshakeWithClient()) |
Robert Flack | a0e70cd | 2015-03-25 12:51:31 +0000 | [diff] [blame] | 370 | { |
| 371 | bool interrupt = false; |
| 372 | bool done = false; |
| 373 | while (!interrupt && !done) |
| 374 | { |
| 375 | if (platform.GetPacketAndSendResponse (UINT32_MAX, error, interrupt, done) != GDBRemoteCommunication::PacketResult::Success) |
| 376 | break; |
| 377 | } |
| 378 | |
| 379 | if (error.Fail()) |
| 380 | { |
| 381 | fprintf(stderr, "error: %s\n", error.AsCString()); |
| 382 | } |
| 383 | } |
| 384 | else |
| 385 | { |
| 386 | fprintf(stderr, "error: handshake with client failed\n"); |
| 387 | } |
| 388 | } |
| 389 | } while (g_server); |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 390 | |
Robert Flack | 8cc4cf1 | 2015-03-06 14:36:33 +0000 | [diff] [blame] | 391 | fprintf(stderr, "lldb-server exiting...\n"); |
Daniel Malea | e0f8f57 | 2013-08-26 23:57:52 +0000 | [diff] [blame] | 392 | |
Greg Clayton | b43767a | 2011-03-22 01:34:44 +0000 | [diff] [blame] | 393 | return 0; |
| 394 | } |