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