1"""
2Test number of threads.
3"""
4
5
6
7import lldb
8from lldbsuite.test.decorators import *
9from lldbsuite.test.lldbtest import *
10from lldbsuite.test import lldbutil
11
12
13class BreakpointAfterJoinTestCase(TestBase):
14
15    mydir = TestBase.compute_mydir(__file__)
16
17    def setUp(self):
18        # Call super's setUp().
19        TestBase.setUp(self)
20        # Find the line number for our breakpoint.
21        self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
22
23    @expectedFailureAll(
24        oslist=["linux"],
25        bugnumber="llvm.org/pr15824 thread states not properly maintained")
26    @expectedFailureAll(
27        oslist=lldbplatformutil.getDarwinOSTriples(),
28        bugnumber="llvm.org/pr15824 thread states not properly maintained and <rdar://problem/28557237>")
29    @expectedFailureAll(
30        oslist=["freebsd"],
31        bugnumber="llvm.org/pr18190 thread states not properly maintained")
32    @expectedFailureNetBSD
33    def test(self):
34        """Test breakpoint handling after a thread join."""
35        self.build(dictionary=self.getBuildFlags())
36
37        exe = self.getBuildArtifact("a.out")
38        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
39
40        # This should create a breakpoint in the main thread.
41        lldbutil.run_break_set_by_file_and_line(
42            self, "main.cpp", self.breakpoint, num_expected_locations=1)
43
44        # The breakpoint list should show 1 location.
45        self.expect(
46            "breakpoint list -f",
47            "Breakpoint location shown correctly",
48            substrs=[
49                "1: file = 'main.cpp', line = %d, exact_match = 0, locations = 1" %
50                self.breakpoint])
51
52        # Run the program.
53        self.runCmd("run", RUN_SUCCEEDED)
54
55        # The stop reason of the thread should be breakpoint.
56        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
57                    substrs=['stopped',
58                             'stop reason = breakpoint'])
59
60        # Get the target process
61        target = self.dbg.GetSelectedTarget()
62        process = target.GetProcess()
63
64        # The exit probably occurred during breakpoint handling, but it isn't
65        # guaranteed.  The main thing we're testing here is that the debugger
66        # handles this cleanly is some way.
67
68        # Get the number of threads
69        num_threads = process.GetNumThreads()
70
71        # Make sure we see at least six threads
72        self.assertTrue(
73            num_threads >= 6,
74            'Number of expected threads and actual threads do not match.')
75
76        # Make sure all threads are stopped
77        for i in range(0, num_threads):
78            self.assertTrue(
79                process.GetThreadAtIndex(i).IsStopped(),
80                "Thread {0} didn't stop during breakpoint.".format(i))
81
82        # Run to completion
83        self.runCmd("continue")
84
85        # If the process hasn't exited, collect some information
86        if process.GetState() != lldb.eStateExited:
87            self.runCmd("thread list")
88            self.runCmd("process status")
89
90        # At this point, the inferior process should have exited.
91        self.assertTrue(
92            process.GetState() == lldb.eStateExited,
93            PROCESS_EXITED)
94