blob: 592446949518cd517bb1078770cee67a5f9b7bec [file] [log] [blame]
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00001"""
2Test SBThread APIs.
3"""
4
5import os, time
6import unittest2
7import lldb
Johnny Chena4603162011-03-09 23:45:56 +00008from lldbutil import get_stopped_thread, get_caller_symbol
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00009from lldbtest import *
10
11class ThreadAPITestCase(TestBase):
12
13 mydir = os.path.join("python_api", "thread")
14
15 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16 @python_api_test
Johnny Chenfdc94ff2011-03-11 01:16:03 +000017 def test_get_process_with_dsym(self):
18 """Test Python SBThread.GetProcess() API."""
19 self.buildDsym()
20 self.get_process()
21
22 @python_api_test
23 def test_get_process_with_dwarf(self):
24 """Test Python SBThread.GetProcess() API."""
25 self.buildDwarf()
26 self.get_process()
27
28 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
29 @python_api_test
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000030 def test_get_stop_description_with_dsym(self):
31 """Test Python SBThread.GetStopDescription() API."""
32 self.buildDsym()
33 self.get_stop_description()
34
35 @python_api_test
36 def test_get_stop_description_with_dwarf(self):
37 """Test Python SBThread.GetStopDescription() API."""
38 self.buildDwarf()
39 self.get_stop_description()
40
Johnny Chena4603162011-03-09 23:45:56 +000041 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
42 @python_api_test
Johnny Chenfdc94ff2011-03-11 01:16:03 +000043 def test_run_to_address_with_dsym(self):
44 """Test Python SBThread.RunToAddress() API."""
45 # We build a different executable than the default buildDwarf() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000046 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chenfdc94ff2011-03-11 01:16:03 +000047 self.buildDsym(dictionary=d)
48 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000049 self.run_to_address(self.exe_name)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000050
51 @python_api_test
52 def test_run_to_address_with_dwarf(self):
53 """Test Python SBThread.RunToAddress() API."""
54 # We build a different executable than the default buildDwarf() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000055 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chenfdc94ff2011-03-11 01:16:03 +000056 self.buildDwarf(dictionary=d)
57 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000058 self.run_to_address(self.exe_name)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000059
60 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
61 @python_api_test
Johnny Chena4603162011-03-09 23:45:56 +000062 def test_step_out_of_malloc_into_function_b_with_dsym(self):
63 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
64 # We build a different executable than the default buildDsym() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000065 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chena4603162011-03-09 23:45:56 +000066 self.buildDsym(dictionary=d)
67 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000068 self.step_out_of_malloc_into_function_b(self.exe_name)
Johnny Chena4603162011-03-09 23:45:56 +000069
70 @python_api_test
71 def test_step_out_of_malloc_into_function_b_with_dwarf(self):
72 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
73 # We build a different executable than the default buildDwarf() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000074 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chena4603162011-03-09 23:45:56 +000075 self.buildDwarf(dictionary=d)
76 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000077 self.step_out_of_malloc_into_function_b(self.exe_name)
Johnny Chena4603162011-03-09 23:45:56 +000078
Johnny Chenf93286f2011-03-10 22:32:47 +000079 @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
Johnny Chen31963ce2011-08-19 00:54:27 +000080 @expectedFailureClang
Johnny Chenf93286f2011-03-10 22:32:47 +000081 @python_api_test
82 def test_step_over_3_times_with_dsym(self):
83 """Test Python SBThread.StepOver() API."""
84 # We build a different executable than the default buildDsym() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000085 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chenf93286f2011-03-10 22:32:47 +000086 self.buildDsym(dictionary=d)
87 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000088 self.step_over_3_times(self.exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +000089
Johnny Chen31963ce2011-08-19 00:54:27 +000090 @expectedFailureClang
Johnny Chenf93286f2011-03-10 22:32:47 +000091 @python_api_test
92 def test_step_over_3_times_with_dwarf(self):
93 """Test Python SBThread.StepOver() API."""
94 # We build a different executable than the default buildDwarf() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000095 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Johnny Chenf93286f2011-03-10 22:32:47 +000096 self.buildDwarf(dictionary=d)
97 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000098 self.step_over_3_times(self.exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +000099
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000100 def setUp(self):
101 # Call super's setUp().
102 TestBase.setUp(self)
Johnny Chen792ed4c2011-05-26 21:50:47 +0000103 # Find the line number within main.cpp to break inside main().
Johnny Chenb7373c92011-06-23 22:11:20 +0000104 self.break_line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.")
105 # Find the line numbers within main2.cpp for step_out_of_malloc_into_function_b() and step_over_3_times().
106 self.step_out_of_malloc = line_number("main2.cpp", "// thread step-out of malloc into function b.")
107 self.after_3_step_overs = line_number("main2.cpp", "// we should reach here after 3 step-over's.")
108
109 # We'll use the test method name as the exe_name for executable comppiled from main2.cpp.
110 self.exe_name = self.testMethodName
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000111
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000112 def get_process(self):
113 """Test Python SBThread.GetProcess() API."""
114 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000115
116 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000117 self.assertTrue(target, VALID_TARGET)
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000118
Johnny Chenb7373c92011-06-23 22:11:20 +0000119 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000120 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000121 self.runCmd("breakpoint list")
122
123 # Launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000124 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000125
Johnny Chen5a0bee72011-06-15 22:14:12 +0000126 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000127 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
128 self.runCmd("process status")
129
130 proc_of_thread = thread.GetProcess()
131 #print "proc_of_thread:", proc_of_thread
Johnny Chen5a0bee72011-06-15 22:14:12 +0000132 self.assertTrue(proc_of_thread.GetProcessID() == process.GetProcessID())
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000133
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000134 def get_stop_description(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +0000135 """Test Python SBThread.GetStopDescription() API."""
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000136 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000137
138 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000139 self.assertTrue(target, VALID_TARGET)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000140
Johnny Chenb7373c92011-06-23 22:11:20 +0000141 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000142 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000143 #self.runCmd("breakpoint list")
144
145 # Launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000146 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000147
Johnny Chen5a0bee72011-06-15 22:14:12 +0000148 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000149 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
150 #self.runCmd("process status")
151
152 # Due to the typemap magic (see lldb.swig), we pass in an (int)length to GetStopDescription
153 # and expect to get a Python string as the result object!
154 # The 100 is just an arbitrary number specifying the buffer size.
155 stop_description = thread.GetStopDescription(100)
156 self.expect(stop_description, exe=False,
157 startstr = 'breakpoint')
158
Johnny Chenb7373c92011-06-23 22:11:20 +0000159 def step_out_of_malloc_into_function_b(self, exe_name):
Johnny Chena4603162011-03-09 23:45:56 +0000160 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
Johnny Chenb7373c92011-06-23 22:11:20 +0000161 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chena4603162011-03-09 23:45:56 +0000162
163 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000164 self.assertTrue(target, VALID_TARGET)
Johnny Chena4603162011-03-09 23:45:56 +0000165
166 breakpoint = target.BreakpointCreateByName('malloc')
Johnny Chen4ebd0192011-05-24 18:22:45 +0000167 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chena4603162011-03-09 23:45:56 +0000168 self.runCmd("breakpoint list")
169
170 # Launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000171 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chena4603162011-03-09 23:45:56 +0000172
Johnny Chend9f2c082011-03-10 19:18:04 +0000173 while True:
Johnny Chen5a0bee72011-06-15 22:14:12 +0000174 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chend9f2c082011-03-10 19:18:04 +0000175 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint")
176 caller_symbol = get_caller_symbol(thread)
177 #print "caller symbol of malloc:", caller_symbol
178 if not caller_symbol:
179 self.fail("Test failed: could not locate the caller symbol of malloc")
180 if caller_symbol == "b(int)":
181 break
Johnny Chena4603162011-03-09 23:45:56 +0000182 #self.runCmd("thread backtrace")
183 #self.runCmd("process status")
Johnny Chen5a0bee72011-06-15 22:14:12 +0000184 process.Continue()
Johnny Chena4603162011-03-09 23:45:56 +0000185
186 thread.StepOut()
Johnny Chend9f2c082011-03-10 19:18:04 +0000187 self.runCmd("thread backtrace")
188 #self.runCmd("process status")
Johnny Chenb7373c92011-06-23 22:11:20 +0000189 self.assertTrue(thread.GetFrameAtIndex(0).GetLineEntry().GetLine() == self.step_out_of_malloc,
Johnny Chena4603162011-03-09 23:45:56 +0000190 "step out of malloc into function b is successful")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000191
Johnny Chenb7373c92011-06-23 22:11:20 +0000192 def step_over_3_times(self, exe_name):
Johnny Chenf93286f2011-03-10 22:32:47 +0000193 """Test Python SBThread.StepOver() API."""
Johnny Chenb7373c92011-06-23 22:11:20 +0000194 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +0000195
196 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000197 self.assertTrue(target, VALID_TARGET)
Johnny Chenf93286f2011-03-10 22:32:47 +0000198
Johnny Chenb7373c92011-06-23 22:11:20 +0000199 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000200 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenf93286f2011-03-10 22:32:47 +0000201 self.runCmd("breakpoint list")
202
203 # Launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000204 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chenf93286f2011-03-10 22:32:47 +0000205
Johnny Chen5a0bee72011-06-15 22:14:12 +0000206 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chenf93286f2011-03-10 22:32:47 +0000207
Johnny Chenb7373c92011-06-23 22:11:20 +0000208 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000209 self.assertTrue(process.GetState() == lldb.eStateStopped)
210 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chenf93286f2011-03-10 22:32:47 +0000211 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
212 self.runCmd("thread backtrace")
213 frame0 = thread.GetFrameAtIndex(0)
214 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000215 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chenf93286f2011-03-10 22:32:47 +0000216
217 thread.StepOver()
218 thread.StepOver()
219 thread.StepOver()
220 self.runCmd("thread backtrace")
221
222 # Verify that we are stopped at the correct source line number in main2.cpp.
223 frame0 = thread.GetFrameAtIndex(0)
224 lineEntry = frame0.GetLineEntry()
225 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonPlanComplete)
Johnny Chen31963ce2011-08-19 00:54:27 +0000226 # Expected failure with clang as the compiler.
227 # rdar://problem/9223880
Johnny Chenb7373c92011-06-23 22:11:20 +0000228 self.assertTrue(lineEntry.GetLine() == self.after_3_step_overs)
Johnny Chenf93286f2011-03-10 22:32:47 +0000229
Johnny Chen02028fa2011-06-23 21:22:01 +0000230 def run_to_address(self, exe_name):
Johnny Chen2d799cc2011-03-11 00:00:15 +0000231 """Test Python SBThread.RunToAddress() API."""
Johnny Chen02028fa2011-06-23 21:22:01 +0000232 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000233
234 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000235 self.assertTrue(target, VALID_TARGET)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000236
Johnny Chenb7373c92011-06-23 22:11:20 +0000237 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000238 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000239 self.runCmd("breakpoint list")
240
241 # Launch the process, and do not stop at the entry point.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000242 process = target.LaunchSimple(None, None, os.getcwd())
Johnny Chen2d799cc2011-03-11 00:00:15 +0000243
Johnny Chen5a0bee72011-06-15 22:14:12 +0000244 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000245
Johnny Chenb7373c92011-06-23 22:11:20 +0000246 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000247 self.assertTrue(process.GetState() == lldb.eStateStopped)
248 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000249 self.assertTrue(thread != None, "There should be a thread stopped due to breakpoint condition")
250 self.runCmd("thread backtrace")
251 frame0 = thread.GetFrameAtIndex(0)
252 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000253 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000254
255 # Get the start/end addresses for this line entry.
256 start_addr = lineEntry.GetStartAddress().GetLoadAddress(target)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000257 end_addr = lineEntry.GetEndAddress().GetLoadAddress(target)
Johnny Chenc1c92a22011-04-19 20:11:58 +0000258 if self.TraceOn():
259 print "start addr:", hex(start_addr)
260 print "end addr:", hex(end_addr)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000261
262 # Disable the breakpoint.
263 self.assertTrue(target.DisableAllBreakpoints())
264 self.runCmd("breakpoint list")
265
266 thread.StepOver()
267 thread.StepOver()
268 thread.StepOver()
269 self.runCmd("thread backtrace")
270
271 # Now ask SBThread to run to the address 'start_addr' we got earlier, which
Johnny Chenb7373c92011-06-23 22:11:20 +0000272 # corresponds to self.step_out_of_malloc line entry's start address.
Johnny Chen2d799cc2011-03-11 00:00:15 +0000273 thread.RunToAddress(start_addr)
274 self.runCmd("process status")
275 #self.runCmd("thread backtrace")
276
Johnny Chenf93286f2011-03-10 22:32:47 +0000277
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000278if __name__ == '__main__':
279 import atexit
280 lldb.SBDebugger.Initialize()
281 atexit.register(lambda: lldb.SBDebugger.Terminate())
282 unittest2.main()