|
| 1 | +""" |
| 2 | +Test that an OS plugin in a dSYM sees the right process state |
| 3 | +when run from a dSYM on attach |
| 4 | +""" |
| 5 | + |
| 6 | +from lldbsuite.test.decorators import * |
| 7 | +from lldbsuite.test.lldbtest import * |
| 8 | +import lldbsuite.test.lldbutil as lldbutil |
| 9 | +from lldbgdbserverutils import get_debugserver_exe |
| 10 | + |
| 11 | +import os |
| 12 | +import lldb |
| 13 | +import time |
| 14 | +import socket |
| 15 | +import shutil |
| 16 | + |
| 17 | + |
| 18 | +class TestOSPluginIndSYM(TestBase): |
| 19 | + NO_DEBUG_INFO_TESTCASE = True |
| 20 | + |
| 21 | + # The port used by debugserver. |
| 22 | + PORT = 54638 |
| 23 | + |
| 24 | + # The number of attempts. |
| 25 | + ATTEMPTS = 10 |
| 26 | + |
| 27 | + # Time given to the binary to launch and to debugserver to attach to it for |
| 28 | + # every attempt. We'll wait a maximum of 10 times 2 seconds while the |
| 29 | + # inferior will wait 10 times 10 seconds. |
| 30 | + TIMEOUT = 2 |
| 31 | + |
| 32 | + def no_debugserver(self): |
| 33 | + if get_debugserver_exe() is None: |
| 34 | + return "no debugserver" |
| 35 | + return None |
| 36 | + |
| 37 | + def port_not_available(self): |
| 38 | + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
| 39 | + if s.connect_ex(("127.0.0.1", self.PORT)) == 0: |
| 40 | + return "{} not available".format(self.PORT) |
| 41 | + return None |
| 42 | + |
| 43 | + @skipUnlessDarwin |
| 44 | + def test_python_os_plugin(self): |
| 45 | + self.do_test_python_os_plugin(False) |
| 46 | + |
| 47 | + @skipTestIfFn(no_debugserver) |
| 48 | + @skipTestIfFn(port_not_available) |
| 49 | + def test_python_os_plugin_remote(self): |
| 50 | + self.do_test_python_os_plugin(True) |
| 51 | + |
| 52 | + def do_test_python_os_plugin(self, remote): |
| 53 | + """Test that the environment for os plugins in dSYM's is correct""" |
| 54 | + executable = self.build_dsym("my_binary") |
| 55 | + |
| 56 | + # Make sure we're set up to load the symbol file's python |
| 57 | + self.runCmd("settings set target.load-script-from-symbol-file true") |
| 58 | + |
| 59 | + target = self.dbg.CreateTarget(None) |
| 60 | + |
| 61 | + error = lldb.SBError() |
| 62 | + |
| 63 | + # Now run the process, and then attach. When the attach |
| 64 | + # succeeds, make sure that we were in the right state when |
| 65 | + # the OS plugins were run. |
| 66 | + if not remote: |
| 67 | + popen = self.spawnSubprocess(executable, []) |
| 68 | + |
| 69 | + process = target.AttachToProcessWithID(lldb.SBListener(), popen.pid, error) |
| 70 | + self.assertSuccess(error, "Attach succeeded") |
| 71 | + else: |
| 72 | + self.setup_remote_platform(executable) |
| 73 | + process = target.process |
| 74 | + self.assertTrue(process.IsValid(), "Got a valid process from debugserver") |
| 75 | + |
| 76 | + # We should have figured out the target from the result of the attach: |
| 77 | + self.assertTrue(target.IsValid, "Got a valid target") |
| 78 | + |
| 79 | + # Make sure that we got the right plugin: |
| 80 | + self.expect( |
| 81 | + "settings show target.process.python-os-plugin-path", |
| 82 | + substrs=["operating_system.py"], |
| 83 | + ) |
| 84 | + |
| 85 | + for thread in process.threads: |
| 86 | + stack_depth = thread.num_frames |
| 87 | + reg_threads = thread.frames[0].reg |
| 88 | + |
| 89 | + # OKAY, that realized the threads, now see if the creation |
| 90 | + # state was correct. The way we use the OS plugin, it doesn't need |
| 91 | + # to create a thread, and doesn't have to call get_register_info, |
| 92 | + # so we don't expect those to get called. |
| 93 | + self.expect( |
| 94 | + "test_report_command", |
| 95 | + substrs=[ |
| 96 | + "in_init=1", |
| 97 | + "in_get_thread_info=1", |
| 98 | + "in_create_thread=2", |
| 99 | + "in_get_register_info=2", |
| 100 | + "in_get_register_data=1", |
| 101 | + ], |
| 102 | + ) |
| 103 | + |
| 104 | + def build_dsym(self, name): |
| 105 | + self.build(debug_info="dsym", dictionary={"EXE": name}) |
| 106 | + executable = self.getBuildArtifact(name) |
| 107 | + dsym_path = self.getBuildArtifact(name + ".dSYM") |
| 108 | + python_dir_path = dsym_path |
| 109 | + python_dir_path = os.path.join(dsym_path, "Contents", "Resources", "Python") |
| 110 | + if not os.path.exists(python_dir_path): |
| 111 | + os.mkdir(python_dir_path) |
| 112 | + python_file_name = name + ".py" |
| 113 | + |
| 114 | + os_plugin_dir = os.path.join(python_dir_path, "OS_Plugin") |
| 115 | + if not os.path.exists(os_plugin_dir): |
| 116 | + os.mkdir(os_plugin_dir) |
| 117 | + |
| 118 | + plugin_dest_path = os.path.join(os_plugin_dir, "operating_system.py") |
| 119 | + plugin_origin_path = os.path.join(self.getSourceDir(), "operating_system.py") |
| 120 | + shutil.copy(plugin_origin_path, plugin_dest_path) |
| 121 | + |
| 122 | + module_dest_path = os.path.join(python_dir_path, python_file_name) |
| 123 | + with open(module_dest_path, "w") as f: |
| 124 | + f.write("def __lldb_init_module(debugger, unused):\n") |
| 125 | + f.write( |
| 126 | + f" debugger.HandleCommand(\"settings set target.process.python-os-plugin-path '{plugin_dest_path}'\")\n" |
| 127 | + ) |
| 128 | + f.close() |
| 129 | + |
| 130 | + return executable |
| 131 | + |
| 132 | + def setup_remote_platform(self, exe): |
| 133 | + # Get debugserver to start up our process for us, and then we |
| 134 | + # can use `process connect` to attach to it. |
| 135 | + debugserver = get_debugserver_exe() |
| 136 | + debugserver_args = ["localhost:{}".format(self.PORT), exe] |
| 137 | + self.spawnSubprocess(debugserver, debugserver_args) |
| 138 | + |
| 139 | + # Select the platform. |
| 140 | + self.runCmd("platform select remote-gdb-server") |
| 141 | + |
| 142 | + # Connect to debugserver |
| 143 | + interpreter = self.dbg.GetCommandInterpreter() |
| 144 | + connected = False |
| 145 | + for i in range(self.ATTEMPTS): |
| 146 | + result = lldb.SBCommandReturnObject() |
| 147 | + interpreter.HandleCommand(f"gdb-remote localhost:{self.PORT}", result) |
| 148 | + connected = result.Succeeded() |
| 149 | + if connected: |
| 150 | + break |
| 151 | + time.sleep(self.TIMEOUT) |
| 152 | + |
| 153 | + self.assertTrue(connected, "could not connect to debugserver") |
0 commit comments