82 lines · python
1"""2Test lldb-dap threads request3"""4 5from lldbsuite.test.decorators import *6from lldbsuite.test.lldbtest import *7from lldbsuite.test import lldbutil8import lldbdap_testcase9 10 11class TestDAP_threads(lldbdap_testcase.DAPTestCaseBase):12 def test_correct_thread(self):13 """14 Tests that the correct thread is selected if we continue from15 a thread that goes away and hit a breakpoint in another thread.16 In this case, the selected thread should be the thread that17 just hit the breakpoint, and not the first thread in the list.18 """19 program = self.getBuildArtifact("a.out")20 self.build_and_launch(program)21 source = "main.cpp"22 breakpoint_line = line_number(source, "// break here")23 lines = [breakpoint_line]24 # Set breakpoint in the thread function25 breakpoint_ids = self.set_source_breakpoints(source, lines)26 self.assertEqual(27 len(breakpoint_ids), len(lines), "expect correct number of breakpoints"28 )29 self.continue_to_breakpoints(breakpoint_ids)30 # We're now stopped at the breakpoint in the first thread, thread #2.31 # Continue to join the first thread and hit the breakpoint in the32 # second thread, thread #3.33 self.dap_server.request_continue()34 stopped_event = self.dap_server.wait_for_stopped()35 # Verify that the description is the relevant breakpoint,36 # preserveFocusHint is False and threadCausedFocus is True37 self.assertTrue(38 stopped_event[0]["body"]["description"].startswith(39 "breakpoint %s." % breakpoint_ids[0]40 )41 )42 self.assertFalse(stopped_event[0]["body"]["preserveFocusHint"])43 self.assertTrue(stopped_event[0]["body"]["threadCausedFocus"])44 # All threads should be named Thread {index}45 threads = self.dap_server.get_threads()46 self.assertTrue(all(len(t["name"]) > 0 for t in threads))47 48 def test_thread_format(self):49 """50 Tests the support for custom thread formats.51 """52 program = self.getBuildArtifact("a.out")53 self.build_and_launch(54 program,55 customThreadFormat="This is thread index #${thread.index}",56 stopCommands=["thread list"],57 )58 source = "main.cpp"59 breakpoint_line = line_number(source, "// break here")60 lines = [breakpoint_line]61 # Set breakpoint in the thread function62 breakpoint_ids = self.set_source_breakpoints(source, lines)63 self.assertEqual(64 len(breakpoint_ids), len(lines), "expect correct number of breakpoints"65 )66 self.continue_to_breakpoints(breakpoint_ids)67 # We are stopped at the first thread68 threads = self.dap_server.get_threads()69 print("got thread", threads)70 if self.getPlatform() == "windows":71 # Windows creates a thread pool once WaitForSingleObject is called72 # by thread.join(). As we are in the thread function, we can't be73 # certain that join() has been called yet and a thread pool has74 # been created, thus we only check for the first two threads.75 names = list(sorted(t["name"] for t in threads))[:2]76 self.assertEqual(77 names, ["This is thread index #1", "This is thread index #2"]78 )79 else:80 self.assertEqual(threads[0]["name"], "This is thread index #1")81 self.assertEqual(threads[1]["name"], "This is thread index #2")82