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