2 Test stepping out from a function in a multi-threaded program.
5 from __future__ import print_function
11 from lldbsuite.test.lldbtest import *
12 import lldbsuite.test.lldbutil as lldbutil
14 class ThreadStepOutTestCase(TestBase):
16 mydir = TestBase.compute_mydir(__file__)
18 @skipIfLinux # Test occasionally times out on the Linux build bot
19 @expectedFailureLinux("llvm.org/pr23477") # Test occasionally times out on the Linux build bot
20 @expectedFailureFreeBSD("llvm.org/pr18066") # inferior does not exit
21 @expectedFailureWindows # Test crashes
22 def test_step_single_thread(self):
23 """Test thread step out on one thread via command interpreter. """
24 self.build(dictionary=self.getBuildFlags())
25 self.step_out_test(self.step_out_single_thread_with_cmd)
27 @skipIfLinux # Test occasionally times out on the Linux build bot
28 @expectedFailureLinux("llvm.org/pr23477") # Test occasionally times out on the Linux build bot
29 @expectedFailureFreeBSD("llvm.org/pr19347") # 2nd thread stops at breakpoint
30 @expectedFailureWindows # Test crashes
31 def test_step_all_threads(self):
32 """Test thread step out on all threads via command interpreter. """
33 self.build(dictionary=self.getBuildFlags())
34 self.step_out_test(self.step_out_all_threads_with_cmd)
36 @skipIfLinux # Test occasionally times out on the Linux build bot
37 @expectedFailureLinux("llvm.org/pr23477") # Test occasionally times out on the Linux build bot
38 @expectedFailureFreeBSD("llvm.org/pr19347")
39 @expectedFailureWindows("llvm.org/pr24681")
40 def test_python(self):
41 """Test thread step out on one thread via Python API (dwarf)."""
42 self.build(dictionary=self.getBuildFlags())
43 self.step_out_test(self.step_out_with_python)
46 # Call super's setUp().
48 # Find the line number for our breakpoint.
49 self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
50 if "gcc" in self.getCompiler() or self.isIntelCompiler():
51 self.step_out_destination = line_number('main.cpp', '// Expect to stop here after step-out (icc and gcc)')
53 self.step_out_destination = line_number('main.cpp', '// Expect to stop here after step-out (clang)')
55 def step_out_single_thread_with_cmd(self):
56 self.step_out_with_cmd("this-thread")
57 self.expect("thread backtrace all", "Thread location after step out is correct",
58 substrs = ["main.cpp:%d" % self.step_out_destination,
59 "main.cpp:%d" % self.breakpoint])
61 def step_out_all_threads_with_cmd(self):
62 self.step_out_with_cmd("all-threads")
63 self.expect("thread backtrace all", "Thread location after step out is correct",
64 substrs = ["main.cpp:%d" % self.step_out_destination])
66 def step_out_with_cmd(self, run_mode):
67 self.runCmd("thread select %d" % self.step_out_thread.GetIndexID())
68 self.runCmd("thread step-out -m %s" % run_mode)
69 self.expect("process status", "Expected stop reason to be step-out",
70 substrs = ["stop reason = step out"])
72 self.expect("thread list", "Selected thread did not change during step-out",
73 substrs = ["* thread #%d" % self.step_out_thread.GetIndexID()])
75 def step_out_with_python(self):
76 self.step_out_thread.StepOut()
78 reason = self.step_out_thread.GetStopReason()
79 self.assertEqual(lldb.eStopReasonPlanComplete, reason,
80 "Expected thread stop reason 'plancomplete', but got '%s'" % lldbutil.stop_reason_to_str(reason))
82 # Verify location after stepping out
83 frame = self.step_out_thread.GetFrameAtIndex(0)
84 desc = lldbutil.get_description(frame.GetLineEntry())
85 expect = "main.cpp:%d" % self.step_out_destination
86 self.assertTrue(expect in desc, "Expected %s but thread stopped at %s" % (expect, desc))
88 def step_out_test(self, step_out_func):
89 """Test single thread step out of a function."""
90 exe = os.path.join(os.getcwd(), "a.out")
91 self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
93 # This should create a breakpoint in the main thread.
94 lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.breakpoint, num_expected_locations=1)
96 # The breakpoint list should show 1 location.
97 self.expect("breakpoint list -f", "Breakpoint location shown correctly",
98 substrs = ["1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" % self.breakpoint])
101 self.runCmd("run", RUN_SUCCEEDED)
103 # Get the target process
104 self.inferior_target = self.dbg.GetSelectedTarget()
105 self.inferior_process = self.inferior_target.GetProcess()
107 # Get the number of threads, ensure we see all three.
108 num_threads = self.inferior_process.GetNumThreads()
109 self.assertEqual(num_threads, 3, 'Number of expected threads and actual threads do not match.')
111 (breakpoint_threads, other_threads) = ([], [])
112 lldbutil.sort_stopped_threads(self.inferior_process,
113 breakpoint_threads=breakpoint_threads,
114 other_threads=other_threads)
116 while len(breakpoint_threads) < 2:
117 self.runCmd("thread continue %s" % " ".join([str(x.GetIndexID()) for x in other_threads]))
118 lldbutil.sort_stopped_threads(self.inferior_process,
119 breakpoint_threads=breakpoint_threads,
120 other_threads=other_threads)
122 self.step_out_thread = breakpoint_threads[0]
124 # Step out of thread stopped at breakpoint
128 self.runCmd("continue")
130 # At this point, the inferior process should have exited.
131 self.assertTrue(self.inferior_process.GetState() == lldb.eStateExited, PROCESS_EXITED)