2 Test some SBValue APIs.
5 from __future__ import print_function
12 import lldbsuite.test.lldbutil as lldbutil
13 from lldbsuite.test.lldbtest import *
15 class ValueAPITestCase(TestBase):
17 mydir = TestBase.compute_mydir(__file__)
20 # Call super's setUp().
22 # We'll use the test method name as the exe_name.
23 self.exe_name = self.testMethodName
24 # Find the line number to of function 'c'.
25 self.line = line_number('main.c', '// Break at this line')
27 @expectedFailureWindows("llvm.org/pr24772")
28 @add_test_categories(['pyapi'])
30 """Exercise some SBValue APIs."""
31 d = {'EXE': self.exe_name}
32 self.build(dictionary=d)
33 self.setTearDownCleanup(dictionary=d)
34 exe = os.path.join(os.getcwd(), self.exe_name)
36 # Create a target by the debugger.
37 target = self.dbg.CreateTarget(exe)
38 self.assertTrue(target, VALID_TARGET)
40 # Create the breakpoint inside function 'main'.
41 breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
42 self.assertTrue(breakpoint, VALID_BREAKPOINT)
44 # Now launch the process, and do not stop at entry point.
45 process = target.LaunchSimple (None, None, self.get_process_working_directory())
46 self.assertTrue(process, PROCESS_IS_VALID)
49 self.assertTrue(process.GetState() == lldb.eStateStopped)
50 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
51 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
52 frame0 = thread.GetFrameAtIndex(0)
54 # Get global variable 'days_of_week'.
55 list = target.FindGlobalVariables('days_of_week', 1)
56 days_of_week = list.GetValueAtIndex(0)
57 self.assertTrue(days_of_week, VALID_VARIABLE)
58 self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
59 self.DebugSBValue(days_of_week)
61 # Get global variable 'weekdays'.
62 list = target.FindGlobalVariables('weekdays', 1)
63 weekdays = list.GetValueAtIndex(0)
64 self.assertTrue(weekdays, VALID_VARIABLE)
65 self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
66 self.DebugSBValue(weekdays)
68 # Get global variable 'g_table'.
69 list = target.FindGlobalVariables('g_table', 1)
70 g_table = list.GetValueAtIndex(0)
71 self.assertTrue(g_table, VALID_VARIABLE)
72 self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
73 self.DebugSBValue(g_table)
75 fmt = lldbutil.BasicFormatter()
76 cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
77 rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
79 print(fmt.format(days_of_week))
80 print(cvf.format(days_of_week))
81 print(cvf.format(weekdays))
82 print(rdf.format(g_table))
84 # Get variable 'my_int_ptr'.
85 value = frame0.FindVariable('my_int_ptr')
86 self.assertTrue(value, VALID_VARIABLE)
87 self.DebugSBValue(value)
89 # Get what 'my_int_ptr' points to.
90 pointed = value.GetChildAtIndex(0)
91 self.assertTrue(pointed, VALID_VARIABLE)
92 self.DebugSBValue(pointed)
94 # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
95 symbol = target.ResolveLoadAddress(int(pointed.GetLocation(), 0)).GetSymbol()
96 self.assertTrue(symbol)
97 self.expect(symbol.GetName(), exe=False,
98 startstr = 'g_my_int')
100 # Get variable 'str_ptr'.
101 value = frame0.FindVariable('str_ptr')
102 self.assertTrue(value, VALID_VARIABLE)
103 self.DebugSBValue(value)
105 # SBValue::TypeIsPointerType() should return true.
106 self.assertTrue(value.TypeIsPointerType())
108 # Verify the SBValue::GetByteSize() API is working correctly.
109 arch = self.getArchitecture()
111 self.assertTrue(value.GetByteSize() == 4)
112 elif arch == 'x86_64':
113 self.assertTrue(value.GetByteSize() == 8)
115 # Get child at index 5 => 'Friday'.
116 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
117 self.assertTrue(child, VALID_VARIABLE)
118 self.DebugSBValue(child)
120 self.expect(child.GetSummary(), exe=False,
121 substrs = ['Friday'])
123 # Now try to get at the same variable using GetValueForExpressionPath().
124 # These two SBValue objects should have the same value.
125 val2 = value.GetValueForExpressionPath('[5]')
126 self.assertTrue(val2, VALID_VARIABLE)
127 self.DebugSBValue(val2)
128 self.assertTrue(child.GetValue() == val2.GetValue() and
129 child.GetSummary() == val2.GetSummary())
131 val_i = target.EvaluateExpression('i')
132 val_s = target.EvaluateExpression('s')
133 val_a = target.EvaluateExpression('a')
134 self.assertTrue(val_s.GetChildMemberWithName('a').AddressOf(), VALID_VARIABLE)
135 self.assertTrue(val_a.Cast(val_i.GetType()).AddressOf(), VALID_VARIABLE)