blob: e0cfa47723827ecad3e95e70e46df2cd39f3b41a [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
Johnny Chen56b92a72011-07-11 19:15:11 +000025 # Find global variable value fails for dwarf if inferior not started
26 # (Was CrashTracer: [USER] 1 crash in Python at _lldb.so: lldb_private::MemoryCache::Read + 94)
27 #
28 # It does not segfaults now. But for dwarf, the variable value is None if
29 # the inferior process does not exist yet. The radar has been updated.
30 #@unittest232.skip("segmentation fault -- skipping")
Johnny Chen466c5932011-06-29 22:45:06 +000031 @python_api_test
32 def test_find_global_variables_with_dwarf(self):
33 """Exercise SBTarget.FindGlobalVariables() API."""
Johnny Chen086b1b72011-06-30 00:24:31 +000034 d = {'EXE': 'b.out'}
35 self.buildDwarf(dictionary=d)
36 self.setTearDownCleanup(dictionary=d)
37 self.find_global_variables('b.out')
Johnny Chen466c5932011-06-29 22:45:06 +000038
39 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
40 @python_api_test
Johnny Chen4bc80de2011-07-07 22:22:51 +000041 def test_find_functions_with_dsym(self):
42 """Exercise SBTaget.FindFunctions() API."""
43 d = {'EXE': 'a.out'}
44 self.buildDsym(dictionary=d)
45 self.setTearDownCleanup(dictionary=d)
46 self.find_functions('a.out')
47
48 @python_api_test
49 def test_find_functions_with_dwarf(self):
50 """Exercise SBTarget.FindFunctions() API."""
51 d = {'EXE': 'b.out'}
52 self.buildDwarf(dictionary=d)
53 self.setTearDownCleanup(dictionary=d)
54 self.find_functions('b.out')
55
56 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
57 @python_api_test
Johnny Chen787f71f2011-04-22 23:20:17 +000058 def test_get_description_with_dsym(self):
59 """Exercise SBTaget.GetDescription() API."""
60 self.buildDsym()
61 self.get_description()
62
63 @python_api_test
64 def test_get_description_with_dwarf(self):
65 """Exercise SBTarget.GetDescription() API."""
66 self.buildDwarf()
67 self.get_description()
68
69 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
70 @python_api_test
Johnny Chen75625aa2011-03-07 22:29:04 +000071 def test_launch_new_process_and_redirect_stdout_with_dsym(self):
72 """Exercise SBTaget.Launch() API."""
73 self.buildDsym()
74 self.launch_new_process_and_redirect_stdout()
75
76 @python_api_test
77 def test_launch_new_process_and_redirect_stdout_with_dwarf(self):
78 """Exercise SBTarget.Launch() API."""
79 self.buildDwarf()
80 self.launch_new_process_and_redirect_stdout()
81
82 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
83 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000084 def test_resolve_symbol_context_with_address_with_dsym(self):
85 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000086 self.buildDsym()
Johnny Chen05178f62011-03-04 23:40:06 +000087 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000088
89 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000090 def test_resolve_symbol_context_with_address_with_dwarf(self):
91 """Exercise SBTarget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000092 self.buildDwarf()
Johnny Chen05178f62011-03-04 23:40:06 +000093 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000094
95 def setUp(self):
96 # Call super's setUp().
97 TestBase.setUp(self)
98 # Find the line number to of function 'c'.
99 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
100 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
101
Johnny Chen086b1b72011-06-30 00:24:31 +0000102 def find_global_variables(self, exe_name):
Johnny Chen466c5932011-06-29 22:45:06 +0000103 """Exercise SBTaget.FindGlobalVariables() API."""
Johnny Chen086b1b72011-06-30 00:24:31 +0000104 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chen466c5932011-06-29 22:45:06 +0000105
106 # Create a target by the debugger.
107 target = self.dbg.CreateTarget(exe)
108 self.assertTrue(target, VALID_TARGET)
109
Johnny Chen56b92a72011-07-11 19:15:11 +0000110 #rdar://problem/9700873
111 # Find global variable value fails for dwarf if inferior not started
112 # (Was CrashTracer: [USER] 1 crash in Python at _lldb.so: lldb_private::MemoryCache::Read + 94)
113 #
114 # Remove the lines to create a breakpoint and to start the inferior
115 # which are workarounds for the dwarf case.
116
117 breakpoint = target.BreakpointCreateByLocation('main.c', self.line1)
118 self.assertTrue(breakpoint, VALID_BREAKPOINT)
119
120 # Now launch the process, and do not stop at entry point.
121 process = target.LaunchSimple(None, None, os.getcwd())
122 self.assertTrue(process, PROCESS_IS_VALID)
Jim Ingham4cda6e02011-10-07 22:23:45 +0000123 # Make sure we hit our breakpoint:
124 thread_list = lldbutil.get_threads_stopped_at_breakpoint (process, breakpoint)
125 self.assertTrue (len(thread_list) == 1)
Johnny Chen56b92a72011-07-11 19:15:11 +0000126
Johnny Chen086b1b72011-06-30 00:24:31 +0000127 value_list = target.FindGlobalVariables('my_global_var_of_char_type', 3)
Johnny Chen466c5932011-06-29 22:45:06 +0000128 self.assertTrue(value_list.GetSize() == 1)
129 my_global_var = value_list.GetValueAtIndex(0)
Johnny Chen56b92a72011-07-11 19:15:11 +0000130 self.DebugSBValue(my_global_var)
Johnny Chen4bc80de2011-07-07 22:22:51 +0000131 self.assertTrue(my_global_var)
Johnny Chen466c5932011-06-29 22:45:06 +0000132 self.expect(my_global_var.GetName(), exe=False,
133 startstr = "my_global_var_of_char_type")
134 self.expect(my_global_var.GetTypeName(), exe=False,
135 startstr = "char")
136 self.expect(my_global_var.GetValue(), exe=False,
137 startstr = "'X'")
138
Johnny Chen086b1b72011-06-30 00:24:31 +0000139 # While we are at it, let's also exercise the similar SBModule.FindGlobalVariables() API.
140 for m in target.module_iter():
141 if m.GetFileSpec().GetDirectory() == os.getcwd() and m.GetFileSpec().GetFilename() == exe_name:
142 value_list = m.FindGlobalVariables(target, 'my_global_var_of_char_type', 3)
143 self.assertTrue(value_list.GetSize() == 1)
144 self.assertTrue(value_list.GetValueAtIndex(0).GetValue() == "'X'")
145 break
146
Johnny Chen4bc80de2011-07-07 22:22:51 +0000147 def find_functions(self, exe_name):
148 """Exercise SBTaget.FindFunctions() API."""
149 exe = os.path.join(os.getcwd(), exe_name)
150
151 # Create a target by the debugger.
152 target = self.dbg.CreateTarget(exe)
153 self.assertTrue(target, VALID_TARGET)
154
155 list = lldb.SBSymbolContextList()
156 num = target.FindFunctions('c', lldb.eFunctionNameTypeAuto, False, list)
157 self.assertTrue(num == 1 and list.GetSize() == 1)
158
159 for sc in list:
Johnny Chenf8ae3c72011-07-07 22:45:54 +0000160 self.assertTrue(sc.GetModule().GetFileSpec().GetFilename() == exe_name)
Johnny Chen4bc80de2011-07-07 22:22:51 +0000161 self.assertTrue(sc.GetSymbol().GetName() == 'c')
162
Johnny Chen787f71f2011-04-22 23:20:17 +0000163 def get_description(self):
164 """Exercise SBTaget.GetDescription() API."""
165 exe = os.path.join(os.getcwd(), "a.out")
166
167 # Create a target by the debugger.
168 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000169 self.assertTrue(target, VALID_TARGET)
Johnny Chen787f71f2011-04-22 23:20:17 +0000170
Johnny Chen90256cd2011-04-23 00:13:34 +0000171 from lldbutil import get_description
Johnny Chenfc87e2d2011-04-25 20:23:05 +0000172
173 # get_description() allows no option to mean lldb.eDescriptionLevelBrief.
174 desc = get_description(target)
175 #desc = get_description(target, option=lldb.eDescriptionLevelBrief)
Johnny Chen90256cd2011-04-23 00:13:34 +0000176 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000177 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000178 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000179 substrs = ['a.out'])
Johnny Chen90256cd2011-04-23 00:13:34 +0000180 self.expect(desc, exe=False, matching=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000181 substrs = ['Target', 'Module', 'Breakpoint'])
182
Johnny Chen90256cd2011-04-23 00:13:34 +0000183 desc = get_description(target, option=lldb.eDescriptionLevelFull)
184 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000185 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000186 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000187 substrs = ['a.out', 'Target', 'Module', 'Breakpoint'])
188
189
Johnny Chen75625aa2011-03-07 22:29:04 +0000190 def launch_new_process_and_redirect_stdout(self):
191 """Exercise SBTaget.Launch() API with redirected stdout."""
192 exe = os.path.join(os.getcwd(), "a.out")
193
194 # Create a target by the debugger.
195 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000196 self.assertTrue(target, VALID_TARGET)
Johnny Chen75625aa2011-03-07 22:29:04 +0000197
Johnny Chend6481352011-03-07 22:46:30 +0000198 # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done.
199 # We should still see the entire stdout redirected once the process is finished.
200 line = line_number('main.c', '// a(3) -> c(3)')
201 breakpoint = target.BreakpointCreateByLocation('main.c', line)
202
Johnny Chen75625aa2011-03-07 22:29:04 +0000203 # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000204 # The inferior should run to completion after "process.Continue()" call.
Johnny Chen75625aa2011-03-07 22:29:04 +0000205 error = lldb.SBError()
206 process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error)
Johnny Chend6481352011-03-07 22:46:30 +0000207 process.Continue()
208 #self.runCmd("process status")
Johnny Chen75625aa2011-03-07 22:29:04 +0000209
210 # The 'stdout.txt' file should now exist.
211 self.assertTrue(os.path.isfile("stdout.txt"),
212 "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.")
213
214 # Read the output file produced by running the program.
215 with open('stdout.txt', 'r') as f:
216 output = f.read()
217
Johnny Chend6481352011-03-07 22:46:30 +0000218 # Let's delete the 'stdout.txt' file as a cleanup step.
219 try:
220 os.remove("stdout.txt")
221 pass
222 except OSError:
223 pass
224
Johnny Chen75625aa2011-03-07 22:29:04 +0000225 self.expect(output, exe=False,
226 substrs = ["a(1)", "b(2)", "a(3)"])
227
228
Johnny Chen05178f62011-03-04 23:40:06 +0000229 def resolve_symbol_context_with_address(self):
230 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +0000231 exe = os.path.join(os.getcwd(), "a.out")
232
233 # Create a target by the debugger.
234 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000235 self.assertTrue(target, VALID_TARGET)
Johnny Chend61816b2011-03-03 01:41:57 +0000236
237 # Now create the two breakpoints inside function 'a'.
238 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
239 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
Johnny Chened401982011-03-03 19:14:00 +0000240 #print "breakpoint1:", breakpoint1
241 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +0000242 self.assertTrue(breakpoint1 and
Johnny Chend61816b2011-03-03 01:41:57 +0000243 breakpoint1.GetNumLocations() == 1,
244 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000245 self.assertTrue(breakpoint2 and
Johnny Chend61816b2011-03-03 01:41:57 +0000246 breakpoint2.GetNumLocations() == 1,
247 VALID_BREAKPOINT)
248
249 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000250 process = target.LaunchSimple(None, None, os.getcwd())
251 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chend61816b2011-03-03 01:41:57 +0000252
253 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000254 self.assertTrue(process.GetState() == lldb.eStateStopped)
255 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000256 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
257 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000258 frame0 = thread.GetFrameAtIndex(0)
259 lineEntry = frame0.GetLineEntry()
260 self.assertTrue(lineEntry.GetLine() == self.line1)
261
262 address1 = lineEntry.GetStartAddress()
263
264 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000265 process.Continue()
266 self.assertTrue(process.GetState() == lldb.eStateStopped)
267 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000268 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
269 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000270 frame0 = thread.GetFrameAtIndex(0)
271 lineEntry = frame0.GetLineEntry()
272 self.assertTrue(lineEntry.GetLine() == self.line2)
273
274 address2 = lineEntry.GetStartAddress()
275
Johnny Chened401982011-03-03 19:14:00 +0000276 #print "address1:", address1
277 #print "address2:", address2
Johnny Chend61816b2011-03-03 01:41:57 +0000278
279 # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
280 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
281 context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
282
Johnny Chen4ebd0192011-05-24 18:22:45 +0000283 self.assertTrue(context1 and context2)
Johnny Chened401982011-03-03 19:14:00 +0000284 #print "context1:", context1
285 #print "context2:", context2
Johnny Chend61816b2011-03-03 01:41:57 +0000286
287 # Verify that the context point to the same function 'a'.
288 symbol1 = context1.GetSymbol()
289 symbol2 = context2.GetSymbol()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000290 self.assertTrue(symbol1 and symbol2)
Johnny Chened401982011-03-03 19:14:00 +0000291 #print "symbol1:", symbol1
292 #print "symbol2:", symbol2
Johnny Chend61816b2011-03-03 01:41:57 +0000293
Johnny Chen9ae98202011-04-23 00:34:56 +0000294 from lldbutil import get_description
295 desc1 = get_description(symbol1)
296 desc2 = get_description(symbol2)
297 self.assertTrue(desc1 and desc2 and desc1 == desc2,
298 "The two addresses should resolve to the same symbol")
Johnny Chend61816b2011-03-03 01:41:57 +0000299
300
301if __name__ == '__main__':
302 import atexit
303 lldb.SBDebugger.Initialize()
304 atexit.register(lambda: lldb.SBDebugger.Terminate())
305 unittest2.main()