blob: de845cc55803598fd8468798fc4625bc72fa9c82 [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
Greg Clayton5569e642012-02-06 01:44:54 +0000155 list = target.FindFunctions('c', lldb.eFunctionNameTypeAuto)
156 self.assertTrue(list.GetSize() == 1)
Johnny Chen4bc80de2011-07-07 22:22:51 +0000157
158 for sc in list:
Johnny Chenf8ae3c72011-07-07 22:45:54 +0000159 self.assertTrue(sc.GetModule().GetFileSpec().GetFilename() == exe_name)
Johnny Chen4bc80de2011-07-07 22:22:51 +0000160 self.assertTrue(sc.GetSymbol().GetName() == 'c')
161
Johnny Chen787f71f2011-04-22 23:20:17 +0000162 def get_description(self):
163 """Exercise SBTaget.GetDescription() API."""
164 exe = os.path.join(os.getcwd(), "a.out")
165
166 # Create a target by the debugger.
167 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000168 self.assertTrue(target, VALID_TARGET)
Johnny Chen787f71f2011-04-22 23:20:17 +0000169
Johnny Chen90256cd2011-04-23 00:13:34 +0000170 from lldbutil import get_description
Johnny Chenfc87e2d2011-04-25 20:23:05 +0000171
172 # get_description() allows no option to mean lldb.eDescriptionLevelBrief.
173 desc = get_description(target)
174 #desc = get_description(target, option=lldb.eDescriptionLevelBrief)
Johnny Chen90256cd2011-04-23 00:13:34 +0000175 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000176 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000177 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000178 substrs = ['a.out'])
Johnny Chen90256cd2011-04-23 00:13:34 +0000179 self.expect(desc, exe=False, matching=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000180 substrs = ['Target', 'Module', 'Breakpoint'])
181
Johnny Chen90256cd2011-04-23 00:13:34 +0000182 desc = get_description(target, option=lldb.eDescriptionLevelFull)
183 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000184 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000185 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000186 substrs = ['a.out', 'Target', 'Module', 'Breakpoint'])
187
188
Johnny Chen75625aa2011-03-07 22:29:04 +0000189 def launch_new_process_and_redirect_stdout(self):
190 """Exercise SBTaget.Launch() API with redirected stdout."""
191 exe = os.path.join(os.getcwd(), "a.out")
192
193 # Create a target by the debugger.
194 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000195 self.assertTrue(target, VALID_TARGET)
Johnny Chen75625aa2011-03-07 22:29:04 +0000196
Johnny Chend6481352011-03-07 22:46:30 +0000197 # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done.
198 # We should still see the entire stdout redirected once the process is finished.
199 line = line_number('main.c', '// a(3) -> c(3)')
200 breakpoint = target.BreakpointCreateByLocation('main.c', line)
201
Johnny Chen75625aa2011-03-07 22:29:04 +0000202 # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000203 # The inferior should run to completion after "process.Continue()" call.
Johnny Chen75625aa2011-03-07 22:29:04 +0000204 error = lldb.SBError()
205 process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error)
Johnny Chend6481352011-03-07 22:46:30 +0000206 process.Continue()
207 #self.runCmd("process status")
Johnny Chen75625aa2011-03-07 22:29:04 +0000208
209 # The 'stdout.txt' file should now exist.
210 self.assertTrue(os.path.isfile("stdout.txt"),
211 "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.")
212
213 # Read the output file produced by running the program.
214 with open('stdout.txt', 'r') as f:
215 output = f.read()
216
Johnny Chend6481352011-03-07 22:46:30 +0000217 # Let's delete the 'stdout.txt' file as a cleanup step.
218 try:
219 os.remove("stdout.txt")
220 pass
221 except OSError:
222 pass
223
Johnny Chen75625aa2011-03-07 22:29:04 +0000224 self.expect(output, exe=False,
225 substrs = ["a(1)", "b(2)", "a(3)"])
226
227
Johnny Chen05178f62011-03-04 23:40:06 +0000228 def resolve_symbol_context_with_address(self):
229 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +0000230 exe = os.path.join(os.getcwd(), "a.out")
231
232 # Create a target by the debugger.
233 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000234 self.assertTrue(target, VALID_TARGET)
Johnny Chend61816b2011-03-03 01:41:57 +0000235
236 # Now create the two breakpoints inside function 'a'.
237 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
238 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
Johnny Chened401982011-03-03 19:14:00 +0000239 #print "breakpoint1:", breakpoint1
240 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +0000241 self.assertTrue(breakpoint1 and
Johnny Chend61816b2011-03-03 01:41:57 +0000242 breakpoint1.GetNumLocations() == 1,
243 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000244 self.assertTrue(breakpoint2 and
Johnny Chend61816b2011-03-03 01:41:57 +0000245 breakpoint2.GetNumLocations() == 1,
246 VALID_BREAKPOINT)
247
248 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000249 process = target.LaunchSimple(None, None, os.getcwd())
250 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chend61816b2011-03-03 01:41:57 +0000251
252 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000253 self.assertTrue(process.GetState() == lldb.eStateStopped)
254 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000255 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
256 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000257 frame0 = thread.GetFrameAtIndex(0)
258 lineEntry = frame0.GetLineEntry()
259 self.assertTrue(lineEntry.GetLine() == self.line1)
260
261 address1 = lineEntry.GetStartAddress()
262
263 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000264 process.Continue()
265 self.assertTrue(process.GetState() == lldb.eStateStopped)
266 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000267 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
268 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000269 frame0 = thread.GetFrameAtIndex(0)
270 lineEntry = frame0.GetLineEntry()
271 self.assertTrue(lineEntry.GetLine() == self.line2)
272
273 address2 = lineEntry.GetStartAddress()
274
Johnny Chened401982011-03-03 19:14:00 +0000275 #print "address1:", address1
276 #print "address2:", address2
Johnny Chend61816b2011-03-03 01:41:57 +0000277
278 # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
279 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
280 context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
281
Johnny Chen4ebd0192011-05-24 18:22:45 +0000282 self.assertTrue(context1 and context2)
Johnny Chened401982011-03-03 19:14:00 +0000283 #print "context1:", context1
284 #print "context2:", context2
Johnny Chend61816b2011-03-03 01:41:57 +0000285
286 # Verify that the context point to the same function 'a'.
287 symbol1 = context1.GetSymbol()
288 symbol2 = context2.GetSymbol()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000289 self.assertTrue(symbol1 and symbol2)
Johnny Chened401982011-03-03 19:14:00 +0000290 #print "symbol1:", symbol1
291 #print "symbol2:", symbol2
Johnny Chend61816b2011-03-03 01:41:57 +0000292
Johnny Chen9ae98202011-04-23 00:34:56 +0000293 from lldbutil import get_description
294 desc1 = get_description(symbol1)
295 desc2 = get_description(symbol2)
296 self.assertTrue(desc1 and desc2 and desc1 == desc2,
297 "The two addresses should resolve to the same symbol")
Johnny Chend61816b2011-03-03 01:41:57 +0000298
299
300if __name__ == '__main__':
301 import atexit
302 lldb.SBDebugger.Initialize()
303 atexit.register(lambda: lldb.SBDebugger.Terminate())
304 unittest2.main()