1""" 2Test that the Python operating system plugin works correctly 3""" 4 5 6 7import os 8import lldb 9from lldbsuite.test.lldbtest import * 10import lldbsuite.test.lldbutil as lldbutil 11 12 13class PluginPythonOSPlugin(TestBase): 14 15 mydir = TestBase.compute_mydir(__file__) 16 NO_DEBUG_INFO_TESTCASE = True 17 18 def test_python_os_plugin(self): 19 """Test that the Python operating system plugin works correctly""" 20 self.build() 21 self.run_python_os_funcionality() 22 23 def run_python_os_step(self): 24 """Test that the Python operating system plugin works correctly when single stepping a virtual thread""" 25 self.build() 26 self.run_python_os_step() 27 28 def verify_os_thread_registers(self, thread): 29 frame = thread.GetFrameAtIndex(0) 30 registers = frame.GetRegisters().GetValueAtIndex(0) 31 reg_value = thread.GetThreadID() + 1 32 for reg in registers: 33 self.assertTrue( 34 reg.GetValueAsUnsigned() == reg_value, 35 "Verify the registers contains the correct value") 36 reg_value = reg_value + 1 37 38 def run_python_os_funcionality(self): 39 """Test that the Python operating system plugin works correctly""" 40 41 # Set debugger into synchronous mode 42 self.dbg.SetAsync(False) 43 44 # Create a target by the debugger. 45 exe = self.getBuildArtifact("a.out") 46 python_os_plugin_path = os.path.join(self.getSourceDir(), 47 "operating_system.py") 48 target = self.dbg.CreateTarget(exe) 49 self.assertTrue(target, VALID_TARGET) 50 51 # Set breakpoints inside and outside methods that take pointers to the 52 # containing struct. 53 lldbutil.run_break_set_by_source_regexp(self, "// Set breakpoint here") 54 55 # Register our shared libraries for remote targets so they get 56 # automatically uploaded 57 arguments = None 58 environment = None 59 60 # Now launch the process, and do not stop at entry point. 61 process = target.LaunchSimple( 62 arguments, environment, self.get_process_working_directory()) 63 self.assertTrue(process, PROCESS_IS_VALID) 64 65 # Make sure there are no OS plug-in created thread when we first stop 66 # at our breakpoint in main 67 thread = process.GetThreadByID(0x111111111) 68 self.assertFalse( 69 thread.IsValid(), 70 "Make sure there is no thread 0x111111111 before we load the python OS plug-in") 71 thread = process.GetThreadByID(0x222222222) 72 self.assertFalse( 73 thread.IsValid(), 74 "Make sure there is no thread 0x222222222 before we load the python OS plug-in") 75 thread = process.GetThreadByID(0x333333333) 76 self.assertFalse( 77 thread.IsValid(), 78 "Make sure there is no thread 0x333333333 before we load the python OS plug-in") 79 80 # Now load the python OS plug-in which should update the thread list and we should have 81 # OS plug-in created threads with the IDs: 0x111111111, 0x222222222, 82 # 0x333333333 83 command = "settings set target.process.python-os-plugin-path '%s'" % python_os_plugin_path 84 self.dbg.HandleCommand(command) 85 86 # Verify our OS plug-in threads showed up 87 thread = process.GetThreadByID(0x111111111) 88 self.assertTrue( 89 thread.IsValid(), 90 "Make sure there is a thread 0x111111111 after we load the python OS plug-in") 91 self.verify_os_thread_registers(thread) 92 thread = process.GetThreadByID(0x222222222) 93 self.assertTrue( 94 thread.IsValid(), 95 "Make sure there is a thread 0x222222222 after we load the python OS plug-in") 96 self.verify_os_thread_registers(thread) 97 thread = process.GetThreadByID(0x333333333) 98 self.assertTrue( 99 thread.IsValid(), 100 "Make sure there is a thread 0x333333333 after we load the python OS plug-in") 101 self.verify_os_thread_registers(thread) 102 103 # Now clear the OS plug-in path to make the OS plug-in created threads 104 # disappear 105 self.dbg.HandleCommand( 106 "settings clear target.process.python-os-plugin-path") 107 108 # Verify the threads are gone after unloading the python OS plug-in 109 thread = process.GetThreadByID(0x111111111) 110 self.assertFalse( 111 thread.IsValid(), 112 "Make sure there is no thread 0x111111111 after we unload the python OS plug-in") 113 thread = process.GetThreadByID(0x222222222) 114 self.assertFalse( 115 thread.IsValid(), 116 "Make sure there is no thread 0x222222222 after we unload the python OS plug-in") 117 thread = process.GetThreadByID(0x333333333) 118 self.assertFalse( 119 thread.IsValid(), 120 "Make sure there is no thread 0x333333333 after we unload the python OS plug-in") 121 122 def run_python_os_step(self): 123 """Test that the Python operating system plugin works correctly and allows single stepping of a virtual thread that is backed by a real thread""" 124 125 # Set debugger into synchronous mode 126 self.dbg.SetAsync(False) 127 128 # Create a target by the debugger. 129 exe = self.getBuildArtifact("a.out") 130 python_os_plugin_path = os.path.join(self.getSourceDir(), 131 "operating_system2.py") 132 target = self.dbg.CreateTarget(exe) 133 self.assertTrue(target, VALID_TARGET) 134 135 # Set breakpoints inside and outside methods that take pointers to the 136 # containing struct. 137 lldbutil.run_break_set_by_source_regexp(self, "// Set breakpoint here") 138 139 # Register our shared libraries for remote targets so they get 140 # automatically uploaded 141 arguments = None 142 environment = None 143 144 # Now launch the process, and do not stop at entry point. 145 process = target.LaunchSimple( 146 arguments, environment, self.get_process_working_directory()) 147 self.assertTrue(process, PROCESS_IS_VALID) 148 149 # Make sure there are no OS plug-in created thread when we first stop 150 # at our breakpoint in main 151 thread = process.GetThreadByID(0x111111111) 152 self.assertFalse( 153 thread.IsValid(), 154 "Make sure there is no thread 0x111111111 before we load the python OS plug-in") 155 156 # Now load the python OS plug-in which should update the thread list and we should have 157 # OS plug-in created threads with the IDs: 0x111111111, 0x222222222, 158 # 0x333333333 159 command = "settings set target.process.python-os-plugin-path '%s'" % python_os_plugin_path 160 self.dbg.HandleCommand(command) 161 162 # Verify our OS plug-in threads showed up 163 thread = process.GetThreadByID(0x111111111) 164 self.assertTrue( 165 thread.IsValid(), 166 "Make sure there is a thread 0x111111111 after we load the python OS plug-in") 167 168 frame = thread.GetFrameAtIndex(0) 169 self.assertTrue( 170 frame.IsValid(), 171 "Make sure we get a frame from thread 0x111111111") 172 line_entry = frame.GetLineEntry() 173 174 self.assertTrue( 175 line_entry.GetFileSpec().GetFilename() == 'main.c', 176 "Make sure we stopped on line 5 in main.c") 177 self.assertTrue( 178 line_entry.GetLine() == 5, 179 "Make sure we stopped on line 5 in main.c") 180 181 # Now single step thread 0x111111111 and make sure it does what we need 182 # it to 183 thread.StepOver() 184 185 frame = thread.GetFrameAtIndex(0) 186 self.assertTrue( 187 frame.IsValid(), 188 "Make sure we get a frame from thread 0x111111111") 189 line_entry = frame.GetLineEntry() 190 191 self.assertTrue( 192 line_entry.GetFileSpec().GetFilename() == 'main.c', 193 "Make sure we stepped from line 5 to line 6 in main.c") 194 self.assertEquals(line_entry.GetLine(), 6, 195 "Make sure we stepped from line 5 to line 6 in main.c") 196