From 440fc0b7f1c5797ebb408cc824bf8b3e746c9627 Mon Sep 17 00:00:00 2001 From: Jason Simmons Date: Tue, 21 Dec 2021 15:49:09 -0800 Subject: [PATCH] Add an Android device ID flag to the Flutter GDB script (flutter/engine#30449) --- engine/src/flutter/sky/tools/flutter_gdb | 72 +++++++++++++----------- 1 file changed, 40 insertions(+), 32 deletions(-) diff --git a/engine/src/flutter/sky/tools/flutter_gdb b/engine/src/flutter/sky/tools/flutter_gdb index ed252dcafb..3b4b00f9cd 100755 --- a/engine/src/flutter/sky/tools/flutter_gdb +++ b/engine/src/flutter/sky/tools/flutter_gdb @@ -35,9 +35,22 @@ def _get_flutter_root(): return path -def _find_package_pid(adb_path, package): +def _get_adb_command(args, flutter_root): + if args.adb is None: + adb_path = os.path.join(flutter_root, ADB_LOCAL_PATH) + else: + adb_path = args.adb + + adb_command = [adb_path] + if args.device is not None: + adb_command.extend(['-s', args.device]) + + return adb_command + + +def _find_package_pid(adb_command, package): """Find the pid of the Flutter application process.""" - ps_output = subprocess.check_output([adb_path, 'shell', 'ps']) + ps_output = subprocess.check_output(adb_command + ['shell', 'ps']) ps_match = re.search('^\S+\s+(\d+).*\s%s' % package, ps_output, re.MULTILINE) if not ps_match: print('Unable to find pid for package %s on device' % package) @@ -45,9 +58,9 @@ def _find_package_pid(adb_path, package): return int(ps_match.group(1)) -def _get_device_abi(adb_path): +def _get_device_abi(adb_command): abi_output = subprocess.check_output( - [adb_path, 'shell', 'getprop', 'ro.product.cpu.abi']).strip() + adb_command + ['shell', 'getprop', 'ro.product.cpu.abi']).strip() if abi_output.startswith('arm64'): return 'arm64' if abi_output.startswith('arm'): @@ -90,15 +103,15 @@ class GdbClient(object): help='Create a sysroot tree suitable for debugging on older (pre-N) versions of Android') parser.set_defaults(func=self.run) - def _copy_system_libs(self, adb_path, package, old_sysroot): + def _copy_system_libs(self, adb_command, package, old_sysroot): """Copy libraries used by the Flutter process from the device to the host.""" - package_pid = _find_package_pid(adb_path, package) + package_pid = _find_package_pid(adb_command, package) if package_pid is None: return False # Find library files that are mapped into the process. proc_maps = subprocess.check_output( - [adb_path, 'shell', 'run-as', package, 'cat', '/proc/%d/maps' % package_pid]) + adb_command + ['shell', 'run-as', package, 'cat', '/proc/%d/maps' % package_pid]) proc_libs = re.findall('(/system/.*\.(?:so|oat))\s*$', proc_maps, re.MULTILINE) if old_sysroot: @@ -119,26 +132,23 @@ class GdbClient(object): local_path = os.path.join(GdbClient.SYSTEM_LIBS_PATH, local_path) if not os.path.exists(os.path.dirname(local_path)): os.makedirs(os.path.dirname(local_path)) - subprocess.call([adb_path, 'pull', lib, local_path], stderr=dev_null) + subprocess.call(adb_command + ['pull', lib, local_path], stderr=dev_null) return True def run(self, args): flutter_root = _get_flutter_root() - if args.adb is None: - adb_path = os.path.join(flutter_root, ADB_LOCAL_PATH) - else: - adb_path = args.adb + adb_command = _get_adb_command(args, flutter_root) if args.pull_libs: - if not self._copy_system_libs(adb_path, args.package, args.old_sysroot): + if not self._copy_system_libs(adb_command, args.package, args.old_sysroot): return 1 subprocess.check_call( - [adb_path, 'forward', 'tcp:%d' % args.gdb_port, 'tcp:%d' % args.gdb_port]) + adb_command + ['forward', 'tcp:%d' % args.gdb_port, 'tcp:%d' % args.gdb_port]) if args.local_engine is None: - abi = _get_device_abi(adb_path) + abi = _get_device_abi(adb_command) local_engine = _default_local_engine(abi) else: local_engine = args.local_engine @@ -174,36 +184,33 @@ class GdbServer(object): def run(self, args): flutter_root = _get_flutter_root() - if args.adb is None: - adb_path = os.path.join(flutter_root, ADB_LOCAL_PATH) - else: - adb_path = args.adb + adb_command = _get_adb_command(args, flutter_root) - package_pid = _find_package_pid(adb_path, args.package) + package_pid = _find_package_pid(adb_command, args.package) if package_pid is None: return 1 - abi = _get_device_abi(adb_path) + abi = _get_device_abi(adb_command) gdb_server_local_path = 'third_party/android_tools/ndk/prebuilt/android-%s/gdbserver/gdbserver' % abi # Copy gdbserver to the package's data directory. - subprocess.check_call([adb_path, 'push', - os.path.join(flutter_root, gdb_server_local_path), - GdbServer.GDB_SERVER_DEVICE_TMP_PATH]) + subprocess.check_call(adb_command + ['push', + os.path.join(flutter_root, gdb_server_local_path), + GdbServer.GDB_SERVER_DEVICE_TMP_PATH]) gdb_server_device_path = '/data/data/%s/gdbserver' % args.package - subprocess.check_call([adb_path, 'shell', 'run-as', args.package, 'cp', '-F', - GdbServer.GDB_SERVER_DEVICE_TMP_PATH, - gdb_server_device_path]) + subprocess.check_call(adb_command + ['shell', 'run-as', args.package, 'cp', '-F', + GdbServer.GDB_SERVER_DEVICE_TMP_PATH, + gdb_server_device_path]) - subprocess.call([adb_path, 'shell', 'run-as', args.package, - 'killall', 'gdbserver']) + subprocess.call(adb_command + ['shell', 'run-as', args.package, + 'killall', 'gdbserver']) # Run gdbserver. try: - subprocess.call([adb_path, 'shell', 'run-as', args.package, - gdb_server_device_path, - '--attach', ':%d' % args.gdb_port, str(package_pid)]) + subprocess.call(adb_command + ['shell', 'run-as', args.package, + gdb_server_device_path, + '--attach', ':%d' % args.gdb_port, str(package_pid)]) except KeyboardInterrupt: pass @@ -212,6 +219,7 @@ def main(): parser = argparse.ArgumentParser(description='Flutter debugger tool') subparsers = parser.add_subparsers(help='sub-command help') parser.add_argument('--adb', type=str, help='path to ADB tool') + parser.add_argument('--device', type=str, help='serial number of the target device') commands = [ GdbClient(),