Skip to content

Commit 7c5bba0

Browse files
saschwartzmemfrob
authored andcommitted
[lldb server] Tidy up LLDB server return codes and associated tests
This diff modifies the LLDB server return codes to more accurately reflect usage error paths. Specifically we always propagate the return codes from the main entrypoints into GDB remote LLDB server, and platform LLDB server. This way, the top-level caller of LLDB server will be able to correctly check whether the executable exited with or without an error. We additionally modify and extend the associated shell unit tests to expect nonzero return codes on error conditions. Test Plan: LLDB tests pass: ``` ninja check-lldb ``` Reviewed By: teemperor Differential Revision: https://reviews.llvm.org/D108351
1 parent 5ce4278 commit 7c5bba0

File tree

4 files changed

+39
-27
lines changed

4 files changed

+39
-27
lines changed
Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
1-
RUN: %lldb-server gdbserver --fd 2>&1 | FileCheck --check-prefixes=FD1,ALL %s
1+
RUN: not %lldb-server gdbserver --fd 2>&1 | FileCheck --check-prefixes=FD1,GDB_REMOTE_ALL %s
22
FD1: error: --fd: missing argument
33

4-
RUN: %lldb-server gdbserver --fd three 2>&1 | FileCheck --check-prefixes=FD2,ALL %s
4+
RUN: not %lldb-server gdbserver --fd three 2>&1 | FileCheck --check-prefixes=FD2,GDB_REMOTE_ALL %s
55
FD2: error: invalid '--fd' argument
66

7-
RUN: %lldb-server gdbserver --bogus 2>&1 | FileCheck --check-prefixes=BOGUS,ALL %s
7+
RUN: not %lldb-server gdbserver --bogus 2>&1 | FileCheck --check-prefixes=BOGUS,GDB_REMOTE_ALL %s
88
BOGUS: error: unknown argument '--bogus'
99

10-
RUN: %lldb-server gdbserver 2>&1 | FileCheck --check-prefixes=CONN,ALL %s
10+
RUN: not %lldb-server gdbserver 2>&1 | FileCheck --check-prefixes=CONN,GDB_REMOTE_ALL %s
1111
CONN: error: no connection arguments
1212

