gdbclient.py: Implement --chroot argument.

This allows executables to be debugged inside a chroot (such as the ART
testing chroot).

Change-Id: I1b790c3c2307e240d37ed0afb9a930d00957acec
This commit is contained in:
Peter Collingbourne
2022-03-03 12:17:43 -08:00
parent 03f2a0c5e6
commit ba5482675f
2 changed files with 17 additions and 12 deletions

View File

@@ -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,9 +184,8 @@ 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
if lldb:

View File

@@ -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,6 +318,9 @@ def do_main():
sysroot = os.path.join(os.environ["ANDROID_PRODUCT_OUT"], "symbols")
# Make sure the environment matches the attached 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"
@@ -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(