diff --git a/lldb/cmake/modules/LLDBConfig.cmake b/lldb/cmake/modules/LLDBConfig.cmake --- a/lldb/cmake/modules/LLDBConfig.cmake +++ b/lldb/cmake/modules/LLDBConfig.cmake @@ -72,6 +72,10 @@ option(LLDB_SKIP_STRIP "Whether to skip stripping of binaries when installing lldb." OFF) option(LLDB_SKIP_DSYM "Whether to skip generating a dSYM when installing lldb." OFF) +set(LLDB_GLOBAL_INIT_DIRECTORY "" CACHE STRING + "Path to the global lldbinit directory. Relative paths are resolved relative to the + directory containing the LLDB library.") + if (LLDB_USE_SYSTEM_DEBUGSERVER) # The custom target for the system debugserver has no install target, so we # need to remove it from the LLVM_DISTRIBUTION_COMPONENTS list. diff --git a/lldb/include/lldb/API/SBCommandInterpreter.h b/lldb/include/lldb/API/SBCommandInterpreter.h --- a/lldb/include/lldb/API/SBCommandInterpreter.h +++ b/lldb/include/lldb/API/SBCommandInterpreter.h @@ -147,6 +147,8 @@ const char *help, const char *syntax, const char *auto_repeat_command); + void SourceInitFileInGlobalDirectory(lldb::SBCommandReturnObject &result); + void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result); void SourceInitFileInHomeDirectory(lldb::SBCommandReturnObject &result, bool is_repl); diff --git a/lldb/include/lldb/Host/Config.h.cmake b/lldb/include/lldb/Host/Config.h.cmake --- a/lldb/include/lldb/Host/Config.h.cmake +++ b/lldb/include/lldb/Host/Config.h.cmake @@ -53,4 +53,6 @@ #define LLDB_LIBDIR_SUFFIX "${LLVM_LIBDIR_SUFFIX}" +#cmakedefine LLDB_GLOBAL_INIT_DIRECTORY R"(${LLDB_GLOBAL_INIT_DIRECTORY})" + #endif // #ifndef LLDB_HOST_CONFIG_H diff --git a/lldb/include/lldb/Interpreter/CommandInterpreter.h b/lldb/include/lldb/Interpreter/CommandInterpreter.h --- a/lldb/include/lldb/Interpreter/CommandInterpreter.h +++ b/lldb/include/lldb/Interpreter/CommandInterpreter.h @@ -253,6 +253,7 @@ void SourceInitFileCwd(CommandReturnObject &result); void SourceInitFileHome(CommandReturnObject &result, bool is_repl); + void SourceInitFileGlobal(CommandReturnObject &result); bool AddCommand(llvm::StringRef name, const lldb::CommandObjectSP &cmd_sp, bool can_replace); diff --git a/lldb/source/API/SBCommandInterpreter.cpp b/lldb/source/API/SBCommandInterpreter.cpp --- a/lldb/source/API/SBCommandInterpreter.cpp +++ b/lldb/source/API/SBCommandInterpreter.cpp @@ -423,6 +423,22 @@ m_opaque_ptr = interpreter; } +void SBCommandInterpreter::SourceInitFileInGlobalDirectory( + SBCommandReturnObject &result) { + LLDB_INSTRUMENT_VA(this, result); + + result.Clear(); + if (IsValid()) { + TargetSP target_sp(m_opaque_ptr->GetDebugger().GetSelectedTarget()); + std::unique_lock lock; + if (target_sp) + lock = std::unique_lock(target_sp->GetAPIMutex()); + m_opaque_ptr->SourceInitFileGlobal(result.ref()); + } else { + result->AppendError("SBCommandInterpreter is not valid"); + } +} + void SBCommandInterpreter::SourceInitFileInHomeDirectory( SBCommandReturnObject &result) { LLDB_INSTRUMENT_VA(this, result); diff --git a/lldb/source/API/SBDebugger.cpp b/lldb/source/API/SBDebugger.cpp --- a/lldb/source/API/SBDebugger.cpp +++ b/lldb/source/API/SBDebugger.cpp @@ -236,6 +236,7 @@ interp.get()->SkipLLDBInitFiles(false); interp.get()->SkipAppInitFiles(false); SBCommandReturnObject result; + interp.SourceInitFileInGlobalDirectory(result); interp.SourceInitFileInHomeDirectory(result, false); } else { interp.get()->SkipLLDBInitFiles(true); diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -2380,6 +2380,21 @@ SourceInitFile(FileSpec(init_file.str()), result); } +void CommandInterpreter::SourceInitFileGlobal(CommandReturnObject &result) { +#ifdef LLDB_GLOBAL_INIT_DIRECTORY + if (!m_skip_lldbinit_files) { + FileSpec init_file(LLDB_GLOBAL_INIT_DIRECTORY); + if (init_file) + init_file.MakeAbsolute(HostInfo::GetShlibDir()); + + init_file.AppendPathComponent("lldbinit"); + SourceInitFile(init_file, result); + return; + } +#endif + result.SetStatus(eReturnStatusSuccessFinishNoResult); +} + const char *CommandInterpreter::GetCommandPrefix() { const char *prefix = GetDebugger().GetIOHandlerCommandPrefix(); return prefix == nullptr ? "" : prefix; diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp --- a/lldb/tools/driver/Driver.cpp +++ b/lldb/tools/driver/Driver.cpp @@ -452,9 +452,14 @@ SBCommandInterpreter sb_interpreter = m_debugger.GetCommandInterpreter(); - // Before we handle any options from the command line, we parse the - // REPL init file or the default file in the user's home directory. + // Process lldbinit files before handling any options from the command line. SBCommandReturnObject result; + sb_interpreter.SourceInitFileInGlobalDirectory(result); + if (m_option_data.m_debug_mode) { + result.PutError(m_debugger.GetErrorFile()); + result.PutOutput(m_debugger.GetOutputFile()); + } + sb_interpreter.SourceInitFileInHomeDirectory(result, m_option_data.m_repl); if (m_option_data.m_debug_mode) { result.PutError(m_debugger.GetErrorFile());