Skip to content

Commit

Permalink
[Driver] Change the way we deal with local lldbinit files.
Browse files Browse the repository at this point in the history
Currently we have special handling for local lldbinit files in the
driver. At the same time, we have an SB API named
`SourceInitFileInCurrentWorkingDirectory` that does the same thing.

This patch removes the special handling from the driver and uses the API
instead. In addition to the obvious advantages of having one canonical
way of doing things and removing code duplication, this change also
means that the code path is the same for global and local lldb init
files.

Differential revision: https://reviews.llvm.org/D61577

llvm-svn: 360077
  • Loading branch information
JDevlieghere committed May 6, 2019
1 parent d9923bb commit 2edcad7
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 65 deletions.
1 change: 1 addition & 0 deletions lldb/lit/Driver/Inputs/.lldbinit
@@ -0,0 +1 @@
settings set -f frame-format "bogus"
9 changes: 9 additions & 0 deletions lldb/lit/Driver/LocalLLDBInit.test
@@ -0,0 +1,9 @@
# RUN: mkdir -p %t.root
# RUN: cp %S/Inputs/.lldbinit %t.root
# RUN: cd %t.root
# RUN: %lldb-init -o 'settings show frame-format' 2>&1 | FileCheck %s --check-prefix=INIT --check-prefix=CHECK
# RUN: %lldb -o 'settings show frame-format' 2>&1 | FileCheck %s --check-prefix=NOINIT --check-prefix=CHECK

# INIT: There is a .lldbinit file in the current directory which is not being read.
# NOINIT-NOT: There is a .lldbinit file in the current directory which is not being read.
# CHECK-NOT: bogus
5 changes: 5 additions & 0 deletions lldb/lit/helper/toolchain.py
Expand Up @@ -40,6 +40,11 @@ def use_lldb_substitutions(config):
extra_args=['--no-lldbinit', '-S',
os.path.join(config.test_source_root,
'lit-lldb-init')]),
ToolSubst('%lldb-init',
command=FindTool('lldb'),
extra_args=['-S',
os.path.join(config.test_source_root,
'lit-lldb-init')]),
lldbmi,
ToolSubst('%debugserver',
command=FindTool(dsname),
Expand Down
8 changes: 3 additions & 5 deletions lldb/source/Interpreter/CommandInterpreter.cpp
Expand Up @@ -2095,16 +2095,14 @@ void CommandInterpreter::SourceInitFile(bool in_cwd,
CommandReturnObject &result) {
FileSpec init_file;
if (in_cwd) {
ExecutionContext exe_ctx(GetExecutionContext());
Target *target = exe_ctx.GetTargetPtr();
if (target) {
lldb::TargetPropertiesSP properties = Target::GetGlobalProperties();
if (properties) {
// 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;

LoadCWDlldbinitFile should_load =
target->TargetProperties::GetLoadCWDlldbinitFile();
LoadCWDlldbinitFile should_load = properties->GetLoadCWDlldbinitFile();
if (should_load == eLoadCWDlldbinitWarn) {
FileSpec dot_lldb(".lldbinit");
FileSystem::Instance().Resolve(dot_lldb);
Expand Down
62 changes: 10 additions & 52 deletions lldb/tools/driver/Driver.cpp
Expand Up @@ -113,24 +113,6 @@ Driver::Driver()

Driver::~Driver() { g_driver = nullptr; }

void Driver::OptionData::AddLocalLLDBInit() {
// 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[PATH_MAX];
local_lldbinit.GetPath(path, sizeof(path));
InitialCmdEntry entry(path, true, true, true);
m_after_file_commands.push_back(entry);
}
}

void Driver::OptionData::AddInitialCommand(std::string command,
CommandPlacement placement,
bool is_file, SBError &error) {
Expand All @@ -150,17 +132,17 @@ void Driver::OptionData::AddInitialCommand(std::string command,
if (is_file) {
SBFileSpec file(command.c_str());
if (file.Exists())
command_set->push_back(InitialCmdEntry(command, is_file, false));
command_set->push_back(InitialCmdEntry(command, is_file));
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, false));
command_set->push_back(InitialCmdEntry(final_path, is_file));
} else
error.SetErrorStringWithFormat(
"file specified in --source (-s) option doesn't exist: '%s'",
command.c_str());
} else
command_set->push_back(InitialCmdEntry(command, is_file, false));
command_set->push_back(InitialCmdEntry(command, is_file));
}

void Driver::WriteCommandsForSourcing(CommandPlacement placement,
Expand All @@ -181,36 +163,6 @@ void Driver::WriteCommandsForSourcing(CommandPlacement placement,
for (const auto &command_entry : *command_set) {
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 = lldb::SBDebugger::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",
Expand All @@ -227,7 +179,6 @@ void Driver::WriteCommandsForSourcing(CommandPlacement placement,
// user only wanted help or version information.
SBError Driver::ProcessArgs(const opt::InputArgList &args, bool &exiting) {
SBError error;
m_option_data.AddLocalLLDBInit();

// This is kind of a pain, but since we make the debugger in the Driver's
// constructor, we can't know at that point whether we should read in init
Expand Down Expand Up @@ -554,6 +505,13 @@ int Driver::MainLoop() {
result.PutOutput(m_debugger.GetOutputFileHandle());
}

// Source the local .lldbinit file if it exists and we're allowed to source.
// Here we want to always print the return object because it contains the
// warning and instructions to load local lldbinit files.
sb_interpreter.SourceInitFileInCurrentWorkingDirectory(result);
result.PutError(m_debugger.GetErrorFileHandle());
result.PutOutput(m_debugger.GetOutputFileHandle());

// We allow the user to specify an exit code when calling quit which we will
// return when exiting.
m_debugger.GetCommandInterpreter().AllowExitCodeOnQuit(true);
Expand Down
10 changes: 2 additions & 8 deletions lldb/tools/driver/Driver.h
Expand Up @@ -47,24 +47,18 @@ class Driver : public lldb::SBBroadcaster {
lldb::SBStream &strm);

struct OptionData {
void AddLocalLLDBInit();
void AddInitialCommand(std::string command, CommandPlacement placement,
bool is_file, lldb::SBError &error);

struct InitialCmdEntry {
InitialCmdEntry(std::string contents, bool in_is_file,
bool is_cwd_lldbinit_file_read, bool in_quiet = false)
bool in_quiet = false)
: contents(std::move(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 source_quietly;

/// Remember if this is reading the local lldbinit file so we can skip it
/// if not permitted.
bool is_cwd_lldbinit_file_read;
};

std::vector<std::string> m_args;
Expand Down

0 comments on commit 2edcad7

Please sign in to comment.