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 // The dynamic script interpreter plugin references Python data symbols (type
53 // and exception objects), which can't be bound lazily, so Python must be
54 // mapped before loading the plugin. Failure isn't fatal. The plugin itself
55 // is initialized by ScriptInterpreterPython::Initialize, which reports the
56 // cached load result.
57 if (llvm::Expected<ScriptInterpreterRuntimeLoader &> python_loader =
59 llvm::consumeError(python_loader->Load());
60 else
61 llvm::consumeError(python_loader.takeError());
62#endif
63
64 // Initialize LLVM and Clang
65 llvm::InitializeAllTargets();
66 llvm::InitializeAllAsmPrinters();
67 llvm::InitializeAllTargetMCs();
68 llvm::InitializeAllDisassemblers();
69
70 // Initialize the command line parser in LLVM. This usually isn't necessary
71 // as we aren't dealing with command line options here, but otherwise some
72 // other code in Clang/LLVM might be tempted to call this function from a
73 // different thread later on which won't work (as the function isn't
74 // thread-safe).
75 const char *arg0 = "lldb";
76 llvm::cl::ParseCommandLineOptions(1, &arg0);
77
78#define LLDB_PLUGIN(p) LLDB_PLUGIN_INITIALIZE(p);
79#include "Plugins/Plugins.def"
80
81 // Scan for any system or user LLDB plug-ins.
83
84 // The process settings need to know about installed plug-ins, so the
85 // Settings must be initialized AFTER PluginManager::Initialize is called.
87
88 // Use the Debugger's LLDBAssert callback.
90
91 // Use the system log to report errors that would otherwise get dropped.
93
95
96 auto LoadPlugin = [](const lldb::DebuggerSP &debugger_sp,
97 const FileSpec &spec,
98 Status &error) -> llvm::sys::DynamicLibrary {
99 llvm::sys::DynamicLibrary dynlib =
100 llvm::sys::DynamicLibrary::getPermanentLibrary(spec.GetPath().c_str());
101 if (dynlib.isValid()) {
102 typedef bool (*LLDBCommandPluginInit)(lldb::SBDebugger debugger);
103
104 lldb::SBDebugger debugger_sb(debugger_sp);
105 // This calls the bool lldb::PluginInitialize(lldb::SBDebugger debugger)
106 // function.
107 // TODO: mangle this differently for your system - on OSX, the first
108 // underscore needs to be removed and the second one stays
109 LLDBCommandPluginInit init_func =
110 (LLDBCommandPluginInit)(uintptr_t)dynlib.getAddressOfSymbol(
111 "_ZN4lldb16PluginInitializeENS_10SBDebuggerE");
112 if (init_func) {
113 if (init_func(debugger_sb))
114 return dynlib;
115 else
117 "plug-in refused to load "
118 "(lldb::PluginInitialize(lldb::SBDebugger) "
119 "returned false)");
120 } else {
122 "plug-in is missing the required initialization: "
123 "lldb::PluginInitialize(lldb::SBDebugger)");
124 }
125 } else {
126 if (FileSystem::Instance().Exists(spec))
128 "this file does not represent a loadable dylib");
129 else
130 error = Status::FromErrorString("no such file");
131 }
132 return llvm::sys::DynamicLibrary();
133 };
134
135 Debugger::Initialize(LoadPlugin);
136
137 // Warm up DataVisualization and common language categories ahead of time.
138 [[maybe_unused]] lldb::TypeCategoryImplSP entry;
140 entry);
142 entry);
145 return llvm::Error::success();
146}
147
150
152
153 // Terminate plug-ins in core LLDB.
155
156 // Terminate and unload and loaded system or user LLDB plug-ins.
158
159#define LLDB_PLUGIN(p) LLDB_PLUGIN_TERMINATE(p);
160#include "Plugins/Plugins.def"
161
162 // Now shutdown the common parts, in reverse order.
164}
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