blob: 6659a2f926a91dea5e95dd6191c027f9d3937d9e [file] [log] [blame]
Johnny Chen5819ab42011-07-15 22:28:10 +00001"""
2Test some SBValue APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class ValueAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "value")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000017 @dsym_test
Johnny Chen5819ab42011-07-15 22:28:10 +000018 def test_with_dsym(self):
19 """Exercise some SBValue APIs."""
20 d = {'EXE': self.exe_name}
21 self.buildDsym(dictionary=d)
22 self.setTearDownCleanup(dictionary=d)
23 self.value_api(self.exe_name)
24
25 @python_api_test
Johnny Chen24086bc2012-04-06 19:54:10 +000026 @dwarf_test
Johnny Chen5819ab42011-07-15 22:28:10 +000027 def test_with_dwarf(self):
28 """Exercise some SBValue APIs."""
29 d = {'EXE': self.exe_name}
30 self.buildDwarf(dictionary=d)
31 self.setTearDownCleanup(dictionary=d)
32 self.value_api(self.exe_name)
33
34 def setUp(self):
35 # Call super's setUp().
36 TestBase.setUp(self)
37 # We'll use the test method name as the exe_name.
38 self.exe_name = self.testMethodName
39 # Find the line number to of function 'c'.
40 self.line = line_number('main.c', '// Break at this line')
41
42 def value_api(self, exe_name):
43 """Exercise some SBValue APIs."""
44 exe = os.path.join(os.getcwd(), exe_name)
45
46 # Create a target by the debugger.
47 target = self.dbg.CreateTarget(exe)
48 self.assertTrue(target, VALID_TARGET)
49
50 # Create the breakpoint inside function 'main'.
51 breakpoint = target.BreakpointCreateByLocation('main.c', self.line)
52 self.assertTrue(breakpoint, VALID_BREAKPOINT)
53
54 # Now launch the process, and do not stop at entry point.
55 process = target.LaunchSimple(None, None, os.getcwd())
56 self.assertTrue(process, PROCESS_IS_VALID)
57
58 # Get Frame #0.
59 self.assertTrue(process.GetState() == lldb.eStateStopped)
60 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
61 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
62 frame0 = thread.GetFrameAtIndex(0)
63
Johnny Chen6cbb8d62011-07-21 23:02:00 +000064 # Get global variable 'days_of_week'.
65 list = target.FindGlobalVariables('days_of_week', 1)
66 days_of_week = list.GetValueAtIndex(0)
67 self.assertTrue(days_of_week, VALID_VARIABLE)
68 self.assertTrue(days_of_week.GetNumChildren() == 7, VALID_VARIABLE)
69 self.DebugSBValue(days_of_week)
70
Johnny Chen36d7d912011-07-22 22:01:35 +000071 # Get global variable 'weekdays'.
72 list = target.FindGlobalVariables('weekdays', 1)
73 weekdays = list.GetValueAtIndex(0)
74 self.assertTrue(weekdays, VALID_VARIABLE)
75 self.assertTrue(weekdays.GetNumChildren() == 5, VALID_VARIABLE)
76 self.DebugSBValue(weekdays)
77
78 # Get global variable 'g_table'.
79 list = target.FindGlobalVariables('g_table', 1)
80 g_table = list.GetValueAtIndex(0)
81 self.assertTrue(g_table, VALID_VARIABLE)
82 self.assertTrue(g_table.GetNumChildren() == 2, VALID_VARIABLE)
83 self.DebugSBValue(g_table)
84
Johnny Chen989b7ef2011-07-22 00:47:58 +000085 fmt = lldbutil.BasicFormatter()
Johnny Chen36d7d912011-07-22 22:01:35 +000086 cvf = lldbutil.ChildVisitingFormatter(indent_child=2)
87 rdf = lldbutil.RecursiveDecentFormatter(indent_child=2)
Johnny Chen989b7ef2011-07-22 00:47:58 +000088 if self.TraceOn():
89 print fmt.format(days_of_week)
90 print cvf.format(days_of_week)
Johnny Chen36d7d912011-07-22 22:01:35 +000091 print cvf.format(weekdays)
92 print rdf.format(g_table)
Johnny Chen989b7ef2011-07-22 00:47:58 +000093
Johnny Chen23413802011-11-15 21:13:13 +000094 # Get variable 'my_int_ptr'.
95 value = frame0.FindVariable('my_int_ptr')
96 self.assertTrue(value, VALID_VARIABLE)
97 self.DebugSBValue(value)
98
99 # Get what 'my_int_ptr' points to.
100 pointed = value.GetChildAtIndex(0)
101 self.assertTrue(pointed, VALID_VARIABLE)
102 self.DebugSBValue(pointed)
103
Johnny Chen4c1b0962011-11-15 23:30:39 +0000104 # While we are at it, verify that 'my_int_ptr' points to 'g_my_int'.
105 symbol = target.ResolveLoadAddress(int(pointed.GetLocation(), 0)).GetSymbol()
106 self.assertTrue(symbol)
107 self.expect(symbol.GetName(), exe=False,
108 startstr = 'g_my_int')
109
Johnny Chen5819ab42011-07-15 22:28:10 +0000110 # Get variable 'str_ptr'.
111 value = frame0.FindVariable('str_ptr')
112 self.assertTrue(value, VALID_VARIABLE)
113 self.DebugSBValue(value)
114
Johnny Chen6853cf62011-07-21 19:31:59 +0000115 # SBValue::TypeIsPointerType() should return true.
116 self.assertTrue(value.TypeIsPointerType())
117
118 # Verify the SBValue::GetByteSize() API is working correctly.
119 arch = self.getArchitecture()
120 if arch == 'i386':
121 self.assertTrue(value.GetByteSize() == 4)
122 elif arch == 'x86_64':
123 self.assertTrue(value.GetByteSize() == 8)
124
Johnny Chen5819ab42011-07-15 22:28:10 +0000125 # Get child at index 5 => 'Friday'.
126 child = value.GetChildAtIndex(5, lldb.eNoDynamicValues, True)
127 self.assertTrue(child, VALID_VARIABLE)
128 self.DebugSBValue(child)
129
130 self.expect(child.GetSummary(), exe=False,
131 substrs = ['Friday'])
132
133 # Now try to get at the same variable using GetValueForExpressionPath().
134 # These two SBValue objects should have the same value.
135 val2 = value.GetValueForExpressionPath('[5]')
136 self.assertTrue(val2, VALID_VARIABLE)
137 self.DebugSBValue(val2)
138 self.assertTrue(child.GetValue() == val2.GetValue() and
139 child.GetSummary() == val2.GetSummary())
140
141if __name__ == '__main__':
142 import atexit
143 lldb.SBDebugger.Initialize()
144 atexit.register(lambda: lldb.SBDebugger.Terminate())
145 unittest2.main()