2 Test some SBValue APIs.
6 from lldbsuite
.test
import lldbutil
7 from lldbsuite
.test
.decorators
import *
8 from lldbsuite
.test
.lldbtest
import *
11 class ValueAPITestCase(TestBase
):
13 # Call super's setUp().
15 # We'll use the test method name as the exe_name.
16 self
.exe_name
= self
.testMethodName
17 # Find the line number to of function 'c'.
18 self
.line
= line_number("main.c", "// Break at this line")
20 @expectedFailureAll(oslist
=["windows"], bugnumber
="llvm.org/pr24772")
22 """Exercise some SBValue APIs."""
23 d
= {"EXE": self
.exe_name
}
24 self
.build(dictionary
=d
)
25 self
.setTearDownCleanup(dictionary
=d
)
26 exe
= self
.getBuildArtifact(self
.exe_name
)
28 # Create a target by the debugger.
29 target
= self
.dbg
.CreateTarget(exe
)
30 self
.assertTrue(target
, VALID_TARGET
)
32 # Create the breakpoint inside function 'main'.
33 breakpoint
= target
.BreakpointCreateByLocation("main.c", self
.line
)
34 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
36 # Now launch the process, and do not stop at entry point.
37 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
38 self
.assertTrue(process
, PROCESS_IS_VALID
)
41 self
.assertState(process
.GetState(), lldb
.eStateStopped
)
42 thread
= lldbutil
.get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
45 "There should be a thread stopped due to breakpoint condition",
47 frame0
= thread
.GetFrameAtIndex(0)
49 # Get global variable 'days_of_week'.
50 list = target
.FindGlobalVariables("days_of_week", 1)
51 days_of_week
= list.GetValueAtIndex(0)
52 self
.assertTrue(days_of_week
, VALID_VARIABLE
)
53 self
.assertEqual(days_of_week
.GetNumChildren(), 7, VALID_VARIABLE
)
54 self
.DebugSBValue(days_of_week
)
56 # Use this to test the "child" and "children" accessors:
57 children
= days_of_week
.children
58 self
.assertEqual(len(children
), 7, VALID_VARIABLE
)
59 for i
in range(0, len(children
)):
60 day
= days_of_week
.child
[i
]
61 list_day
= children
[i
]
62 self
.assertNotEqual(day
, None)
63 self
.assertNotEqual(list_day
, None)
64 self
.assertEqual(day
.GetSummary(), list_day
.GetSummary(), VALID_VARIABLE
)
66 # Spot check the actual value:
67 first_day
= days_of_week
.child
[1]
68 self
.assertEqual(first_day
.GetSummary(), '"Monday"', VALID_VARIABLE
)
70 # Get global variable 'weekdays'.
71 list = target
.FindGlobalVariables("weekdays", 1)
72 weekdays
= list.GetValueAtIndex(0)
73 self
.assertTrue(weekdays
, VALID_VARIABLE
)
74 self
.assertEqual(weekdays
.GetNumChildren(), 5, VALID_VARIABLE
)
75 self
.DebugSBValue(weekdays
)
77 # Get global variable 'g_table'.
78 list = target
.FindGlobalVariables("g_table", 1)
79 g_table
= list.GetValueAtIndex(0)
80 self
.assertTrue(g_table
, VALID_VARIABLE
)
81 self
.assertEqual(g_table
.GetNumChildren(), 2, VALID_VARIABLE
)
82 self
.DebugSBValue(g_table
)
84 fmt
= lldbutil
.BasicFormatter()
85 cvf
= lldbutil
.ChildVisitingFormatter(indent_child
=2)
86 rdf
= lldbutil
.RecursiveDecentFormatter(indent_child
=2)
88 print(fmt
.format(days_of_week
))
89 print(cvf
.format(days_of_week
))
90 print(cvf
.format(weekdays
))
91 print(rdf
.format(g_table
))
93 # Get variable 'my_int_ptr'.
94 value
= frame0
.FindVariable("my_int_ptr")
95 self
.assertTrue(value
, VALID_VARIABLE
)
96 self
.DebugSBValue(value
)
98 # Get what 'my_int_ptr' points to.
99 pointed
= value
.GetChildAtIndex(0)
100 self
.assertTrue(pointed
, VALID_VARIABLE
)
101 self
.DebugSBValue(pointed
)
103 # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
104 symbol
= target
.ResolveLoadAddress(int(pointed
.GetLocation(), 0)).GetSymbol()
105 self
.assertTrue(symbol
)
106 self
.expect(symbol
.GetName(), exe
=False, startstr
="g_my_int")
108 # Get variable 'str_ptr'.
109 value
= frame0
.FindVariable("str_ptr")
110 self
.assertTrue(value
, VALID_VARIABLE
)
111 self
.DebugSBValue(value
)
113 # SBValue::TypeIsPointerType() should return true.
114 self
.assertTrue(value
.TypeIsPointerType())
116 # Verify the SBValue::GetByteSize() API is working correctly.
117 arch
= self
.getArchitecture()
119 self
.assertEqual(value
.GetByteSize(), 4)
120 elif arch
== "x86_64":
121 self
.assertEqual(value
.GetByteSize(), 8)
123 # Get child at index 5 => 'Friday'.
124 child
= value
.GetChildAtIndex(5, lldb
.eNoDynamicValues
, True)
125 self
.assertTrue(child
, VALID_VARIABLE
)
126 self
.DebugSBValue(child
)
128 self
.expect(child
.GetSummary(), exe
=False, substrs
=["Friday"])
130 # Now try to get at the same variable using GetValueForExpressionPath().
131 # These two SBValue objects should have the same value.
132 val2
= value
.GetValueForExpressionPath("[5]")
133 self
.assertTrue(val2
, VALID_VARIABLE
)
134 self
.DebugSBValue(val2
)
136 child
.GetValue() == val2
.GetValue()
137 and child
.GetSummary() == val2
.GetSummary()
140 val_i
= target
.EvaluateExpression("i")
141 val_s
= target
.EvaluateExpression("s")
142 val_a
= target
.EvaluateExpression("a")
144 val_s
.GetChildMemberWithName("a").GetAddress().IsValid(), VALID_VARIABLE
146 self
.assertTrue(val_s
.GetChildMemberWithName("a").AddressOf(), VALID_VARIABLE
)
147 self
.assertTrue(val_a
.Cast(val_i
.GetType()).AddressOf(), VALID_VARIABLE
)
149 # Test some other cases of the Cast API. We allow casts from one struct type
150 # to another, which is a little weird, but we don't support casting from a
151 # smaller type to a larger as we often wouldn't know how to get the extra data:
152 val_f
= target
.EvaluateExpression("f")
153 bad_cast
= val_s
.Cast(val_f
.GetType())
156 "Can only cast to a type that is equal to or smaller than the orignal type.",
158 weird_cast
= val_f
.Cast(val_s
.GetType())
159 self
.assertSuccess(weird_cast
.GetError(), "Can cast from a larger to a smaller")
161 weird_cast
.GetChildMemberWithName("a").GetValueAsSigned(0),
163 "Got the right value",
166 # Check that lldb.value implements truth testing.
167 self
.assertFalse(lldb
.value(frame0
.FindVariable("bogus")))
168 self
.assertTrue(lldb
.value(frame0
.FindVariable("uinthex")))
171 int(lldb
.value(frame0
.FindVariable("uinthex"))),
173 "uinthex == 3768803088",
176 int(lldb
.value(frame0
.FindVariable("sinthex"))),
178 "sinthex == -526164208",
181 # Check value_iter works correctly.
183 lldb
.value(frame0
.FindVariable("uinthex")),
184 lldb
.value(frame0
.FindVariable("sinthex")),
189 frame0
.FindVariable("uinthex").GetValueAsUnsigned(),
191 "unsigned uinthex == 3768803088",
194 frame0
.FindVariable("sinthex").GetValueAsUnsigned(),
196 "unsigned sinthex == 3768803088",
200 frame0
.FindVariable("uinthex").GetValueAsSigned(),
202 "signed uinthex == -526164208",
205 frame0
.FindVariable("sinthex").GetValueAsSigned(),
207 "signed sinthex == -526164208",
210 # Check that hex value printing works as expected.
212 frame0
.FindVariable("fixed_int_ptr").GetValue(),
213 "0x000000aa" if target
.addr_size
== 4 else "0x00000000000000aa",
215 self
.runCmd("settings set target.show-hex-variable-values-with-leading-zeroes false")
217 frame0
.FindVariable("another_fixed_int_ptr").GetValue(),
221 frame0
.FindVariable("a_null_int_ptr").GetValue(),