diff options
-rw-r--r-- | lldb/include/lldb/API/SBFileSpec.h | 3 | ||||
-rw-r--r-- | lldb/include/lldb/API/SBHostOS.h | 3 | ||||
-rw-r--r-- | lldb/include/lldb/Target/Target.h | 10 | ||||
-rw-r--r-- | lldb/scripts/interface/SBFileSpec.i | 5 | ||||
-rw-r--r-- | lldb/scripts/interface/SBHostOS.i | 3 | ||||
-rw-r--r-- | lldb/source/API/SBFileSpec.cpp | 6 | ||||
-rw-r--r-- | lldb/source/API/SBHostOS.cpp | 16 | ||||
-rw-r--r-- | lldb/source/Interpreter/CommandInterpreter.cpp | 42 | ||||
-rw-r--r-- | lldb/source/Target/Target.cpp | 18 | ||||
-rw-r--r-- | lldb/tools/driver/Driver.cpp | 52 | ||||
-rw-r--r-- | lldb/tools/driver/Driver.h | 6 |
11 files changed, 148 insertions, 16 deletions
diff --git a/lldb/include/lldb/API/SBFileSpec.h b/lldb/include/lldb/API/SBFileSpec.h index d6f38f5b2d7..2f9d6bab89f 100644 --- a/lldb/include/lldb/API/SBFileSpec.h +++ b/lldb/include/lldb/API/SBFileSpec.h @@ -60,6 +60,9 @@ public: bool GetDescription (lldb::SBStream &description) const; + void + AppendPathComponent (const char *file_or_directory); + private: friend class SBAttachInfo; friend class SBBlock; diff --git a/lldb/include/lldb/API/SBHostOS.h b/lldb/include/lldb/API/SBHostOS.h index d9bc9736563..a3675856a13 100644 --- a/lldb/include/lldb/API/SBHostOS.h +++ b/lldb/include/lldb/API/SBHostOS.h @@ -28,6 +28,9 @@ public: static lldb::SBFileSpec GetLLDBPath (lldb::PathType path_type); + static lldb::SBFileSpec + GetUserHomeDirectory (); + static void ThreadCreated (const char *name); diff --git a/lldb/include/lldb/Target/Target.h b/lldb/include/lldb/Target/Target.h index 0cdb248a9b4..5932dda4619 100644 --- a/lldb/include/lldb/Target/Target.h +++ b/lldb/include/lldb/Target/Target.h @@ -53,6 +53,13 @@ typedef enum LoadScriptFromSymFile eLoadScriptFromSymFileWarn } LoadScriptFromSymFile; +typedef enum LoadCWDlldbinitFile +{ + eLoadCWDlldbinitTrue, + eLoadCWDlldbinitFalse, + eLoadCWDlldbinitWarn +} LoadCWDlldbinitFile; + //---------------------------------------------------------------------- // TargetProperties //---------------------------------------------------------------------- @@ -192,6 +199,9 @@ public: LoadScriptFromSymFile GetLoadScriptFromSymbolFile() const; + LoadCWDlldbinitFile + GetLoadCWDlldbinitFile () const; + Disassembler::HexImmediateStyle GetHexImmediateStyle() const; diff --git a/lldb/scripts/interface/SBFileSpec.i b/lldb/scripts/interface/SBFileSpec.i index c153f2bd86f..a0e5da21187 100644 --- a/lldb/scripts/interface/SBFileSpec.i +++ b/lldb/scripts/interface/SBFileSpec.i @@ -72,7 +72,10 @@ public: bool GetDescription (lldb::SBStream &description) const; - + + void + AppendPathComponent (const char *file_or_directory); + %pythoncode %{ def __get_fullpath__(self): spec_dir = self.GetDirectory() diff --git a/lldb/scripts/interface/SBHostOS.i b/lldb/scripts/interface/SBHostOS.i index d9f42160bf0..ed2e8b0477b 100644 --- a/lldb/scripts/interface/SBHostOS.i +++ b/lldb/scripts/interface/SBHostOS.i @@ -22,6 +22,9 @@ public: static lldb::SBFileSpec GetLLDBPath (lldb::PathType path_type); + static lldb::SBFileSpec + GetUserHomeDirectory (); + static void ThreadCreated (const char *name); diff --git a/lldb/source/API/SBFileSpec.cpp b/lldb/source/API/SBFileSpec.cpp index dd7435de1b5..23bc5bc8eb6 100644 --- a/lldb/source/API/SBFileSpec.cpp +++ b/lldb/source/API/SBFileSpec.cpp @@ -211,3 +211,9 @@ SBFileSpec::GetDescription (SBStream &description) const strm.PutCString (path); return true; } + +void +SBFileSpec::AppendPathComponent (const char *fn) +{ + m_opaque_ap->AppendPathComponent (fn); +} diff --git a/lldb/source/API/SBHostOS.cpp b/lldb/source/API/SBHostOS.cpp index 008ca4d9672..cd3cff3b2e9 100644 --- a/lldb/source/API/SBHostOS.cpp +++ b/lldb/source/API/SBHostOS.cpp @@ -17,6 +17,9 @@ #include "lldb/Host/HostThread.h" #include "lldb/Host/ThreadLauncher.h" +#include "llvm/Support/Path.h" +#include "llvm/ADT/SmallVector.h" + using namespace lldb; using namespace lldb_private; @@ -53,6 +56,19 @@ SBHostOS::GetLLDBPath (lldb::PathType path_type) return sb_fspec; } +SBFileSpec +SBHostOS::GetUserHomeDirectory () +{ + SBFileSpec sb_fspec; + + llvm::SmallString<64> home_dir_path; + llvm::sys::path::home_directory (home_dir_path); + FileSpec homedir (home_dir_path.c_str(), true); + + sb_fspec.SetFileSpec (homedir); + return sb_fspec; +} + lldb::thread_t SBHostOS::ThreadCreate ( diff --git a/lldb/source/Interpreter/CommandInterpreter.cpp b/lldb/source/Interpreter/CommandInterpreter.cpp index bab12462489..e31b3367720 100644 --- a/lldb/source/Interpreter/CommandInterpreter.cpp +++ b/lldb/source/Interpreter/CommandInterpreter.cpp @@ -2319,12 +2319,44 @@ CommandInterpreter::SourceInitFile (bool in_cwd, CommandReturnObject &result) FileSpec init_file; if (in_cwd) { - // In the current working directory we don't load any program specific - // .lldbinit files, we only look for a "./.lldbinit" file. - if (m_skip_lldbinit_files) - return; + ExecutionContext exe_ctx(GetExecutionContext()); + Target *target = exe_ctx.GetTargetPtr(); + if (target) + { + // In the current working directory we don't load any program specific + // .lldbinit files, we only look for a ".lldbinit" file. + if (m_skip_lldbinit_files) + return; - init_file.SetFile ("./.lldbinit", true); + LoadCWDlldbinitFile should_load = target->TargetProperties::GetLoadCWDlldbinitFile (); + if (should_load == eLoadCWDlldbinitWarn) + { + FileSpec dot_lldb (".lldbinit", true); + llvm::SmallString<64> home_dir_path; + llvm::sys::path::home_directory (home_dir_path); + FileSpec homedir_dot_lldb (home_dir_path.c_str(), false); + homedir_dot_lldb.AppendPathComponent (".lldbinit"); + homedir_dot_lldb.ResolvePath (); + if (dot_lldb.Exists () + && dot_lldb.GetDirectory() != homedir_dot_lldb.GetDirectory()) + { + result.AppendErrorWithFormat ( + "There is a .lldbinit file in the current directory which is not being read.\n" + "To silence this warning without sourcing in the local .lldbinit,\n" + "add the following to the lldbinit file in your home directory:\n" + " settings set target.load-cwd-lldbinit false\n" + "To allow lldb to source .lldbinit files in the current working directory,\n" + "set the value of this variable to true. Only do so if you understand and\n" + "accept the security risk."); + result.SetStatus (eReturnStatusFailed); + return; + } + } + else if (should_load == eLoadCWDlldbinitTrue) + { + init_file.SetFile ("./.lldbinit", true); + } + } } else { diff --git a/lldb/source/Target/Target.cpp b/lldb/source/Target/Target.cpp index 88083e0d342..7d3543cd524 100644 --- a/lldb/source/Target/Target.cpp +++ b/lldb/source/Target/Target.cpp @@ -3363,6 +3363,15 @@ g_load_script_from_sym_file_values[] = }; static OptionEnumValueElement +g_load_current_working_dir_lldbinit_values[] = +{ + { eLoadCWDlldbinitTrue, "true", "Load .lldbinit files from current directory"}, + { eLoadCWDlldbinitFalse, "false", "Do not load .lldbinit files from current directory"}, + { eLoadCWDlldbinitWarn, "warn", "Warn about loading .lldbinit files from current directory"}, + { 0, nullptr, nullptr } +}; + +static OptionEnumValueElement g_memory_module_load_level_values[] = { { eMemoryModuleLoadLevelMinimal, "minimal" , "Load minimal information when loading modules from memory. Currently this setting loads sections only."}, @@ -3418,6 +3427,7 @@ g_properties[] = { "hex-immediate-style" , OptionValue::eTypeEnum , false, Disassembler::eHexStyleC, nullptr, g_hex_immediate_style_values, "Which style to use for printing hexadecimal disassembly values." }, { "use-fast-stepping" , OptionValue::eTypeBoolean , false, true, nullptr, nullptr, "Use a fast stepping algorithm based on running from branch to branch rather than instruction single-stepping." }, { "load-script-from-symbol-file" , OptionValue::eTypeEnum , false, eLoadScriptFromSymFileWarn, nullptr, g_load_script_from_sym_file_values, "Allow LLDB to load scripting resources embedded in symbol files when available." }, + { "load-cwd-lldbinit" , OptionValue::eTypeEnum , false, eLoadCWDlldbinitWarn, nullptr, g_load_current_working_dir_lldbinit_values, "Allow LLDB to .lldbinit files from the current directory automatically." }, { "memory-module-load-level" , OptionValue::eTypeEnum , false, eMemoryModuleLoadLevelComplete, nullptr, g_memory_module_load_level_values, "Loading modules from memory can be slow as reading the symbol tables and other data can take a long time depending on your connection to the debug target. " "This setting helps users control how much information gets loaded when loading modules from memory." @@ -3465,6 +3475,7 @@ enum ePropertyHexImmediateStyle, ePropertyUseFastStepping, ePropertyLoadScriptFromSymbolFile, + ePropertyLoadCWDlldbinitFile, ePropertyMemoryModuleLoadLevel, ePropertyDisplayExpressionsInCrashlogs, ePropertyTrapHandlerNames, @@ -3945,6 +3956,13 @@ TargetProperties::GetLoadScriptFromSymbolFile () const return (LoadScriptFromSymFile)m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value); } +LoadCWDlldbinitFile +TargetProperties::GetLoadCWDlldbinitFile () const +{ + const uint32_t idx = ePropertyLoadCWDlldbinitFile; + return (LoadCWDlldbinitFile) m_collection_sp->GetPropertyAtIndexAsEnumeration(nullptr, idx, g_properties[idx].default_uint_value); +} + Disassembler::HexImmediateStyle TargetProperties::GetHexImmediateStyle () const { diff --git a/lldb/tools/driver/Driver.cpp b/lldb/tools/driver/Driver.cpp index ffd551679e3..71cefbd305b 100644 --- a/lldb/tools/driver/Driver.cpp +++ b/lldb/tools/driver/Driver.cpp @@ -38,6 +38,7 @@ #include "lldb/API/SBLanguageRuntime.h" #include "lldb/API/SBListener.h" #include "lldb/API/SBStream.h" +#include "lldb/API/SBStringList.h" #include "lldb/API/SBTarget.h" #include "lldb/API/SBThread.h" #include "lldb/API/SBProcess.h" @@ -441,13 +442,24 @@ Driver::OptionData::Clear () m_script_lang = lldb::eScriptLanguageDefault; m_initial_commands.clear (); m_after_file_commands.clear (); - // If there is a local .lldbinit, source that: - SBFileSpec local_lldbinit("./.lldbinit", true); - if (local_lldbinit.Exists()) + + // If there is a local .lldbinit, add that to the + // list of things to be sourced, if the settings + // permit it. + SBFileSpec local_lldbinit (".lldbinit", true); + + SBFileSpec homedir_dot_lldb = SBHostOS::GetUserHomeDirectory(); + homedir_dot_lldb.AppendPathComponent (".lldbinit"); + + // Only read .lldbinit in the current working directory + // if it's not the same as the .lldbinit in the home + // directory (which is already being read in). + if (local_lldbinit.Exists() + && strcmp (local_lldbinit.GetDirectory(), homedir_dot_lldb.GetDirectory()) != 0) { char path[2048]; local_lldbinit.GetPath(path, 2047); - InitialCmdEntry entry(path, true, true); + InitialCmdEntry entry(path, true, true, true); m_after_file_commands.push_back (entry); } @@ -486,18 +498,18 @@ Driver::OptionData::AddInitialCommand (const char *command, CommandPlacement pla { SBFileSpec file(command); if (file.Exists()) - command_set->push_back (InitialCmdEntry(command, is_file)); + command_set->push_back (InitialCmdEntry(command, is_file, false)); else if (file.ResolveExecutableLocation()) { char final_path[PATH_MAX]; file.GetPath (final_path, sizeof(final_path)); - command_set->push_back (InitialCmdEntry(final_path, is_file)); + command_set->push_back (InitialCmdEntry(final_path, is_file, false)); } else error.SetErrorStringWithFormat("file specified in --source (-s) option doesn't exist: '%s'", optarg); } else - command_set->push_back (InitialCmdEntry(command, is_file)); + command_set->push_back (InitialCmdEntry(command, is_file, false)); } void @@ -550,6 +562,30 @@ Driver::WriteCommandsForSourcing (CommandPlacement placement, SBStream &strm) const char *command = command_entry.contents.c_str(); if (command_entry.is_file) { + // If this command_entry is a file to be sourced, and it's the ./.lldbinit file (the .lldbinit + // file in the current working directory), only read it if target.load-cwd-lldbinit is 'true'. + if (command_entry.is_cwd_lldbinit_file_read) + { + SBStringList strlist = m_debugger.GetInternalVariableValue ("target.load-cwd-lldbinit", + m_debugger.GetInstanceName()); + if (strlist.GetSize() == 1 && strcmp (strlist.GetStringAtIndex(0), "warn") == 0) + { + FILE *output = m_debugger.GetOutputFileHandle (); + ::fprintf (output, + "There is a .lldbinit file in the current directory which is not being read.\n" + "To silence this warning without sourcing in the local .lldbinit,\n" + "add the following to the lldbinit file in your home directory:\n" + " settings set target.load-cwd-lldbinit false\n" + "To allow lldb to source .lldbinit files in the current working directory,\n" + "set the value of this variable to true. Only do so if you understand and\n" + "accept the security risk.\n"); + return; + } + if (strlist.GetSize() == 1 && strcmp (strlist.GetStringAtIndex(0), "false") == 0) + { + return; + } + } bool source_quietly = m_option_data.m_source_quietly || command_entry.source_quietly; strm.Printf("command source -s %i '%s'\n", source_quietly, command); } @@ -1033,7 +1069,7 @@ Driver::MainLoop () SBStream commands_stream; // First source in the commands specified to be run before the file arguments are processed. - WriteCommandsForSourcing(eCommandPlacementBeforeFile, commands_stream); + WriteCommandsForSourcing (eCommandPlacementBeforeFile, commands_stream); const size_t num_args = m_option_data.m_args.size(); if (num_args > 0) diff --git a/lldb/tools/driver/Driver.h b/lldb/tools/driver/Driver.h index 639ac41d7fe..8ac59240bc2 100644 --- a/lldb/tools/driver/Driver.h +++ b/lldb/tools/driver/Driver.h @@ -81,14 +81,16 @@ public: struct InitialCmdEntry { - InitialCmdEntry (const char *in_contents, bool in_is_file, bool in_quiet = false) : + InitialCmdEntry (const char *in_contents, bool in_is_file, bool is_cwd_lldbinit_file_read, bool in_quiet = false) : contents (in_contents), is_file (in_is_file), - source_quietly(in_quiet) + is_cwd_lldbinit_file_read (is_cwd_lldbinit_file_read), + source_quietly (in_quiet) {} std::string contents; bool is_file; + bool is_cwd_lldbinit_file_read; // if this is reading ./.lldbinit - so we may skip if not permitted bool source_quietly; }; |