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