blob: 0de456396c75e10402a38f4296567c4c5208b5ff [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 Chen787f71f2011-04-22 23:20:17 +000017 def test_get_description_with_dsym(self):
18 """Exercise SBTaget.GetDescription() API."""
19 self.buildDsym()
20 self.get_description()
21
22 @python_api_test
23 def test_get_description_with_dwarf(self):
24 """Exercise SBTarget.GetDescription() API."""
25 self.buildDwarf()
26 self.get_description()
27
28 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
29 @python_api_test
Johnny Chen75625aa2011-03-07 22:29:04 +000030 def test_launch_new_process_and_redirect_stdout_with_dsym(self):
31 """Exercise SBTaget.Launch() API."""
32 self.buildDsym()
33 self.launch_new_process_and_redirect_stdout()
34
35 @python_api_test
36 def test_launch_new_process_and_redirect_stdout_with_dwarf(self):
37 """Exercise SBTarget.Launch() API."""
38 self.buildDwarf()
39 self.launch_new_process_and_redirect_stdout()
40
41 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
42 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000043 def test_resolve_symbol_context_with_address_with_dsym(self):
44 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000045 self.buildDsym()
Johnny Chen05178f62011-03-04 23:40:06 +000046 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000047
48 @python_api_test
Johnny Chen05178f62011-03-04 23:40:06 +000049 def test_resolve_symbol_context_with_address_with_dwarf(self):
50 """Exercise SBTarget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +000051 self.buildDwarf()
Johnny Chen05178f62011-03-04 23:40:06 +000052 self.resolve_symbol_context_with_address()
Johnny Chend61816b2011-03-03 01:41:57 +000053
54 def setUp(self):
55 # Call super's setUp().
56 TestBase.setUp(self)
57 # Find the line number to of function 'c'.
58 self.line1 = line_number('main.c', '// Find the line number for breakpoint 1 here.')
59 self.line2 = line_number('main.c', '// Find the line number for breakpoint 2 here.')
60
Johnny Chen787f71f2011-04-22 23:20:17 +000061 def get_description(self):
62 """Exercise SBTaget.GetDescription() API."""
63 exe = os.path.join(os.getcwd(), "a.out")
64
65 # Create a target by the debugger.
66 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000067 self.assertTrue(target, VALID_TARGET)
Johnny Chen787f71f2011-04-22 23:20:17 +000068
Johnny Chen90256cd2011-04-23 00:13:34 +000069 from lldbutil import get_description
Johnny Chenfc87e2d2011-04-25 20:23:05 +000070
71 # get_description() allows no option to mean lldb.eDescriptionLevelBrief.
72 desc = get_description(target)
73 #desc = get_description(target, option=lldb.eDescriptionLevelBrief)
Johnny Chen90256cd2011-04-23 00:13:34 +000074 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +000075 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +000076 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +000077 substrs = ['a.out'])
Johnny Chen90256cd2011-04-23 00:13:34 +000078 self.expect(desc, exe=False, matching=False,
Johnny Chen787f71f2011-04-22 23:20:17 +000079 substrs = ['Target', 'Module', 'Breakpoint'])
80
Johnny Chen90256cd2011-04-23 00:13:34 +000081 desc = get_description(target, option=lldb.eDescriptionLevelFull)
82 if not desc:
Johnny Chen787f71f2011-04-22 23:20:17 +000083 self.fail("SBTarget.GetDescription() failed")
Johnny Chen90256cd2011-04-23 00:13:34 +000084 self.expect(desc, exe=False,
Johnny Chen787f71f2011-04-22 23:20:17 +000085 substrs = ['a.out', 'Target', 'Module', 'Breakpoint'])
86
87
Johnny Chen75625aa2011-03-07 22:29:04 +000088 def launch_new_process_and_redirect_stdout(self):
89 """Exercise SBTaget.Launch() API with redirected stdout."""
90 exe = os.path.join(os.getcwd(), "a.out")
91
92 # Create a target by the debugger.
93 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000094 self.assertTrue(target, VALID_TARGET)
Johnny Chen75625aa2011-03-07 22:29:04 +000095
Johnny Chend6481352011-03-07 22:46:30 +000096 # Add an extra twist of stopping the inferior in a breakpoint, and then continue till it's done.
97 # We should still see the entire stdout redirected once the process is finished.
98 line = line_number('main.c', '// a(3) -> c(3)')
99 breakpoint = target.BreakpointCreateByLocation('main.c', line)
100
Johnny Chen75625aa2011-03-07 22:29:04 +0000101 # Now launch the process, do not stop at entry point, and redirect stdout to "stdout.txt" file.
Johnny Chend6481352011-03-07 22:46:30 +0000102 # The inferior should run to completion after "process.Continue()" call, so there's no need
103 # to assign to self.process to have the inferior kiiled during test teardown.
Johnny Chen75625aa2011-03-07 22:29:04 +0000104 error = lldb.SBError()
105 process = target.Launch (self.dbg.GetListener(), None, None, None, "stdout.txt", None, None, 0, False, error)
Johnny Chend6481352011-03-07 22:46:30 +0000106 process.Continue()
107 #self.runCmd("process status")
Johnny Chen75625aa2011-03-07 22:29:04 +0000108
109 # The 'stdout.txt' file should now exist.
110 self.assertTrue(os.path.isfile("stdout.txt"),
111 "'stdout.txt' exists due to redirected stdout via SBTarget.Launch() API.")
112
113 # Read the output file produced by running the program.
114 with open('stdout.txt', 'r') as f:
115 output = f.read()
116
Johnny Chend6481352011-03-07 22:46:30 +0000117 # Let's delete the 'stdout.txt' file as a cleanup step.
118 try:
119 os.remove("stdout.txt")
120 pass
121 except OSError:
122 pass
123
Johnny Chen75625aa2011-03-07 22:29:04 +0000124 self.expect(output, exe=False,
125 substrs = ["a(1)", "b(2)", "a(3)"])
126
127
Johnny Chen05178f62011-03-04 23:40:06 +0000128 def resolve_symbol_context_with_address(self):
129 """Exercise SBTaget.ResolveSymbolContextForAddress() API."""
Johnny Chend61816b2011-03-03 01:41:57 +0000130 exe = os.path.join(os.getcwd(), "a.out")
131
132 # Create a target by the debugger.
133 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000134 self.assertTrue(target, VALID_TARGET)
Johnny Chend61816b2011-03-03 01:41:57 +0000135
136 # Now create the two breakpoints inside function 'a'.
137 breakpoint1 = target.BreakpointCreateByLocation('main.c', self.line1)
138 breakpoint2 = target.BreakpointCreateByLocation('main.c', self.line2)
Johnny Chened401982011-03-03 19:14:00 +0000139 #print "breakpoint1:", breakpoint1
140 #print "breakpoint2:", breakpoint2
Johnny Chen4ebd0192011-05-24 18:22:45 +0000141 self.assertTrue(breakpoint1 and
Johnny Chend61816b2011-03-03 01:41:57 +0000142 breakpoint1.GetNumLocations() == 1,
143 VALID_BREAKPOINT)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000144 self.assertTrue(breakpoint2 and
Johnny Chend61816b2011-03-03 01:41:57 +0000145 breakpoint2.GetNumLocations() == 1,
146 VALID_BREAKPOINT)
147
148 # Now launch the process, and do not stop at entry point.
Johnny Chenbd9b6e92011-04-19 19:54:06 +0000149 self.process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chend61816b2011-03-03 01:41:57 +0000150
151 self.process = target.GetProcess()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000152 self.assertTrue(self.process, PROCESS_IS_VALID)
Johnny Chend61816b2011-03-03 01:41:57 +0000153
154 # Frame #0 should be on self.line1.
155 self.assertTrue(self.process.GetState() == lldb.eStateStopped)
156 thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000157 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
158 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000159 frame0 = thread.GetFrameAtIndex(0)
160 lineEntry = frame0.GetLineEntry()
161 self.assertTrue(lineEntry.GetLine() == self.line1)
162
163 address1 = lineEntry.GetStartAddress()
164
165 # Continue the inferior, the breakpoint 2 should be hit.
166 self.process.Continue()
167 self.assertTrue(self.process.GetState() == lldb.eStateStopped)
168 thread = lldbutil.get_stopped_thread(self.process, lldb.eStopReasonBreakpoint)
Johnny Chened401982011-03-03 19:14:00 +0000169 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
170 #self.runCmd("process status")
Johnny Chend61816b2011-03-03 01:41:57 +0000171 frame0 = thread.GetFrameAtIndex(0)
172 lineEntry = frame0.GetLineEntry()
173 self.assertTrue(lineEntry.GetLine() == self.line2)
174
175 address2 = lineEntry.GetStartAddress()
176
Johnny Chened401982011-03-03 19:14:00 +0000177 #print "address1:", address1
178 #print "address2:", address2
Johnny Chend61816b2011-03-03 01:41:57 +0000179
180 # Now call SBTarget.ResolveSymbolContextForAddress() with the addresses from our line entry.
181 context1 = target.ResolveSymbolContextForAddress(address1, lldb.eSymbolContextEverything)
182 context2 = target.ResolveSymbolContextForAddress(address2, lldb.eSymbolContextEverything)
183
Johnny Chen4ebd0192011-05-24 18:22:45 +0000184 self.assertTrue(context1 and context2)
Johnny Chened401982011-03-03 19:14:00 +0000185 #print "context1:", context1
186 #print "context2:", context2
Johnny Chend61816b2011-03-03 01:41:57 +0000187
188 # Verify that the context point to the same function 'a'.
189 symbol1 = context1.GetSymbol()
190 symbol2 = context2.GetSymbol()
Johnny Chen4ebd0192011-05-24 18:22:45 +0000191 self.assertTrue(symbol1 and symbol2)
Johnny Chened401982011-03-03 19:14:00 +0000192 #print "symbol1:", symbol1
193 #print "symbol2:", symbol2
Johnny Chend61816b2011-03-03 01:41:57 +0000194
Johnny Chen9ae98202011-04-23 00:34:56 +0000195 from lldbutil import get_description
196 desc1 = get_description(symbol1)
197 desc2 = get_description(symbol2)
198 self.assertTrue(desc1 and desc2 and desc1 == desc2,
199 "The two addresses should resolve to the same symbol")
Johnny Chend61816b2011-03-03 01:41:57 +0000200
201
202if __name__ == '__main__':
203 import atexit
204 lldb.SBDebugger.Initialize()
205 atexit.register(lambda: lldb.SBDebugger.Terminate())
206 unittest2.main()