xref: /llvm-project/lldb/test/API/functionalities/thread/exit_during_break/TestExitDuringBreak.py (revision 9c2468821ec51defd09c246fea4a47886fff8c01)
1"""
2Test number of threads.
3"""
4
5
6import lldb
7from lldbsuite.test.decorators import *
8from lldbsuite.test.lldbtest import *
9from lldbsuite.test import lldbutil
10
11
12class ExitDuringBreakpointTestCase(TestBase):
13    def setUp(self):
14        # Call super's setUp().
15        TestBase.setUp(self)
16        # Find the line number for our breakpoint.
17        self.breakpoint = line_number("main.cpp", "// Set breakpoint here")
18
19    def test(self):
20        """Test thread exit during breakpoint handling."""
21        self.build()
22        exe = self.getBuildArtifact("a.out")
23        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
24
25        # This should create a breakpoint in the main thread.
26        lldbutil.run_break_set_by_file_and_line(
27            self, "main.cpp", self.breakpoint, num_expected_locations=1
28        )
29
30        # Run the program.
31        self.runCmd("run", RUN_SUCCEEDED)
32
33        # The stop reason of the thread should be breakpoint.
34        self.expect(
35            "thread list",
36            STOPPED_DUE_TO_BREAKPOINT,
37            substrs=["stopped", "stop reason = breakpoint"],
38        )
39
40        # Get the target process
41        target = self.dbg.GetSelectedTarget()
42        process = target.GetProcess()
43
44        # The exit probably occurred during breakpoint handling, but it isn't
45        # guaranteed.  The main thing we're testing here is that the debugger
46        # handles this cleanly is some way.
47
48        # Get the number of threads
49        num_threads = process.GetNumThreads()
50
51        # Make sure we see at least five threads
52        self.assertGreaterEqual(
53            num_threads,
54            5,
55            "Number of expected threads and actual threads do not match.",
56        )
57
58        # Run to completion
59        self.runCmd("continue")
60
61        # At this point, the inferior process should have exited.
62        self.assertEqual(process.GetState(), lldb.eStateExited, PROCESS_EXITED)
63