2 Test some SBValue APIs.
5 from __future__ import print_function
12 from lldbsuite.test.decorators import *
13 from lldbsuite.test.lldbtest import *
14 from lldbsuite.test import lldbutil
17 class ValueAPITestCase(TestBase):
19 mydir = TestBase.compute_mydir(__file__)
22 # Call super's setUp().
24 # We'll use the test method name as the exe_name.
25 self.exe_name = self.testMethodName
26 # Find the line number to of function 'c'.
27 self.line = line_number('main.c', '// Break at this line')
29 @expectedFailureAll(oslist=["windows"], bugnumber="llvm.org/pr24772")
30 @add_test_categories(['pyapi'])
32 """Exercise some SBValue APIs."""
33 d = {'EXE': self.exe_name}
34 self.build(dictionary=d)
35 self.setTearDownCleanup(dictionary=d)
36 exe = os.path.join(os.getcwd(), self.exe_name)
38 # Create a target by the debugger.
39 target = self.dbg.CreateTarget(exe)
40 self.assertTrue(target, VALID_TARGET)
42 # Create the breakpoint inside function 'main'.
43 breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
44 self.assertTrue(breakpoint, VALID_BREAKPOINT)
46 # Now launch the process, and do not stop at entry point.
47 process = target.LaunchSimple(
48 None, None, self.get_process_working_directory())
49 self.assertTrue(process, PROCESS_IS_VALID)
52 self.assertTrue(process.GetState() == lldb.eStateStopped)
53 thread = lldbutil.get_stopped_thread(
54 process, lldb.eStopReasonBreakpoint)
57 "There should be a thread stopped due to breakpoint condition")
58 frame0 = thread.GetFrameAtIndex(0)
60 # Get global variable 'days_of_week'.
61 list = target.FindGlobalVariables('days_of_week', 1)
62 days_of_week = list.GetValueAtIndex(0)
63 self.assertTrue(days_of_week, VALID_VARIABLE)
64 self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
65 self.DebugSBValue(days_of_week)
67 # Get global variable 'weekdays'.
68 list = target.FindGlobalVariables('weekdays', 1)
69 weekdays = list.GetValueAtIndex(0)
70 self.assertTrue(weekdays, VALID_VARIABLE)
71 self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
72 self.DebugSBValue(weekdays)
74 # Get global variable 'g_table'.
75 list = target.FindGlobalVariables('g_table', 1)
76 g_table = list.GetValueAtIndex(0)
77 self.assertTrue(g_table, VALID_VARIABLE)
78 self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
79 self.DebugSBValue(g_table)
81 fmt = lldbutil.BasicFormatter()
82 cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
83 rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
85 print(fmt.format(days_of_week))
86 print(cvf.format(days_of_week))
87 print(cvf.format(weekdays))
88 print(rdf.format(g_table))
90 # Get variable 'my_int_ptr'.
91 value = frame0.FindVariable('my_int_ptr')
92 self.assertTrue(value, VALID_VARIABLE)
93 self.DebugSBValue(value)
95 # Get what 'my_int_ptr' points to.
96 pointed = value.GetChildAtIndex(0)
97 self.assertTrue(pointed, VALID_VARIABLE)
98 self.DebugSBValue(pointed)
100 # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
101 symbol = target.ResolveLoadAddress(
102 int(pointed.GetLocation(), 0)).GetSymbol()
103 self.assertTrue(symbol)
104 self.expect(symbol.GetName(), exe=False,
107 # Get variable 'str_ptr'.
108 value = frame0.FindVariable('str_ptr')
109 self.assertTrue(value, VALID_VARIABLE)
110 self.DebugSBValue(value)
112 # SBValue::TypeIsPointerType() should return true.
113 self.assertTrue(value.TypeIsPointerType())
115 # Verify the SBValue::GetByteSize() API is working correctly.
116 arch = self.getArchitecture()
118 self.assertTrue(value.GetByteSize() == 4)
119 elif arch == 'x86_64':
120 self.assertTrue(value.GetByteSize() == 8)
122 # Get child at index 5 => 'Friday'.
123 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
124 self.assertTrue(child, VALID_VARIABLE)
125 self.DebugSBValue(child)
127 self.expect(child.GetSummary(), exe=False,
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)
135 self.assertTrue(child.GetValue() == val2.GetValue() and
136 child.GetSummary() == val2.GetSummary())
138 val_i = target.EvaluateExpression('i')
139 val_s = target.EvaluateExpression('s')
140 val_a = target.EvaluateExpression('a')
142 val_s.GetChildMemberWithName('a').AddressOf(),
146 val_i.GetType()).AddressOf(),
149 self.assertTrue(int(lldb.value(frame0.FindVariable('uinthex')))
150 == 3768803088, 'uinthex == 3768803088')
151 self.assertTrue(int(lldb.value(frame0.FindVariable('sinthex')))
152 == -526164208, 'sinthex == -526164208')
155 frame0.FindVariable('uinthex').GetValueAsUnsigned() == 3768803088,
156 'unsigned uinthex == 3768803088')
158 frame0.FindVariable('sinthex').GetValueAsUnsigned() == 3768803088,
159 'unsigned sinthex == 3768803088')
162 frame0.FindVariable('uinthex').GetValueAsSigned() == -
164 'signed uinthex == -526164208')
166 frame0.FindVariable('sinthex').GetValueAsSigned() == -
168 'signed sinthex == -526164208')