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 def test_print_obj(self): 27 """ 28 Test "print object" where another thread blocks the print object from making progress. 29 30 Set a breakpoint on the line in my_pthread_routine. Then switch threads 31 to the main thread, and do print the lock_me object. Since that will 32 try to get the lock already gotten by my_pthread_routime thread, it will 33 have to switch to running all threads, and that should then succeed. 34 """ 35 d = {'EXE': 'b.out'} 36 self.build(dictionary=d) 37 self.setTearDownCleanup(dictionary=d) 38 exe = self.getBuildArtifact('b.out') 39 40 target = self.dbg.CreateTarget(exe) 41 self.assertTrue(target, VALID_TARGET) 42 43 breakpoint = target.BreakpointCreateByLocation(self.source, self.line) 44 self.assertTrue(breakpoint, VALID_BREAKPOINT) 45 self.runCmd("breakpoint list") 46 47 # Launch the process, and do not stop at the entry point. 48 process = target.LaunchSimple( 49 None, None, self.get_process_working_directory()) 50 51 self.runCmd("thread backtrace all") 52 53 # Let's get the current stopped thread. We'd like to switch to the 54 # other thread to issue our 'po lock_me' command. 55 import lldbsuite.test.lldbutil as lldbutil 56 this_thread = lldbutil.get_stopped_thread( 57 process, lldb.eStopReasonBreakpoint) 58 self.assertTrue(this_thread) 59 60 # Find the other thread. The iteration protocol of SBProcess and the 61 # rich comparison methods (__eq__/__ne__) of SBThread come in handy. 62 other_thread = None 63 for t in process: 64 if t != this_thread: 65 other_thread = t 66 break 67 68 # Set the other thread as the selected thread to issue our 'po' 69 # command.other 70 self.assertTrue(other_thread) 71 process.SetSelectedThread(other_thread) 72 if self.TraceOn(): 73 print("selected thread:" + lldbutil.get_description(other_thread)) 74 self.runCmd("thread backtrace") 75 76 # We want to traverse the frame to the one corresponding to blocked.m to 77 # issue our 'po lock_me' command. 78 79 depth = other_thread.GetNumFrames() 80 for i in range(depth): 81 frame = other_thread.GetFrameAtIndex(i) 82 name = frame.GetFunctionName() 83 if name == 'main': 84 other_thread.SetSelectedFrame(i) 85 if self.TraceOn(): 86 print("selected frame:" + lldbutil.get_description(frame)) 87 break 88 89 self.expect("po lock_me", OBJECT_PRINTED_CORRECTLY, 90 substrs=['I am pretty special.']) 91