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