xref: /freebsd/contrib/llvm-project/lldb/tools/lldb-server/lldb-server.cpp (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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/lldb-private.h"
12 
13 #include "llvm/ADT/STLExtras.h"
14 #include "llvm/ADT/StringRef.h"
15 #include "llvm/Support/ManagedStatic.h"
16 #include "llvm/Support/PrettyStackTrace.h"
17 #include "llvm/Support/Signals.h"
18 
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 static llvm::ManagedStatic<lldb_private::SystemLifetimeManager>
23     g_debugger_lifetime;
24 
25 static void display_usage(const char *progname) {
26   fprintf(stderr, "Usage:\n"
27                   "  %s v[ersion]\n"
28                   "  %s g[dbserver] [options]\n"
29                   "  %s p[latform] [options]\n"
30                   "Invoke subcommand for additional help\n",
31           progname, progname, progname);
32   exit(0);
33 }
34 
35 // Forward declarations of subcommand main methods.
36 int main_gdbserver(int argc, char *argv[]);
37 int main_platform(int argc, char *argv[]);
38 
39 namespace llgs {
40 static void initialize() {
41   if (auto e = g_debugger_lifetime->Initialize(
42           llvm::make_unique<SystemInitializerLLGS>(), nullptr))
43     llvm::consumeError(std::move(e));
44 }
45 
46 static void terminate_debugger() { g_debugger_lifetime->Terminate(); }
47 } // namespace llgs
48 
49 // main
50 int main(int argc, char *argv[]) {
51   llvm::StringRef ToolName = argv[0];
52   llvm::sys::PrintStackTraceOnErrorSignal(ToolName);
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