Skip to content

Commit 0d33849

Browse files
committed
[lldb] Store the command in the CommandReturnObject (llvm#125132)
As suggested in llvm#125006. Depending on which PR lands first, I'll update `TestCommandInterepterPrintCallback.py` to check that the `CommandReturnObject` passed to the callback has the correct command. (cherry picked from commit 906eeed)
1 parent 6113505 commit 0d33849

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

lldb/include/lldb/API/SBCommandReturnObject.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,10 @@ class LLDB_API SBCommandReturnObject {
4242

4343
bool IsValid() const;
4444

45+
/// Get the command as the user typed it. Empty string if commands were run on
46+
/// behalf of lldb.
47+
const char *GetCommand();
48+
4549
const char *GetOutput();
4650

4751
const char *GetError();

lldb/include/lldb/Interpreter/CommandReturnObject.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ class CommandReturnObject {
3131

3232
~CommandReturnObject() = default;
3333

34+
/// Get the command as the user typed it. Empty string if commands were run on
35+
/// behalf of lldb.
36+
const std::string &GetCommand() const { return m_command; }
37+
38+
void SetCommand(std::string command) { m_command = std::move(command); }
39+
3440
/// Format any inline diagnostics with an indentation of \c indent.
3541
std::string GetInlineDiagnosticString(unsigned indent) const;
3642

@@ -172,6 +178,8 @@ class CommandReturnObject {
172178
private:
173179
enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
174180

181+
std::string m_command;
182+
175183
StreamTee m_out_stream;
176184
StreamTee m_err_stream;
177185
std::vector<DiagnosticDetail> m_diagnostics;

lldb/source/API/SBCommandReturnObject.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ SBCommandReturnObject::operator bool() const {
8484
return true;
8585
}
8686

87+
const char *SBCommandReturnObject::GetCommand() {
88+
LLDB_INSTRUMENT_VA(this);
89+
90+
ConstString output(ref().GetCommand());
91+
return output.AsCString(/*value_if_empty*/ "");
92+
}
93+
8794
const char *SBCommandReturnObject::GetOutput() {
8895
LLDB_INSTRUMENT_VA(this);
8996

lldb/source/Interpreter/CommandInterpreter.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1903,12 +1903,13 @@ bool CommandInterpreter::HandleCommand(const char *command_line,
19031903
std::string real_original_command_string(command_string);
19041904

19051905
Log *log = GetLog(LLDBLog::Commands);
1906-
llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")",
1907-
command_line);
1908-
19091906
LLDB_LOGF(log, "Processing command: %s", command_line);
19101907
LLDB_SCOPED_TIMERF("Processing command: %s.", command_line);
19111908

1909+
// Set the command in the CommandReturnObject here so that it's there even if
1910+
// the command is interrupted.
1911+
result.SetCommand(command_line);
1912+
19121913
if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted initiating command")) {
19131914
result.AppendError("... Interrupted");
19141915
return false;
@@ -2657,7 +2658,8 @@ void CommandInterpreter::HandleCommands(const StringList &commands,
26572658
(uint64_t)idx, cmd, error_msg);
26582659
m_debugger.SetAsyncExecution(old_async_execution);
26592660
return;
2660-
} else if (options.GetPrintResults()) {
2661+
}
2662+
if (options.GetPrintResults()) {
26612663
result.AppendMessageWithFormatv("Command #{0} '{1}' failed with {2}",
26622664
(uint64_t)idx + 1, cmd, error_msg);
26632665
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import lldb
2+
from lldbsuite.test.decorators import *
3+
from lldbsuite.test.lldbtest import *
4+
from lldbsuite.test import lldbutil
5+
6+
7+
class SBCommandReturnObjectTest(TestBase):
8+
NO_DEBUG_INFO_TESTCASE = True
9+
10+
def test(self):
11+
res = lldb.SBCommandReturnObject()
12+
self.assertEqual(res.GetCommand(), "")
13+
14+
ci = self.dbg.GetCommandInterpreter()
15+
ci.HandleCommand("help help", res)
16+
self.assertTrue(res.Succeeded())
17+
self.assertEqual(res.GetCommand(), "help help")

0 commit comments

Comments
 (0)