blob: 4d8e9b4872d37973aa82b13b404abf3fbb3d8838 [file] [log] [blame]
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00001"""
2Test SBThread APIs.
3"""
4
Zachary Turner35d017f2015-10-23 17:04:29 +00005from __future__ import print_function
6
Zachary Turner77db4a82015-10-22 20:06:20 +00007import lldb_shared
8
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00009import os, time
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000010import lldb
Johnny Chena4603162011-03-09 23:45:56 +000011from lldbutil import get_stopped_thread, get_caller_symbol
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000012from lldbtest import *
13
14class ThreadAPITestCase(TestBase):
15
Greg Clayton4570d3e2013-12-10 23:19:29 +000016 mydir = TestBase.compute_mydir(__file__)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000017
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000018 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000019 def test_get_process(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000020 """Test Python SBThread.GetProcess() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000021 self.build()
Johnny Chenfdc94ff2011-03-11 01:16:03 +000022 self.get_process()
23
24 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000025 def test_get_stop_description(self):
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000026 """Test Python SBThread.GetStopDescription() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000027 self.build()
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000028 self.get_stop_description()
29
30 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000031 def test_run_to_address(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000032 """Test Python SBThread.RunToAddress() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000033 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000034 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000035 self.build(dictionary=d)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000036 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000037 self.run_to_address(self.exe_name)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000038
39 @python_api_test
Ed Maste362f5de2014-08-01 17:04:31 +000040 @expectedFailureFreeBSD # llvm.org/pr20476
Zachary Turner4a58dd32015-09-16 18:08:33 +000041 @expectedFailureWindows # Test crashes
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000042 def test_step_out_of_malloc_into_function_b(self):
Johnny Chena4603162011-03-09 23:45:56 +000043 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000044 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000045 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000046 self.build(dictionary=d)
Johnny Chena4603162011-03-09 23:45:56 +000047 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000048 self.step_out_of_malloc_into_function_b(self.exe_name)
Johnny Chena4603162011-03-09 23:45:56 +000049
Johnny Chenf93286f2011-03-10 22:32:47 +000050 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000051 def test_step_over_3_times(self):
Johnny Chenf93286f2011-03-10 22:32:47 +000052 """Test Python SBThread.StepOver() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000053 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000054 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000055 self.build(dictionary=d)
Johnny Chenf93286f2011-03-10 22:32:47 +000056 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000057 self.step_over_3_times(self.exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +000058
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000059 def setUp(self):
60 # Call super's setUp().
61 TestBase.setUp(self)
Johnny Chen792ed4c2011-05-26 21:50:47 +000062 # Find the line number within main.cpp to break inside main().
Johnny Chenb7373c92011-06-23 22:11:20 +000063 self.break_line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.")
64 # Find the line numbers within main2.cpp for step_out_of_malloc_into_function_b() and step_over_3_times().
65 self.step_out_of_malloc = line_number("main2.cpp", "// thread step-out of malloc into function b.")
66 self.after_3_step_overs = line_number("main2.cpp", "// we should reach here after 3 step-over's.")
67
68 # We'll use the test method name as the exe_name for executable comppiled from main2.cpp.
69 self.exe_name = self.testMethodName
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000070
Johnny Chenfdc94ff2011-03-11 01:16:03 +000071 def get_process(self):
72 """Test Python SBThread.GetProcess() API."""
73 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenfdc94ff2011-03-11 01:16:03 +000074
75 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000076 self.assertTrue(target, VALID_TARGET)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000077
Johnny Chenb7373c92011-06-23 22:11:20 +000078 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000079 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000080 self.runCmd("breakpoint list")
81
82 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000083 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenfdc94ff2011-03-11 01:16:03 +000084
Johnny Chen5a0bee72011-06-15 22:14:12 +000085 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000086 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chenfdc94ff2011-03-11 01:16:03 +000087 self.runCmd("process status")
88
89 proc_of_thread = thread.GetProcess()
Zachary Turner35d017f2015-10-23 17:04:29 +000090 #print("proc_of_thread:", proc_of_thread)
Johnny Chen5a0bee72011-06-15 22:14:12 +000091 self.assertTrue(proc_of_thread.GetProcessID() == process.GetProcessID())
Johnny Chenfdc94ff2011-03-11 01:16:03 +000092
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000093 def get_stop_description(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000094 """Test Python SBThread.GetStopDescription() API."""
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000095 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000096
97 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000098 self.assertTrue(target, VALID_TARGET)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000099
Johnny Chenb7373c92011-06-23 22:11:20 +0000100 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000101 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000102 #self.runCmd("breakpoint list")
103
104 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000105 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000106
Johnny Chen5a0bee72011-06-15 22:14:12 +0000107 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000108 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000109 #self.runCmd("process status")
110
111 # Due to the typemap magic (see lldb.swig), we pass in an (int)length to GetStopDescription
Johnny Chencdd7e8b2011-12-19 19:38:09 +0000112 # and expect to get a Python string as the return object!
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000113 # The 100 is just an arbitrary number specifying the buffer size.
114 stop_description = thread.GetStopDescription(100)
115 self.expect(stop_description, exe=False,
116 startstr = 'breakpoint')
117
Johnny Chenb7373c92011-06-23 22:11:20 +0000118 def step_out_of_malloc_into_function_b(self, exe_name):
Johnny Chena4603162011-03-09 23:45:56 +0000119 """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 +0000120 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chena4603162011-03-09 23:45:56 +0000121
122 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000123 self.assertTrue(target, VALID_TARGET)
Johnny Chena4603162011-03-09 23:45:56 +0000124
125 breakpoint = target.BreakpointCreateByName('malloc')
Johnny Chen4ebd0192011-05-24 18:22:45 +0000126 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chena4603162011-03-09 23:45:56 +0000127
128 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000129 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chena4603162011-03-09 23:45:56 +0000130
Johnny Chend9f2c082011-03-10 19:18:04 +0000131 while True:
Johnny Chen5a0bee72011-06-15 22:14:12 +0000132 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000133 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend9f2c082011-03-10 19:18:04 +0000134 caller_symbol = get_caller_symbol(thread)
Johnny Chend9f2c082011-03-10 19:18:04 +0000135 if not caller_symbol:
136 self.fail("Test failed: could not locate the caller symbol of malloc")
Ed Maste49f359a2014-03-07 19:02:20 +0000137
138 # Our top frame may be an inlined function in malloc() (e.g., on
139 # FreeBSD). Apply a simple heuristic of stepping out until we find
140 # a non-malloc caller
141 while caller_symbol.startswith("malloc"):
142 thread.StepOut()
143 self.assertTrue(thread.IsValid(), "Thread valid after stepping to outer malloc")
144 caller_symbol = get_caller_symbol(thread)
145
Johnny Chend9f2c082011-03-10 19:18:04 +0000146 if caller_symbol == "b(int)":
147 break
Johnny Chen5a0bee72011-06-15 22:14:12 +0000148 process.Continue()
Johnny Chena4603162011-03-09 23:45:56 +0000149
Tamas Berghammer09d74b72015-06-25 15:19:22 +0000150 # On Linux malloc calls itself in some case. Remove the breakpoint because we don't want
151 # to hit it during step-out.
152 target.BreakpointDelete(breakpoint.GetID())
153
Johnny Chena4603162011-03-09 23:45:56 +0000154 thread.StepOut()
Johnny Chend9f2c082011-03-10 19:18:04 +0000155 self.runCmd("thread backtrace")
Johnny Chenb7373c92011-06-23 22:11:20 +0000156 self.assertTrue(thread.GetFrameAtIndex(0).GetLineEntry().GetLine() == self.step_out_of_malloc,
Johnny Chena4603162011-03-09 23:45:56 +0000157 "step out of malloc into function b is successful")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000158
Johnny Chenb7373c92011-06-23 22:11:20 +0000159 def step_over_3_times(self, exe_name):
Johnny Chenf93286f2011-03-10 22:32:47 +0000160 """Test Python SBThread.StepOver() API."""
Johnny Chenb7373c92011-06-23 22:11:20 +0000161 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +0000162
163 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000164 self.assertTrue(target, VALID_TARGET)
Johnny Chenf93286f2011-03-10 22:32:47 +0000165
Johnny Chenb7373c92011-06-23 22:11:20 +0000166 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000167 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenf93286f2011-03-10 22:32:47 +0000168 self.runCmd("breakpoint list")
169
170 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000171 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenf93286f2011-03-10 22:32:47 +0000172
Johnny Chen5a0bee72011-06-15 22:14:12 +0000173 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chenf93286f2011-03-10 22:32:47 +0000174
Johnny Chenb7373c92011-06-23 22:11:20 +0000175 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000176 self.assertTrue(process.GetState() == lldb.eStateStopped)
177 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000178 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chenf93286f2011-03-10 22:32:47 +0000179 self.runCmd("thread backtrace")
180 frame0 = thread.GetFrameAtIndex(0)
181 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000182 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chenf93286f2011-03-10 22:32:47 +0000183
184 thread.StepOver()
185 thread.StepOver()
186 thread.StepOver()
187 self.runCmd("thread backtrace")
188
189 # Verify that we are stopped at the correct source line number in main2.cpp.
190 frame0 = thread.GetFrameAtIndex(0)
191 lineEntry = frame0.GetLineEntry()
192 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonPlanComplete)
Johnny Chen31963ce2011-08-19 00:54:27 +0000193 # Expected failure with clang as the compiler.
194 # rdar://problem/9223880
Johnny Chen7da349d2011-09-26 19:05:08 +0000195 #
196 # Which has been fixed on the lldb by compensating for inaccurate line
197 # table information with r140416.
Johnny Chenb7373c92011-06-23 22:11:20 +0000198 self.assertTrue(lineEntry.GetLine() == self.after_3_step_overs)
Johnny Chenf93286f2011-03-10 22:32:47 +0000199
Johnny Chen02028fa2011-06-23 21:22:01 +0000200 def run_to_address(self, exe_name):
Johnny Chen2d799cc2011-03-11 00:00:15 +0000201 """Test Python SBThread.RunToAddress() API."""
Johnny Chen02028fa2011-06-23 21:22:01 +0000202 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000203
204 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000205 self.assertTrue(target, VALID_TARGET)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000206
Johnny Chenb7373c92011-06-23 22:11:20 +0000207 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000208 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000209 self.runCmd("breakpoint list")
210
211 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000212 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen2d799cc2011-03-11 00:00:15 +0000213
Johnny Chen5a0bee72011-06-15 22:14:12 +0000214 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000215
Johnny Chenb7373c92011-06-23 22:11:20 +0000216 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000217 self.assertTrue(process.GetState() == lldb.eStateStopped)
218 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000219 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chen2d799cc2011-03-11 00:00:15 +0000220 self.runCmd("thread backtrace")
221 frame0 = thread.GetFrameAtIndex(0)
222 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000223 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000224
225 # Get the start/end addresses for this line entry.
226 start_addr = lineEntry.GetStartAddress().GetLoadAddress(target)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000227 end_addr = lineEntry.GetEndAddress().GetLoadAddress(target)
Johnny Chenc1c92a22011-04-19 20:11:58 +0000228 if self.TraceOn():
Zachary Turner35d017f2015-10-23 17:04:29 +0000229 print("start addr:", hex(start_addr))
230 print("end addr:", hex(end_addr))
Johnny Chen2d799cc2011-03-11 00:00:15 +0000231
232 # Disable the breakpoint.
233 self.assertTrue(target.DisableAllBreakpoints())
234 self.runCmd("breakpoint list")
235
236 thread.StepOver()
237 thread.StepOver()
238 thread.StepOver()
239 self.runCmd("thread backtrace")
240
241 # Now ask SBThread to run to the address 'start_addr' we got earlier, which
Johnny Chenb7373c92011-06-23 22:11:20 +0000242 # corresponds to self.step_out_of_malloc line entry's start address.
Johnny Chen2d799cc2011-03-11 00:00:15 +0000243 thread.RunToAddress(start_addr)
244 self.runCmd("process status")
245 #self.runCmd("thread backtrace")