Update the lldb driver to support the -O and -S options when passing --repl

At the moment when --repl is passed to lldb it silently ignores any
commands passed via the options below:

--one-line-before-file <command>
                     Tells the debugger to execute this one-line lldb command before any file provided on the command line has been loaded.
--one-line <command>
                     Tells the debugger to execute this one-line lldb command after any file provided on the command line has been loaded.
--source-before-file <file>
                     Tells the debugger to read in and execute the lldb commands in the given file, before any file has been loaded.
--source <file>
                     Tells the debugger to read in and execute the lldb commands in the given file, after any file has been loaded.
-O <value>           Alias for --one-line-before-file
-o <value>           Alias for --one-line
-S <value>           Alias for --source-before-file
-s <value>           Alias for --source

The -O and -S options are quite useful when writing tests for the REPL
though, e.g. to change settings prior to entering REPL mode. This
patch updates the driver to still respect the commands supplied via -O
and -S when passing --repl instead of silently ignoring them. As -s
and -o don't really make sense in REPL mode, commands supplied via
those options are still ignored, but the driver now emits a warning to
make that clear to the user.

Patch by Nathan Hawes!

Differential Revision: https://reviews.llvm.org/D59681

llvm-svn: 356911
This commit is contained in:
Adrian Prantl 2019-03-25 15:38:18 +00:00
parent 3a22c3cc2b
commit 040f94cc7e
2 changed files with 138 additions and 119 deletions

View File

@ -0,0 +1,7 @@
# RUN: echo ':quit' | %lldb -x --repl -O 'expr 42' -S %S/Inputs/Print2.in -o 'expr 999999' -s %s 2>&1 | FileCheck %s
# CHECK: {{w}}arning: commands specified to run after file load (via -o or -s) are ignored in REPL mode
# CHECK: (int) $0 = 42
# CHECK: (int) $1 = 2
# CHECK-NOT: 999999
expr 999999

View File

@ -609,6 +609,9 @@ int Driver::MainLoop() {
// are processed.
WriteCommandsForSourcing(eCommandPlacementBeforeFile, commands_stream);
// If we're not in --repl mode, add the commands to process the file
// arguments, and the commands specified to run afterwards.
if (!m_option_data.m_repl) {
const size_t num_args = m_option_data.m_args.size();
if (num_args > 0) {
char arch_name[64];
@ -650,6 +653,11 @@ int Driver::MainLoop() {
}
WriteCommandsForSourcing(eCommandPlacementAfterFile, commands_stream);
} else if (!m_option_data.m_after_file_commands.empty()) {
// We're in repl mode and after-file-load commands were specified.
WithColor::warning() << "commands specified to run after file load (via -o "
"or -s) are ignored in REPL mode.\n";
}
if (GetDebugMode()) {
result.PutError(m_debugger.GetErrorFileHandle());
@ -659,19 +667,6 @@ int Driver::MainLoop() {
bool handle_events = true;
bool spawn_thread = false;
if (m_option_data.m_repl) {
const char *repl_options = nullptr;
if (!m_option_data.m_repl_options.empty())
repl_options = m_option_data.m_repl_options.c_str();
SBError error(m_debugger.RunREPL(m_option_data.m_repl_lang, repl_options));
if (error.Fail()) {
const char *error_cstr = error.GetCString();
if ((error_cstr != nullptr) && (error_cstr[0] != 0))
WithColor::error() << error_cstr << '\n';
else
WithColor::error() << error.GetError() << '\n';
}
} else {
// Check if we have any data in the commands stream, and if so, save it to a
// temp file
// so we can then run the command interpreter using the file contents.
@ -742,7 +737,7 @@ int Driver::MainLoop() {
}
// Now set the input file handle to STDIN and run the command
// interpreter again in interactive mode and let the debugger
// interpreter again in interactive mode or repl mode and let the debugger
// take ownership of stdin
bool go_interactive = true;
@ -753,6 +748,20 @@ int Driver::MainLoop() {
if (go_interactive) {
m_debugger.SetInputFileHandle(stdin, true);
if (m_option_data.m_repl) {
const char *repl_options = nullptr;
if (!m_option_data.m_repl_options.empty())
repl_options = m_option_data.m_repl_options.c_str();
SBError error(m_debugger.RunREPL(m_option_data.m_repl_lang, repl_options));
if (error.Fail()) {
const char *error_cstr = error.GetCString();
if ((error_cstr != nullptr) && (error_cstr[0] != 0))
WithColor::error() << error_cstr << '\n';
else
WithColor::error() << error.GetError() << '\n';
}
} else {
m_debugger.RunCommandInterpreter(handle_events, spawn_thread);
}
}
@ -838,13 +847,16 @@ EXAMPLES:
lldb -c /path/to/core
Command options can be combined with either mode and cause lldb to run the
Command options can be combined with these modes and cause lldb to run the
specified commands before or after events, like loading the file or crashing,
in the order provided on the command line.
lldb -O 'settings set stop-disassembly-count 20' -o 'run' -o 'bt'
lldb -S /source/before/file -s /source/after/file
lldb -K /source/before/crash -k /source/after/crash
Note: In REPL mode no file is loaded, so commands specified to run after
loading the file (via -o or -s) will be ignored.
)___";
llvm::outs() << examples;
}