diff --git a/python-packages/gdbrunner/__init__.py b/python-packages/gdbrunner/__init__.py index 9cc6fb745..8dff373b2 100644 --- a/python-packages/gdbrunner/__init__.py +++ b/python-packages/gdbrunner/__init__.py @@ -244,6 +244,33 @@ def find_file(device, executable_path, sysroot, run_as_cmd=None): return (open(path, "r"), found_locally) raise RuntimeError('Could not find executable {}'.format(executable_path)) +def find_executable_path(device, executable_name, run_as_cmd=None): + """Find a device executable from its name + + This function calls which on the device to retrieve the absolute path of + the executable. + + Args: + device: the AndroidDevice object to use. + executable_name: the name of the executable to find. + run_as_cmd: if necessary, run-as or su command to prepend + + Returns: + The absolute path of the executable. + + Raises: + RuntimeError: could not find the executable. + """ + cmd = ["which", executable_name] + if run_as_cmd: + cmd = run_as_cmd + cmd + + try: + output, _ = device.shell(cmd) + return output + except adb.ShellError: + raise RuntimeError("Could not find executable '{}' on " + "device".format(executable_name)) def find_binary(device, pid, sysroot, run_as_cmd=None): """Finds a device executable file corresponding to |pid|.""" diff --git a/scripts/gdbclient.py b/scripts/gdbclient.py index 2e37e0b1b..ff593b3e8 100755 --- a/scripts/gdbclient.py +++ b/scripts/gdbclient.py @@ -144,9 +144,15 @@ def handle_switches(args, sysroot): elif args.run_cmd: if not args.run_cmd[0]: sys.exit("empty command passed to -r") - if not args.run_cmd[0].startswith("/"): - sys.exit("commands passed to -r must use absolute paths") run_cmd = args.run_cmd + if not run_cmd[0].startswith("/"): + try: + run_cmd[0] = gdbrunner.find_executable_path(device, args.run_cmd[0], + run_as_cmd=args.su_cmd) + except RuntimeError: + sys.exit("Could not find executable '{}' passed to -r, " + "please provide an absolute path.".format(args.run_cmd[0])) + binary_file, local = gdbrunner.find_file(device, run_cmd[0], sysroot, run_as_cmd=args.su_cmd) if binary_file is None: