Merge "gdbrunner: take a run-as/su command instead of a user."
am: 3099f0c89d
Change-Id: Ib2b38ad645459273214f76abb648bcc2ba0ed478
This commit is contained in:
@@ -79,17 +79,6 @@ class ArgumentParser(argparse.ArgumentParser):
|
||||
return result
|
||||
|
||||
|
||||
def get_run_as_cmd(user, cmd):
|
||||
"""Generate a run-as or su command depending on user."""
|
||||
|
||||
if user is None:
|
||||
return cmd
|
||||
elif user == "root":
|
||||
return ["su", "0"] + cmd
|
||||
else:
|
||||
return ["run-as", user] + cmd
|
||||
|
||||
|
||||
def get_processes(device):
|
||||
"""Return a dict from process name to list of running PIDs on the device."""
|
||||
|
||||
@@ -146,7 +135,7 @@ def get_pids(device, process_name):
|
||||
|
||||
|
||||
def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path,
|
||||
target_pid, run_cmd, debug_socket, port, user=None):
|
||||
target_pid, run_cmd, debug_socket, port, run_as_cmd=None):
|
||||
"""Start gdbserver in the background and forward necessary ports.
|
||||
|
||||
Args:
|
||||
@@ -157,7 +146,7 @@ def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path,
|
||||
run_cmd: Command to run on the device.
|
||||
debug_socket: Device path to place gdbserver unix domain socket.
|
||||
port: Host port to forward the debug_socket to.
|
||||
user: Device user to run gdbserver as.
|
||||
run_as_cmd: run-as or su command to prepend to commands.
|
||||
|
||||
Returns:
|
||||
Popen handle to the `adb shell` process gdbserver was started with.
|
||||
@@ -181,7 +170,9 @@ def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path,
|
||||
device.forward("tcp:{}".format(port),
|
||||
"localfilesystem:{}".format(debug_socket))
|
||||
atexit.register(lambda: device.forward_remove("tcp:{}".format(port)))
|
||||
gdbserver_cmd = get_run_as_cmd(user, gdbserver_cmd)
|
||||
|
||||
if run_as_cmd:
|
||||
gdbserver_cmd = run_as_cmd + gdbserver_cmd
|
||||
|
||||
gdbserver_output_path = os.path.join(tempfile.gettempdir(),
|
||||
"gdbclient.log")
|
||||
@@ -191,7 +182,7 @@ def start_gdbserver(device, gdbserver_local_path, gdbserver_remote_path,
|
||||
stderr=gdbserver_output)
|
||||
|
||||
|
||||
def find_file(device, executable_path, sysroot, user=None):
|
||||
def find_file(device, executable_path, sysroot, run_as_cmd=None):
|
||||
"""Finds a device executable file.
|
||||
|
||||
This function first attempts to find the local file which will
|
||||
@@ -202,7 +193,7 @@ def find_file(device, executable_path, sysroot, user=None):
|
||||
device: the AndroidDevice object to use.
|
||||
executable_path: absolute path to the executable or symlink.
|
||||
sysroot: absolute path to the built symbol sysroot.
|
||||
user: if necessary, the user to download the file as.
|
||||
run_as_cmd: if necessary, run-as or su command to prepend
|
||||
|
||||
Returns:
|
||||
A tuple containing (<open file object>, <was found locally>).
|
||||
@@ -231,8 +222,11 @@ def find_file(device, executable_path, sysroot, user=None):
|
||||
file_name = "gdbclient-binary-{}".format(os.getppid())
|
||||
remote_temp_path = "/data/local/tmp/{}".format(file_name)
|
||||
local_path = os.path.join(tempfile.gettempdir(), file_name)
|
||||
cmd = get_run_as_cmd(user,
|
||||
["cat", executable_path, ">", remote_temp_path])
|
||||
|
||||
cmd = ["cat", executable_path, ">", remote_temp_path]
|
||||
if run_as_cmd:
|
||||
cmd = run_as_cmd + cmd
|
||||
|
||||
try:
|
||||
device.shell(cmd)
|
||||
except adb.ShellError:
|
||||
|
||||
@@ -123,6 +123,8 @@ def handle_switches(args, sysroot):
|
||||
pid = None
|
||||
run_cmd = None
|
||||
|
||||
args.su_cmd = ["su", args.user] if args.user else []
|
||||
|
||||
if args.target_pid:
|
||||
# Fetch the binary using the PID later.
|
||||
pid = args.target_pid
|
||||
@@ -136,12 +138,12 @@ def handle_switches(args, sysroot):
|
||||
sys.exit("commands passed to -r must use absolute paths")
|
||||
run_cmd = args.run_cmd
|
||||
binary_file, local = gdbrunner.find_file(device, run_cmd[0], sysroot,
|
||||
user=args.user)
|
||||
run_as_cmd=args.su_cmd)
|
||||
if binary_file is None:
|
||||
assert pid is not None
|
||||
try:
|
||||
binary_file, local = gdbrunner.find_binary(device, pid, sysroot,
|
||||
user=args.user)
|
||||
run_as_cmd=args.su_cmd)
|
||||
except adb.ShellError:
|
||||
sys.exit("failed to pull binary for PID {}".format(pid))
|
||||
|
||||
@@ -242,7 +244,7 @@ def main():
|
||||
gdbrunner.start_gdbserver(
|
||||
device, gdbserver_local_path, gdbserver_remote_path,
|
||||
target_pid=pid, run_cmd=run_cmd, debug_socket=debug_socket,
|
||||
port=args.port, user=args.user)
|
||||
port=args.port, run_as_cmd=args.su_cmd)
|
||||
|
||||
# Generate a gdb script.
|
||||
gdb_commands = generate_gdb_script(sysroot=sysroot,
|
||||
|
||||
Reference in New Issue
Block a user