Elliott Hughes | 17de6ce | 2021-06-23 18:00:46 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 2 | # |
| 3 | # Copyright (C) 2015 The Android Open Source Project |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | # |
| 17 | |
| 18 | import adb |
| 19 | import argparse |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 20 | import json |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 21 | import logging |
| 22 | import os |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 23 | import posixpath |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 24 | import re |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 25 | import shutil |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 26 | import subprocess |
| 27 | import sys |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 28 | import tempfile |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 29 | import textwrap |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 30 | |
| 31 | # Shared functions across gdbclient.py and ndk-gdb.py. |
| 32 | import gdbrunner |
| 33 | |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 34 | g_temp_dirs = [] |
| 35 | |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 36 | |
| 37 | def read_toolchain_config(root): |
| 38 | """Finds out current toolchain path and version.""" |
| 39 | def get_value(str): |
| 40 | return str[str.index('"') + 1:str.rindex('"')] |
| 41 | |
| 42 | config_path = os.path.join(root, 'build', 'soong', 'cc', 'config', |
| 43 | 'global.go') |
| 44 | with open(config_path) as f: |
| 45 | contents = f.readlines() |
| 46 | clang_base = "" |
| 47 | clang_version = "" |
| 48 | for line in contents: |
| 49 | line = line.strip() |
| 50 | if line.startswith('ClangDefaultBase'): |
| 51 | clang_base = get_value(line) |
| 52 | elif line.startswith('ClangDefaultVersion'): |
| 53 | clang_version = get_value(line) |
| 54 | return (clang_base, clang_version) |
| 55 | |
| 56 | |
Haibo Huang | e4d8bfd | 2020-07-22 16:37:35 -0700 | [diff] [blame] | 57 | def get_lldb_path(toolchain_path): |
| 58 | for lldb_name in ['lldb.sh', 'lldb.cmd', 'lldb', 'lldb.exe']: |
| 59 | debugger_path = os.path.join(toolchain_path, "bin", lldb_name) |
| 60 | if os.path.isfile(debugger_path): |
| 61 | return debugger_path |
| 62 | return None |
| 63 | |
| 64 | |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 65 | def get_lldb_server_path(root, clang_base, clang_version, arch): |
| 66 | arch = { |
| 67 | 'arm': 'arm', |
| 68 | 'arm64': 'aarch64', |
| 69 | 'x86': 'i386', |
| 70 | 'x86_64': 'x86_64', |
| 71 | }[arch] |
| 72 | return os.path.join(root, clang_base, "linux-x86", |
| 73 | clang_version, "runtimes_ndk_cxx", arch, "lldb-server") |
| 74 | |
| 75 | |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 76 | def get_tracer_pid(device, pid): |
| 77 | if pid is None: |
| 78 | return 0 |
| 79 | |
| 80 | line, _ = device.shell(["grep", "-e", "^TracerPid:", "/proc/{}/status".format(pid)]) |
| 81 | tracer_pid = re.sub('TracerPid:\t(.*)\n', r'\1', line) |
| 82 | return int(tracer_pid) |
| 83 | |
| 84 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 85 | def parse_args(): |
| 86 | parser = gdbrunner.ArgumentParser() |
| 87 | |
| 88 | group = parser.add_argument_group(title="attach target") |
| 89 | group = group.add_mutually_exclusive_group(required=True) |
| 90 | group.add_argument( |
| 91 | "-p", dest="target_pid", metavar="PID", type=int, |
| 92 | help="attach to a process with specified PID") |
| 93 | group.add_argument( |
| 94 | "-n", dest="target_name", metavar="NAME", |
| 95 | help="attach to a process with specified name") |
| 96 | group.add_argument( |
| 97 | "-r", dest="run_cmd", metavar="CMD", nargs=argparse.REMAINDER, |
| 98 | help="run a binary on the device, with args") |
| 99 | |
| 100 | parser.add_argument( |
| 101 | "--port", nargs="?", default="5039", |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 102 | help="override the port used on the host [default: 5039]") |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 103 | parser.add_argument( |
| 104 | "--user", nargs="?", default="root", |
| 105 | help="user to run commands as on the device [default: root]") |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 106 | parser.add_argument( |
Alex Light | a8f224d | 2020-11-10 10:30:19 -0800 | [diff] [blame] | 107 | "--setup-forwarding", default=None, |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 108 | choices=["lldb", "vscode-lldb"], |
| 109 | help=("Set up lldb-server and port forwarding. Prints commands or " + |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 110 | ".vscode/launch.json configuration needed to connect the debugging " + |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 111 | "client to the server. 'vscode' with llbd and 'vscode-lldb' both " + |
| 112 | "require the 'vadimcn.vscode-lldb' extension.")) |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 113 | |
Peter Collingbourne | 63bf108 | 2018-12-19 20:51:42 -0800 | [diff] [blame] | 114 | parser.add_argument( |
| 115 | "--env", nargs=1, action="append", metavar="VAR=VALUE", |
| 116 | help="set environment variable when running a binary") |
| 117 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 118 | return parser.parse_args() |
| 119 | |
| 120 | |
Elliott Hughes | 1a2f12d | 2017-06-02 13:15:59 -0700 | [diff] [blame] | 121 | def verify_device(root, device): |
Junichi Uekawa | 6612c92 | 2020-09-07 11:20:59 +0900 | [diff] [blame] | 122 | names = set([device.get_prop("ro.build.product"), device.get_prop("ro.product.name")]) |
Josh Gao | 466e289 | 2017-07-13 15:39:05 -0700 | [diff] [blame] | 123 | target_device = os.environ["TARGET_PRODUCT"] |
Junichi Uekawa | 6612c92 | 2020-09-07 11:20:59 +0900 | [diff] [blame] | 124 | if target_device not in names: |
Josh Gao | 466e289 | 2017-07-13 15:39:05 -0700 | [diff] [blame] | 125 | msg = "TARGET_PRODUCT ({}) does not match attached device ({})" |
Junichi Uekawa | 6612c92 | 2020-09-07 11:20:59 +0900 | [diff] [blame] | 126 | sys.exit(msg.format(target_device, ", ".join(names))) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 127 | |
| 128 | |
| 129 | def get_remote_pid(device, process_name): |
| 130 | processes = gdbrunner.get_processes(device) |
| 131 | if process_name not in processes: |
| 132 | msg = "failed to find running process {}".format(process_name) |
| 133 | sys.exit(msg) |
| 134 | pids = processes[process_name] |
| 135 | if len(pids) > 1: |
| 136 | msg = "multiple processes match '{}': {}".format(process_name, pids) |
| 137 | sys.exit(msg) |
| 138 | |
| 139 | # Fetch the binary using the PID later. |
| 140 | return pids[0] |
| 141 | |
| 142 | |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 143 | def make_temp_dir(prefix): |
| 144 | global g_temp_dirs |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 145 | result = tempfile.mkdtemp(prefix='lldbclient-linker-') |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 146 | g_temp_dirs.append(result) |
| 147 | return result |
| 148 | |
| 149 | |
| 150 | def ensure_linker(device, sysroot, interp): |
| 151 | """Ensure that the device's linker exists on the host. |
| 152 | |
| 153 | PT_INTERP is usually /system/bin/linker[64], but on the device, that file is |
| 154 | a symlink to /apex/com.android.runtime/bin/linker[64]. The symbolized linker |
| 155 | binary on the host is located in ${sysroot}/apex, not in ${sysroot}/system, |
| 156 | so add the ${sysroot}/apex path to the solib search path. |
| 157 | |
| 158 | PT_INTERP will be /system/bin/bootstrap/linker[64] for executables using the |
| 159 | non-APEX/bootstrap linker. No search path modification is needed. |
| 160 | |
| 161 | For a tapas build, only an unbundled app is built, and there is no linker in |
| 162 | ${sysroot} at all, so copy the linker from the device. |
| 163 | |
| 164 | Returns: |
| 165 | A directory to add to the soinfo search path or None if no directory |
| 166 | needs to be added. |
| 167 | """ |
| 168 | |
| 169 | # Static executables have no interpreter. |
| 170 | if interp is None: |
| 171 | return None |
| 172 | |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 173 | # lldb will search for the linker using the PT_INTERP path. First try to find |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 174 | # it in the sysroot. |
| 175 | local_path = os.path.join(sysroot, interp.lstrip("/")) |
| 176 | if os.path.exists(local_path): |
| 177 | return None |
| 178 | |
| 179 | # If the linker on the device is a symlink, search for the symlink's target |
| 180 | # in the sysroot directory. |
| 181 | interp_real, _ = device.shell(["realpath", interp]) |
| 182 | interp_real = interp_real.strip() |
| 183 | local_path = os.path.join(sysroot, interp_real.lstrip("/")) |
| 184 | if os.path.exists(local_path): |
| 185 | if posixpath.basename(interp) == posixpath.basename(interp_real): |
| 186 | # Add the interpreter's directory to the search path. |
| 187 | return os.path.dirname(local_path) |
| 188 | else: |
| 189 | # If PT_INTERP is linker_asan[64], but the sysroot file is |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 190 | # linker[64], then copy the local file to the name lldb expects. |
| 191 | result = make_temp_dir('lldbclient-linker-') |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 192 | shutil.copy(local_path, os.path.join(result, posixpath.basename(interp))) |
| 193 | return result |
| 194 | |
| 195 | # Pull the system linker. |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 196 | result = make_temp_dir('lldbclient-linker-') |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 197 | device.pull(interp, os.path.join(result, posixpath.basename(interp))) |
| 198 | return result |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 199 | |
| 200 | |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 201 | def handle_switches(args, sysroot): |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 202 | """Fetch the targeted binary and determine how to attach lldb. |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 203 | |
| 204 | Args: |
| 205 | args: Parsed arguments. |
| 206 | sysroot: Local sysroot path. |
| 207 | |
| 208 | Returns: |
| 209 | (binary_file, attach_pid, run_cmd). |
| 210 | Precisely one of attach_pid or run_cmd will be None. |
| 211 | """ |
| 212 | |
| 213 | device = args.device |
| 214 | binary_file = None |
| 215 | pid = None |
| 216 | run_cmd = None |
| 217 | |
Josh Gao | 057c273 | 2017-05-24 15:55:50 -0700 | [diff] [blame] | 218 | args.su_cmd = ["su", args.user] if args.user else [] |
| 219 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 220 | if args.target_pid: |
| 221 | # Fetch the binary using the PID later. |
| 222 | pid = args.target_pid |
| 223 | elif args.target_name: |
| 224 | # Fetch the binary using the PID later. |
| 225 | pid = get_remote_pid(device, args.target_name) |
| 226 | elif args.run_cmd: |
| 227 | if not args.run_cmd[0]: |
| 228 | sys.exit("empty command passed to -r") |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 229 | run_cmd = args.run_cmd |
Kevin Rocard | 258c89e | 2017-07-12 18:21:29 -0700 | [diff] [blame] | 230 | if not run_cmd[0].startswith("/"): |
| 231 | try: |
| 232 | run_cmd[0] = gdbrunner.find_executable_path(device, args.run_cmd[0], |
| 233 | run_as_cmd=args.su_cmd) |
| 234 | except RuntimeError: |
| 235 | sys.exit("Could not find executable '{}' passed to -r, " |
| 236 | "please provide an absolute path.".format(args.run_cmd[0])) |
| 237 | |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 238 | binary_file, local = gdbrunner.find_file(device, run_cmd[0], sysroot, |
Josh Gao | 057c273 | 2017-05-24 15:55:50 -0700 | [diff] [blame] | 239 | run_as_cmd=args.su_cmd) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 240 | if binary_file is None: |
| 241 | assert pid is not None |
| 242 | try: |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 243 | binary_file, local = gdbrunner.find_binary(device, pid, sysroot, |
Josh Gao | 057c273 | 2017-05-24 15:55:50 -0700 | [diff] [blame] | 244 | run_as_cmd=args.su_cmd) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 245 | except adb.ShellError: |
| 246 | sys.exit("failed to pull binary for PID {}".format(pid)) |
| 247 | |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 248 | if not local: |
| 249 | logging.warning("Couldn't find local unstripped executable in {}," |
| 250 | " symbols may not be available.".format(sysroot)) |
| 251 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 252 | return (binary_file, pid, run_cmd) |
| 253 | |
Alex Light | a8f224d | 2020-11-10 10:30:19 -0800 | [diff] [blame] | 254 | def generate_vscode_lldb_script(root, sysroot, binary_name, port, solib_search_path): |
| 255 | # TODO It would be nice if we didn't need to copy this or run the |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 256 | # lldbclient.py program manually. Doing this would probably require |
Alex Light | a8f224d | 2020-11-10 10:30:19 -0800 | [diff] [blame] | 257 | # writing a vscode extension or modifying an existing one. |
| 258 | # TODO: https://code.visualstudio.com/api/references/vscode-api#debug and |
| 259 | # https://code.visualstudio.com/api/extension-guides/debugger-extension and |
| 260 | # https://github.com/vadimcn/vscode-lldb/blob/6b775c439992b6615e92f4938ee4e211f1b060cf/extension/pickProcess.ts#L6 |
| 261 | res = { |
| 262 | "name": "(lldbclient.py) Attach {} (port: {})".format(binary_name.split("/")[-1], port), |
| 263 | "type": "lldb", |
| 264 | "request": "custom", |
| 265 | "relativePathBase": root, |
| 266 | "sourceMap": { "/b/f/w" : root, '': root, '.': root }, |
| 267 | "initCommands": ['settings append target.exec-search-paths {}'.format(' '.join(solib_search_path))], |
| 268 | "targetCreateCommands": ["target create {}".format(binary_name), |
| 269 | "target modules search-paths add / {}/".format(sysroot)], |
| 270 | "processCreateCommands": ["gdb-remote {}".format(port)] |
| 271 | } |
| 272 | return json.dumps(res, indent=4) |
| 273 | |
Haibo Huang | 07e1707 | 2020-05-12 16:51:29 -0700 | [diff] [blame] | 274 | def generate_lldb_script(root, sysroot, binary_name, port, solib_search_path): |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 275 | commands = [] |
| 276 | commands.append( |
| 277 | 'settings append target.exec-search-paths {}'.format(' '.join(solib_search_path))) |
| 278 | |
| 279 | commands.append('target create {}'.format(binary_name)) |
Haibo Huang | 987436c | 2020-09-22 21:01:31 -0700 | [diff] [blame] | 280 | # For RBE support. |
| 281 | commands.append("settings append target.source-map '/b/f/w' '{}'".format(root)) |
| 282 | commands.append("settings append target.source-map '' '{}'".format(root)) |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 283 | commands.append('target modules search-paths add / {}/'.format(sysroot)) |
| 284 | commands.append('gdb-remote {}'.format(port)) |
| 285 | return '\n'.join(commands) |
| 286 | |
| 287 | |
| 288 | def generate_setup_script(debugger_path, sysroot, linker_search_dir, binary_file, is64bit, port, debugger, connect_timeout=5): |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 289 | # Generate a setup script. |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 290 | root = os.environ["ANDROID_BUILD_TOP"] |
| 291 | symbols_dir = os.path.join(sysroot, "system", "lib64" if is64bit else "lib") |
| 292 | vendor_dir = os.path.join(sysroot, "vendor", "lib64" if is64bit else "lib") |
| 293 | |
| 294 | solib_search_path = [] |
| 295 | symbols_paths = ["", "hw", "ssl/engines", "drm", "egl", "soundfx"] |
| 296 | vendor_paths = ["", "hw", "egl"] |
| 297 | solib_search_path += [os.path.join(symbols_dir, x) for x in symbols_paths] |
| 298 | solib_search_path += [os.path.join(vendor_dir, x) for x in vendor_paths] |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 299 | if linker_search_dir is not None: |
| 300 | solib_search_path += [linker_search_dir] |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 301 | |
Alex Light | a8f224d | 2020-11-10 10:30:19 -0800 | [diff] [blame] | 302 | if debugger == "vscode-lldb": |
| 303 | return generate_vscode_lldb_script( |
| 304 | root, sysroot, binary_file.name, port, solib_search_path) |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 305 | elif debugger == 'lldb': |
| 306 | return generate_lldb_script( |
Haibo Huang | 07e1707 | 2020-05-12 16:51:29 -0700 | [diff] [blame] | 307 | root, sysroot, binary_file.name, port, solib_search_path) |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 308 | else: |
| 309 | raise Exception("Unknown debugger type " + debugger) |
| 310 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 311 | |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 312 | def do_main(): |
Josh Gao | 466e289 | 2017-07-13 15:39:05 -0700 | [diff] [blame] | 313 | required_env = ["ANDROID_BUILD_TOP", |
| 314 | "ANDROID_PRODUCT_OUT", "TARGET_PRODUCT"] |
| 315 | for env in required_env: |
| 316 | if env not in os.environ: |
| 317 | sys.exit( |
| 318 | "Environment variable '{}' not defined, have you run lunch?".format(env)) |
| 319 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 320 | args = parse_args() |
| 321 | device = args.device |
Josh Gao | 44b84a8 | 2015-10-28 11:57:37 -0700 | [diff] [blame] | 322 | |
| 323 | if device is None: |
| 324 | sys.exit("ERROR: Failed to find device.") |
| 325 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 326 | root = os.environ["ANDROID_BUILD_TOP"] |
Josh Gao | 466e289 | 2017-07-13 15:39:05 -0700 | [diff] [blame] | 327 | sysroot = os.path.join(os.environ["ANDROID_PRODUCT_OUT"], "symbols") |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 328 | |
| 329 | # Make sure the environment matches the attached device. |
Elliott Hughes | 1a2f12d | 2017-06-02 13:15:59 -0700 | [diff] [blame] | 330 | verify_device(root, device) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 331 | |
| 332 | debug_socket = "/data/local/tmp/debug_socket" |
| 333 | pid = None |
| 334 | run_cmd = None |
| 335 | |
| 336 | # Fetch binary for -p, -n. |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 337 | binary_file, pid, run_cmd = handle_switches(args, sysroot) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 338 | |
| 339 | with binary_file: |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 340 | if sys.platform.startswith("linux"): |
| 341 | platform_name = "linux-x86" |
| 342 | elif sys.platform.startswith("darwin"): |
| 343 | platform_name = "darwin-x86" |
| 344 | else: |
| 345 | sys.exit("Unknown platform: {}".format(sys.platform)) |
| 346 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 347 | arch = gdbrunner.get_binary_arch(binary_file) |
| 348 | is64bit = arch.endswith("64") |
| 349 | |
| 350 | # Make sure we have the linker |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 351 | clang_base, clang_version = read_toolchain_config(root) |
| 352 | toolchain_path = os.path.join(root, clang_base, platform_name, |
| 353 | clang_version) |
| 354 | llvm_readobj_path = os.path.join(toolchain_path, "bin", "llvm-readobj") |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 355 | interp = gdbrunner.get_binary_interp(binary_file.name, llvm_readobj_path) |
| 356 | linker_search_dir = ensure_linker(device, sysroot, interp) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 357 | |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 358 | tracer_pid = get_tracer_pid(device, pid) |
| 359 | if tracer_pid == 0: |
Peter Collingbourne | 63bf108 | 2018-12-19 20:51:42 -0800 | [diff] [blame] | 360 | cmd_prefix = args.su_cmd |
| 361 | if args.env: |
| 362 | cmd_prefix += ['env'] + [v[0] for v in args.env] |
| 363 | |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 364 | # Start lldb-server. |
| 365 | server_local_path = get_lldb_server_path(root, clang_base, clang_version, arch) |
| 366 | server_remote_path = "/data/local/tmp/{}-lldb-server".format(arch) |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 367 | gdbrunner.start_gdbserver( |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 368 | device, server_local_path, server_remote_path, |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 369 | target_pid=pid, run_cmd=run_cmd, debug_socket=debug_socket, |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 370 | port=args.port, run_as_cmd=cmd_prefix, lldb=True) |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 371 | else: |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 372 | print( |
| 373 | "Connecting to tracing pid {} using local port {}".format( |
| 374 | tracer_pid, args.port)) |
Elliott Hughes | 89e1ecf | 2017-06-30 14:03:32 -0700 | [diff] [blame] | 375 | gdbrunner.forward_gdbserver_port(device, local=args.port, |
| 376 | remote="tcp:{}".format(args.port)) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 377 | |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 378 | debugger_path = get_lldb_path(toolchain_path) |
| 379 | debugger = args.setup_forwarding or 'lldb' |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 380 | |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 381 | # Generate the lldb script. |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 382 | setup_commands = generate_setup_script(debugger_path=debugger_path, |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 383 | sysroot=sysroot, |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 384 | linker_search_dir=linker_search_dir, |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 385 | binary_file=binary_file, |
| 386 | is64bit=is64bit, |
| 387 | port=args.port, |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 388 | debugger=debugger) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 389 | |
Alex Light | a8f224d | 2020-11-10 10:30:19 -0800 | [diff] [blame] | 390 | if not args.setup_forwarding: |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 391 | # Print a newline to separate our messages from the GDB session. |
| 392 | print("") |
David Pursell | 639d1c4 | 2015-10-20 15:38:32 -0700 | [diff] [blame] | 393 | |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 394 | # Start lldb. |
| 395 | gdbrunner.start_gdb(debugger_path, setup_commands, lldb=True) |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 396 | else: |
| 397 | print("") |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 398 | print(setup_commands) |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 399 | print("") |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 400 | if args.setup_forwarding == "vscode-lldb": |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 401 | print(textwrap.dedent(""" |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 402 | Paste the above json into .vscode/launch.json and start the debugger as |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 403 | normal. Press enter in this terminal once debugging is finished to shut |
| 404 | lldb-server down and close all the ports.""")) |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 405 | else: |
Haibo Huang | e194fce | 2020-01-06 14:40:27 -0800 | [diff] [blame] | 406 | print(textwrap.dedent(""" |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 407 | Paste the lldb commands above into the lldb frontend to set up the |
| 408 | lldb-server connection. Press enter in this terminal once debugging is |
| 409 | finished to shut lldb-server down and close all the ports.""")) |
Alex Light | 9247665 | 2019-01-17 11:18:48 -0800 | [diff] [blame] | 410 | print("") |
Elliott Hughes | 4c8e875 | 2021-06-25 14:23:22 -0700 | [diff] [blame] | 411 | raw_input("Press enter to shut down lldb-server") |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 412 | |
Ryan Prichard | 5d1c3cb | 2019-06-04 16:35:02 -0700 | [diff] [blame] | 413 | |
| 414 | def main(): |
| 415 | try: |
| 416 | do_main() |
| 417 | finally: |
| 418 | global g_temp_dirs |
| 419 | for temp_dir in g_temp_dirs: |
| 420 | shutil.rmtree(temp_dir) |
| 421 | |
| 422 | |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 423 | if __name__ == "__main__": |
| 424 | main() |