blob: 2fea893a1ebffea84a24ba027d6a7f0cdf4fe614 [file] [log] [blame]
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00001"""
2Test SBThread APIs.
3"""
4
Zachary Turner77db4a82015-10-22 20:06:20 +00005import lldb_shared
6
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00007import os, time
Johnny Chen2f6f7ba2011-03-07 21:28:57 +00008import lldb
Johnny Chena4603162011-03-09 23:45:56 +00009from lldbutil import get_stopped_thread, get_caller_symbol
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000010from lldbtest import *
11
12class ThreadAPITestCase(TestBase):
13
Greg Clayton4570d3e2013-12-10 23:19:29 +000014 mydir = TestBase.compute_mydir(__file__)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000015
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000016 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000017 def test_get_process(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000018 """Test Python SBThread.GetProcess() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000019 self.build()
Johnny Chenfdc94ff2011-03-11 01:16:03 +000020 self.get_process()
21
22 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000023 def test_get_stop_description(self):
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000024 """Test Python SBThread.GetStopDescription() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000025 self.build()
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000026 self.get_stop_description()
27
28 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000029 def test_run_to_address(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000030 """Test Python SBThread.RunToAddress() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000031 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000032 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000033 self.build(dictionary=d)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000034 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000035 self.run_to_address(self.exe_name)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000036
37 @python_api_test
Ed Maste362f5de2014-08-01 17:04:31 +000038 @expectedFailureFreeBSD # llvm.org/pr20476
Zachary Turner4a58dd32015-09-16 18:08:33 +000039 @expectedFailureWindows # Test crashes
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000040 def test_step_out_of_malloc_into_function_b(self):
Johnny Chena4603162011-03-09 23:45:56 +000041 """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 +000042 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000043 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000044 self.build(dictionary=d)
Johnny Chena4603162011-03-09 23:45:56 +000045 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000046 self.step_out_of_malloc_into_function_b(self.exe_name)
Johnny Chena4603162011-03-09 23:45:56 +000047
Johnny Chenf93286f2011-03-10 22:32:47 +000048 @python_api_test
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000049 def test_step_over_3_times(self):
Johnny Chenf93286f2011-03-10 22:32:47 +000050 """Test Python SBThread.StepOver() API."""
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000051 # We build a different executable than the default build() does.
Johnny Chenb7373c92011-06-23 22:11:20 +000052 d = {'CXX_SOURCES': 'main2.cpp', 'EXE': self.exe_name}
Tamas Berghammerc8fd1302015-09-30 10:12:40 +000053 self.build(dictionary=d)
Johnny Chenf93286f2011-03-10 22:32:47 +000054 self.setTearDownCleanup(dictionary=d)
Johnny Chenb7373c92011-06-23 22:11:20 +000055 self.step_over_3_times(self.exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +000056
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000057 def setUp(self):
58 # Call super's setUp().
59 TestBase.setUp(self)
Johnny Chen792ed4c2011-05-26 21:50:47 +000060 # Find the line number within main.cpp to break inside main().
Johnny Chenb7373c92011-06-23 22:11:20 +000061 self.break_line = line_number("main.cpp", "// Set break point at this line and check variable 'my_char'.")
62 # Find the line numbers within main2.cpp for step_out_of_malloc_into_function_b() and step_over_3_times().
63 self.step_out_of_malloc = line_number("main2.cpp", "// thread step-out of malloc into function b.")
64 self.after_3_step_overs = line_number("main2.cpp", "// we should reach here after 3 step-over's.")
65
66 # We'll use the test method name as the exe_name for executable comppiled from main2.cpp.
67 self.exe_name = self.testMethodName
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000068
Johnny Chenfdc94ff2011-03-11 01:16:03 +000069 def get_process(self):
70 """Test Python SBThread.GetProcess() API."""
71 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chenfdc94ff2011-03-11 01:16:03 +000072
73 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000074 self.assertTrue(target, VALID_TARGET)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000075
Johnny Chenb7373c92011-06-23 22:11:20 +000076 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000077 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenfdc94ff2011-03-11 01:16:03 +000078 self.runCmd("breakpoint list")
79
80 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +000081 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenfdc94ff2011-03-11 01:16:03 +000082
Johnny Chen5a0bee72011-06-15 22:14:12 +000083 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +000084 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chenfdc94ff2011-03-11 01:16:03 +000085 self.runCmd("process status")
86
87 proc_of_thread = thread.GetProcess()
88 #print "proc_of_thread:", proc_of_thread
Johnny Chen5a0bee72011-06-15 22:14:12 +000089 self.assertTrue(proc_of_thread.GetProcessID() == process.GetProcessID())
Johnny Chenfdc94ff2011-03-11 01:16:03 +000090
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000091 def get_stop_description(self):
Johnny Chenfdc94ff2011-03-11 01:16:03 +000092 """Test Python SBThread.GetStopDescription() API."""
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000093 exe = os.path.join(os.getcwd(), "a.out")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000094
95 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +000096 self.assertTrue(target, VALID_TARGET)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +000097
Johnny Chenb7373c92011-06-23 22:11:20 +000098 breakpoint = target.BreakpointCreateByLocation("main.cpp", self.break_line)
Johnny Chen4ebd0192011-05-24 18:22:45 +000099 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000100 #self.runCmd("breakpoint list")
101
102 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000103 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000104
Johnny Chen5a0bee72011-06-15 22:14:12 +0000105 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000106 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000107 #self.runCmd("process status")
108
109 # Due to the typemap magic (see lldb.swig), we pass in an (int)length to GetStopDescription
Johnny Chencdd7e8b2011-12-19 19:38:09 +0000110 # and expect to get a Python string as the return object!
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000111 # The 100 is just an arbitrary number specifying the buffer size.
112 stop_description = thread.GetStopDescription(100)
113 self.expect(stop_description, exe=False,
114 startstr = 'breakpoint')
115
Johnny Chenb7373c92011-06-23 22:11:20 +0000116 def step_out_of_malloc_into_function_b(self, exe_name):
Johnny Chena4603162011-03-09 23:45:56 +0000117 """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 +0000118 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chena4603162011-03-09 23:45:56 +0000119
120 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000121 self.assertTrue(target, VALID_TARGET)
Johnny Chena4603162011-03-09 23:45:56 +0000122
123 breakpoint = target.BreakpointCreateByName('malloc')
Johnny Chen4ebd0192011-05-24 18:22:45 +0000124 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chena4603162011-03-09 23:45:56 +0000125
126 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000127 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chena4603162011-03-09 23:45:56 +0000128
Johnny Chend9f2c082011-03-10 19:18:04 +0000129 while True:
Johnny Chen5a0bee72011-06-15 22:14:12 +0000130 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000131 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
Johnny Chend9f2c082011-03-10 19:18:04 +0000132 caller_symbol = get_caller_symbol(thread)
Johnny Chend9f2c082011-03-10 19:18:04 +0000133 if not caller_symbol:
134 self.fail("Test failed: could not locate the caller symbol of malloc")
Ed Maste49f359a2014-03-07 19:02:20 +0000135
136 # Our top frame may be an inlined function in malloc() (e.g., on
137 # FreeBSD). Apply a simple heuristic of stepping out until we find
138 # a non-malloc caller
139 while caller_symbol.startswith("malloc"):
140 thread.StepOut()
141 self.assertTrue(thread.IsValid(), "Thread valid after stepping to outer malloc")
142 caller_symbol = get_caller_symbol(thread)
143
Johnny Chend9f2c082011-03-10 19:18:04 +0000144 if caller_symbol == "b(int)":
145 break
Johnny Chen5a0bee72011-06-15 22:14:12 +0000146 process.Continue()
Johnny Chena4603162011-03-09 23:45:56 +0000147
Tamas Berghammer09d74b72015-06-25 15:19:22 +0000148 # On Linux malloc calls itself in some case. Remove the breakpoint because we don't want
149 # to hit it during step-out.
150 target.BreakpointDelete(breakpoint.GetID())
151
Johnny Chena4603162011-03-09 23:45:56 +0000152 thread.StepOut()
Johnny Chend9f2c082011-03-10 19:18:04 +0000153 self.runCmd("thread backtrace")
Johnny Chenb7373c92011-06-23 22:11:20 +0000154 self.assertTrue(thread.GetFrameAtIndex(0).GetLineEntry().GetLine() == self.step_out_of_malloc,
Johnny Chena4603162011-03-09 23:45:56 +0000155 "step out of malloc into function b is successful")
Johnny Chen2f6f7ba2011-03-07 21:28:57 +0000156
Johnny Chenb7373c92011-06-23 22:11:20 +0000157 def step_over_3_times(self, exe_name):
Johnny Chenf93286f2011-03-10 22:32:47 +0000158 """Test Python SBThread.StepOver() API."""
Johnny Chenb7373c92011-06-23 22:11:20 +0000159 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chenf93286f2011-03-10 22:32:47 +0000160
161 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000162 self.assertTrue(target, VALID_TARGET)
Johnny Chenf93286f2011-03-10 22:32:47 +0000163
Johnny Chenb7373c92011-06-23 22:11:20 +0000164 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000165 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chenf93286f2011-03-10 22:32:47 +0000166 self.runCmd("breakpoint list")
167
168 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000169 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chenf93286f2011-03-10 22:32:47 +0000170
Johnny Chen5a0bee72011-06-15 22:14:12 +0000171 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chenf93286f2011-03-10 22:32:47 +0000172
Johnny Chenb7373c92011-06-23 22:11:20 +0000173 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000174 self.assertTrue(process.GetState() == lldb.eStateStopped)
175 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000176 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chenf93286f2011-03-10 22:32:47 +0000177 self.runCmd("thread backtrace")
178 frame0 = thread.GetFrameAtIndex(0)
179 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000180 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chenf93286f2011-03-10 22:32:47 +0000181
182 thread.StepOver()
183 thread.StepOver()
184 thread.StepOver()
185 self.runCmd("thread backtrace")
186
187 # Verify that we are stopped at the correct source line number in main2.cpp.
188 frame0 = thread.GetFrameAtIndex(0)
189 lineEntry = frame0.GetLineEntry()
190 self.assertTrue(thread.GetStopReason() == lldb.eStopReasonPlanComplete)
Johnny Chen31963ce2011-08-19 00:54:27 +0000191 # Expected failure with clang as the compiler.
192 # rdar://problem/9223880
Johnny Chen7da349d2011-09-26 19:05:08 +0000193 #
194 # Which has been fixed on the lldb by compensating for inaccurate line
195 # table information with r140416.
Johnny Chenb7373c92011-06-23 22:11:20 +0000196 self.assertTrue(lineEntry.GetLine() == self.after_3_step_overs)
Johnny Chenf93286f2011-03-10 22:32:47 +0000197
Johnny Chen02028fa2011-06-23 21:22:01 +0000198 def run_to_address(self, exe_name):
Johnny Chen2d799cc2011-03-11 00:00:15 +0000199 """Test Python SBThread.RunToAddress() API."""
Johnny Chen02028fa2011-06-23 21:22:01 +0000200 exe = os.path.join(os.getcwd(), exe_name)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000201
202 target = self.dbg.CreateTarget(exe)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000203 self.assertTrue(target, VALID_TARGET)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000204
Johnny Chenb7373c92011-06-23 22:11:20 +0000205 breakpoint = target.BreakpointCreateByLocation('main2.cpp', self.step_out_of_malloc)
Johnny Chen4ebd0192011-05-24 18:22:45 +0000206 self.assertTrue(breakpoint, VALID_BREAKPOINT)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000207 self.runCmd("breakpoint list")
208
209 # Launch the process, and do not stop at the entry point.
Greg Claytonc6947512013-12-13 19:18:59 +0000210 process = target.LaunchSimple (None, None, self.get_process_working_directory())
Johnny Chen2d799cc2011-03-11 00:00:15 +0000211
Johnny Chen5a0bee72011-06-15 22:14:12 +0000212 self.assertTrue(process, PROCESS_IS_VALID)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000213
Johnny Chenb7373c92011-06-23 22:11:20 +0000214 # Frame #0 should be on self.step_out_of_malloc.
Johnny Chen5a0bee72011-06-15 22:14:12 +0000215 self.assertTrue(process.GetState() == lldb.eStateStopped)
216 thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
Greg Clayton53c5ddf2013-03-19 17:59:30 +0000217 self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
Johnny Chen2d799cc2011-03-11 00:00:15 +0000218 self.runCmd("thread backtrace")
219 frame0 = thread.GetFrameAtIndex(0)
220 lineEntry = frame0.GetLineEntry()
Johnny Chenb7373c92011-06-23 22:11:20 +0000221 self.assertTrue(lineEntry.GetLine() == self.step_out_of_malloc)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000222
223 # Get the start/end addresses for this line entry.
224 start_addr = lineEntry.GetStartAddress().GetLoadAddress(target)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000225 end_addr = lineEntry.GetEndAddress().GetLoadAddress(target)
Johnny Chenc1c92a22011-04-19 20:11:58 +0000226 if self.TraceOn():
227 print "start addr:", hex(start_addr)
228 print "end addr:", hex(end_addr)
Johnny Chen2d799cc2011-03-11 00:00:15 +0000229
230 # Disable the breakpoint.
231 self.assertTrue(target.DisableAllBreakpoints())
232 self.runCmd("breakpoint list")
233
234 thread.StepOver()
235 thread.StepOver()
236 thread.StepOver()
237 self.runCmd("thread backtrace")
238
239 # Now ask SBThread to run to the address 'start_addr' we got earlier, which
Johnny Chenb7373c92011-06-23 22:11:20 +0000240 # corresponds to self.step_out_of_malloc line entry's start address.
Johnny Chen2d799cc2011-03-11 00:00:15 +0000241 thread.RunToAddress(start_addr)
242 self.runCmd("process status")
243 #self.runCmd("thread backtrace")