blob: b916cb548b67405f58e64689aefadb6d8b18b5a0 [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."""
19 self.buildDsym()
20 self.find_global_variables()
21
22 @python_api_test
23 def test_find_global_variables_with_dwarf(self):
24 """Exercise SBTarget.FindGlobalVariables() API."""
25 self.buildDwarf()
26 self.find_global_variables()
27
28 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
29 @python_api_test
Johnny Chen787f71f2011-04-22 23:20:17 +000030 def test_get_description_with_dsym(self):
31 """Exercise SBTaget.GetDescription() API."""
32 self.buildDsym()
33 self.get_description()
34
35 @python_api_test
36 def test_get_description_with_dwarf(self):
37 """Exercise SBTarget.GetDescription() API."""
38 self.buildDwarf()
39 self.get_description()
40
41 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
42 @python_api_test
Johnny Chen75625aa2011-03-07 22:29:04 +000043 def test_launch_new_process_and_redirect_stdout_with_dsym(self):
44 """Exercise SBTaget.Launch() API."""
45 self.buildDsym()
46 self.launch_new_process_and_redirect_stdout()
47
48 @python_api_test
49 def test_launch_new_process_and_redirect_stdout_with_dwarf(self):
50 """Exercise SBTarget.Launch() API."""
51 self.buildDwarf()
52 self.launch_new_process_and_redirect_stdout()
53
54 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
55 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000056 def test_resolve_symbol_context_with_address_with_dsym(self):
57 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000058 self.buildDsym()
Johnny Chen05178f62011-03-04 23:40:06 +000059 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000060
61 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000062 def test_resolve_symbol_context_with_address_with_dwarf(self):
63 """Exercise SBTarget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000064 self.buildDwarf()
Johnny Chen05178f62011-03-04 23:40:06 +000065 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000066
67 def setUp(self):
68 # Call super's setUp().
69 TestBase.setUp(self)
70 # Find the line number to of function 'c'.
71 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
72 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
73
Johnny Chen466c5932011-06-29 22:45:06 +000074 def find_global_variables(self):
75 """Exercise SBTaget.FindGlobalVariables() API."""
76 exe = os.path.join(os.getcwd(), "a.out")
77
78 # Create a target by the debugger.
79 target = self.dbg.CreateTarget(exe)
80 self.assertTrue(target, VALID_TARGET)
81
82 value_list = target.FindGlobalVariables('my_global_var_of_char_type', 1)
83 self.assertTrue(value_list.GetSize() == 1)
84 my_global_var = value_list.GetValueAtIndex(0)
85 self.expect(my_global_var.GetName(), exe=False,
86 startstr = "my_global_var_of_char_type")
87 self.expect(my_global_var.GetTypeName(), exe=False,
88 startstr = "char")
89 self.expect(my_global_var.GetValue(), exe=False,
90 startstr = "'X'")
91
Johnny Chen787f71f2011-04-22 23:20:17 +000092 def get_description(self):
93 """Exercise SBTaget.GetDescription() API."""
94 exe = os.path.join(os.getcwd(), "a.out")
95
96 # Create a target by the debugger.
97 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000098 self.assertTrue(target, VALID_TARGET)
Johnny Chen787f71f2011-04-22 23:20:17 +000099
Johnny Chen90256cd2011-04-23 00:13:34 +0000100 from lldbutil import get_description
Johnny Chenfc87e2d2011-04-25 20:23:05 +0000101
102 # get_description() allows no option to mean lldb.eDescriptionLevelBrief.
103 desc = get_description(target)
104 #desc = get_description(target, option=lldb.eDescriptionLevelBrief)
Johnny Chen90256cd2011-04-23 00:13:34 +0000105 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000106 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000107 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000108 substrs = ['a.out'])
Johnny Chen90256cd2011-04-23 00:13:34 +0000109 self.expect(desc, exe=False, matching=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000110 substrs = ['Target', 'Module', 'Breakpoint'])
111
Johnny Chen90256cd2011-04-23 00:13:34 +0000112 desc = get_description(target, option=lldb.eDescriptionLevelFull)
113 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +0000114 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +0000115 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +0000116 substrs = ['a.out', 'Target', 'Module', 'Breakpoint'])
117
118
Johnny Chen75625aa2011-03-07 22:29:04 +0000119 def launch_new_process_and_redirect_stdout(self):
120 """Exercise SBTaget.Launch() API with redirected stdout."""
121 exe = os.path.join(os.getcwd(), "a.out")
122
123 # Create a target by the debugger.
124 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000125 self.assertTrue(target, VALID_TARGET)
Johnny Chen75625aa2011-03-07 22:29:04 +0000126
Johnny Chend6481352011-03-07 22:46:30 +0000127 # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done.
128 # We should still see the entire stdout redirected once the process is finished.
129 line = line_number('main.c', '// a(3) -> c(3)')
130 breakpoint = target.BreakpointCreateByLocation('main.c', line)
131
Johnny Chen75625aa2011-03-07 22:29:04 +0000132 # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000133 # The inferior should run to completion after "process.Continue()" call.
Johnny Chen75625aa2011-03-07 22:29:04 +0000134 error = lldb.SBError()
135 process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error)
Johnny Chend6481352011-03-07 22:46:30 +0000136 process.Continue()
137 #self.runCmd("process status")
Johnny Chen75625aa2011-03-07 22:29:04 +0000138
139 # The 'stdout.txt' file should now exist.
140 self.assertTrue(os.path.isfile("stdout.txt"),
141 "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.")
142
143 # Read the output file produced by running the program.
144 with open('stdout.txt', 'r') as f:
145 output = f.read()
146
Johnny Chend6481352011-03-07 22:46:30 +0000147 # Let's delete the 'stdout.txt' file as a cleanup step.
148 try:
149 os.remove("stdout.txt")
150 pass
151 except OSError:
152 pass
153
Johnny Chen75625aa2011-03-07 22:29:04 +0000154 self.expect(output, exe=False,
155 substrs = ["a(1)", "b(2)", "a(3)"])
156
157
Johnny Chen05178f62011-03-04 23:40:06 +0000158 def resolve_symbol_context_with_address(self):
159 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +0000160 exe = os.path.join(os.getcwd(), "a.out")
161
162 # Create a target by the debugger.
163 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000164 self.assertTrue(target, VALID_TARGET)
Johnny Chend61816b2011-03-03 01:41:57 +0000165
166 # Now create the two breakpoints inside function 'a'.
167 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
168 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
Johnny Chened401982011-03-03 19:14:00 +0000169 #print "breakpoint1:", breakpoint1
170 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +0000171 self.assertTrue(breakpoint1 and
Johnny Chend61816b2011-03-03 01:41:57 +0000172 breakpoint1.GetNumLocations() == 1,
173 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000174 self.assertTrue(breakpoint2 and
Johnny Chend61816b2011-03-03 01:41:57 +0000175 breakpoint2.GetNumLocations() == 1,
176 VALID_BREAKPOINT)
177
178 # Now launch the process, and do not stop at entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000179 process = target.LaunchSimple(None, None, os.getcwd())
180 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chend61816b2011-03-03 01:41:57 +0000181
182 # Frame #0 should be on self.line1.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000183 self.assertTrue(process.GetState() == lldb.eStateStopped)
184 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000185 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
186 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000187 frame0 = thread.GetFrameAtIndex(0)
188 lineEntry = frame0.GetLineEntry()
189 self.assertTrue(lineEntry.GetLine() == self.line1)
190
191 address1 = lineEntry.GetStartAddress()
192
193 # Continue the inferior, the breakpoint 2 should be hit.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000194 process.Continue()
195 self.assertTrue(process.GetState() == lldb.eStateStopped)
196 thread = lldbutil.get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000197 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
198 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000199 frame0 = thread.GetFrameAtIndex(0)
200 lineEntry = frame0.GetLineEntry()
201 self.assertTrue(lineEntry.GetLine() == self.line2)
202
203 address2 = lineEntry.GetStartAddress()
204
Johnny Chened401982011-03-03 19:14:00 +0000205 #print "address1:", address1
206 #print "address2:", address2
Johnny Chend61816b2011-03-03 01:41:57 +0000207
208 # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
209 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
210 context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
211
Johnny Chen4ebd0192011-05-24 18:22:45 +0000212 self.assertTrue(context1 and context2)
Johnny Chened401982011-03-03 19:14:00 +0000213 #print "context1:", context1
214 #print "context2:", context2
Johnny Chend61816b2011-03-03 01:41:57 +0000215
216 # Verify that the context point to the same function 'a'.
217 symbol1 = context1.GetSymbol()
218 symbol2 = context2.GetSymbol()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000219 self.assertTrue(symbol1 and symbol2)
Johnny Chened401982011-03-03 19:14:00 +0000220 #print "symbol1:", symbol1
221 #print "symbol2:", symbol2
Johnny Chend61816b2011-03-03 01:41:57 +0000222
Johnny Chen9ae98202011-04-23 00:34:56 +0000223 from lldbutil import get_description
224 desc1 = get_description(symbol1)
225 desc2 = get_description(symbol2)
226 self.assertTrue(desc1 and desc2 and desc1 == desc2,
227 "The two addresses should resolve to the same symbol")
Johnny Chend61816b2011-03-03 01:41:57 +0000228
229
230if __name__ == '__main__':
231 import atexit
232 lldb.SBDebugger.Initialize()
233 atexit.register(lambda: lldb.SBDebugger.Terminate())
234 unittest2.main()