1"""
2Test number of threads.
3"""
4
5import os, time
6import unittest2
7import lldb
8from lldbtest import *
9import lldbutil
10
11class MultipleBreakpointTestCase(TestBase):
12
13    mydir = os.path.join("functionalities", "thread", "multi_break")
14
15    @unittest2.skipUnless(sys.platform.startswith("darwin"), "requires Darwin")
16    @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
17    @dsym_test
18    def test_with_dsym(self):
19        """Test simultaneous breakpoints in multiple threads."""
20        self.buildDsym(dictionary=self.getBuildFlags())
21        self.multiple_breakpoint_test()
22
23    @expectedFailureFreeBSD("llvm.org/pr16696") # threaded inferior not implemented on FreeBSD yet
24    @expectedFailureDarwin("llvm.org/pr15824") # thread states not properly maintained
25    @dwarf_test
26    def test_with_dwarf(self):
27        """Test simultaneous breakpoints in multiple threads."""
28        self.buildDwarf(dictionary=self.getBuildFlags())
29        self.multiple_breakpoint_test()
30
31    def setUp(self):
32        # Call super's setUp().
33        TestBase.setUp(self)
34        # Find the line number for our breakpoint.
35        self.breakpoint = line_number('main.cpp', '// Set breakpoint here')
36
37    def multiple_breakpoint_test(self):
38        """Test simultaneous breakpoints in multiple threads."""
39        exe = os.path.join(os.getcwd(), "a.out")
40        self.runCmd("file " + exe, CURRENT_EXECUTABLE_SET)
41
42        # This should create a breakpoint in the main thread.
43        lldbutil.run_break_set_by_file_and_line (self, "main.cpp", self.breakpoint, num_expected_locations=1)
44
45        # The breakpoint list should show 1 location.
46        self.expect("breakpoint list -f", "Breakpoint location shown correctly",
47            substrs = ["1: file = 'main.cpp', line = %d, locations = 1" % self.breakpoint])
48
49        # Run the program.
50        self.runCmd("run", RUN_SUCCEEDED)
51
52        # The stop reason of the thread should be breakpoint.
53        # The breakpoint may be hit in either thread 2 or thread 3.
54        self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT,
55            substrs = ['stopped',
56                       'stop reason = breakpoint'])
57
58        # Get the target process
59        target = self.dbg.GetSelectedTarget()
60        process = target.GetProcess()
61
62        # Get the number of threads
63        num_threads = process.GetNumThreads()
64
65        # Make sure we see all three threads
66        self.assertTrue(num_threads == 3, 'Number of expected threads and actual threads do not match.')
67
68        # Get the thread objects
69        thread1 = process.GetThreadAtIndex(0)
70        thread2 = process.GetThreadAtIndex(1)
71        thread3 = process.GetThreadAtIndex(2)
72
73        # Make sure both threads are stopped
74        self.assertTrue(thread1.IsStopped(), "Primary thread didn't stop during breakpoint")
75        self.assertTrue(thread2.IsStopped(), "Secondary thread didn't stop during breakpoint")
76        self.assertTrue(thread3.IsStopped(), "Tertiary thread didn't stop during breakpoint")
77
78        # Delete the first breakpoint then continue
79        self.runCmd("breakpoint delete 1")
80
81        # Run to completion
82        self.runCmd("continue")
83
84        # At this point, the inferior process should have exited.
85        self.assertTrue(process.GetState() == lldb.eStateExited, PROCESS_EXITED)
86
87if __name__ == '__main__':
88    import atexit
89    lldb.SBDebugger.Initialize()
90    atexit.register(lambda: lldb.SBDebugger.Terminate())
91    unittest2.main()
92