1 //===-- SystemInitializerFull.cpp -----------------------------------------===//
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 "SystemInitializerFull.h"
10 #include "lldb/API/SBCommandInterpreter.h"
11 #include "lldb/Core/Debugger.h"
12 #include "lldb/Core/PluginManager.h"
13 #include "lldb/Host/Config.h"
14 #include "lldb/Host/Host.h"
15 #include "lldb/Initialization/SystemInitializerCommon.h"
16 #include "lldb/Interpreter/CommandInterpreter.h"
17 #include "lldb/Target/ProcessTrace.h"
18 #include "lldb/Utility/Reproducer.h"
19 #include "lldb/Utility/Timer.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/TargetSelect.h"
22 
23 #pragma clang diagnostic push
24 #pragma clang diagnostic ignored "-Wglobal-constructors"
25 #include "llvm/ExecutionEngine/MCJIT.h"
26 #pragma clang diagnostic pop
27 
28 #include <string>
29 
30 #define LLDB_PLUGIN(p) LLDB_PLUGIN_DECLARE(p)
31 #include "Plugins/Plugins.def"
32 
33 #if LLDB_ENABLE_PYTHON
34 #include "Plugins/ScriptInterpreter/Python/ScriptInterpreterPython.h"
35 
36 constexpr lldb_private::HostInfo::SharedLibraryDirectoryHelper
37     *g_shlib_dir_helper =
38         lldb_private::ScriptInterpreterPython::SharedLibraryDirectoryHelper;
39 
40 #else
41 constexpr lldb_private::HostInfo::SharedLibraryDirectoryHelper
42     *g_shlib_dir_helper = 0;
43 #endif
44 
45 using namespace lldb_private;
46 
47 SystemInitializerFull::SystemInitializerFull()
48     : SystemInitializerCommon(g_shlib_dir_helper) {}
49 SystemInitializerFull::~SystemInitializerFull() = default;
50 
51 llvm::Error SystemInitializerFull::Initialize() {
52   llvm::Error error = SystemInitializerCommon::Initialize();
53   if (error) {
54     // During active replay, the ::Initialize call is replayed like any other
55     // SB API call and the return value is ignored. Since we can't intercept
56     // this, we terminate here before the uninitialized debugger inevitably
57     // crashes.
58     if (repro::Reproducer::Instance().IsReplaying())
59       llvm::report_fatal_error(std::move(error));
60     return error;
61   }
62 
63   // Initialize LLVM and Clang
64   llvm::InitializeAllTargets();
65   llvm::InitializeAllAsmPrinters();
66   llvm::InitializeAllTargetMCs();
67   llvm::InitializeAllDisassemblers();
68   // Initialize the command line parser in LLVM. This usually isn't necessary
69   // as we aren't dealing with command line options here, but otherwise some
70   // other code in Clang/LLVM might be tempted to call this function from a
71   // different thread later on which won't work (as the function isn't
72   // thread-safe).
73   const char *arg0 = "lldb";
74   llvm::cl::ParseCommandLineOptions(1, &arg0);
75 
76 #define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
77 #include "Plugins/Plugins.def"
78 
79   // Initialize plug-ins in core LLDB
80   ProcessTrace::Initialize();
81 
82   // Scan for any system or user LLDB plug-ins
83   PluginManager::Initialize();
84 
85   // The process settings need to know about installed plug-ins, so the
86   // Settings must be initialized AFTER PluginManager::Initialize is called.
87   Debugger::SettingsInitialize();
88 
89   return llvm::Error::success();
90 }
91 
92 void SystemInitializerFull::Terminate() {
93   Debugger::SettingsTerminate();
94 
95   // Terminate plug-ins in core LLDB
96   ProcessTrace::Terminate();
97 
98   // Terminate and unload and loaded system or user LLDB plug-ins
99   PluginManager::Terminate();
100 
101 #define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
102 #include "Plugins/Plugins.def"
103 
104   // Now shutdown the common parts, in reverse order.
105   SystemInitializerCommon::Terminate();
106 }
107