xref: /freebsd/contrib/llvm-project/lldb/tools/lldb-server/lldb-server.cpp (revision 18054d0220cfc8df9c9568c437bd6fbb59d53c3c)
1 //===-- lldb-server.cpp -----------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "SystemInitializerLLGS.h"
10 #include "lldb/Initialization/SystemLifetimeManager.h"
11 #include "lldb/Version/Version.h"
12 
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/InitLLVM.h"
16 #include "llvm/Support/ManagedStatic.h"
17 #include "llvm/Support/PrettyStackTrace.h"
18 #include "llvm/Support/Signals.h"
19 
20 #include <cstdio>
21 #include <cstdlib>
22 
23 static llvm::ManagedStatic<lldb_private::SystemLifetimeManager>
24     g_debugger_lifetime;
25 
26 static void display_usage(const char *progname) {
27   fprintf(stderr, "Usage:\n"
28                   "  %s v[ersion]\n"
29                   "  %s g[dbserver] [options]\n"
30                   "  %s p[latform] [options]\n"
31                   "Invoke subcommand for additional help\n",
32           progname, progname, progname);
33   exit(0);
34 }
35 
36 // Forward declarations of subcommand main methods.
37 int main_gdbserver(int argc, char *argv[]);
38 int main_platform(int argc, char *argv[]);
39 
40 namespace llgs {
41 static void initialize() {
42   if (auto e = g_debugger_lifetime->Initialize(
43           std::make_unique<SystemInitializerLLGS>(), nullptr))
44     llvm::consumeError(std::move(e));
45 }
46 
47 static void terminate_debugger() { g_debugger_lifetime->Terminate(); }
48 } // namespace llgs
49 
50 // main
51 int main(int argc, char *argv[]) {
52   llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false);
53   llvm::PrettyStackTraceProgram X(argc, argv);
54 
55   int option_error = 0;
56   const char *progname = argv[0];
57   if (argc < 2) {
58     display_usage(progname);
59     exit(option_error);
60   }
61 
62   switch (argv[1][0]) {
63   case 'g':
64     llgs::initialize();
65     main_gdbserver(argc, argv);
66     llgs::terminate_debugger();
67     break;
68   case 'p':
69     llgs::initialize();
70     main_platform(argc, argv);
71     llgs::terminate_debugger();
72     break;
73   case 'v':
74     fprintf(stderr, "%s\n", lldb_private::GetVersion());
75     break;
76   default:
77     display_usage(progname);
78     exit(option_error);
79   }
80 }
81