Skip to content

Commit da2969b

Browse files
authored
[LLDB] Update DIL to handle smart pointers; add more tests. (#143786)
This updates the DIL implementation to handle smart pointers (accessing field members and dereferencing) in the same way the current 'frame variable' implementation does. It also adds tests for handling smart pointers, as well as some additional DIL tests.
1 parent 772009c commit da2969b

File tree

21 files changed

+412
-1
lines changed

21 files changed

+412
-1
lines changed

lldb/source/ValueObject/DILEval.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,12 @@ Interpreter::Visit(const UnaryOpNode *node) {
253253
rhs = dynamic_rhs;
254254

255255
lldb::ValueObjectSP child_sp = rhs->Dereference(error);
256+
if (!child_sp && m_use_synthetic) {
257+
if (lldb::ValueObjectSP synth_obj_sp = rhs->GetSyntheticValue()) {
258+
error.Clear();
259+
child_sp = synth_obj_sp->Dereference(error);
260+
}
261+
}
256262
if (error.Fail())
257263
return llvm::make_error<DILDiagnosticError>(m_expr, error.AsCString(),
258264
node->GetLocation());
@@ -280,6 +286,7 @@ Interpreter::Visit(const MemberOfNode *node) {
280286
auto base_or_err = Evaluate(node->GetBase());
281287
if (!base_or_err)
282288
return base_or_err;
289+
bool expr_is_ptr = node->GetIsArrow();
283290
lldb::ValueObjectSP base = *base_or_err;
284291

285292
// Perform some basic type & correctness checking.
@@ -319,11 +326,11 @@ Interpreter::Visit(const MemberOfNode *node) {
319326
return llvm::make_error<DILDiagnosticError>(
320327
m_expr, errMsg, node->GetLocation(), node->GetFieldName().size());
321328
}
329+
expr_is_ptr = false;
322330
}
323331
}
324332

325333
if (m_check_ptr_vs_member) {
326-
bool expr_is_ptr = node->GetIsArrow();
327334
bool base_is_ptr = base->IsPointerType();
328335

329336
if (expr_is_ptr != base_is_ptr) {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
"""
2+
Make sure 'frame var' using DIL parser/evaluator works for bit fields.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test import lldbutil
9+
10+
import os
11+
import shutil
12+
import time
13+
14+
15+
class TestFrameVarDILBitField(TestBase):
16+
# If your test case doesn't stress debug info, then
17+
# set this to true. That way it won't be run once for
18+
# each debug info format.
19+
NO_DEBUG_INFO_TESTCASE = True
20+
21+
def test_frame_var(self):
22+
self.build()
23+
lldbutil.run_to_source_breakpoint(
24+
self, "Set a breakpoint here", lldb.SBFileSpec("main.cpp")
25+
)
26+
27+
self.runCmd("settings set target.experimental.use-DIL true")
28+
self.expect_var_path("bf.a", value="1023")
29+
self.expect_var_path("bf.b", value="9")
30+
self.expect_var_path("bf.c", value="false")
31+
self.expect_var_path("bf.d", value="true")
32+
33+
self.expect_var_path("abf.a", value="1023")
34+
self.expect_var_path("abf.b", value="'\\x0f'")
35+
self.expect_var_path("abf.c", value="3")
36+
37+
# Perform an operation to ensure we actually read the value.
38+
# Address-of is not allowed for bit-fields.
39+
self.expect(
40+
"frame variable '&bf.a'",
41+
error=True,
42+
substrs=["'bf.a' doesn't have a valid address"],
43+
)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <cstdint>
2+
3+
int main(int argc, char **argv) {
4+
enum BitFieldEnum : uint32_t { kZero, kOne };
5+
6+
struct BitFieldStruct {
7+
uint16_t a : 10;
8+
uint32_t b : 4;
9+
bool c : 1;
10+
bool d : 1;
11+
int32_t e : 32;
12+
uint32_t f : 32;
13+
uint32_t g : 31;
14+
uint64_t h : 31;
15+
uint64_t i : 33;
16+
BitFieldEnum j : 10;
17+
};
18+
19+
BitFieldStruct bf;
20+
bf.a = 0b1111111111;
21+
bf.b = 0b1001;
22+
bf.c = 0b0;
23+
bf.d = 0b1;
24+
bf.e = 0b1;
25+
bf.f = 0b1;
26+
bf.g = 0b1;
27+
bf.h = 0b1;
28+
bf.i = 0b1;
29+
bf.j = BitFieldEnum::kOne;
30+
31+
struct AlignedBitFieldStruct {
32+
uint16_t a : 10;
33+
uint8_t b : 4;
34+
unsigned char : 0;
35+
uint16_t c : 2;
36+
};
37+
38+
uint32_t data = ~0;
39+
AlignedBitFieldStruct abf = (AlignedBitFieldStruct &)data;
40+
41+
return 0; // Set a breakpoint here
42+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
"""
2+
Make sure 'frame var' using DIL parser/evaluator works for indirection.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test import lldbutil
9+
10+
import os
11+
import shutil
12+
import time
13+
14+
15+
class TestFrameVarDILIndirection(TestBase):
16+
# If your test case doesn't stress debug info, then
17+
# set this to true. That way it won't be run once for
18+
# each debug info format.
19+
NO_DEBUG_INFO_TESTCASE = True
20+
21+
def test_frame_var(self):
22+
self.build()
23+
lldbutil.run_to_source_breakpoint(
24+
self, "Set a breakpoint here", lldb.SBFileSpec("main.cpp")
25+
)
26+
27+
self.runCmd("settings set target.experimental.use-DIL true")
28+
self.expect_var_path("*p", value="1")
29+
self.expect_var_path("p", type="int *")
30+
self.expect_var_path("*my_p", value="1")
31+
self.expect_var_path("my_p", type="myp")
32+
self.expect_var_path("*my_pr", type="int *")
33+
self.expect_var_path("my_pr", type="mypr")
34+
35+
self.expect(
36+
"frame variable '*1'",
37+
error=True,
38+
substrs=["Unexpected token: <'1' (numeric_constant)>"],
39+
)
40+
self.expect(
41+
"frame variable '*val'",
42+
error=True,
43+
substrs=[
44+
"dereference failed: not a pointer, reference or array type: (int) val"
45+
],
46+
)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
int main(int argc, char **argv) {
2+
int val = 1;
3+
int *p = &val;
4+
5+
typedef int *myp;
6+
myp my_p = &val;
7+
8+
typedef int *&mypr;
9+
mypr my_pr = p;
10+
11+
return 0; // Set a breakpoint here
12+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CXX_SOURCES := main.cpp
2+
3+
include Makefile.rules
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
"""
2+
Test DIL pointer dereferencing.
3+
"""
4+
5+
import lldb
6+
from lldbsuite.test.lldbtest import *
7+
from lldbsuite.test.decorators import *
8+
from lldbsuite.test import lldbutil
9+
10+
import os
11+
import shutil
12+
import time
13+
14+
15+
class TestFrameVarDILPointerDereference(TestBase):
16+
NO_DEBUG_INFO_TESTCASE = True
17+
18+
def test_frame_var(self):
19+
self.build()
20+
lldbutil.run_to_source_breakpoint(
21+
self, "Set a breakpoint here", lldb.SBFileSpec("main.cpp")
22+
)
23+
24+
self.runCmd("settings set target.experimental.use-DIL true")
25+
self.expect_var_path("*p_int0", value="0")
26+
self.expect_var_path("*cp_int5", value="5")
27+
self.expect_var_path("&pp_void0[2]", type="void **")
28+
self.expect_var_path("**pp_int0", value="0")
29+
self.expect_var_path("&**pp_int0", type="int *")
30+
self.expect(
31+
"frame variable '&p_void[0]'",
32+
error=True,
33+
substrs=["subscript of pointer to incomplete type 'void'"],
34+
)
35+
36+
# Verify some of the returned values.
37+
pp_void0_2_got = self.expect_var_path("&pp_void0[2]", type="void **")
38+
# Initialized in C++ code to point to the same value
39+
pp_void0_2_exp = self.expect_var_path("pp_void0_2", type="void **")
40+
self.assertEqual(
41+
pp_void0_2_got.GetValueAsAddress(), pp_void0_2_exp.GetValueAsAddress()
42+
)
43+
pp_int0_2stars_got = self.expect_var_path("&**pp_int0", type="int *")
44+
pp_int0_2stars_exp = self.expect_var_path("pp_int0_2stars", type="int *")
45+
self.assertEqual(
46+
pp_int0_2stars_got.GetValueAsAddress(),
47+
pp_int0_2stars_exp.GetValueAsAddress(),
48+
)
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#include <cstddef>
2+
3+
int main(int argc, char **argv) {
4+
int *p_null = nullptr;
5+
const char *p_char1 = "hello";
6+
7+
typedef const char *my_char_ptr;
8+
my_char_ptr my_p_char1 = p_char1;
9+
10+
int offset = 5;
11+
int array[10];
12+
array[0] = 0;
13+
array[offset] = offset;
14+
15+
int (&array_ref)[10] = array;
16+
17+
int *p_int0 = &array[0];
18+
int **pp_int0 = &p_int0;
19+
const int *cp_int0 = &array[0];
20+
const int *cp_int5 = &array[offset];
21+
22+
typedef int *td_int_ptr_t;
23+
td_int_ptr_t td_int_ptr0 = &array[0];
24+
25+
void *p_void = (void *)p_char1;
26+
void **pp_void0 = &p_void;
27+
void **pp_void1 = pp_void0 + 1;
28+
29+
void **pp_void0_2 = &pp_void0[2];
30+
int *pp_int0_2stars = &**pp_int0;
31+
std::nullptr_t std_nullptr_t = nullptr;
32+
33+
return 0; // Set a breakpoint here
34+
}

0 commit comments

Comments
 (0)