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"
15#include "lldb/Host/Config.h"
16#include "lldb/Host/Host.h"
20#include "lldb/Utility/Timer.h"
22#include "llvm/Support/CommandLine.h"
23#include "llvm/Support/TargetSelect.h"
24
25#if LLDB_ENABLE_PYTHON
28#endif
29
30#pragma clang diagnostic push
31#pragma clang diagnostic ignored "-Wglobal-constructors"
32#include "llvm/ExecutionEngine/MCJIT.h"
33#pragma clang diagnostic pop
34
35#include <string>
36
37#define LLDB_PLUGIN(p) LLDB_PLUGIN_DECLARE(p)
38#include "Plugins/Plugins.def"
39
40using namespace lldb_private;
41
44
47 if (error)
48 return error;
49
50#if LLDB_ENABLE_PYTHON
51 // Map libpython into the process before any code that might reference it
52 // runs. This is required by both the static script interpreter (whose
53 // Initialize() invokes Python via the LLDB_PLUGIN_INITIALIZE loop below)
54 // and the dynamic plugin (whose dlopen needs Python's symbols visible in
55 // the process). The loader is once_flag-cached and a no-op when libpython
56 // is already in the process (e.g. `import lldb` from Python).
57 llvm::Expected<ScriptInterpreterRuntimeLoader &> python_loader =
59 if (!python_loader)
60 return python_loader.takeError();
61 if (llvm::Error err = python_loader->Load())
62 return err;
63#endif
64
65 // Initialize LLVM and Clang
66 llvm::InitializeAllTargets();
67 llvm::InitializeAllAsmPrinters();
68 llvm::InitializeAllTargetMCs();
69 llvm::InitializeAllDisassemblers();
70
71 // Initialize the command line parser in LLVM. This usually isn't necessary
72 // as we aren't dealing with command line options here, but otherwise some
73 // other code in Clang/LLVM might be tempted to call this function from a
74 // different thread later on which won't work (as the function isn't
75 // thread-safe).
76 const char *arg0 = "lldb";
77 llvm::cl::ParseCommandLineOptions(1, &arg0);
78
79#define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
80#include "Plugins/Plugins.def"
81
82 // Scan for any system or user LLDB plug-ins.
84
85 // The process settings need to know about installed plug-ins, so the
86 // Settings must be initialized AFTER PluginManager::Initialize is called.
88
89 // Use the Debugger's LLDBAssert callback.
91
92 // Use the system log to report errors that would otherwise get dropped.
94
96
97 auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
98 const FileSpec &spec,
99 Status &error) -> llvm::sys::DynamicLibrary {
100 llvm::sys::DynamicLibrary dynlib =
101 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
102 if (dynlib.isValid()) {
103 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);
104
105 lldb::SBDebugger debugger_sb(debugger_sp);
106 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
107 // function.
108 // TODO: mangle this differently for your system - on OSX, the first
109 // underscore needs to be removed and the second one stays
110 LLDBCommandPluginInit init_func =
111 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
112 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
113 if (init_func) {
114 if (init_func(debugger_sb))
115 return dynlib;
116 else
118 "plug-in refused to load "
119 "(lldb::PluginInitialize(lldb::SBDebugger) "
120 "returned false)");
121 } else {
123 "plug-in is missing the required initialization: "
124 "lldb::PluginInitialize(lldb::SBDebugger)");
125 }
126 } else {
127 if (FileSystem::Instance().Exists(spec))
129 "this file does not represent a loadable dylib");
130 else
131 error = Status::FromErrorString("no such file");
132 }
133 return llvm::sys::DynamicLibrary();
134 };
135
136 Debugger::Initialize(LoadPlugin);
137
138 return llvm::Error::success();
139}
140
143
145
146 // Terminate plug-ins in core LLDB.
148
149 // Terminate and unload and loaded system or user LLDB plug-ins.
151
152#define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
153#include "Plugins/Plugins.def"
154
155 // Now shutdown the common parts, in reverse order.
157}
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:364
static void AssertCallback(llvm::StringRef message, llvm::StringRef backtrace, llvm::StringRef prompt)
static void Terminate()
Definition Debugger.cpp:798
static void SettingsInitialize()
Definition Debugger.cpp:827
static void Initialize(LoadPluginCallbackType load_plugin_callback)
Definition Debugger.cpp:789
static void SettingsTerminate()
Definition Debugger.cpp:829
A file utility class.
Definition FileSpec.h:57
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:327
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:442
@ eScriptLanguagePython
std::shared_ptr< lldb_private::Debugger > DebuggerSP