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 = nullptr;
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     return error;
55 
56   // Initialize LLVM and Clang
57   llvm::InitializeAllTargets();
58   llvm::InitializeAllAsmPrinters();
59   llvm::InitializeAllTargetMCs();
60   llvm::InitializeAllDisassemblers();
61   // Initialize the command line parser in LLVM. This usually isn't necessary
62   // as we aren't dealing with command line options here, but otherwise some
63   // other code in Clang/LLVM might be tempted to call this function from a
64   // different thread later on which won't work (as the function isn't
65   // thread-safe).
66   const char *arg0 = "lldb";
67   llvm::cl::ParseCommandLineOptions(1, &arg0);
68 
69 #define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
70 #include "Plugins/Plugins.def"
71 
72   // Initialize plug-ins in core LLDB
73   ProcessTrace::Initialize();
74 
75   // Scan for any system or user LLDB plug-ins
76   PluginManager::Initialize();
77 
78   // The process settings need to know about installed plug-ins, so the
79   // Settings must be initialized AFTER PluginManager::Initialize is called.
80   Debugger::SettingsInitialize();
81 
82   return llvm::Error::success();
83 }
84 
85 void SystemInitializerFull::Terminate() {
86   Debugger::SettingsTerminate();
87 
88   // Terminate plug-ins in core LLDB
89   ProcessTrace::Terminate();
90 
91   // Terminate and unload and loaded system or user LLDB plug-ins
92   PluginManager::Terminate();
93 
94 #define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
95 #include "Plugins/Plugins.def"
96 
97   // Now shutdown the common parts, in reverse order.
98   SystemInitializerCommon::Terminate();
99 }
100