blob: 3cfe011816e2737c8b9df67cd24c5ed255f4e1e7 [file] [log] [blame]
Johnny Chend61816b2011-03-03 01:41:57 +00001"""
2Test SBTarget APIs.
3"""
4
5import os, time
6import re
7import unittest2
8import lldb, lldbutil
9from lldbtest import *
10
11class TargetAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "target")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chen466c5932011-06-29 22:45:06 +000017 def test_find_global_variables_with_dsym(self):
18 """Exercise SBTaget.FindGlobalVariables() API."""
Johnny Chen086b1b72011-06-30 00:24:31 +000019 d = {'EXE': 'a.out'}
20 self.buildDsym(dictionary=d)
21 self.setTearDownCleanup(dictionary=d)
22 self.find_global_variables('a.out')
Johnny Chen466c5932011-06-29 22:45:06 +000023
Johnny Chen086b1b72011-06-30 00:24:31 +000024 #rdar://problem/9700873
25 @unittest2.skip("segmentation fault -- skipping")
Johnny Chen466c5932011-06-29 22:45:06 +000026 @python_api_test
27 def test_find_global_variables_with_dwarf(self):
28 """Exercise SBTarget.FindGlobalVariables() API."""
Johnny Chen086b1b72011-06-30 00:24:31 +000029 d = {'EXE': 'b.out'}
30 self.buildDwarf(dictionary=d)
31 self.setTearDownCleanup(dictionary=d)
32 self.find_global_variables('b.out')
Johnny Chen466c5932011-06-29 22:45:06 +000033
34 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
35 @python_api_test
Johnny Chen787f71f2011-04-22 23:20:17 +000036 def test_get_description_with_dsym(self):
37 """Exercise SBTaget.GetDescription() API."""
38 self.buildDsym()
39 self.get_description()
40
41 @python_api_test
42 def test_get_description_with_dwarf(self):
43 """Exercise SBTarget.GetDescription() API."""
44 self.buildDwarf()
45 self.get_description()
46
47 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
48 @python_api_test
Johnny Chen75625aa2011-03-07 22:29:04 +000049 def test_launch_new_process_and_redirect_stdout_with_dsym(self):
50 """Exercise SBTaget.Launch() API."""
51 self.buildDsym()
52 self.launch_new_process_and_redirect_stdout()
53
54 @python_api_test
55 def test_launch_new_process_and_redirect_stdout_with_dwarf(self):
56 """Exercise SBTarget.Launch() API."""
57 self.buildDwarf()
58 self.launch_new_process_and_redirect_stdout()
59
60 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
61 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000062 def test_resolve_symbol_context_with_address_with_dsym(self):
63 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000064 self.buildDsym()
Johnny Chen05178f62011-03-04 23:40:06 +000065 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000066
67 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000068 def test_resolve_symbol_context_with_address_with_dwarf(self):
69 """Exercise SBTarget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000070 self.buildDwarf()
Johnny Chen05178f62011-03-04 23:40:06 +000071 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000072
73 def setUp(self):
74 # Call super's setUp().
75 TestBase.setUp(self)
76 # Find the line number to of function 'c'.
77 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
78 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
79
Johnny Chen086b1b72011-06-30 00:24:31 +000080 def find_global_variables(self, exe_name):
Johnny Chen466c5932011-06-29 22:45:06 +000081 """Exercise SBTaget.FindGlobalVariables() API."""
Johnny Chen086b1b72011-06-30 00:24:31 +000082 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chen466c5932011-06-29 22:45:06 +000083
84 # Create a target by the debugger.
85 target = self.dbg.CreateTarget(exe)
86 self.assertTrue(target, VALID_TARGET)
87
Johnny Chen086b1b72011-06-30 00:24:31 +000088 value_list = target.FindGlobalVariables('my_global_var_of_char_type', 3)
Johnny Chen466c5932011-06-29 22:45:06 +000089 self.assertTrue(value_list.GetSize() == 1)
90 my_global_var = value_list.GetValueAtIndex(0)
91 self.expect(my_global_var.GetName(), exe=False,
92 startstr = "my_global_var_of_char_type")
93 self.expect(my_global_var.GetTypeName(), exe=False,
94 startstr = "char")
95 self.expect(my_global_var.GetValue(), exe=False,
96 startstr = "'X'")
97
Johnny Chen086b1b72011-06-30 00:24:31 +000098 # While we are at it, let's also exercise the similar SBModule.FindGlobalVariables() API.
99 for m in target.module_iter():
100 if m.GetFileSpec().GetDirectory() == os.getcwd() and m.GetFileSpec().GetFilename() == exe_name:
101 value_list = m.FindGlobalVariables(target, 'my_global_var_of_char_type', 3)
102 self.assertTrue(value_list.GetSize() == 1)
103 self.assertTrue(value_list.GetValueAtIndex(0).GetValue() == "'X'")
104 break
105
Johnny Chen787f71f2011-04-22 23:20:17 +0000106 def get_description(self):
107 """Exercise SBTaget.GetDescription() API."""
108 exe = os.path.join(os.getcwd(), "a.out")
109
110 # Create a target by the debugger.
111 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000112 self.assertTrue(target, VALID_TARGET)
Johnny Chen787f71f2011-04-22 23:20:17 +0000113
Johnny Chen90256cd2011-04-23 00:13:34 +0000114 from lldbutil import get_description
Johnny Chenfc87e2d2011-04-25 20:23:05 +0000115
116 # get_description() allows no option to mean lldb.eDescriptionLevelBrief.
117 desc = get_description(target)
118 #desc = get_description(target, option=lldb.eDescriptionLevelBrief)
Johnny Chen90256cd2011-04-23 00:13:34 +0000119 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000120 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000121 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000122 substrs = ['a.out'])
Johnny Chen90256cd2011-04-23 00:13:34 +0000123 self.expect(desc, exe=False, matching=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000124 substrs = ['Target', 'Module', 'Breakpoint'])
125
Johnny Chen90256cd2011-04-23 00:13:34 +0000126 desc = get_description(target, option=lldb.eDescriptionLevelFull)
127 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000128 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000129 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000130 substrs = ['a.out', 'Target', 'Module', 'Breakpoint'])
131
132
Johnny Chen75625aa2011-03-07 22:29:04 +0000133 def launch_new_process_and_redirect_stdout(self):
134 """Exercise SBTaget.Launch() API with redirected stdout."""
135 exe = os.path.join(os.getcwd(), "a.out")
136
137 # Create a target by the debugger.
138 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000139 self.assertTrue(target, VALID_TARGET)
Johnny Chen75625aa2011-03-07 22:29:04 +0000140
Johnny Chend6481352011-03-07 22:46:30 +0000141 # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done.
142 # We should still see the entire stdout redirected once the process is finished.
143 line = line_number('main.c', '// a(3) -> c(3)')
144 breakpoint = target.BreakpointCreateByLocation('main.c', line)
145
Johnny Chen75625aa2011-03-07 22:29:04 +0000146 # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000147 # The inferior should run to completion after "process.Continue()" call.
Johnny Chen75625aa2011-03-07 22:29:04 +0000148 error = lldb.SBError()
149 process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error)
Johnny Chend6481352011-03-07 22:46:30 +0000150 process.Continue()
151 #self.runCmd("process status")
Johnny Chen75625aa2011-03-07 22:29:04 +0000152
153 # The 'stdout.txt' file should now exist.
154 self.assertTrue(os.path.isfile("stdout.txt"),
155 "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.")
156
157 # Read the output file produced by running the program.
158 with open('stdout.txt', 'r') as f:
159 output = f.read()
160
Johnny Chend6481352011-03-07 22:46:30 +0000161 # Let's delete the 'stdout.txt' file as a cleanup step.
162 try:
163 os.remove("stdout.txt")
164 pass
165 except OSError:
166 pass
167
Johnny Chen75625aa2011-03-07 22:29:04 +0000168 self.expect(output, exe=False,
169 substrs = ["a(1)", "b(2)", "a(3)"])
170
171
Johnny Chen05178f62011-03-04 23:40:06 +0000172 def resolve_symbol_context_with_address(self):
173 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +0000174 exe = os.path.join(os.getcwd(), "a.out")
175
176 # Create a target by the debugger.
177 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000178 self.assertTrue(target, VALID_TARGET)
Johnny Chend61816b2011-03-03 01:41:57 +0000179
180 # Now create the two breakpoints inside function 'a'.
181 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
182 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
Johnny Chened401982011-03-03 19:14:00 +0000183 #print "breakpoint1:", breakpoint1
184 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +0000185 self.assertTrue(breakpoint1 and
Johnny Chend61816b2011-03-03 01:41:57 +0000186 breakpoint1.GetNumLocations() == 1,
187 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000188 self.assertTrue(breakpoint2 and
Johnny Chend61816b2011-03-03 01:41:57 +0000189 breakpoint2.GetNumLocations() == 1,
190 VALID_BREAKPOINT)
191
192 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000193 process = target.LaunchSimple(None, None, os.getcwd())
194 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chend61816b2011-03-03 01:41:57 +0000195
196 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000197 self.assertTrue(process.GetState() == lldb.eStateStopped)
198 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000199 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
200 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000201 frame0 = thread.GetFrameAtIndex(0)
202 lineEntry = frame0.GetLineEntry()
203 self.assertTrue(lineEntry.GetLine() == self.line1)
204
205 address1 = lineEntry.GetStartAddress()
206
207 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000208 process.Continue()
209 self.assertTrue(process.GetState() == lldb.eStateStopped)
210 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000211 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
212 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000213 frame0 = thread.GetFrameAtIndex(0)
214 lineEntry = frame0.GetLineEntry()
215 self.assertTrue(lineEntry.GetLine() == self.line2)
216
217 address2 = lineEntry.GetStartAddress()
218
Johnny Chened401982011-03-03 19:14:00 +0000219 #print "address1:", address1
220 #print "address2:", address2
Johnny Chend61816b2011-03-03 01:41:57 +0000221
222 # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
223 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
224 context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
225
Johnny Chen4ebd0192011-05-24 18:22:45 +0000226 self.assertTrue(context1 and context2)
Johnny Chened401982011-03-03 19:14:00 +0000227 #print "context1:", context1
228 #print "context2:", context2
Johnny Chend61816b2011-03-03 01:41:57 +0000229
230 # Verify that the context point to the same function 'a'.
231 symbol1 = context1.GetSymbol()
232 symbol2 = context2.GetSymbol()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000233 self.assertTrue(symbol1 and symbol2)
Johnny Chened401982011-03-03 19:14:00 +0000234 #print "symbol1:", symbol1
235 #print "symbol2:", symbol2
Johnny Chend61816b2011-03-03 01:41:57 +0000236
Johnny Chen9ae98202011-04-23 00:34:56 +0000237 from lldbutil import get_description
238 desc1 = get_description(symbol1)
239 desc2 = get_description(symbol2)
240 self.assertTrue(desc1 and desc2 and desc1 == desc2,
241 "The two addresses should resolve to the same symbol")
Johnny Chend61816b2011-03-03 01:41:57 +0000242
243
244if __name__ == '__main__':
245 import atexit
246 lldb.SBDebugger.Initialize()
247 atexit.register(lambda: lldb.SBDebugger.Terminate())
248 unittest2.main()