Skip to content

Commit e36c407

Browse files
committed
[lldb] Still echo the command if we print the error.
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 c5ac7d6 commit e36c407

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3283,6 +3283,7 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
32833283
if (line.empty())
32843284
return;
32853285
}
3286+
bool echoed_command = false;
32863287
if (!is_interactive) {
32873288
// When using a non-interactive file handle (like when sourcing commands
32883289
// from a file) we need to echo the command out so we don't just see the
@@ -3291,6 +3292,7 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
32913292
LockedStreamFile locked_stream =
32923293
io_handler.GetOutputStreamFileSP()->Lock();
32933294
locked_stream.Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3295+
echoed_command = true;
32943296
}
32953297
}
32963298

@@ -3310,10 +3312,22 @@ void CommandInterpreter::IOHandlerInputComplete(IOHandler &io_handler,
33103312
lldb_private::CommandReturnObject result(m_debugger.GetUseColor());
33113313
HandleCommand(line.c_str(), eLazyBoolCalculate, result);
33123314

3313-
// Now emit the command output text from the command we just executed
3314-
if ((result.Succeeded() &&
3315-
io_handler.GetFlags().Test(eHandleCommandFlagPrintResult)) ||
3316-
io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors)) {
3315+
const bool print_result =
3316+
result.Succeeded() &&
3317+
io_handler.GetFlags().Test(eHandleCommandFlagPrintResult);
3318+
const bool print_error =
3319+
io_handler.GetFlags().Test(eHandleCommandFlagPrintErrors);
3320+
3321+
// Now emit the command output text from the command we just executed.
3322+
if (print_result || print_error) {
3323+
// If the command failed and we didn't echo it, echo it now so the user
3324+
// knows which command produced the error.
3325+
if (!echoed_command && !result.Succeeded() && print_error) {
3326+
LockedStreamFile locked_stream =
3327+
io_handler.GetOutputStreamFileSP()->Lock();
3328+
locked_stream.Printf("%s%s\n", io_handler.GetPrompt(), line.c_str());
3329+
}
3330+
33173331
auto DefaultPrintCallback = [&](const CommandReturnObject &result) {
33183332
// Display any inline diagnostics first.
33193333
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)