13-
ALL: Use '{{.*}} g[dbserver] --help' for a complete list of options.
13+
RUN: %lldb-server platform 2>&1 | FileCheck --check-prefixes=LLDB_PLATFORM_ALL %s
14+
15+
RUN: %lldb-server platform --fd 2>&1 | FileCheck --check-prefixes=FD3,LLDB_PLATFORM_ALL %s
16+
FD3: lldb-server: unrecognized option `--fd'
17+
18+
RUN: not %lldb-server platform --min-gdbserver-port 42 --max-gdbserver-port 43 2>&1 | FileCheck --check-prefixes=PORT1,LLDB_PLATFORM_ALL %s
19+
PORT1: error: port number 42 is not in the valid user port range of 1024 - 49152
20+
21+
RUN: %lldb-server version 2>&1 | FileCheck --check-prefixes=VERSION %s
22+
VERSION: lldb version
23+
24+
GDB_REMOTE_ALL: Use '{{.*}} g[dbserver] --help' for a complete list of options.
25+
LLDB_PLATFORM_ALL: lldb-server platform [--log-file log-file-name] [--log-channels log-channel-list] [--port-file port-file-path] --server --listen port
1426

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Windows does not build lldb-server.
22
# UNSUPPORTED: system-windows
3-
# RUN: %platformserver --server --listen :1234 --min-gdbserver-port 1234 --max-gdbserver-port 1234 2>&1 | FileCheck %s
3+
# RUN: not %platformserver --server --listen :1234 --min-gdbserver-port 1234 --max-gdbserver-port 1234 2>&1 | FileCheck %s
44
# CHECK: error: --min-gdbserver-port (1234) is not lower than --max-gdbserver-port (1234)

lldb/tools/lldb-server/lldb-platform.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ static void display_usage(const char *progname, const char *subcommand) {
9292
"log-channel-list] [--port-file port-file-path] --server "
9393
"--listen port\n",
9494
progname, subcommand);
95-
exit(0);
9695
}
9796

9897
static Status save_socket_id_to_file(const std::string &socket_id,
@@ -165,7 +164,6 @@ int main_platform(int argc, char *argv[]) {
165164
FileSpec socket_file;
166165
bool show_usage = false;
167166
int option_error = 0;
168-
int socket_error = -1;
169167

170168
std::string short_options(OptionParser::GetShortOptionString(g_long_options));
171169

@@ -249,7 +247,7 @@ int main_platform(int argc, char *argv[]) {
249247
}
250248

251249
if (!LLDBServerUtilities::SetupLogging(log_file, log_channels, 0))
252-
return -1;
250+
return 1;
253251

254252
// Make a port map for a port range that was specified.
255253
if (min_gdbserver_port && min_gdbserver_port < max_gdbserver_port) {
@@ -269,7 +267,7 @@ int main_platform(int argc, char *argv[]) {
269267

270268
if (show_usage || option_error) {
271269
display_usage(progname, subcommand);
272-
exit(option_error);
270+
return option_error;
273271
}
274272

275273
// Skip any options we consumed with getopt_long_only.
@@ -288,13 +286,13 @@ int main_platform(int argc, char *argv[]) {
288286
listen_host_port, children_inherit_listen_socket, error));
289287
if (error.Fail()) {
290288
fprintf(stderr, "failed to create acceptor: %s", error.AsCString());
291-
exit(socket_error);
289+
return 1;
292290
}
293291

294292
error = acceptor_up->Listen(backlog);
295293
if (error.Fail()) {
296294
printf("failed to listen: %s\n", error.AsCString());
297-
exit(socket_error);
295+
return 1;
298296
}
299297
if (socket_file) {
300298
error =
@@ -322,7 +320,7 @@ int main_platform(int argc, char *argv[]) {
322320
error = acceptor_up->Accept(children_inherit_accept_socket, conn);
323321
if (error.Fail()) {
324322
WithColor::error() << error.AsCString() << '\n';
325-
exit(socket_error);
323+
return 1;
326324
}
327325
printf("Connection established.\n");
328326
if (g_server) {

lldb/tools/lldb-server/lldb-server.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "lldb/lldb-private.h"
1212

1313
#include "llvm/ADT/STLExtras.h"
14+
#include "llvm/ADT/ScopeExit.h"
1415
#include "llvm/ADT/StringRef.h"
1516
#include "llvm/Support/InitLLVM.h"
1617
#include "llvm/Support/ManagedStatic.h"
@@ -52,29 +53,30 @@ int main(int argc, char *argv[]) {
5253
llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false);
5354
llvm::PrettyStackTraceProgram X(argc, argv);
5455

55-
int option_error = 0;
5656
const char *progname = argv[0];
5757
if (argc < 2) {
5858
display_usage(progname);
59-
exit(option_error);
59+
return 1;
6060
}
6161

6262
switch (argv[1][0]) {
63-
case 'g':
63+
case 'g': {
6464
llgs::initialize();
65-
main_gdbserver(argc, argv);
66-
llgs::terminate_debugger();
67-
break;
68-
case 'p':
65+
auto deinit = llvm::make_scope_exit([]() { llgs::terminate_debugger(); });
66+
return main_gdbserver(argc, argv);
67+
}
68+
case 'p': {
6969
llgs::initialize();
70-
main_platform(argc, argv);
71-
llgs::terminate_debugger();
72-
break;
73-
case 'v':
70+
auto deinit = llvm::make_scope_exit([]() { llgs::terminate_debugger(); });
71+
return main_platform(argc, argv);
72+
}
73+
case 'v': {
7474
fprintf(stderr, "%s\n", lldb_private::GetVersion());
75-
break;
76-
default:
75+
return 0;
76+
}
77+
default: {
7778
display_usage(progname);
78-
exit(option_error);
79+
return 1;
80+
}
7981
}
8082
}

0 commit comments

Comments
 (0)