Skip to content

Commit 9c290f5

Browse files
committed
[lldb] Store the return ValueObject in the CommandReturnObject
There are a lot of lldb commands whose result is really a ValueObject that we then print with the ValueObjectPrinter. Now that we have the ability to access the SBCommandReturnObject through a callback (#125006), we can store the resultant ValueObject in the return object, allowing an IDE to access the SBValue and do its own rich formatting. rdar://143965453
1 parent f71b83b commit 9c290f5

File tree

8 files changed

+46
-4
lines changed

8 files changed

+46
-4
lines changed

lldb/include/lldb/API/SBCommandReturnObject.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ class LLDB_API SBCommandReturnObject {
136136

137137
void SetError(const char *error_cstr);
138138

139+
lldb::SBValue GetReturnValue(lldb::DynamicValueType use_dynamic);
140+
139141
protected:
140142
friend class SBCommandInterpreter;
141143
friend class SBOptions;

lldb/include/lldb/API/SBValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ class LLDB_API SBValue {
442442

443443
protected:
444444
friend class SBBlock;
445+
friend class SBCommandReturnObject;
445446
friend class SBFrame;
446447
friend class SBModule;
447448
friend class SBTarget;

lldb/include/lldb/Interpreter/CommandReturnObject.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "lldb/Utility/StreamString.h"
1515
#include "lldb/Utility/StreamTee.h"
1616
#include "lldb/Utility/StructuredData.h"
17+
#include "lldb/lldb-forward.h"
1718
#include "lldb/lldb-private.h"
1819

1920
#include "llvm/ADT/StringRef.h"
@@ -165,6 +166,12 @@ class CommandReturnObject {
165166
return m_diagnostic_indent;
166167
}
167168

169+
lldb::ValueObjectSP GetValueObjectSP() const { return m_value_object_sp; }
170+
171+
void SetValueObjectSP(lldb::ValueObjectSP value_object_sp) {
172+
m_value_object_sp = value_object_sp;
173+
}
174+
168175
lldb::ReturnStatus GetStatus() const;
169176

170177
void SetStatus(lldb::ReturnStatus status);
@@ -197,6 +204,9 @@ class CommandReturnObject {
197204

198205
lldb::ReturnStatus m_status = lldb::eReturnStatusStarted;
199206

207+
/// An optional ValueObjectSP if the command created one.
208+
lldb::ValueObjectSP m_value_object_sp;
209+
200210
bool m_did_change_process_state = false;
201211
bool m_suppress_immediate_output = false;
202212

lldb/source/API/SBCommandReturnObject.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "lldb/API/SBFile.h"
1313
#include "lldb/API/SBStream.h"
1414
#include "lldb/API/SBStructuredData.h"
15+
#include "lldb/API/SBValue.h"
1516
#include "lldb/Core/StructuredDataImpl.h"
1617
#include "lldb/Interpreter/CommandReturnObject.h"
1718
#include "lldb/Utility/ConstString.h"
@@ -62,8 +63,8 @@ SBCommandReturnObject::SBCommandReturnObject(const SBCommandReturnObject &rhs) {
6263
m_opaque_up = clone(rhs.m_opaque_up);
6364
}
6465

65-
SBCommandReturnObject &SBCommandReturnObject::
66-
operator=(const SBCommandReturnObject &rhs) {
66+
SBCommandReturnObject &
67+
SBCommandReturnObject::operator=(const SBCommandReturnObject &rhs) {
6768
LLDB_INSTRUMENT_VA(this, rhs);
6869

6970
if (this != &rhs)
@@ -356,3 +357,12 @@ void SBCommandReturnObject::SetError(const char *error_cstr) {
356357
if (error_cstr)
357358
ref().AppendError(error_cstr);
358359
}
360+
361+
SBValue
362+
SBCommandReturnObject::GetReturnValue(lldb::DynamicValueType use_dynamic) {
363+
LLDB_INSTRUMENT_VA(this, use_dynamic);
364+
365+
SBValue sb_value;
366+
sb_value.SetSP(ref().GetValueObjectSP(), use_dynamic);
367+
return sb_value;
368+
}

lldb/source/Commands/CommandObjectDWIMPrint.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ void CommandObjectDWIMPrint::DoExecute(StringRef command,
205205
ExpressionResults expr_result = target.EvaluateExpression(
206206
expr, exe_scope, valobj_sp, eval_options, &fixed_expression);
207207

208+
if (valobj_sp)
209+
result.SetValueObjectSP(valobj_sp);
210+
208211
// Record the position of the expression in the command.
209212
std::optional<uint16_t> indent;
210213
if (fixed_expression.empty()) {

lldb/source/Commands/CommandObjectExpression.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ bool CommandObjectExpression::EvaluateExpression(llvm::StringRef expr,
434434
}
435435

436436
if (result_valobj_sp) {
437+
result.SetValueObjectSP(result_valobj_sp);
438+
437439
Format format = m_format_options.GetFormat();
438440

439441
if (result_valobj_sp->GetError().Success()) {

lldb/source/Commands/CommandObjectFrame.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ class CommandObjectFrameDiagnose : public CommandObjectParsed {
152152
return;
153153
}
154154

155+
result.SetValueObjectSP(valobj_sp);
155156
DumpValueObjectOptions::DeclPrintingHelper helper =
156157
[&valobj_sp](ConstString type, ConstString var,
157158
const DumpValueObjectOptions &opts,
@@ -317,9 +318,9 @@ class CommandObjectFrameSelect : public CommandObjectParsed {
317318
} else if (*m_options.relative_frame_offset > 0) {
318319
// I don't want "up 20" where "20" takes you past the top of the stack
319320
// to produce an error, but rather to just go to the top. OTOH, start
320-
// by seeing if the requested frame exists, in which case we can avoid
321+
// by seeing if the requested frame exists, in which case we can avoid
321322
// counting the stack here...
322-
const uint32_t frame_requested = frame_idx
323+
const uint32_t frame_requested = frame_idx
323324
+ *m_options.relative_frame_offset;
324325
StackFrameSP frame_sp = thread->GetStackFrameAtIndex(frame_requested);
325326
if (frame_sp)

lldb/test/API/api/command-return-object/TestSBCommandReturnObject.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,16 @@ def test_get_command(self):
3333
ci.HandleCommand("help help", res)
3434
self.assertTrue(res.Succeeded())
3535
self.assertEqual(res.GetCommand(), "help help")
36+
37+
value = res.GetReturnValue(lldb.eNoDynamicValues)
38+
self.assertFalse(value)
39+
40+
def test_get_value(self):
41+
res = lldb.SBCommandReturnObject()
42+
ci = self.dbg.GetCommandInterpreter()
43+
ci.HandleCommand("p 1 + 1", res)
44+
self.assertTrue(res.Succeeded())
45+
46+
value = res.GetReturnValue(lldb.eNoDynamicValues)
47+
self.assertTrue(value)
48+
self.assertEqual(value.GetValue(), "2")

0 commit comments

Comments
 (0)