6 from lldbsuite
.test
.decorators
import *
7 from lldbsuite
.test
.lldbtest
import *
8 from lldbsuite
.test
import lldbutil
9 from lldbsuite
.test
.lldbutil
import get_stopped_thread
, get_caller_symbol
12 class ThreadAPITestCase(TestBase
):
13 def test_get_process(self
):
14 """Test Python SBThread.GetProcess() API."""
18 def test_get_stop_description(self
):
19 """Test Python SBThread.GetStopDescription() API."""
21 self
.get_stop_description()
23 def test_run_to_address(self
):
24 """Test Python SBThread.RunToAddress() API."""
25 # We build a different executable than the default build() does.
26 d
= {"CXX_SOURCES": "main2.cpp", "EXE": self
.exe_name
}
27 self
.build(dictionary
=d
)
28 self
.setTearDownCleanup(dictionary
=d
)
29 self
.run_to_address(self
.exe_name
)
31 @skipIfAsan # The output looks different under ASAN.
32 @expectedFailureAll(oslist
=["linux"], archs
=["arm"], bugnumber
="llvm.org/pr45892")
33 @expectedFailureAll(oslist
=["windows"])
34 def test_step_out_of_malloc_into_function_b(self
):
35 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
36 # We build a different executable than the default build() does.
37 d
= {"CXX_SOURCES": "main2.cpp", "EXE": self
.exe_name
}
38 self
.build(dictionary
=d
)
39 self
.setTearDownCleanup(dictionary
=d
)
40 self
.step_out_of_malloc_into_function_b(self
.exe_name
)
42 def test_step_over_3_times(self
):
43 """Test Python SBThread.StepOver() API."""
44 # We build a different executable than the default build() does.
45 d
= {"CXX_SOURCES": "main2.cpp", "EXE": self
.exe_name
}
46 self
.build(dictionary
=d
)
47 self
.setTearDownCleanup(dictionary
=d
)
48 self
.step_over_3_times(self
.exe_name
)
50 def test_negative_indexing(self
):
51 """Test SBThread.frame with negative indexes."""
53 self
.validate_negative_indexing()
56 # Call super's setUp().
58 # Find the line number within main.cpp to break inside main().
59 self
.break_line
= line_number(
60 "main.cpp", "// Set break point at this line and check variable 'my_char'."
62 # Find the line numbers within main2.cpp for
63 # step_out_of_malloc_into_function_b() and step_over_3_times().
64 self
.step_out_of_malloc
= line_number(
65 "main2.cpp", "// thread step-out of malloc into function b."
67 self
.after_3_step_overs
= line_number(
68 "main2.cpp", "// we should reach here after 3 step-over's."
71 # We'll use the test method name as the exe_name for executable
72 # compiled from main2.cpp.
73 self
.exe_name
= self
.testMethodName
75 def get_process(self
):
76 """Test Python SBThread.GetProcess() API."""
77 exe
= self
.getBuildArtifact("a.out")
79 target
= self
.dbg
.CreateTarget(exe
)
80 self
.assertTrue(target
, VALID_TARGET
)
82 breakpoint
= target
.BreakpointCreateByLocation("main.cpp", self
.break_line
)
83 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
84 self
.runCmd("breakpoint list")
86 # Launch the process, and do not stop at the entry point.
87 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
89 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
91 thread
.IsValid(), "There should be a thread stopped due to breakpoint"
93 self
.runCmd("process status")
95 proc_of_thread
= thread
.GetProcess()
96 self
.trace("proc_of_thread:", proc_of_thread
)
97 self
.assertEqual(proc_of_thread
.GetProcessID(), process
.GetProcessID())
99 def get_stop_description(self
):
100 """Test Python SBThread.GetStopDescription() API."""
101 exe
= self
.getBuildArtifact("a.out")
103 target
= self
.dbg
.CreateTarget(exe
)
104 self
.assertTrue(target
, VALID_TARGET
)
106 breakpoint
= target
.BreakpointCreateByLocation("main.cpp", self
.break_line
)
107 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
108 # self.runCmd("breakpoint list")
110 # Launch the process, and do not stop at the entry point.
111 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
113 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
115 thread
.IsValid(), "There should be a thread stopped due to breakpoint"
118 # Get the stop reason. GetStopDescription expects that we pass in the size of the description
119 # we expect plus an additional byte for the null terminator.
121 # Test with a buffer that is exactly as large as the expected stop reason.
123 "breakpoint 1.1", thread
.GetStopDescription(len("breakpoint 1.1") + 1)
126 # Test some smaller buffer sizes.
127 self
.assertEqual("breakpoint", thread
.GetStopDescription(len("breakpoint") + 1))
128 self
.assertEqual("break", thread
.GetStopDescription(len("break") + 1))
129 self
.assertEqual("b", thread
.GetStopDescription(len("b") + 1))
131 # Test that we can pass in a much larger size and still get the right output.
133 "breakpoint 1.1", thread
.GetStopDescription(len("breakpoint 1.1") + 100)
136 def step_out_of_malloc_into_function_b(self
, exe_name
):
137 """Test Python SBThread.StepOut() API to step out of a malloc call where the call site is at function b()."""
138 exe
= self
.getBuildArtifact(exe_name
)
140 target
= self
.dbg
.CreateTarget(exe
)
141 self
.assertTrue(target
, VALID_TARGET
)
143 breakpoint
= target
.BreakpointCreateByName("malloc")
144 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
146 # Launch the process, and do not stop at the entry point.
147 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
150 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
152 thread
.IsValid(), "There should be a thread stopped due to breakpoint"
154 caller_symbol
= get_caller_symbol(thread
)
155 if not caller_symbol
:
156 self
.fail("Test failed: could not locate the caller symbol of malloc")
158 # Our top frame may be an inlined function in malloc() (e.g., on
159 # FreeBSD). Apply a simple heuristic of stepping out until we find
160 # a non-malloc caller
161 while caller_symbol
.startswith("malloc"):
164 thread
.IsValid(), "Thread valid after stepping to outer malloc"
166 caller_symbol
= get_caller_symbol(thread
)
168 if caller_symbol
== "b(int)":
172 # On Linux malloc calls itself in some case. Remove the breakpoint because we don't want
173 # to hit it during step-out.
174 target
.BreakpointDelete(breakpoint
.GetID())
177 self
.runCmd("thread backtrace")
179 thread
.GetFrameAtIndex(0).GetLineEntry().GetLine(),
180 self
.step_out_of_malloc
,
181 "step out of malloc into function b is successful",
184 def step_over_3_times(self
, exe_name
):
185 """Test Python SBThread.StepOver() API."""
186 exe
= self
.getBuildArtifact(exe_name
)
188 target
= self
.dbg
.CreateTarget(exe
)
189 self
.assertTrue(target
, VALID_TARGET
)
191 breakpoint
= target
.BreakpointCreateByLocation(
192 "main2.cpp", self
.step_out_of_malloc
194 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
195 self
.runCmd("breakpoint list")
197 # Launch the process, and do not stop at the entry point.
198 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
200 self
.assertTrue(process
, PROCESS_IS_VALID
)
202 # Frame #0 should be on self.step_out_of_malloc.
203 self
.assertState(process
.GetState(), lldb
.eStateStopped
)
204 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
207 "There should be a thread stopped due to breakpoint condition",
209 self
.runCmd("thread backtrace")
210 frame0
= thread
.GetFrameAtIndex(0)
211 lineEntry
= frame0
.GetLineEntry()
212 self
.assertEqual(lineEntry
.GetLine(), self
.step_out_of_malloc
)
217 self
.runCmd("thread backtrace")
219 # Verify that we are stopped at the correct source line number in
221 frame0
= thread
.GetFrameAtIndex(0)
222 lineEntry
= frame0
.GetLineEntry()
223 self
.assertStopReason(thread
.GetStopReason(), lldb
.eStopReasonPlanComplete
)
224 # Expected failure with clang as the compiler.
225 # rdar://problem/9223880
227 # Which has been fixed on the lldb by compensating for inaccurate line
228 # table information with r140416.
229 self
.assertEqual(lineEntry
.GetLine(), self
.after_3_step_overs
)
231 def run_to_address(self
, exe_name
):
232 """Test Python SBThread.RunToAddress() API."""
233 exe
= self
.getBuildArtifact(exe_name
)
235 target
= self
.dbg
.CreateTarget(exe
)
236 self
.assertTrue(target
, VALID_TARGET
)
238 breakpoint
= target
.BreakpointCreateByLocation(
239 "main2.cpp", self
.step_out_of_malloc
241 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
242 self
.runCmd("breakpoint list")
244 # Launch the process, and do not stop at the entry point.
245 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
247 self
.assertTrue(process
, PROCESS_IS_VALID
)
249 # Frame #0 should be on self.step_out_of_malloc.
250 self
.assertState(process
.GetState(), lldb
.eStateStopped
)
251 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
254 "There should be a thread stopped due to breakpoint condition",
256 self
.runCmd("thread backtrace")
257 frame0
= thread
.GetFrameAtIndex(0)
258 lineEntry
= frame0
.GetLineEntry()
259 self
.assertEqual(lineEntry
.GetLine(), self
.step_out_of_malloc
)
261 # Get the start/end addresses for this line entry.
262 start_addr
= lineEntry
.GetStartAddress().GetLoadAddress(target
)
263 end_addr
= lineEntry
.GetEndAddress().GetLoadAddress(target
)
265 print("start addr:", hex(start_addr
))
266 print("end addr:", hex(end_addr
))
268 # Disable the breakpoint.
269 self
.assertTrue(target
.DisableAllBreakpoints())
270 self
.runCmd("breakpoint list")
275 self
.runCmd("thread backtrace")
277 # Now ask SBThread to run to the address 'start_addr' we got earlier, which
278 # corresponds to self.step_out_of_malloc line entry's start address.
279 thread
.RunToAddress(start_addr
)
280 self
.runCmd("process status")
281 # self.runCmd("thread backtrace")
283 def validate_negative_indexing(self
):
284 exe
= self
.getBuildArtifact("a.out")
286 target
= self
.dbg
.CreateTarget(exe
)
287 self
.assertTrue(target
, VALID_TARGET
)
289 breakpoint
= target
.BreakpointCreateByLocation("main.cpp", self
.break_line
)
290 self
.assertTrue(breakpoint
, VALID_BREAKPOINT
)
291 self
.runCmd("breakpoint list")
293 # Launch the process, and do not stop at the entry point.
294 process
= target
.LaunchSimple(None, None, self
.get_process_working_directory())
296 thread
= get_stopped_thread(process
, lldb
.eStopReasonBreakpoint
)
298 thread
.IsValid(), "There should be a thread stopped due to breakpoint"
300 self
.runCmd("process status")
302 pos_range
= range(thread
.num_frames
)
303 neg_range
= range(thread
.num_frames
, 0, -1)
304 for pos
, neg
in zip(pos_range
, neg_range
):
305 self
.assertEqual(thread
.frame
[pos
].idx
, thread
.frame
[-neg
].idx
)