Skip to content

Commit eb501b2

Browse files
authored
[lldb] Still echo the command if we print the error. (#171931)
When the command interpreter is asked to not echo commands but still print errors, a user has no idea what command caused the error. For example, when I add `bogus` in my `~/.lldbinit`: ``` $ lldb error: 'bogus' is not a valid command. ``` Things are even more confusing when we have inline diagnostics, which point to nothing. For example, when I add `settings set target.run-args -foo` to my `~/.lldbinit`: ``` ❯ lldb ˄˜˜˜ ╰─ error: unknown or ambiguous option ``` We should still echo the command if the command fails, making it obvious which command caused the failure and fixing the inline diagnostics. Fixes #171514
1 parent 47b4c6a commit eb501b2

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3371,14 +3371,15 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
33713371
if (line.empty())
33723372
return;
33733373
}
3374+
bool echoed_command = false;
33743375
if (!is_interactive) {
33753376
// When using a non-interactive file handle (like when sourcing commands
33763377
// from a file) we need to echo the command out so we don't just see the
33773378
// command output and no command...
33783379
if (EchoCommandNonInteractive(line, io_handler.GetFlags())) {
3379-
LockedStreamFile locked_stream =
3380-
io_handler.GetOutputStreamFileSP()->Lock();
3381-
locked_stream.Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3380+
io_handler.GetOutputStreamFileSP()->Lock()
3381+
<< io_handler.GetPrompt() << line << '\n';
3382+
echoed_command = true;
33823383
}
33833384
}
33843385

@@ -3398,10 +3399,21 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
33983399
lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
33993400
HandleCommand(line.c_str(), eLazyBoolCalculate, result);
34003401

3401-
// Now emit the command output text from the command we just executed
3402-
if ((result.Succeeded() &&
3403-
io_handler.GetFlags().Test(eHandleCommandFlagPrintResult)) ||
3404-
io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors)) {
3402+
const bool print_result =
3403+
result.Succeeded() &&
3404+
io_handler.GetFlags().Test(eHandleCommandFlagPrintResult);
3405+
const bool print_error =
3406+
io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors);
3407+
3408+
// Now emit the command output text from the command we just executed.
3409+
if (print_result || print_error) {
3410+
// If the command failed and we didn't echo it, echo it now so the user
3411+
// knows which command produced the error.
3412+
if (!echoed_command && !result.Succeeded() && print_error) {
3413+
io_handler.GetOutputStreamFileSP()->Lock()
3414+
<< io_handler.GetPrompt() << line << '\n';
3415+
}
3416+
34053417
auto DefaultPrintCallback = [&](const CommandReturnObject &result) {
34063418
// Display any inline diagnostics first.
34073419
const bool inline_diagnostics = !result.GetImmediateErrorStream() &&
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# This should succeed and not be echoed.
2+
expr 1+2
3+
# This should fail and be echoed.
4+
bogus_command
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Test that failed commands are echoed even when echoing is disabled.
2+
# This ensures users can see which command produced an error.
3+
4+
RUN: mkdir -p %t.home
5+
RUN: cp %S/Inputs/FailedCommand.in %t.home/.lldbinit
6+
RUN: env HOME=%t.home %lldb-init -b 2>&1 | FileCheck %s
7+
8+
CHECK-NOT: expr 1+2
9+
CHECK: (lldb) bogus_command
10+
CHECK: error: 'bogus_command' is not a valid command

0 commit comments

Comments
 (0)