1""" 2Test thread creation after process attach. 3""" 4 5 6 7import lldb 8from lldbsuite.test.decorators import * 9from lldbsuite.test.lldbtest import * 10from lldbsuite.test import lldbutil 11 12 13class CreateAfterAttachTestCase(TestBase): 14 15 mydir = TestBase.compute_mydir(__file__) 16 17 @skipIfFreeBSD # Hangs. May be the same as Linux issue llvm.org/pr16229 but 18 # not yet investigated. Revisit once required functionality 19 # is implemented for FreeBSD. 20 # Occasionally hangs on Windows, may be same as other issues. 21 @skipIfWindows 22 @skipIfiOSSimulator 23 @expectedFailureNetBSD 24 def test_create_after_attach_with_popen(self): 25 """Test thread creation after process attach.""" 26 self.build(dictionary=self.getBuildFlags(use_cpp11=False)) 27 self.create_after_attach(use_fork=False) 28 29 @skipIfFreeBSD # Hangs. Revisit once required functionality is implemented 30 # for FreeBSD. 31 @skipIfRemote 32 @skipIfWindows # Windows doesn't have fork. 33 @skipIfiOSSimulator 34 @expectedFailureNetBSD 35 def test_create_after_attach_with_fork(self): 36 """Test thread creation after process attach.""" 37 self.build(dictionary=self.getBuildFlags(use_cpp11=False)) 38 self.create_after_attach(use_fork=True) 39 40 def setUp(self): 41 # Call super's setUp(). 42 TestBase.setUp(self) 43 # Find the line numbers for our breakpoints. 44 self.break_1 = line_number('main.cpp', '// Set first breakpoint here') 45 self.break_2 = line_number('main.cpp', '// Set second breakpoint here') 46 self.break_3 = line_number('main.cpp', '// Set third breakpoint here') 47 48 def create_after_attach(self, use_fork): 49 """Test thread creation after process attach.""" 50 51 exe = self.getBuildArtifact("a.out") 52 53 # Spawn a new process 54 if use_fork: 55 pid = self.forkSubprocess(exe) 56 else: 57 popen = self.spawnSubprocess(exe) 58 pid = popen.pid 59 60 # Attach to the spawned process 61 self.runCmd("process attach -p " + str(pid)) 62 63 target = self.dbg.GetSelectedTarget() 64 65 process = target.GetProcess() 66 self.assertTrue(process, PROCESS_IS_VALID) 67 68 # This should create a breakpoint in the main thread. 69 lldbutil.run_break_set_by_file_and_line( 70 self, "main.cpp", self.break_1, num_expected_locations=1) 71 72 # This should create a breakpoint in the second child thread. 73 lldbutil.run_break_set_by_file_and_line( 74 self, "main.cpp", self.break_2, num_expected_locations=1) 75 76 # This should create a breakpoint in the first child thread. 77 lldbutil.run_break_set_by_file_and_line( 78 self, "main.cpp", self.break_3, num_expected_locations=1) 79 80 # Note: With std::thread, we cannot rely on particular thread numbers. Using 81 # std::thread may cause the program to spin up a thread pool (and it does on 82 # Windows), so the thread numbers are non-deterministic. 83 84 # Run to the first breakpoint 85 self.runCmd("continue") 86 87 # The stop reason of the thread should be breakpoint. 88 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 89 substrs=['stopped', 90 '* thread #', 91 'main', 92 'stop reason = breakpoint']) 93 94 # Change a variable to escape the loop 95 self.runCmd("expression main_thread_continue = 1") 96 97 # Run to the second breakpoint 98 self.runCmd("continue") 99 100 # The stop reason of the thread should be breakpoint. 101 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 102 substrs=['stopped', 103 '* thread #', 104 'thread_2_func', 105 'stop reason = breakpoint']) 106 107 # Change a variable to escape the loop 108 self.runCmd("expression child_thread_continue = 1") 109 110 # Run to the third breakpoint 111 self.runCmd("continue") 112 113 # The stop reason of the thread should be breakpoint. 114 # Thread 3 may or may not have already exited. 115 self.expect("thread list", STOPPED_DUE_TO_BREAKPOINT, 116 substrs=['stopped', 117 '* thread #', 118 'thread_1_func', 119 'stop reason = breakpoint']) 120 121 # Run to completion 122 self.runCmd("continue") 123 124 # At this point, the inferior process should have exited. 125 self.assertTrue( 126 process.GetState() == lldb.eStateExited, 127 PROCESS_EXITED) 128