2011-03-22 09:34:44 +08:00
|
|
|
//===-- lldb-platform.cpp ---------------------------------------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-03-22 09:34:44 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include <errno.h>
|
2014-08-07 02:16:26 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#endif
|
2011-03-22 09:34:44 +08:00
|
|
|
#include <signal.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2019-08-14 07:50:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-03-25 20:51:31 +08:00
|
|
|
#include <sys/wait.h>
|
2019-08-14 07:50:54 +08:00
|
|
|
#endif
|
2015-07-07 02:05:19 +08:00
|
|
|
#include <fstream>
|
2011-03-23 08:09:55 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
|
|
|
#include "llvm/Support/FileUtilities.h"
|
2019-09-14 04:08:27 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2015-10-22 03:34:26 +08:00
|
|
|
|
|
|
|
#include "Acceptor.h"
|
2015-05-27 21:34:04 +08:00
|
|
|
#include "LLDBServerUtilities.h"
|
2015-02-11 18:29:30 +08:00
|
|
|
#include "Plugins/Process/gdb-remote/GDBRemoteCommunicationServerPlatform.h"
|
2011-03-24 12:28:38 +08:00
|
|
|
#include "Plugins/Process/gdb-remote/ProcessGDBRemoteLog.h"
|
2014-10-07 05:22:36 +08:00
|
|
|
#include "lldb/Host/ConnectionFileDescriptor.h"
|
2014-08-07 02:16:26 +08:00
|
|
|
#include "lldb/Host/HostGetOpt.h"
|
2013-11-23 09:58:15 +08:00
|
|
|
#include "lldb/Host/OptionParser.h"
|
2015-10-16 07:54:09 +08:00
|
|
|
#include "lldb/Host/common/TCPSocket.h"
|
2017-03-23 02:40:07 +08:00
|
|
|
#include "lldb/Utility/FileSpec.h"
|
2017-05-12 12:51:55 +08:00
|
|
|
#include "lldb/Utility/Status.h"
|
2015-02-11 18:29:30 +08:00
|
|
|
|
2011-03-23 08:09:55 +08:00
|
|
|
using namespace lldb;
|
|
|
|
using namespace lldb_private;
|
2015-05-27 21:34:04 +08:00
|
|
|
using namespace lldb_private::lldb_server;
|
2015-03-31 17:52:22 +08:00
|
|
|
using namespace lldb_private::process_gdb_remote;
|
2015-05-27 21:34:04 +08:00
|
|
|
using namespace llvm;
|
2011-03-23 08:09:55 +08:00
|
|
|
|
2013-04-05 04:35:24 +08:00
|
|
|
// option descriptors for getopt_long_only()
|
2011-03-22 09:34:44 +08:00
|
|
|
|
2015-02-18 23:39:41 +08:00
|
|
|
static int g_debug = 0;
|
|
|
|
static int g_verbose = 0;
|
2015-03-25 20:51:31 +08:00
|
|
|
static int g_server = 0;
|
2011-03-22 09:34:44 +08:00
|
|
|
|
|
|
|
static struct option g_long_options[] = {
|
|
|
|
{"debug", no_argument, &g_debug, 1},
|
|
|
|
{"verbose", no_argument, &g_verbose, 1},
|
[lldb] NFC modernize codebase with modernize-use-nullptr
Summary:
NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]]
This commit is the result of modernizing the LLDB codebase by using
`nullptr` instread of `0` or `NULL`. See
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html
for more information.
This is the command I ran and I to fix and format the code base:
```
run-clang-tidy.py \
-header-filter='.*' \
-checks='-*,modernize-use-nullptr' \
-fix ~/dev/llvm-project/lldb/.* \
-format \
-style LLVM \
-p ~/llvm-builds/debug-ninja-gcc
```
NOTE: There were also changes to `llvm/utils/unittest` but I did not
include them because I felt that maybe this library shall be updated in
isolation somehow.
NOTE: I know this is a rather large commit but it is a nobrainer in most
parts.
Reviewers: martong, espindola, shafik, #lldb, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits
Tags: #lldb, #llvm
Differential Revision: https://reviews.llvm.org/D61847
llvm-svn: 361484
2019-05-23 19:14:47 +08:00
|
|
|
{"log-file", required_argument, nullptr, 'l'},
|
|
|
|
{"log-channels", required_argument, nullptr, 'c'},
|
|
|
|
{"listen", required_argument, nullptr, 'L'},
|
|
|
|
{"port-offset", required_argument, nullptr, 'p'},
|
|
|
|
{"gdbserver-port", required_argument, nullptr, 'P'},
|
|
|
|
{"min-gdbserver-port", required_argument, nullptr, 'm'},
|
|
|
|
{"max-gdbserver-port", required_argument, nullptr, 'M'},
|
|
|
|
{"socket-file", required_argument, nullptr, 'f'},
|
2015-03-25 20:51:31 +08:00
|
|
|
{"server", no_argument, &g_server, 1},
|
[lldb] NFC modernize codebase with modernize-use-nullptr
Summary:
NFC = [[ https://llvm.org/docs/Lexicon.html#nfc | Non functional change ]]
This commit is the result of modernizing the LLDB codebase by using
`nullptr` instread of `0` or `NULL`. See
https://clang.llvm.org/extra/clang-tidy/checks/modernize-use-nullptr.html
for more information.
This is the command I ran and I to fix and format the code base:
```
run-clang-tidy.py \
-header-filter='.*' \
-checks='-*,modernize-use-nullptr' \
-fix ~/dev/llvm-project/lldb/.* \
-format \
-style LLVM \
-p ~/llvm-builds/debug-ninja-gcc
```
NOTE: There were also changes to `llvm/utils/unittest` but I did not
include them because I felt that maybe this library shall be updated in
isolation somehow.
NOTE: I know this is a rather large commit but it is a nobrainer in most
parts.
Reviewers: martong, espindola, shafik, #lldb, JDevlieghere
Reviewed By: JDevlieghere
Subscribers: arsenm, jvesely, nhaehnle, hiraditya, JDevlieghere, teemperor, rnkovacs, emaste, kubamracek, nemanjai, ki.stfu, javed.absar, arichardson, kbarton, jrtc27, MaskRay, atanasyan, dexonsmith, arphaman, jfb, jsji, jdoerfert, lldb-commits, llvm-commits
Tags: #lldb, #llvm
Differential Revision: https://reviews.llvm.org/D61847
llvm-svn: 361484
2019-05-23 19:14:47 +08:00
|
|
|
{nullptr, 0, nullptr, 0}};
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-21 09:44:58 +08:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#define LOW_PORT (IPPORT_RESERVED)
|
|
|
|
#define HIGH_PORT (IPPORT_HIFIRSTAUTO)
|
|
|
|
#else
|
|
|
|
#define LOW_PORT (1024u)
|
|
|
|
#define HIGH_PORT (49151u)
|
|
|
|
#endif
|
|
|
|
|
2019-08-14 07:50:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2011-03-22 09:34:44 +08:00
|
|
|
// Watch for signals
|
|
|
|
static void signal_handler(int signo) {
|
|
|
|
switch (signo) {
|
2013-08-27 07:57:52 +08:00
|
|
|
case SIGHUP:
|
|
|
|
// Use SIGINT first, if that does not work, use SIGHUP as a last resort.
|
|
|
|
// And we should not call exit() here because it results in the global
|
|
|
|
// destructors
|
|
|
|
// to be invoked and wreaking havoc on the threads still running.
|
2015-03-06 22:36:33 +08:00
|
|
|
Host::SystemLog(Host::eSystemLogWarning,
|
|
|
|
"SIGHUP received, exiting lldb-server...\n");
|
2013-08-27 07:57:52 +08:00
|
|
|
abort();
|
|
|
|
break;
|
2011-03-22 09:34:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-08-14 07:50:54 +08:00
|
|
|
#endif
|
2011-03-22 09:34:44 +08:00
|
|
|
|
2015-02-18 23:39:41 +08:00
|
|
|
static void display_usage(const char *progname, const char *subcommand) {
|
2015-07-07 02:05:19 +08:00
|
|
|
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);
|
2013-08-27 07:57:52 +08:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
2017-05-12 12:51:55 +08:00
|
|
|
static Status save_socket_id_to_file(const std::string &socket_id,
|
|
|
|
const FileSpec &file_spec) {
|
2020-02-11 16:05:37 +08:00
|
|
|
FileSpec temp_file_spec(file_spec.GetDirectory().GetStringRef());
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error(llvm::sys::fs::create_directory(temp_file_spec.GetPath()));
|
2015-07-07 02:05:19 +08:00
|
|
|
if (error.Fail())
|
2017-05-12 12:51:55 +08:00
|
|
|
return Status("Failed to create directory %s: %s",
|
|
|
|
temp_file_spec.GetCString(), error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2017-03-21 21:49:50 +08:00
|
|
|
llvm::SmallString<64> temp_file_path;
|
2015-07-15 02:54:52 +08:00
|
|
|
temp_file_spec.AppendPathComponent("port-file.%%%%%%");
|
2020-08-17 16:25:20 +08:00
|
|
|
temp_file_path = temp_file_spec.GetPath();
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2019-09-14 04:08:27 +08:00
|
|
|
Status status;
|
|
|
|
if (auto Err =
|
|
|
|
handleErrors(llvm::writeFileAtomically(
|
2020-08-17 16:25:20 +08:00
|
|
|
temp_file_path, file_spec.GetPath(), socket_id),
|
2019-09-17 06:55:49 +08:00
|
|
|
[&status, &file_spec](const AtomicFileWriteError &E) {
|
2019-09-14 04:08:27 +08:00
|
|
|
std::string ErrorMsgBuffer;
|
|
|
|
llvm::raw_string_ostream S(ErrorMsgBuffer);
|
|
|
|
E.log(S);
|
|
|
|
|
|
|
|
switch (E.Error) {
|
|
|
|
case atomic_write_error::failed_to_create_uniq_file:
|
|
|
|
status = Status("Failed to create temp file: %s",
|
|
|
|
ErrorMsgBuffer.c_str());
|
2019-09-14 06:00:03 +08:00
|
|
|
break;
|
2019-09-14 04:08:27 +08:00
|
|
|
case atomic_write_error::output_stream_error:
|
|
|
|
status = Status("Failed to write to port file.");
|
2019-09-14 06:00:03 +08:00
|
|
|
break;
|
2019-09-14 04:08:27 +08:00
|
|
|
case atomic_write_error::failed_to_rename_temp_file:
|
|
|
|
status = Status("Failed to rename file %s to %s: %s",
|
|
|
|
ErrorMsgBuffer.c_str(),
|
|
|
|
file_spec.GetPath().c_str(),
|
|
|
|
ErrorMsgBuffer.c_str());
|
2019-09-14 06:00:03 +08:00
|
|
|
break;
|
2019-09-14 04:08:27 +08:00
|
|
|
}
|
|
|
|
})) {
|
|
|
|
return Status("Failed to atomically write file %s",
|
|
|
|
file_spec.GetPath().c_str());
|
|
|
|
}
|
|
|
|
return status;
|
2015-07-07 02:05:19 +08:00
|
|
|
}
|
|
|
|
|
2011-03-22 09:34:44 +08:00
|
|
|
// main
|
2015-02-18 23:39:41 +08:00
|
|
|
int main_platform(int argc, char *argv[]) {
|
2013-08-27 07:57:52 +08:00
|
|
|
const char *progname = argv[0];
|
2015-02-18 23:39:41 +08:00
|
|
|
const char *subcommand = argv[1];
|
|
|
|
argc--;
|
|
|
|
argv++;
|
2019-08-14 07:50:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2013-11-23 09:58:15 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2013-08-27 07:57:52 +08:00
|
|
|
signal(SIGHUP, signal_handler);
|
2019-08-14 07:50:54 +08:00
|
|
|
#endif
|
2011-03-22 09:34:44 +08:00
|
|
|
int long_option_index = 0;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error;
|
2011-06-17 09:22:15 +08:00
|
|
|
std::string listen_host_port;
|
2011-11-01 07:51:19 +08:00
|
|
|
int ch;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-05-27 21:34:04 +08:00
|
|
|
std::string log_file;
|
|
|
|
StringRef
|
|
|
|
log_channels; // e.g. "lldb process threads:gdb-remote default:linux all"
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-02-11 18:29:30 +08:00
|
|
|
GDBRemoteCommunicationServerPlatform::PortMap gdbserver_portmap;
|
2013-11-21 09:44:58 +08:00
|
|
|
int min_gdbserver_port = 0;
|
|
|
|
int max_gdbserver_port = 0;
|
2013-11-23 02:55:04 +08:00
|
|
|
uint16_t port_offset = 0;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
FileSpec socket_file;
|
2013-11-21 09:44:58 +08:00
|
|
|
bool show_usage = false;
|
|
|
|
int option_error = 0;
|
2015-03-25 20:51:31 +08:00
|
|
|
int socket_error = -1;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-23 09:58:15 +08:00
|
|
|
std::string short_options(OptionParser::GetShortOptionString(g_long_options));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-23 02:55:04 +08:00
|
|
|
#if __GLIBC__
|
|
|
|
optind = 0;
|
|
|
|
#else
|
|
|
|
optreset = 1;
|
|
|
|
optind = 1;
|
|
|
|
#endif
|
|
|
|
|
2013-11-23 09:58:15 +08:00
|
|
|
while ((ch = getopt_long_only(argc, argv, short_options.c_str(),
|
|
|
|
g_long_options, &long_option_index)) != -1) {
|
2011-03-22 09:34:44 +08:00
|
|
|
switch (ch) {
|
|
|
|
case 0: // Any optional that auto set themselves will return 0
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2011-03-23 08:09:55 +08:00
|
|
|
case 'L':
|
2011-06-17 09:22:15 +08:00
|
|
|
listen_host_port.append(optarg);
|
2015-05-27 21:34:04 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
case 'l': // Set Log File
|
2015-07-07 02:05:19 +08:00
|
|
|
if (optarg && optarg[0])
|
2015-10-22 03:34:26 +08:00
|
|
|
log_file.assign(optarg);
|
2015-07-07 02:05:19 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-21 09:44:58 +08:00
|
|
|
case 'c': // Log Channels
|
2013-11-23 02:55:04 +08:00
|
|
|
if (optarg && optarg[0])
|
|
|
|
log_channels = StringRef(optarg);
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
|
2013-11-23 02:55:04 +08:00
|
|
|
case 'f': // Socket file
|
|
|
|
if (optarg && optarg[0])
|
2018-11-02 05:05:36 +08:00
|
|
|
socket_file.SetFile(optarg, FileSpec::Style::native);
|
2013-11-23 02:55:04 +08:00
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-23 02:55:04 +08:00
|
|
|
case 'p': {
|
2017-07-05 22:54:46 +08:00
|
|
|
if (!llvm::to_integer(optarg, port_offset)) {
|
|
|
|
llvm::errs() << "error: invalid port offset string " << optarg << "\n";
|
2013-11-23 02:55:04 +08:00
|
|
|
option_error = 4;
|
2017-07-05 22:54:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-06 19:43:25 +08:00
|
|
|
if (port_offset < LOW_PORT || port_offset > HIGH_PORT) {
|
2017-07-05 22:54:46 +08:00
|
|
|
llvm::errs() << llvm::formatv("error: port offset {0} is not in the "
|
|
|
|
"valid user port range of {1} - {2}\n",
|
|
|
|
port_offset, LOW_PORT, HIGH_PORT);
|
|
|
|
option_error = 5;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case 'P':
|
2013-11-21 09:44:58 +08:00
|
|
|
case 'm':
|
|
|
|
case 'M': {
|
2017-07-05 22:54:46 +08:00
|
|
|
uint16_t portnum;
|
|
|
|
if (!llvm::to_integer(optarg, portnum)) {
|
|
|
|
llvm::errs() << "error: invalid port number string " << optarg << "\n";
|
2013-11-21 09:44:58 +08:00
|
|
|
option_error = 2;
|
2017-07-05 22:54:46 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-07-06 19:43:25 +08:00
|
|
|
if (portnum < LOW_PORT || portnum > HIGH_PORT) {
|
2017-07-05 22:54:46 +08:00
|
|
|
llvm::errs() << llvm::formatv("error: port number {0} is not in the "
|
|
|
|
"valid user port range of {1} - {2}\n",
|
|
|
|
portnum, LOW_PORT, HIGH_PORT);
|
|
|
|
option_error = 1;
|
|
|
|
break;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2017-07-05 22:54:46 +08:00
|
|
|
if (ch == 'P')
|
2020-11-17 23:41:04 +08:00
|
|
|
gdbserver_portmap.AllowPort(portnum);
|
2017-07-05 22:54:46 +08:00
|
|
|
else if (ch == 'm')
|
|
|
|
min_gdbserver_port = portnum;
|
|
|
|
else
|
|
|
|
max_gdbserver_port = portnum;
|
2016-09-07 04:57:50 +08:00
|
|
|
} break;
|
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
case 'h': /* fall-through is intentional */
|
|
|
|
case '?':
|
2013-11-21 09:44:58 +08:00
|
|
|
show_usage = true;
|
2016-09-07 04:57:50 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-27 21:34:04 +08:00
|
|
|
if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0))
|
|
|
|
return -1;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-21 09:44:58 +08:00
|
|
|
// Make a port map for a port range that was specified.
|
2019-03-07 05:52:19 +08:00
|
|
|
if (min_gdbserver_port && min_gdbserver_port < max_gdbserver_port) {
|
2020-11-17 23:41:04 +08:00
|
|
|
gdbserver_portmap = GDBRemoteCommunicationServerPlatform::PortMap(
|
|
|
|
min_gdbserver_port, max_gdbserver_port);
|
2019-03-07 05:52:19 +08:00
|
|
|
} else if (min_gdbserver_port || max_gdbserver_port) {
|
|
|
|
fprintf(stderr, "error: --min-gdbserver-port (%u) is not lower than "
|
2013-11-21 09:44:58 +08:00
|
|
|
"--max-gdbserver-port (%u)\n",
|
|
|
|
min_gdbserver_port, max_gdbserver_port);
|
|
|
|
option_error = 3;
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
// Print usage and exit if no listening port is specified.
|
|
|
|
if (listen_host_port.empty())
|
2013-11-21 09:44:58 +08:00
|
|
|
show_usage = true;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2013-11-21 09:44:58 +08:00
|
|
|
if (show_usage || option_error) {
|
2015-02-18 23:39:41 +08:00
|
|
|
display_usage(progname, subcommand);
|
2013-11-21 09:44:58 +08:00
|
|
|
exit(option_error);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-12-08 22:08:19 +08:00
|
|
|
// Skip any options we consumed with getopt_long_only.
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
lldb_private::Args inferior_arguments;
|
|
|
|
inferior_arguments.SetArguments(argc, const_cast<const char **>(argv));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
const bool children_inherit_listen_socket = false;
|
2015-03-31 08:27:10 +08:00
|
|
|
// the test suite makes many connections in parallel, let's not miss any.
|
2015-10-22 03:34:26 +08:00
|
|
|
// The highest this should get reasonably is a function of the number
|
|
|
|
// of target CPUs. For now, let's just use 100.
|
2015-03-31 08:27:10 +08:00
|
|
|
const int backlog = 100;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
std::unique_ptr<Acceptor> acceptor_up(Acceptor::Create(
|
|
|
|
listen_host_port, children_inherit_listen_socket, error));
|
2015-10-16 07:54:09 +08:00
|
|
|
if (error.Fail()) {
|
2015-10-22 03:34:26 +08:00
|
|
|
fprintf(stderr, "failed to create acceptor: %s", error.AsCString());
|
2015-10-16 07:54:09 +08:00
|
|
|
exit(socket_error);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
error = acceptor_up->Listen(backlog);
|
2015-10-16 07:54:09 +08:00
|
|
|
if (error.Fail()) {
|
2015-10-22 03:34:26 +08:00
|
|
|
printf("failed to listen: %s\n", error.AsCString());
|
2015-10-16 07:54:09 +08:00
|
|
|
exit(socket_error);
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2015-10-22 03:34:26 +08:00
|
|
|
if (socket_file) {
|
2016-09-07 04:57:50 +08:00
|
|
|
error =
|
2015-10-22 03:34:26 +08:00
|
|
|
save_socket_id_to_file(acceptor_up->GetLocalSocketId(), socket_file);
|
2015-10-16 07:54:09 +08:00
|
|
|
if (error.Fail()) {
|
2015-12-08 22:08:19 +08:00
|
|
|
fprintf(stderr, "failed to write socket id to %s: %s\n",
|
|
|
|
socket_file.GetPath().c_str(), error.AsCString());
|
2015-07-07 02:05:19 +08:00
|
|
|
return 1;
|
2011-03-22 09:34:44 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-12-03 03:35:49 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
do {
|
2015-05-27 21:34:04 +08:00
|
|
|
GDBRemoteCommunicationServerPlatform platform(
|
|
|
|
acceptor_up->GetSocketProtocol(), acceptor_up->GetSocketScheme());
|
|
|
|
|
2013-11-21 09:44:58 +08:00
|
|
|
if (port_offset > 0)
|
|
|
|
platform.SetPortOffset(port_offset);
|
|
|
|
|
2013-08-27 07:57:52 +08:00
|
|
|
if (!gdbserver_portmap.empty()) {
|
2015-02-18 23:39:41 +08:00
|
|
|
platform.SetPortMap(std::move(gdbserver_portmap));
|
2013-11-21 09:44:58 +08:00
|
|
|
}
|
2015-03-31 08:27:10 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
const bool children_inherit_accept_socket = true;
|
2015-03-31 08:27:10 +08:00
|
|
|
Connection *conn = nullptr;
|
2015-10-22 03:34:26 +08:00
|
|
|
error = acceptor_up->Accept(children_inherit_accept_socket, conn);
|
2015-10-16 07:54:09 +08:00
|
|
|
if (error.Fail()) {
|
2015-10-22 03:34:26 +08:00
|
|
|
printf("error: %s\n", error.AsCString());
|
2015-10-16 07:54:09 +08:00
|
|
|
exit(socket_error);
|
|
|
|
}
|
2015-10-22 03:34:26 +08:00
|
|
|
printf("Connection established.\n");
|
|
|
|
if (g_server) {
|
|
|
|
// Collect child zombie processes.
|
2019-08-14 07:50:54 +08:00
|
|
|
#if !defined(_WIN32)
|
2015-10-22 03:34:26 +08:00
|
|
|
while (waitpid(-1, nullptr, WNOHANG) > 0)
|
2016-09-07 04:57:50 +08:00
|
|
|
;
|
2019-08-14 07:50:54 +08:00
|
|
|
#endif
|
2015-10-22 03:34:26 +08:00
|
|
|
if (fork()) {
|
2015-03-31 08:24:51 +08:00
|
|
|
// Parent doesn't need a connection to the lldb client
|
2015-10-22 03:34:26 +08:00
|
|
|
delete conn;
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-22 03:34:26 +08:00
|
|
|
// Parent will continue to listen for new connections.
|
2015-03-25 20:51:31 +08:00
|
|
|
continue;
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2015-03-25 20:51:31 +08:00
|
|
|
// Child process will handle the connection and exit.
|
|
|
|
g_server = 0;
|
|
|
|
// Listening socket is owned by parent process.
|
2015-10-22 03:34:26 +08:00
|
|
|
acceptor_up.release();
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
} else {
|
2015-03-25 20:51:31 +08:00
|
|
|
// If not running as a server, this process will not accept
|
|
|
|
// connections while a connection is active.
|
|
|
|
acceptor_up.reset();
|
|
|
|
}
|
2020-04-02 20:40:59 +08:00
|
|
|
platform.SetConnection(std::unique_ptr<Connection>(conn));
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-10-29 03:49:50 +08:00
|
|
|
if (platform.IsConnected()) {
|
2013-11-23 02:55:04 +08:00
|
|
|
if (inferior_arguments.GetArgumentCount() > 0) {
|
2015-03-25 20:51:31 +08:00
|
|
|
lldb::pid_t pid = LLDB_INVALID_PROCESS_ID;
|
2020-11-24 22:01:29 +08:00
|
|
|
llvm::Optional<uint16_t> port = 0;
|
2015-03-25 20:51:31 +08:00
|
|
|
std::string socket_name;
|
2017-05-12 12:51:55 +08:00
|
|
|
Status error = platform.LaunchGDBServer(inferior_arguments,
|
|
|
|
"", // hostname
|
|
|
|
pid, port, socket_name);
|
2015-03-25 20:51:31 +08:00
|
|
|
if (error.Success())
|
2020-11-24 22:01:29 +08:00
|
|
|
platform.SetPendingGdbServer(pid, *port, socket_name);
|
2015-03-25 20:51:31 +08:00
|
|
|
else
|
|
|
|
fprintf(stderr, "failed to start gdbserver: %s\n", error.AsCString());
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
|
2015-03-25 20:51:31 +08:00
|
|
|
// After we connected, we need to get an initial ack from...
|
Introduce a MainLoop class and switch llgs to use it
Summary:
This is the first part of our effort to make llgs single threaded. Currently, llgs consists of
about three threads and the synchronisation between them is a major source of latency when
debugging linux and android applications.
In order to be able to go single threaded, we must have the ability to listen for events from
multiple sources (primarily, client commands coming over the network and debug events from the
inferior) and perform necessary actions. For this reason I introduce the concept of a MainLoop.
A main loop has the ability to register callback's which will be invoked upon receipt of certain
events. MainLoopPosix has the ability to listen for file descriptors and signals.
For the moment, I have merely made the GDBRemoteCommunicationServerLLGS class use MainLoop
instead of waiting on the network socket directly, but the other threads still remain. In the
followup patches I indend to migrate NativeProcessLinux to this class and remove the remaining
threads.
Reviewers: ovyalov, clayborg, amccarth, zturner, emaste
Subscribers: tberghammer, lldb-commits
Differential Revision: http://reviews.llvm.org/D11066
llvm-svn: 242018
2015-07-13 18:44:55 +08:00
|
|
|
if (platform.HandshakeWithClient()) {
|
2015-03-25 20:51:31 +08:00
|
|
|
bool interrupt = false;
|
|
|
|
bool done = false;
|
|
|
|
while (!interrupt && !done) {
|
Introduce chrono to more gdb-remote functions
Summary:
This replaces the usage of raw integers with duration classes in the gdb-remote
packet management functions. The values are still converted back to integers once
they go into the generic Communication class -- that I am leaving to a separate
change.
The changes are mostly straight-forward (*), the only tricky part was
representation of infinite timeouts.
Currently, we use UINT32_MAX to denote infinite timeout. This is not well suited
for duration classes, as they tend to do arithmetic on the values, and the
identity of the MAX value can easily get lost (e.g.
microseconds(seconds(UINT32_MAX)).count() != UINT32_MAX). We cannot use zero to
represent infinity (as Listener classes do) because we already use it to do
non-blocking polling reads. For this reason, I chose to have an explicit value
for infinity.
The way I achieved that is via llvm::Optional, and I think it reads quite
natural. Passing llvm::None as "timeout" means "no timeout", while passing zero
means "poll". The only tricky part is this breaks implicit conversions (seconds
are implicitly convertible to microseconds, but Optional<seconds> cannot be
easily converted into Optional<microseconds>). For this reason I added a special
class Timeout, inheriting from Optional, and enabling the necessary conversions
one would normally expect.
(*) The other tricky part was GDBRemoteCommunication::PopPacketFromQueue, which
was needlessly complicated. I've simplified it, but that one is only used in
non-stop mode, and so is untested.
Reviewers: clayborg, zturner, jingham
Subscribers: lldb-commits
Differential Revision: https://reviews.llvm.org/D26971
llvm-svn: 287864
2016-11-24 18:54:49 +08:00
|
|
|
if (platform.GetPacketAndSendResponse(llvm::None, error, interrupt,
|
2016-09-07 04:57:50 +08:00
|
|
|
done) !=
|
2015-10-22 03:34:26 +08:00
|
|
|
GDBRemoteCommunication::PacketResult::Success)
|
|
|
|
break;
|
2015-03-25 20:51:31 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
|
2015-12-08 22:08:19 +08:00
|
|
|
if (error.Fail()) {
|
2015-03-25 20:51:31 +08:00
|
|
|
fprintf(stderr, "error: %s\n", error.AsCString());
|
2011-03-23 08:09:55 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
} else {
|
2015-03-25 20:51:31 +08:00
|
|
|
fprintf(stderr, "error: handshake with client failed\n");
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
|
|
|
}
|
2015-03-25 20:51:31 +08:00
|
|
|
} while (g_server);
|
2011-03-22 09:34:44 +08:00
|
|
|
|
2015-03-06 22:36:33 +08:00
|
|
|
fprintf(stderr, "lldb-server exiting...\n");
|
2013-08-27 07:57:52 +08:00
|
|
|
|
2011-03-22 09:34:44 +08:00
|
|
|
return 0;
|
|
|
|
}
|