Skip to content

Commit 6c99a34

Browse files
committed
[lldb] Add support for a "global" lldbinit file
This patch adds introduces a new kind of an lldbinit file. Unlike the lldbinit in the home directory (useful for customizing lldb to the needs of a particular user), or the cwd lldbinit file (useful for project-specific settings), this file can be used to customize an entire lldb installation to a particular environment. The feature is enabled at build time, by setting the LLDB_GLOBAL_INIT_DIRECTORY variable to a path to a directory which should contain an "lldbinit" file. Lldb will then load the file at startup, if it exists, and if automatic init loading has not been disabled. Relative paths will be resolved (at runtime) relative to the location of the lldb library (liblldb or LLDB.framework). The system-wide lldbinit file will be loaded first, before any $HOME/.lldbinit and $CWD/.lldbinit files are processed, so that those can override any system-wide settings. More information can be found on the RFC thread at <https://discourse.llvm.org/t/rfc-system-wide-lldbinit/59933>. Differential Revision: https://reviews.llvm.org/D119831
1 parent de2c0a2 commit 6c99a34

File tree

8 files changed

+48
-2
lines changed

8 files changed

+48
-2
lines changed

lldb/cmake/modules/LLDBConfig.cmake

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ option(LLDB_USE_SYSTEM_DEBUGSERVER "Use the system's debugserver for testing (Da
7272
option(LLDB_SKIP_STRIP "Whether to skip stripping of binaries when installing lldb." OFF)
7373
option(LLDB_SKIP_DSYM "Whether to skip generating a dSYM when installing lldb." OFF)
7474

75+
set(LLDB_GLOBAL_INIT_DIRECTORY "" CACHE STRING
76+
"Path to the global lldbinit directory. Relative paths are resolved relative to the
77+
directory containing the LLDB library.")
78+
7579
if (LLDB_USE_SYSTEM_DEBUGSERVER)
7680
# The custom target for the system debugserver has no install target, so we
7781
# need to remove it from the LLVM_DISTRIBUTION_COMPONENTS list.

lldb/include/lldb/API/SBCommandInterpreter.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ class SBCommandInterpreter {
147147
const char *help, const char *syntax,
148148
const char *auto_repeat_command);
149149

150+
void SourceInitFileInGlobalDirectory(lldb::SBCommandReturnObject &result);
151+
150152
void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result);
151153
void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result,
152154
bool is_repl);

lldb/include/lldb/Host/Config.h.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,6 @@
5353

5454
#define LLDB_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}"
5555

56+
#cmakedefine LLDB_GLOBAL_INIT_DIRECTORY R"(${LLDB_GLOBAL_INIT_DIRECTORY})"
57+
5658
#endif // #ifndef LLDB_HOST_CONFIG_H

lldb/include/lldb/Interpreter/CommandInterpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ class CommandInterpreter : public Broadcaster,
253253

254254
void SourceInitFileCwd(CommandReturnObject &result);
255255
void SourceInitFileHome(CommandReturnObject &result, bool is_repl);
256+
void SourceInitFileGlobal(CommandReturnObject &result);
256257

257258
bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp,
258259
bool can_replace);

lldb/source/API/SBCommandInterpreter.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,22 @@ void SBCommandInterpreter::reset(
423423
m_opaque_ptr = interpreter;
424424
}
425425

426+
void SBCommandInterpreter::SourceInitFileInGlobalDirectory(
427+
SBCommandReturnObject &result) {
428+
LLDB_INSTRUMENT_VA(this, result);
429+
430+
result.Clear();
431+
if (IsValid()) {
432+
TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget());
433+
std::unique_lock<std::recursive_mutex> lock;
434+
if (target_sp)
435+
lock = std::unique_lock<std::recursive_mutex>(target_sp->GetAPIMutex());
436+
m_opaque_ptr->SourceInitFileGlobal(result.ref());
437+
} else {
438+
result->AppendError("SBCommandInterpreter is not valid");
439+
}
440+
}
441+
426442
void SBCommandInterpreter::SourceInitFileInHomeDirectory(
427443
SBCommandReturnObject &result) {
428444
LLDB_INSTRUMENT_VA(this, result);

lldb/source/API/SBDebugger.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ SBDebugger SBDebugger::Create(bool source_init_files,
236236
interp.get()->SkipLLDBInitFiles(false);
237237
interp.get()->SkipAppInitFiles(false);
238238
SBCommandReturnObject result;
239+
interp.SourceInitFileInGlobalDirectory(result);
239240
interp.SourceInitFileInHomeDirectory(result, false);
240241
} else {
241242
interp.get()->SkipLLDBInitFiles(true);

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2380,6 +2380,21 @@ void CommandInterpreter::SourceInitFileHome(CommandReturnObject &result,
23802380
SourceInitFile(FileSpec(init_file.str()), result);
23812381
}
23822382

2383+
void CommandInterpreter::SourceInitFileGlobal(CommandReturnObject &result) {
2384+
#ifdef LLDB_GLOBAL_INIT_DIRECTORY
2385+
if (!m_skip_lldbinit_files) {
2386+
FileSpec init_file(LLDB_GLOBAL_INIT_DIRECTORY);
2387+
if (init_file)
2388+
init_file.MakeAbsolute(HostInfo::GetShlibDir());
2389+
2390+
init_file.AppendPathComponent("lldbinit");
2391+
SourceInitFile(init_file, result);
2392+
return;
2393+
}
2394+
#endif
2395+
result.SetStatus(eReturnStatusSuccessFinishNoResult);
2396+
}
2397+
23832398
const char *CommandInterpreter::GetCommandPrefix() {
23842399
const char *prefix = GetDebugger().GetIOHandlerCommandPrefix();
23852400
return prefix == nullptr ? "" : prefix;

lldb/tools/driver/Driver.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,14 @@ int Driver::MainLoop() {
452452

453453
SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter();
454454

455-
// Before we handle any options from the command line, we parse the
456-
// REPL init file or the default file in the user's home directory.
455+
// Process lldbinit files before handling any options from the command line.
457456
SBCommandReturnObject result;
457+
sb_interpreter.SourceInitFileInGlobalDirectory(result);
458+
if (m_option_data.m_debug_mode) {
459+
result.PutError(m_debugger.GetErrorFile());
460+
result.PutOutput(m_debugger.GetOutputFile());
461+
}
462+
458463
sb_interpreter.SourceInitFileInHomeDirectory(result, m_option_data.m_repl);
459464
if (m_option_data.m_debug_mode) {
460465
result.PutError(m_debugger.GetErrorFile());

0 commit comments

Comments
 (0)