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