Skip to content

Commit 906eeed

Browse files
authored
[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.
1 parent 84fbed8 commit 906eeed

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

lldb/include/lldb/API/SBCommandReturnObject.h

+4
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

+8
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

@@ -182,6 +188,8 @@ class CommandReturnObject {
182188
private:
183189
enum { eStreamStringIndex = 0, eImmediateStreamIndex = 1 };
184190

191+
std::string m_command;
192+
185193
StreamTee m_out_stream;
186194
StreamTee m_err_stream;
187195
std::vector<DiagnosticDetail> m_diagnostics;

lldb/source/API/SBCommandReturnObject.cpp

+7
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

+6-4
Original file line numberDiff line numberDiff line change
@@ -1887,12 +1887,13 @@ bool CommandInterpreter::HandleCommand(const char *command_line,
18871887
std::string real_original_command_string(command_string);
18881888

18891889
Log *log = GetLog(LLDBLog::Commands);
1890-
llvm::PrettyStackTraceFormat stack_trace("HandleCommand(command = \"%s\")",
1891-
command_line);
1892-
18931890
LLDB_LOGF(log, "Processing command: %s", command_line);
18941891
LLDB_SCOPED_TIMERF("Processing command: %s.", command_line);
18951892

1893+
// Set the command in the CommandReturnObject here so that it's there even if
1894+
// the command is interrupted.
1895+
result.SetCommand(command_line);
1896+
18961897
if (INTERRUPT_REQUESTED(GetDebugger(), "Interrupted initiating command")) {
18971898
result.AppendError("... Interrupted");
18981899
return false;
@@ -2644,7 +2645,8 @@ void CommandInterpreter::HandleCommands(
26442645
(uint64_t)idx, cmd, error_msg);
26452646
m_debugger.SetAsyncExecution(old_async_execution);
26462647
return;
2647-
} else if (options.GetPrintResults()) {
2648+
}
2649+
if (options.GetPrintResults()) {
26482650
result.AppendMessageWithFormatv("Command #{0} '{1}' failed with {2}",
26492651
(uint64_t)idx + 1, cmd, error_msg);
26502652
}
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)