LLDB mainline
SystemInitializerFull.cpp
Go to the documentation of this file.
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
11#include "lldb/API/SBDebugger.h"
12#include "lldb/Core/Debugger.h"
14#include "lldb/Core/Progress.h"
16#include "lldb/Host/Config.h"
17#include "lldb/Host/Host.h"
21#include "lldb/Utility/Timer.h"
23#include "llvm/Support/CommandLine.h"
24#include "llvm/Support/TargetSelect.h"
25
26#if LLDB_ENABLE_PYTHON
29#endif
30
31#pragma clang diagnostic push
32#pragma clang diagnostic ignored "-Wglobal-constructors"
33#include "llvm/ExecutionEngine/MCJIT.h"
34#pragma clang diagnostic pop
35
36#include <string>
37
38#define LLDB_PLUGIN(p) LLDB_PLUGIN_DECLARE(p)
39#include "Plugins/Plugins.def"
40
41using namespace lldb_private;
42
45
48 if (error)
49 return error;
50
51#if LLDB_ENABLE_PYTHON
52 // Map libpython into the process before any code that might reference it
53 // runs. This is required by both the static script interpreter (whose
54 // Initialize() invokes Python via the LLDB_PLUGIN_INITIALIZE loop below)
55 // and the dynamic plugin (whose dlopen needs Python's symbols visible in
56 // the process). The loader is once_flag-cached and a no-op when libpython
57 // is already in the process (e.g. `import lldb` from Python).
58 llvm::Expected<ScriptInterpreterRuntimeLoader &> python_loader =
60 if (!python_loader)
61 return python_loader.takeError();
62 if (llvm::Error err = python_loader->Load())
63 return err;
64#endif
65
66 // Initialize LLVM and Clang
67 llvm::InitializeAllTargets();
68 llvm::InitializeAllAsmPrinters();
69 llvm::InitializeAllTargetMCs();
70 llvm::InitializeAllDisassemblers();
71
72 // Initialize the command line parser in LLVM. This usually isn't necessary
73 // as we aren't dealing with command line options here, but otherwise some
74 // other code in Clang/LLVM might be tempted to call this function from a
75 // different thread later on which won't work (as the function isn't
76 // thread-safe).
77 const char *arg0 = "lldb";
78 llvm::cl::ParseCommandLineOptions(1, &arg0);
79
80#define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
81#include "Plugins/Plugins.def"
82
83 // Scan for any system or user LLDB plug-ins.
85
86 // The process settings need to know about installed plug-ins, so the
87 // Settings must be initialized AFTER PluginManager::Initialize is called.
89
90 // Use the Debugger's LLDBAssert callback.
92
93 // Use the system log to report errors that would otherwise get dropped.
95
97
98 auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
99 const FileSpec &spec,
100 Status &error) -> llvm::sys::DynamicLibrary {
101 llvm::sys::DynamicLibrary dynlib =
102 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
103 if (dynlib.isValid()) {
104 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);
105
106 lldb::SBDebugger debugger_sb(debugger_sp);
107 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
108 // function.
109 // TODO: mangle this differently for your system - on OSX, the first
110 // underscore needs to be removed and the second one stays
111 LLDBCommandPluginInit init_func =
112 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
113 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
114 if (init_func) {
115 if (init_func(debugger_sb))
116 return dynlib;
117 else
119 "plug-in refused to load "
120 "(lldb::PluginInitialize(lldb::SBDebugger) "
121 "returned false)");
122 } else {
124 "plug-in is missing the required initialization: "
125 "lldb::PluginInitialize(lldb::SBDebugger)");
126 }
127 } else {
128 if (FileSystem::Instance().Exists(spec))
130 "this file does not represent a loadable dylib");
131 else
132 error = Status::FromErrorString("no such file");
133 }
134 return llvm::sys::DynamicLibrary();
135 };
136
137 Debugger::Initialize(LoadPlugin);
138
139 // Warm up DataVisualization and common language categories ahead of time.
140 [[maybe_unused]] lldb::TypeCategoryImplSP entry;
142 entry);
144 entry);
147 return llvm::Error::success();
148}
149
152
154
155 // Terminate plug-ins in core LLDB.
157
158 // Terminate and unload and loaded system or user LLDB plug-ins.
160
161#define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
162#include "Plugins/Plugins.def"
163
164 // Now shutdown the common parts, in reverse order.
166}
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOG(log,...)
The LLDB_LOG* macros defined below are the way to emit log messages.
Definition Log.h:375
static bool GetCategory(ConstString category, lldb::TypeCategoryImplSP &entry, bool allow_create=true)
static void AssertCallback(llvm::StringRef message, llvm::StringRef backtrace, llvm::StringRef prompt)
static void Terminate()
Definition Debugger.cpp:856
static void SettingsInitialize()
Definition Debugger.cpp:885
static void Initialize(LoadPluginCallbackType load_plugin_callback)
Definition Debugger.cpp:847
static void SettingsTerminate()
Definition Debugger.cpp:887
A file utility class.
Definition FileSpec.h:56
static FileSystem & Instance()
static llvm::Expected< ScriptInterpreterRuntimeLoader & > Get(lldb::ScriptLanguage language)
Returns the loader for language.
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
void SetLLDBAssertCallback(LLDBAssertCallback callback)
Replace the LLDB assert callback.
const char * GetVersion()
Retrieves a string representing the complete LLDB version, which includes the lldb version number,...
Definition Version.cpp:38
void SetLLDBErrorLog(Log *log)
Getter and setter for the error log (see g_error_log).
Definition Log.cpp:524
@ eScriptLanguagePython
@ eLanguageTypeObjC_plus_plus
Objective-C++.
@ eLanguageTypeSwift
Swift.
@ eLanguageTypeObjC
Objective-C.
@ eLanguageTypeC_plus_plus
ISO C++:1998.
std::shared_ptr< lldb_private::Debugger > DebuggerSP
std::shared_ptr< lldb_private::TypeCategoryImpl > TypeCategoryImplSP