1"""
2Test "print object" where another thread blocks the print object from making progress.
3"""
4
5from __future__ import print_function
6
7
8import lldb
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class PrintObjTestCase(TestBase):
15
16    mydir = TestBase.compute_mydir(__file__)
17
18    def setUp(self):
19        # Call super's setUp().
20        TestBase.setUp(self)
21        # My source program.
22        self.source = "blocked.m"
23        # Find the line numbers to break at.
24        self.line = line_number(self.source, '// Set a breakpoint here.')
25
26    @skipIfReproducer # FIXME: Unexpected packet during (active) replay
27    def test_print_obj(self):
28        """
29        Test "print object" where another thread blocks the print object from making progress.
30
31        Set a breakpoint on the line in my_pthread_routine.  Then switch threads
32        to the main thread, and do print the lock_me object.  Since that will
33        try to get the lock already gotten by my_pthread_routime thread, it will
34        have to switch to running all threads, and that should then succeed.
35        """
36        d = {'EXE': 'b.out'}
37        self.build(dictionary=d)
38        self.setTearDownCleanup(dictionary=d)
39        exe = self.getBuildArtifact('b.out')
40
41        target = self.dbg.CreateTarget(exe)
42        self.assertTrue(target, VALID_TARGET)
43
44        breakpoint = target.BreakpointCreateByLocation(self.source, self.line)
45        self.assertTrue(breakpoint, VALID_BREAKPOINT)
46        self.runCmd("breakpoint list")
47
48        # Launch the process, and do not stop at the entry point.
49        process = target.LaunchSimple(
50            None, None, self.get_process_working_directory())
51
52        self.runCmd("thread backtrace all")
53
54        # Let's get the current stopped thread.  We'd like to switch to the
55        # other thread to issue our 'po lock_me' command.
56        import lldbsuite.test.lldbutil as lldbutil
57        this_thread = lldbutil.get_stopped_thread(
58            process, lldb.eStopReasonBreakpoint)
59        self.assertTrue(this_thread)
60
61        # Find the other thread.  The iteration protocol of SBProcess and the
62        # rich comparison methods (__eq__/__ne__) of SBThread come in handy.
63        other_thread = None
64        for t in process:
65            if t != this_thread:
66                other_thread = t
67                break
68
69        # Set the other thread as the selected thread to issue our 'po'
70        # command.other
71        self.assertTrue(other_thread)
72        process.SetSelectedThread(other_thread)
73        if self.TraceOn():
74            print("selected thread:" + lldbutil.get_description(other_thread))
75        self.runCmd("thread backtrace")
76
77        # We want to traverse the frame to the one corresponding to blocked.m to
78        # issue our 'po lock_me' command.
79
80        depth = other_thread.GetNumFrames()
81        for i in range(depth):
82            frame = other_thread.GetFrameAtIndex(i)
83            name = frame.GetFunctionName()
84            if name == 'main':
85                other_thread.SetSelectedFrame(i)
86                if self.TraceOn():
87                    print("selected frame:" + lldbutil.get_description(frame))
88                break
89
90        self.expect("po lock_me", OBJECT_PRINTED_CORRECTLY,
91                    substrs=['I am pretty special.'])
92