diff --git a/python-packages/gdbrunner/__init__.py b/python-packages/gdbrunner/__init__.py index 4446fcbc0..2eabf99d2 100644 --- a/python-packages/gdbrunner/__init__.py +++ b/python-packages/gdbrunner/__init__.py @@ -142,8 +142,8 @@ def get_pids(device, process_name): def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path, - target_pid, run_cmd, debug_socket, port, run_as_cmd=None, - lldb=False): + target_pid, run_cmd, debug_socket, port, run_as_cmd=[], + lldb=False, chroot=""): """Start gdbserver in the background and forward necessary ports. Args: @@ -162,15 +162,15 @@ def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path, assert target_pid is None or run_cmd is None + if chroot: + run_as_cmd = ["chroot", chroot] + run_as_cmd + # Remove the old socket file. - rm_cmd = ["rm", debug_socket] - if run_as_cmd: - rm_cmd = run_as_cmd + rm_cmd - device.shell_nocheck(rm_cmd) + device.shell_nocheck(run_as_cmd + ["rm", debug_socket]) # Push gdbserver to the target. if gdbserver_local_path is not None: - device.push(gdbserver_local_path, gdbserver_remote_path) + device.push(gdbserver_local_path, chroot + gdbserver_remote_path) # Run gdbserver. gdbserver_cmd = [gdbserver_remote_path] @@ -184,10 +184,9 @@ def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path, else: gdbserver_cmd += ["--"] + run_cmd - forward_gdbserver_port(device, local=port, remote="localfilesystem:{}".format(debug_socket)) + forward_gdbserver_port(device, local=port, remote="localfilesystem:{}".format(chroot + debug_socket)) - if run_as_cmd: - gdbserver_cmd = run_as_cmd + gdbserver_cmd + gdbserver_cmd = run_as_cmd + gdbserver_cmd if lldb: gdbserver_output_path = os.path.join(tempfile.gettempdir(), diff --git a/scripts/gdbclient.py b/scripts/gdbclient.py index b9764cd7e..539e6d0d5 100755 --- a/scripts/gdbclient.py +++ b/scripts/gdbclient.py @@ -102,6 +102,9 @@ def parse_args(): parser.add_argument( "--env", nargs=1, action="append", metavar="VAR=VALUE", help="set environment variable when running a binary") + parser.add_argument( + "--chroot", nargs='?', default="", metavar="PATH", + help="run command in a chroot in the given directory") return parser.parse_args() @@ -315,7 +318,10 @@ def do_main(): sysroot = os.path.join(os.environ["ANDROID_PRODUCT_OUT"], "symbols") # Make sure the environment matches the attached device. - verify_device(root, device) + # Skip when running in a chroot because the chroot lunch target may not + # match the device's lunch target. + if not args.chroot: + verify_device(root, device) debug_socket = "/data/local/tmp/debug_socket" pid = None @@ -356,7 +362,7 @@ def do_main(): gdbrunner.start_gdbserver( device, server_local_path, server_remote_path, target_pid=pid, run_cmd=run_cmd, debug_socket=debug_socket, - port=args.port, run_as_cmd=cmd_prefix, lldb=True) + port=args.port, run_as_cmd=cmd_prefix, lldb=True, chroot=args.chroot) else: print( "Connecting to tracing pid {} using local port {}".format(