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/InitLLVM.h"
16#include "llvm/Support/ManagedStatic.h"
17#include "llvm/Support/PrettyStackTrace.h"
18#include "llvm/Support/Signals.h"
19
20#include <stdio.h>
21#include <stdlib.h>
22
23static llvm::ManagedStatic<lldb_private::SystemLifetimeManager>
24    g_debugger_lifetime;
25
26static 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.
37int main_gdbserver(int argc, char *argv[]);
38int main_platform(int argc, char *argv[]);
39
40namespace llgs {
41static void initialize() {
42  if (auto e = g_debugger_lifetime->Initialize(
43          std::make_unique<SystemInitializerLLGS>(), nullptr))
44    llvm::consumeError(std::move(e));
45}
46
47static void terminate_debugger() { g_debugger_lifetime->Terminate(); }
48} // namespace llgs
49
50// main
51int main(int argc, char *argv[]) {
52  llvm::InitLLVM IL(argc, argv, /*InstallPipeSignalExitHandler=*/false);
53  llvm::StringRef ToolName = argv[0];
54  llvm::sys::PrintStackTraceOnErrorSignal(ToolName);
55  llvm::PrettyStackTraceProgram X(argc, argv);
56
57  int option_error = 0;
58  const char *progname = argv[0];
59  if (argc < 2) {
60    display_usage(progname);
61    exit(option_error);
62  }
63
64  switch (argv[1][0]) {
65  case 'g':
66    llgs::initialize();
67    main_gdbserver(argc, argv);
68    llgs::terminate_debugger();
69    break;
70  case 'p':
71    llgs::initialize();
72    main_platform(argc, argv);
73    llgs::terminate_debugger();
74    break;
75  case 'v':
76    fprintf(stderr, "%s\n", lldb_private::GetVersion());
77    break;
78  default:
79    display_usage(progname);
80    exit(option_error);
81  }
82}
83