Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 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 |
| 20 | import logging |
| 21 | import os |
| 22 | import subprocess |
| 23 | import sys |
| 24 | |
| 25 | # Shared functions across gdbclient.py and ndk-gdb.py. |
| 26 | import gdbrunner |
| 27 | |
| 28 | def get_gdbserver_path(root, arch): |
| 29 | path = "{}/prebuilts/misc/android-{}/gdbserver{}/gdbserver{}" |
| 30 | if arch.endswith("64"): |
| 31 | return path.format(root, arch, "64", "64") |
| 32 | else: |
| 33 | return path.format(root, arch, "", "") |
| 34 | |
| 35 | |
| 36 | def parse_args(): |
| 37 | parser = gdbrunner.ArgumentParser() |
| 38 | |
| 39 | group = parser.add_argument_group(title="attach target") |
| 40 | group = group.add_mutually_exclusive_group(required=True) |
| 41 | group.add_argument( |
| 42 | "-p", dest="target_pid", metavar="PID", type=int, |
| 43 | help="attach to a process with specified PID") |
| 44 | group.add_argument( |
| 45 | "-n", dest="target_name", metavar="NAME", |
| 46 | help="attach to a process with specified name") |
| 47 | group.add_argument( |
| 48 | "-r", dest="run_cmd", metavar="CMD", nargs=argparse.REMAINDER, |
| 49 | help="run a binary on the device, with args") |
| 50 | |
| 51 | parser.add_argument( |
| 52 | "--port", nargs="?", default="5039", |
| 53 | help="override the port used on the host") |
| 54 | parser.add_argument( |
| 55 | "--user", nargs="?", default="root", |
| 56 | help="user to run commands as on the device [default: root]") |
| 57 | |
| 58 | return parser.parse_args() |
| 59 | |
| 60 | |
| 61 | def dump_var(root, variable): |
| 62 | make_args = ["make", "CALLED_FROM_SETUP=true", |
| 63 | "BUILD_SYSTEM={}/build/core".format(root), |
| 64 | "--no-print-directory", "-f", |
| 65 | "{}/build/core/config.mk".format(root), |
| 66 | "dumpvar-{}".format(variable)] |
| 67 | |
Josh Gao | 6382f17 | 2015-10-02 15:58:05 -0700 | [diff] [blame] | 68 | make_output = subprocess.check_output(make_args, cwd=root) |
Josh Gao | 043bad7 | 2015-09-22 11:43:08 -0700 | [diff] [blame] | 69 | return make_output.splitlines()[0] |
| 70 | |
| 71 | |
| 72 | def verify_device(root, props): |
| 73 | names = set([props["ro.build.product"], props["ro.product.device"]]) |
| 74 | target_device = dump_var(root, "TARGET_DEVICE") |
| 75 | if target_device not in names: |
| 76 | msg = "TARGET_DEVICE ({}) does not match attached device ({})" |
| 77 | sys.exit(msg.format(target_device, ", ".join(names))) |
| 78 | |
| 79 | |
| 80 | def get_remote_pid(device, process_name): |
| 81 | processes = gdbrunner.get_processes(device) |
| 82 | if process_name not in processes: |
| 83 | msg = "failed to find running process {}".format(process_name) |
| 84 | sys.exit(msg) |
| 85 | pids = processes[process_name] |
| 86 | if len(pids) > 1: |
| 87 | msg = "multiple processes match '{}': {}".format(process_name, pids) |
| 88 | sys.exit(msg) |
| 89 | |
| 90 | # Fetch the binary using the PID later. |
| 91 | return pids[0] |
| 92 | |
| 93 | |
| 94 | def ensure_linker(device, sysroot, is64bit): |
| 95 | local_path = os.path.join(sysroot, "system", "bin", "linker") |
| 96 | remote_path = "/system/bin/linker" |
| 97 | if is64bit: |
| 98 | local_path += "64" |
| 99 | remote_path += "64" |
| 100 | if not os.path.exists(local_path): |
| 101 | device.pull(remote_path, local_path) |
| 102 | |
| 103 | |
| 104 | def handle_switches(args): |
| 105 | """Fetch the targeted binary and determine how to attach gdb. |
| 106 | |
| 107 | Args: |
| 108 | args: Parsed arguments. |
| 109 | sysroot: Local sysroot path. |
| 110 | |
| 111 | Returns: |
| 112 | (binary_file, attach_pid, run_cmd). |
| 113 | Precisely one of attach_pid or run_cmd will be None. |
| 114 | """ |
| 115 | |
| 116 | device = args.device |
| 117 | binary_file = None |
| 118 | pid = None |
| 119 | run_cmd = None |
| 120 | |
| 121 | if args.target_pid: |
| 122 | # Fetch the binary using the PID later. |
| 123 | pid = args.target_pid |
| 124 | elif args.target_name: |
| 125 | # Fetch the binary using the PID later. |
| 126 | pid = get_remote_pid(device, args.target_name) |
| 127 | elif args.run_cmd: |
| 128 | if not args.run_cmd[0]: |
| 129 | sys.exit("empty command passed to -r") |
| 130 | if not args.run_cmd[0].startswith("/"): |
| 131 | sys.exit("commands passed to -r must use absolute paths") |
| 132 | run_cmd = args.run_cmd |
| 133 | binary_file = gdbrunner.pull_file(device, run_cmd[0], user=args.user) |
| 134 | if binary_file is None: |
| 135 | assert pid is not None |
| 136 | try: |
| 137 | binary_file = gdbrunner.pull_binary(device, pid=pid, user=args.user) |
| 138 | except adb.ShellError: |
| 139 | sys.exit("failed to pull binary for PID {}".format(pid)) |
| 140 | |
| 141 | return (binary_file, pid, run_cmd) |
| 142 | |
| 143 | def generate_gdb_script(sysroot, binary_file, is64bit, port): |
| 144 | # Generate a gdb script. |
| 145 | # TODO: Detect the zygote and run 'art-on' automatically. |
| 146 | root = os.environ["ANDROID_BUILD_TOP"] |
| 147 | symbols_dir = os.path.join(sysroot, "system", "lib64" if is64bit else "lib") |
| 148 | vendor_dir = os.path.join(sysroot, "vendor", "lib64" if is64bit else "lib") |
| 149 | |
| 150 | solib_search_path = [] |
| 151 | symbols_paths = ["", "hw", "ssl/engines", "drm", "egl", "soundfx"] |
| 152 | vendor_paths = ["", "hw", "egl"] |
| 153 | solib_search_path += [os.path.join(symbols_dir, x) for x in symbols_paths] |
| 154 | solib_search_path += [os.path.join(vendor_dir, x) for x in vendor_paths] |
| 155 | solib_search_path = ":".join(solib_search_path) |
| 156 | |
| 157 | gdb_commands = "" |
| 158 | gdb_commands += "file '{}'\n".format(binary_file.name) |
| 159 | gdb_commands += "set solib-absolute-prefix {}\n".format(sysroot) |
| 160 | gdb_commands += "set solib-search-path {}\n".format(solib_search_path) |
| 161 | |
| 162 | dalvik_gdb_script = os.path.join(root, "development", "scripts", "gdb", |
| 163 | "dalvik.gdb") |
| 164 | if not os.path.exists(dalvik_gdb_script): |
| 165 | logging.warning(("couldn't find {} - ART debugging options will not " + |
| 166 | "be available").format(dalvik_gdb_script)) |
| 167 | else: |
| 168 | gdb_commands += "source {}\n".format(dalvik_gdb_script) |
| 169 | |
| 170 | gdb_commands += "target remote :{}\n".format(port) |
| 171 | return gdb_commands |
| 172 | |
| 173 | |
| 174 | def main(): |
| 175 | args = parse_args() |
| 176 | device = args.device |
| 177 | props = device.get_props() |
| 178 | |
| 179 | root = os.environ["ANDROID_BUILD_TOP"] |
| 180 | sysroot = dump_var(root, "abs-TARGET_OUT_UNSTRIPPED") |
| 181 | |
| 182 | # Make sure the environment matches the attached device. |
| 183 | verify_device(root, props) |
| 184 | |
| 185 | debug_socket = "/data/local/tmp/debug_socket" |
| 186 | pid = None |
| 187 | run_cmd = None |
| 188 | |
| 189 | # Fetch binary for -p, -n. |
| 190 | binary_file, pid, run_cmd = handle_switches(args) |
| 191 | |
| 192 | with binary_file: |
| 193 | arch = gdbrunner.get_binary_arch(binary_file) |
| 194 | is64bit = arch.endswith("64") |
| 195 | |
| 196 | # Make sure we have the linker |
| 197 | ensure_linker(device, sysroot, is64bit) |
| 198 | |
| 199 | # Start gdbserver. |
| 200 | gdbserver_local_path = get_gdbserver_path(root, arch) |
| 201 | gdbserver_remote_path = "/data/local/tmp/{}-gdbserver".format(arch) |
| 202 | gdbrunner.start_gdbserver( |
| 203 | device, gdbserver_local_path, gdbserver_remote_path, |
| 204 | target_pid=pid, run_cmd=run_cmd, debug_socket=debug_socket, |
| 205 | port=args.port, user=args.user) |
| 206 | |
| 207 | # Generate a gdb script. |
| 208 | gdb_commands = generate_gdb_script(sysroot=sysroot, |
| 209 | binary_file=binary_file, |
| 210 | is64bit=is64bit, |
| 211 | port=args.port) |
| 212 | |
| 213 | # Find where gdb is |
| 214 | if sys.platform.startswith("linux"): |
| 215 | platform_name = "linux-x86" |
| 216 | elif sys.platform.startswith("darwin"): |
| 217 | platform_name = "darwin-x86" |
| 218 | else: |
| 219 | sys.exit("Unknown platform: {}".format(sys.platform)) |
| 220 | gdb_path = os.path.join(root, "prebuilts", "gdb", platform_name, "bin", |
| 221 | "gdb") |
| 222 | |
| 223 | # Start gdb. |
| 224 | gdbrunner.start_gdb(gdb_path, gdb_commands) |
| 225 | |
| 226 | if __name__ == "__main__": |
| 227 | main() |