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