Can debug again on Pixely devices ...

This commit is contained in:
Aaron Culliney 2019-10-06 16:21:38 -07:00
parent 135ccb6b2d
commit ad95368a8c
2 changed files with 516 additions and 856 deletions

View File

@ -0,0 +1,516 @@
#
# Copyright (C) 2015 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
import atexit
import base64
import logging
import os
import re
import subprocess
class FindDeviceError(RuntimeError):
pass
class DeviceNotFoundError(FindDeviceError):
def __init__(self, serial):
self.serial = serial
super(DeviceNotFoundError, self).__init__(
'No device with serial {}'.format(serial))
class NoUniqueDeviceError(FindDeviceError):
def __init__(self):
super(NoUniqueDeviceError, self).__init__('No unique device')
class ShellError(RuntimeError):
def __init__(self, cmd, stdout, stderr, exit_code):
super(ShellError, self).__init__(
'`{0}` exited with code {1}'.format(cmd, exit_code))
self.cmd = cmd
self.stdout = stdout
self.stderr = stderr
self.exit_code = exit_code
def get_devices(adb_path='adb'):
with open(os.devnull, 'wb') as devnull:
subprocess.check_call([adb_path, 'start-server'], stdout=devnull,
stderr=devnull)
out = split_lines(subprocess.check_output([adb_path, 'devices']))
# The first line of `adb devices` just says "List of attached devices", so
# skip that.
devices = []
for line in out[1:]:
if not line.strip():
continue
if 'offline' in line:
continue
serial, _ = re.split(r'\s+', line, maxsplit=1)
devices.append(serial)
return devices
def _get_unique_device(product=None, adb_path='adb'):
devices = get_devices(adb_path=adb_path)
if len(devices) != 1:
raise NoUniqueDeviceError()
return AndroidDevice(devices[0], product, adb_path)
def _get_device_by_serial(serial, product=None, adb_path='adb'):
for device in get_devices(adb_path=adb_path):
if device == serial:
return AndroidDevice(serial, product, adb_path)
raise DeviceNotFoundError(serial)
def get_device(serial=None, product=None, adb_path='adb'):
"""Get a uniquely identified AndroidDevice if one is available.
Raises:
DeviceNotFoundError:
The serial specified by `serial` or $ANDROID_SERIAL is not
connected.
NoUniqueDeviceError:
Neither `serial` nor $ANDROID_SERIAL was set, and the number of
devices connected to the system is not 1. Having 0 connected
devices will also result in this error.
Returns:
An AndroidDevice associated with the first non-None identifier in the
following order of preference:
1) The `serial` argument.
2) The environment variable $ANDROID_SERIAL.
3) The single device connnected to the system.
"""
if serial is not None:
return _get_device_by_serial(serial, product, adb_path)
android_serial = os.getenv('ANDROID_SERIAL')
if android_serial is not None:
return _get_device_by_serial(android_serial, product, adb_path)
return _get_unique_device(product, adb_path=adb_path)
def _get_device_by_type(flag, adb_path):
with open(os.devnull, 'wb') as devnull:
subprocess.check_call([adb_path, 'start-server'], stdout=devnull,
stderr=devnull)
try:
serial = subprocess.check_output(
[adb_path, flag, 'get-serialno']).strip()
except subprocess.CalledProcessError:
raise RuntimeError('adb unexpectedly returned nonzero')
if serial == 'unknown':
raise NoUniqueDeviceError()
return _get_device_by_serial(serial, adb_path=adb_path)
def get_usb_device(adb_path='adb'):
"""Get the unique USB-connected AndroidDevice if it is available.
Raises:
NoUniqueDeviceError:
0 or multiple devices are connected via USB.
Returns:
An AndroidDevice associated with the unique USB-connected device.
"""
return _get_device_by_type('-d', adb_path=adb_path)
def get_emulator_device(adb_path='adb'):
"""Get the unique emulator AndroidDevice if it is available.
Raises:
NoUniqueDeviceError:
0 or multiple emulators are running.
Returns:
An AndroidDevice associated with the unique running emulator.
"""
return _get_device_by_type('-e', adb_path=adb_path)
# If necessary, modifies subprocess.check_output() or subprocess.Popen() args
# to run the subprocess via Windows PowerShell to work-around an issue in
# Python 2's subprocess class on Windows where it doesn't support Unicode.
def _get_subprocess_args(args):
# Only do this slow work-around if Unicode is in the cmd line on Windows.
# PowerShell takes 600-700ms to startup on a 2013-2014 machine, which is
# very slow.
if os.name != 'nt' or all(not isinstance(arg, unicode) for arg in args[0]):
return args
def escape_arg(arg):
# Escape for the parsing that the C Runtime does in Windows apps. In
# particular, this will take care of double-quotes.
arg = subprocess.list2cmdline([arg])
# Escape single-quote with another single-quote because we're about
# to...
arg = arg.replace(u"'", u"''")
# ...put the arg in a single-quoted string for PowerShell to parse.
arg = u"'" + arg + u"'"
return arg
# Escape command line args.
argv = map(escape_arg, args[0])
# Cause script errors (such as adb not found) to stop script immediately
# with an error.
ps_code = u'$ErrorActionPreference = "Stop"\r\n'
# Add current directory to the PATH var, to match cmd.exe/CreateProcess()
# behavior.
ps_code += u'$env:Path = ".;" + $env:Path\r\n'
# Precede by &, the PowerShell call operator, and separate args by space.
ps_code += u'& ' + u' '.join(argv)
# Make the PowerShell exit code the exit code of the subprocess.
ps_code += u'\r\nExit $LastExitCode'
# Encode as UTF-16LE (without Byte-Order-Mark) which Windows natively
# understands.
ps_code = ps_code.encode('utf-16le')
# Encode the PowerShell command as base64 and use the special
# -EncodedCommand option that base64 decodes. Base64 is just plain ASCII,
# so it should have no problem passing through Win32 CreateProcessA()
# (which python erroneously calls instead of CreateProcessW()).
return (['powershell.exe', '-NoProfile', '-NonInteractive',
'-EncodedCommand', base64.b64encode(ps_code)],) + args[1:]
# Call this instead of subprocess.check_output() to work-around issue in Python
# 2's subprocess class on Windows where it doesn't support Unicode.
def _subprocess_check_output(*args, **kwargs):
try:
return subprocess.check_output(*_get_subprocess_args(args), **kwargs)
except subprocess.CalledProcessError as e:
# Show real command line instead of the powershell.exe command line.
raise subprocess.CalledProcessError(e.returncode, args[0],
output=e.output)
# Call this instead of subprocess.Popen(). Like _subprocess_check_output().
def _subprocess_Popen(*args, **kwargs):
return subprocess.Popen(*_get_subprocess_args(args), **kwargs)
def split_lines(s):
"""Splits lines in a way that works even on Windows and old devices.
Windows will see \r\n instead of \n, old devices do the same, old devices
on Windows will see \r\r\n.
"""
# rstrip is used here to workaround a difference between splineslines and
# re.split:
# >>> 'foo\n'.splitlines()
# ['foo']
# >>> re.split(r'\n', 'foo\n')
# ['foo', '']
return re.split(r'[\r\n]+', s.rstrip())
def version(adb_path=None):
"""Get the version of adb (in terms of ADB_SERVER_VERSION)."""
adb_path = adb_path if adb_path is not None else ['adb']
version_output = subprocess.check_output(adb_path + ['version'])
pattern = r'^Android Debug Bridge version 1.0.(\d+)$'
result = re.match(pattern, version_output.splitlines()[0])
if not result:
return 0
return int(result.group(1))
class AndroidDevice(object):
# Delimiter string to indicate the start of the exit code.
_RETURN_CODE_DELIMITER = 'x'
# Follow any shell command with this string to get the exit
# status of a program since this isn't propagated by adb.
#
# The delimiter is needed because `printf 1; echo $?` would print
# "10", and we wouldn't be able to distinguish the exit code.
_RETURN_CODE_PROBE = [';', 'echo', '{0}$?'.format(_RETURN_CODE_DELIMITER)]
# Maximum search distance from the output end to find the delimiter.
# adb on Windows returns \r\n even if adbd returns \n. Some old devices
# seem to actually return \r\r\n.
_RETURN_CODE_SEARCH_LENGTH = len(
'{0}255\r\r\n'.format(_RETURN_CODE_DELIMITER))
def __init__(self, serial, product=None, adb_path='adb'):
self.serial = serial
self.product = product
self.adb_cmd = [adb_path]
if self.serial is not None:
self.adb_cmd.extend(['-s', serial])
if self.product is not None:
self.adb_cmd.extend(['-p', product])
self._linesep = None
self._features = None
@property
def linesep(self):
if self._linesep is None:
self._linesep = subprocess.check_output(self.adb_cmd +
['shell', 'echo'])
return self._linesep
@property
def features(self):
if self._features is None:
try:
self._features = split_lines(self._simple_call(['features']))
except subprocess.CalledProcessError:
self._features = []
return self._features
def has_shell_protocol(self):
return version(self.adb_cmd) >= 35 and 'shell_v2' in self.features
def _make_shell_cmd(self, user_cmd):
command = self.adb_cmd + ['shell'] + user_cmd
if not self.has_shell_protocol():
command += self._RETURN_CODE_PROBE
return command
def _parse_shell_output(self, out):
"""Finds the exit code string from shell output.
Args:
out: Shell output string.
Returns:
An (exit_code, output_string) tuple. The output string is
cleaned of any additional stuff we appended to find the
exit code.
Raises:
RuntimeError: Could not find the exit code in |out|.
"""
search_text = out
if len(search_text) > self._RETURN_CODE_SEARCH_LENGTH:
# We don't want to search over massive amounts of data when we know
# the part we want is right at the end.
search_text = search_text[-self._RETURN_CODE_SEARCH_LENGTH:]
partition = search_text.rpartition(self._RETURN_CODE_DELIMITER)
if partition[1] == '':
raise RuntimeError('Could not find exit status in shell output.')
result = int(partition[2])
# partition[0] won't contain the full text if search_text was
# truncated, pull from the original string instead.
out = out[:-len(partition[1]) - len(partition[2])]
return result, out
def _simple_call(self, cmd):
logging.info(' '.join(self.adb_cmd + cmd))
return _subprocess_check_output(
self.adb_cmd + cmd, stderr=subprocess.STDOUT)
def shell(self, cmd):
"""Calls `adb shell`
Args:
cmd: command to execute as a list of strings.
Returns:
A (stdout, stderr) tuple. Stderr may be combined into stdout
if the device doesn't support separate streams.
Raises:
ShellError: the exit code was non-zero.
"""
exit_code, stdout, stderr = self.shell_nocheck(cmd)
if exit_code != 0:
raise ShellError(cmd, stdout, stderr, exit_code)
return stdout, stderr
def shell_nocheck(self, cmd):
"""Calls `adb shell`
Args:
cmd: command to execute as a list of strings.
Returns:
An (exit_code, stdout, stderr) tuple. Stderr may be combined
into stdout if the device doesn't support separate streams.
"""
cmd = self._make_shell_cmd(cmd)
logging.info(' '.join(cmd))
p = _subprocess_Popen(
cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = p.communicate()
if self.has_shell_protocol():
exit_code = p.returncode
else:
exit_code, stdout = self._parse_shell_output(stdout)
return exit_code, stdout, stderr
def shell_popen(self, cmd, kill_atexit=True, preexec_fn=None,
creationflags=0, **kwargs):
"""Calls `adb shell` and returns a handle to the adb process.
This function provides direct access to the subprocess used to run the
command, without special return code handling. Users that need the
return value must retrieve it themselves.
Args:
cmd: Array of command arguments to execute.
kill_atexit: Whether to kill the process upon exiting.
preexec_fn: Argument forwarded to subprocess.Popen.
creationflags: Argument forwarded to subprocess.Popen.
**kwargs: Arguments forwarded to subprocess.Popen.
Returns:
subprocess.Popen handle to the adb shell instance
"""
command = self.adb_cmd + ['shell'] + cmd
# Make sure a ctrl-c in the parent script doesn't kill gdbserver.
if os.name == 'nt':
creationflags |= subprocess.CREATE_NEW_PROCESS_GROUP
else:
if preexec_fn is None:
preexec_fn = os.setpgrp
elif preexec_fn is not os.setpgrp:
fn = preexec_fn
def _wrapper():
fn()
os.setpgrp()
preexec_fn = _wrapper
p = _subprocess_Popen(command, creationflags=creationflags,
preexec_fn=preexec_fn, **kwargs)
if kill_atexit:
atexit.register(p.kill)
return p
def install(self, filename, replace=False):
cmd = ['install']
if replace:
cmd.append('-r')
cmd.append(filename)
return self._simple_call(cmd)
def push(self, local, remote):
return self._simple_call(['push', local, remote])
def pull(self, remote, local):
return self._simple_call(['pull', remote, local])
def sync(self, directory=None):
cmd = ['sync']
if directory is not None:
cmd.append(directory)
return self._simple_call(cmd)
def tcpip(self, port):
return self._simple_call(['tcpip', port])
def usb(self):
return self._simple_call(['usb'])
def reboot(self):
return self._simple_call(['reboot'])
def remount(self):
return self._simple_call(['remount'])
def root(self):
return self._simple_call(['root'])
def unroot(self):
return self._simple_call(['unroot'])
def connect(self, host):
return self._simple_call(['connect', host])
def disconnect(self, host):
return self._simple_call(['disconnect', host])
def forward(self, local, remote):
return self._simple_call(['forward', local, remote])
def forward_list(self):
return self._simple_call(['forward', '--list'])
def forward_no_rebind(self, local, remote):
return self._simple_call(['forward', '--no-rebind', local, remote])
def forward_remove(self, local):
return self._simple_call(['forward', '--remove', local])
def forward_remove_all(self):
return self._simple_call(['forward', '--remove-all'])
def reverse(self, remote, local):
return self._simple_call(['reverse', remote, local])
def reverse_list(self):
return self._simple_call(['reverse', '--list'])
def reverse_no_rebind(self, local, remote):
return self._simple_call(['reverse', '--no-rebind', local, remote])
def reverse_remove_all(self):
return self._simple_call(['reverse', '--remove-all'])
def reverse_remove(self, remote):
return self._simple_call(['reverse', '--remove', remote])
def wait(self):
return self._simple_call(['wait-for-device'])
def get_props(self):
result = {}
output, _ = self.shell(['getprop'])
output = split_lines(output)
pattern = re.compile(r'^\[([^]]+)\]: \[(.*)\]')
for line in output:
match = pattern.match(line)
if match is None:
# apple2ix NOTE : don't freak out here ...
#raise RuntimeError('invalid getprop line: "{}"'.format(line))
continue
key = match.group(1)
value = match.group(2)
if key in result:
raise RuntimeError('duplicate getprop key: "{}"'.format(key))
result[key] = value
return result
def get_prop(self, prop_name):
output = split_lines(self.shell(['getprop', prop_name])[0])
if len(output) != 1:
raise RuntimeError('Too many lines in getprop output:\n' +
'\n'.join(output))
value = output[0]
if not value.strip():
return None
return value
def set_prop(self, prop_name, value):
self.shell(['setprop', prop_name, value])

View File

@ -1,856 +0,0 @@
#!/bin/sh
#
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This wrapper script is used to launch a native debugging session
# on a given NDK application. The application must be debuggable, i.e.
# its android:debuggable attribute must be set to 'true' in the
# <application> element of its manifest.
#
# See docs/NDK-GDB.TXT for usage description. Essentially, you just
# need to launch ndk-gdb from your application project directory
# after doing ndk-build && ant install && <start-application-on-device>
#
PROGDIR=`dirname $0`
PROGDIR=`cd $PROGDIR && pwd -P`
#set -x
# Check if absolute NDK path contain space
#
case $PROGDIR in
*\ *) echo "ERROR: NDK path cannot contain space"
exit 1
;;
esac
NDK_BUILDTOOLS_PATH=$PROGDIR/build/tools
. $PROGDIR/build/tools/prebuilt-common.sh
. $PROGDIR/build/tools/ndk-common.sh
force_32bit_binaries
# Find if a given shell program is available.
# We need to take care of the fact that the 'which <foo>' command
# may return either an empty string (Linux) or something like
# "no <foo> in ..." (Darwin). Also, we need to redirect stderr
# to /dev/null for Cygwin
#
# $1: program name
# Out: program path, or empty string
# Return: 0 on success, != 0 on error
#
find_program ()
{
local PROG RET
PROG=$(which "$1" 2>/dev/null)
RET=$?
if [ $RET != 0 ]; then
PROG=
fi
echo "$PROG"
return $RET
}
quote_spaces ()
{
echo "$@" | sed -e 's! !\ !g'
}
# If ADB_CMD is not defined, try to find a program named 'adb'
# in our path.
ADB_CMD=${ADB_CMD:-$(find_program adb)}
ADB_FLAGS=${ADB_FLAGS:-}
DEVICE_SERIAL=
JDB_CMD=${JDB_CMD:-$(find_program jdb)}
AWK_CMD=${AWK_CMD:-$(find_program awk)}
DEBUG_PORT=5039
JDB_PORT=65534
UNKNOWN_ABI=$(find_ndk_unknown_archs)
# Delay in seconds between launching the activity and attaching gdbserver on it.
# This is needed because there is no way to know when the activity has really
# started, and sometimes this takes a few seconds.
DELAY=2
PARAMETERS=
OPTION_HELP=no
OPTION_PROJECT=
OPTION_FORCE=no
OPTION_ADB=
OPTION_EXEC=
OPTION_START=no
OPTION_LAUNCH=
OPTION_LAUNCH_LIST=no
OPTION_DELAY=
OPTION_WAIT="-D"
OPTION_PACKAGE_NAME=
check_parameter ()
{
if [ -z "$2" ]; then
echo "ERROR: Missing parameter after option '$1'"
exit 1
fi
}
check_adb_flags ()
{
if [ -n "$ADB_FLAGS" ] ; then
echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
exit 1
fi
}
get_build_var ()
{
if [ -z "$GNUMAKE" ] ; then
GNUMAKE=make
fi
$GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 | tail -1
}
get_build_var_for_abi ()
{
if [ -z "$GNUMAKE" ] ; then
GNUMAKE=make
fi
$GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2 | tail -1
}
# Used to run an awk script on the manifest
run_awk_manifest_script ()
{
$AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
}
if [ "$HOST_OS" = "cygwin" ] ; then
# Return native path representation from cygwin one
# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
# Return: path in host windows representation, e.g. C:/some/thing
#
# We use mixed mode (i.e. / as the directory separator) because
# all the tools we use recognize it properly, and it avoids lots
# of escaping nonsense associated with "\"
#
native_path ()
{
cygpath -m $1
}
else # HOST_OS != windows
native_path ()
{
echo "$1"
}
fi # HOST_OS != windows
# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
# might fail, e.g. when invoked with:
#
# cd $NDKROOT
# ./ndk-gdb --project=/path/to/project
#
path_is_absolute ()
{
local P P2
P=$1 # copy path
P2=${P#/} # remove / prefix, if any
[ "$P" != "$P2" ]
}
if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
fi
VERBOSE=no
while [ -n "$1" ]; do
opt="$1"
optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
case "$opt" in
--help|-h|-\?)
OPTION_HELP=yes
;;
--verbose)
VERBOSE=yes
;;
-s)
check_parameter $1 $2
check_adb_flags
ADB_FLAGS=" -s"
DEVICE_SERIAL=$2
shift
;;
-s*)
check_adb_flags
optarg=`expr -- "$opt" : '-s\(.*\)'`
ADB_FLAGS=" -s"
DEVICE_SERIAL=$optarg
;;
-p)
check_parameter $1 $2
OPTION_PROJECT="$2"
shift
;;
-p*)
optarg=`expr -- "$opt" : '-p\(.*\)'`
OPTION_PROJECT="$optarg"
;;
--exec=*)
OPTION_EXEC="$optarg"
;;
-x)
check_parameter $1 $2
OPTION_EXEC="$2"
shift
;;
-x*)
optarg=`expr -- "$opt" : '-x\(.*\)'`
OPTION_EXEC="$optarg"
;;
-e)
check_adb_flags
ADB_FLAGS=" -e"
;;
-d)
check_adb_flags
ADB_FLAGS=" -d"
;;
--adb=*) # specify ADB command
OPTION_ADB="$optarg"
;;
--awk=*)
AWK_CMD="$optarg"
;;
--project=*)
OPTION_PROJECT="$optarg"
;;
--port=*)
DEBUG_PORT="$optarg"
;;
--force)
OPTION_FORCE="yes"
;;
--launch-list)
OPTION_LAUNCH_LIST="yes"
;;
--launch=*)
OPTION_LAUNCH="$optarg"
;;
--start)
OPTION_START=yes
;;
--delay=*)
OPTION_DELAY="$optarg"
;;
--nowait)
JDB_PORT=
OPTION_WAIT=
;;
--package=*)
OPTION_PACKAGE_NAME="$optarg"
;;
-*) # unknown options
echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
exit 1
;;
*) # Simply record parameter
if [ -z "$PARAMETERS" ] ; then
PARAMETERS="$opt"
else
PARAMETERS="$PARAMETERS $opt"
fi
;;
esac
shift
done
if [ -z "$JDB_CMD" ] && [ -n "$OPTION_WAIT" ]; then
echo "ERROR: 'jdb' not found; you must either install the JDK, or specify --nowait"
exit 1
fi
if [ -n "$JDB_PORT" ] && [ "$JDB_PORT" = "$DEBUG_PORT" ]; then
echo "ERROR: --port specified cannot be $JDB_PORT without --nowait"
exit 1
fi
if [ "$OPTION_HELP" = "yes" ] ; then
echo "Usage: $PROGNAME [options]"
echo ""
echo "Setup a gdb debugging session for your Android NDK application."
echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
echo ""
echo "Valid options:"
echo ""
echo " --help|-h|-? Print this help"
echo " --verbose Enable verbose mode"
echo " --force Kill existing debug session if it exists"
echo " --nowait Don't have application wait for debugger to attach"
echo " (This might cause you to miss some early JNI breakpoints)"
echo " --start Launch application instead of attaching to existing one"
echo " --launch=<name> Same as --start, but specify activity name (see below)"
echo " --launch-list List all launchable activity names from manifest"
echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
echo " --project=<path> Specify application project path"
echo " -p <path> Same as --project=<path>"
echo " --package=<name> Specify package name"
echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
echo " -x <file> Same as --exec=<file>"
echo " --adb=<file> Use specific adb command [$ADB_CMD]"
echo " --awk=<file> Use specific awk command [$AWK_CMD]"
echo " -e Connect to single emulator instance"
echo " -d Connect to single target device"
echo " -s <serial> Connect to specific emulator or device"
echo ""
exit 0
fi
log "Android NDK installation path: $ANDROID_NDK_ROOT"
if [ -n "$OPTION_EXEC" ] ; then
if [ ! -f "$OPTION_EXEC" ]; then
echo "ERROR: Invalid initialization file: $OPTION_EXEC"
exit 1
fi
fi
if [ -n "$OPTION_DELAY" ] ; then
DELAY="$OPTION_DELAY"
fi
# Check ADB tool version
if [ -n "$OPTION_ADB" ] ; then
ADB_CMD=$OPTION_ADB
log "Using specific adb command: $ADB_CMD"
else
if [ -z "$ADB_CMD" ] ; then
echo "ERROR: The 'adb' tool is not in your path."
echo " You can change your PATH variable, or use"
echo " --adb=<executable> to point to a valid one."
exit 1
fi
log "Using default adb command: $ADB_CMD"
fi
ADB_CMD=$(quote_spaces $ADB_CMD)
ADB_VERSION=$("$ADB_CMD" version 2>/dev/null)
if [ $? != 0 ] ; then
echo "ERROR: Could not run ADB with: $ADB_CMD"
exit 1
fi
log "ADB version found: $ADB_VERSION"
if [ "x$DEVICE_SERIAL" = "x" ]; then
log "Using ADB flags: $ADB_FLAGS"
else
log "Using ADB flags: $ADB_FLAGS" \"$DEVICE_SERIAL\"
fi
JDB_CMD=$(quote_spaces $JDB_CMD)
log "Using JDB command: $JDB_CMD"
# Run an ADB command with the right ADB flags
# $1+: adb command parameter
adb_cmd ()
{
if [ "x$DEVICE_SERIAL" = "x" ]; then
"$ADB_CMD" $ADB_FLAGS "$@"
else
# NOTE: We escape $ADB_CMD and $DEVICE_SERIAL in case they contains spaces.
"$ADB_CMD" $ADB_FLAGS "$DEVICE_SERIAL" "$@"
fi
}
# Used internally by adb_var_shell and adb_var_shell2.
# $1: 1 to redirect stderr to $1, 0 otherwise.
# $2: Variable name that will contain the result
# $3+: Command options
_adb_var_shell ()
{
# We need a temporary file to store the output of our command
local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
REDIRECT_STDERR=$1
VARNAME=$2
shift; shift;
CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
# Run the command, while storing the standard output to CMD_OUT
# and appending the exit code as the last line.
if [ "$REDIRECT_STDERR" != 0 ]; then
adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
else
adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
fi
# Get last line in log, which contains the exit code from the command
RET=`sed -e '$!d' $CMD_OUT`
# Get output, which corresponds to everything except the last line
OUT=`sed -e '$d' $CMD_OUT`
rm -f $CMD_OUT
eval $VARNAME=\"\$OUT\"
return $RET
}
# Run a command through 'adb shell' and captures its standard output
# into a variable. The function's exit code is the same than the command's.
#
# This is required because there is a bug where "adb shell" always returns
# 0 on the host, even if the command fails on the device.
#
# $1: Variable name (e.g. FOO)
# On exit, $FOO is set to the command's standard output
#
# The return status will be 0 (success) if the command succeeded
# or 1 (failure) otherwise.
adb_var_shell ()
{
_adb_var_shell 0 "$@"
}
# A variant of adb_var_shell that stores both stdout and stderr in the output
# $1: Variable name
adb_var_shell2 ()
{
_adb_var_shell 1 "$@"
}
# Return the PID of a given package or program, or 0 if it doesn't run
# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
# Out: PID number, or 0 if not running
get_pid_of ()
{
adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
}
# Check the awk tool
AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
if [ $? != 0 ] ; then
echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
exit 1
fi
if [ "$AWK_TEST" != "Pass" ] ; then
echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
exit 1
fi
# Name of the manifest file
MANIFEST=AndroidManifest.xml
# Find the root of the application project.
if [ -n "$OPTION_PROJECT" ] ; then
PROJECT=$OPTION_PROJECT
log "Using specified project path: $PROJECT"
if [ ! -d "$PROJECT" ] ; then
echo "ERROR: Your --project option does not point to a directory!"
exit 1
fi
if [ ! -f "$PROJECT/$MANIFEST" ] ; then
echo "ERROR: Your --project does not point to an Android project path!"
echo " It is missing a $MANIFEST file."
exit 1
fi
else
# Assume we are in the project directory
if [ -f "$MANIFEST" ] ; then
PROJECT=.
else
PROJECT=
CURDIR=`pwd`
while [ "$CURDIR" != "/" ] ; do
if [ -f "$CURDIR/$MANIFEST" ] ; then
PROJECT="$CURDIR"
break
fi
CURDIR=`dirname $CURDIR`
done
if [ -z "$PROJECT" ] ; then
echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
exit 1
fi
fi
log "Using auto-detected project path: $PROJECT"
fi
if [ ! -z "$OPTION_PACKAGE_NAME" ]; then
PACKAGE_NAME="$OPTION_PACKAGE_NAME"
log "Using package name: $PACKAGE_NAME"
else
# Extract the package name from the manifest
PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
echo " Please check that the file is well-formed!"
exit 1
fi
log "Found package name: $PACKAGE_NAME"
fi
# If --launch-list is used, list all launchable activities, and be done with it
if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
log "Extracting list of launchable activities from manifest:"
run_awk_manifest_script extract-launchable.awk
exit 0
fi
APP_ABIS=`get_build_var APP_ABI`
if [ "$APP_ABIS" != "${APP_ABIS%%all*}" ] ; then
# replace first "all" with all available ABIs
ALL_ABIS=`get_build_var NDK_ALL_ABIS`
APP_ABIS_FRONT="${APP_ABIS%%all*}"
APP_ABIS_BACK="${APP_ABIS#*all}"
APP_ABIS="${APP_ABIS_FRONT}${ALL_ABIS}${APP_ABIS_BACK}"
fi
# replace "armeabi-v7a-hard" with "armeabi-v7a"
APP_ABIS=`echo $APP_ABIS | sed -e 's/armeabi-v7a-hard/armeabi-v7a/g'`
log "ABIs targetted by application: $APP_ABIS"
# Check the ADB command, and that we can connect to the device/emulator
ADB_TEST=`adb_cmd shell ls`
if [ $? != 0 ] ; then
echo "ERROR: Could not connect to device or emulator!"
echo " Please check that an emulator is running or a device is connected"
echo " through USB to this machine. You can use -e, -d and -s <serial>"
echo " in case of multiple ones."
exit 1
fi
# Check that the device is running Froyo (API Level 8) or higher
#
adb_var_shell API_LEVEL getprop ro.build.version.sdk
if [ $? != 0 -o -z "$API_LEVEL" ] ; then
echo "ERROR: Could not find target device's supported API level!"
echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
exit 1
fi
log "Device API Level: $API_LEVEL"
if [ "$API_LEVEL" -lt "8" ] ; then
echo "ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher."
echo "The target device is running API level $API_LEVEL!"
exit 1
fi
# Get the target device's supported ABI(s)
# And check that they are supported by the application
#
COMPAT_ABI=none
# All modern Android images must support ro.product.cpu.abilist32
# and ro.product.cpu.abilist64. Otherwise fall back to obsolete
# ro.product.cpu.abi and ro.product.cpu.abi2
adb_var_shell CPU_ABILIST64 getprop ro.product.cpu.abilist64
adb_var_shell CPU_ABILIST32 getprop ro.product.cpu.abilist32
CPU_ABIS="$CPU_ABILIST64,$CPU_ABILIST32"
if [ -z "$CPU_ABILIST64" ] && [ -z "$CPU_ABILIST32" ] ; then
adb_var_shell CPU_ABI1 getprop ro.product.cpu.abi
adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
CPU_ABIS="$CPU_ABI1,$CPU_ABI2"
fi
# Replace all ',' with space and add trailing space to
# ease whole-word matching of APP_ABI
CPU_ABILIST64=$(echo $CPU_ABILIST64 | tr ',' ' ')
CPU_ABILIST32=$(echo $CPU_ABILIST32 | tr ',' ' ')
CPU_ABIS=$(echo $CPU_ABIS | tr ',' ' ')
log "Device CPU ABIs: $CPU_ABIS"
APP_ABIS=$APP_ABIS" "
adb_var_shell BCFILES run-as $PACKAGE_NAME /system/bin/sh -c "ls lib/*.bc"
####if [ $? = 0 ]; then
#### COMPAT_ABI="$UNKNOWN_ABI"
####else
# Assume that compatible ABI is 32-bit
COMPAT_ABI_BITS=32
# First look compatible ABI in the list of 64-bit ABIs
if [ -n "$CPU_ABILIST64" ] ; then
for CPU_ABI64 in $CPU_ABILIST64; do
if [ "$APP_ABIS" != "${APP_ABIS%$CPU_ABI64 *}" ] ; then
COMPAT_ABI=$CPU_ABI64
COMPAT_ABI_BITS=64
break
fi
done
fi
# If we found nothing - look among 32-bit ABIs
if [ "$COMPAT_ABI" = none ] && [ -n "$CPU_ABILIST32" ] ; then
for CPU_ABI32 in $CPU_ABILIST32; do
if [ "$APP_ABIS" != "${APP_ABIS%$CPU_ABI32 *}" ] ; then
COMPAT_ABI=$CPU_ABI32
break
fi
done
fi
# Lastly, lets check ro.product.cpu.abi and ro.product.cpu.abi2
if [ "$COMPAT_ABI" = none ] && [ -z "$CPU_ABILIST64" ] && [ -z "$CPU_ABILIST32" ]; then
for CPU_ABI in $CPU_ABIS; do
if [ "$APP_ABIS" != "${APP_ABIS%$CPU_ABI *}" ] ; then
COMPAT_ABI=$CPU_ABI
break
fi
done
fi
####fi
if [ "$COMPAT_ABI" = none ] ; then
COMPAT_ABI='armeabi'
fi
log "Compatible device ABI: $COMPAT_ABI"
# Get information from the build system
GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
log "Using gdb setup init: $GDBSETUP_INIT"
# Find the prefix for gdb-client
if [ "$COMPAT_ABI" != "$UNKNOWN_ABI" ]; then
TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
else
TOOLCHAIN_ABI=$(echo $CPU_ABIS | awk '{print $NF}')
TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $TOOLCHAIN_ABI`
fi
log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
log "Using app out directory: $APP_OUT"
# Check that the application is debuggable, or nothing will work
####DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
####RET=$?
####log "Found debuggable flag: $DEBUGGABLE"
####if [ "$RET" != 0 -o "$DEBUGGABLE" != "true" ] ; then
#### # If gdb.setup exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
#### # ok to not have android:debuggable set to true in the original manifest.
#### # However, if this is not the case, then complain!!
#### if [ -f $PROJECT/libs/$COMPAT_ABI/gdb.setup ] ; then
#### log "Found gdb.setup under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
#### else
#### echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
#### echo ""
#### echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
#### echo ""
#### echo " - Modify your manifest to set android:debuggable attribute to \"true\","
#### echo " then rebuild normally."
#### echo ""
#### echo "After one of these, re-install to the device!"
#### exit 1
#### fi
####else
# DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
# debuggable flag in the manifest without calling ndk-build afterwards.
if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdb.setup ] ; then
echo "ERROR: Could not find gdb.setup under $PROJECT/libs/$COMPAT_ABI"
echo " This usually means you modified your AndroidManifest.xml to set"
echo " the android:debuggable flag to 'true' but did not rebuild the"
echo " native binaries. Please call 'ndk-build' to do so,"
echo " *then* re-install to the device!"
exit 1
fi
####fi
# Find the <dataDir> of the package on the device
adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
if [ $? != 0 -o -z "$DATA_DIR" ] ; then
echo "ERROR: Could not extract package's data directory. Are you sure that"
echo " your installed application is debuggable?"
exit 1
fi
log "Found data directory: '$DATA_DIR'"
# Let's check that 'gdbserver' is properly installed on the device too. If 'gdbserver'
# is not there, push 'gdbserver' found in prebuilt.
#
DEVICE_GDBSERVER=$DATA_DIR/lib/gdbserver
adb_var_shell2 GDBSERVER_RESULT run-as $PACKAGE_NAME ls $DEVICE_GDBSERVER
if [ $? != 0 ]; then
# Figure out what's the target-arch and find gdbserver in prebuilt.
TARGET_ARCH=none
for ANDROID_ARCH in $ANDROID_NDK_ROOT/prebuilt/android-*; do
ANDROID_ARCH=${ANDROID_ARCH#$ANDROID_NDK_ROOT/prebuilt/android-}
if [ "$COMPAT_ABI" = "$ANDROID_ARCH" ]; then
TARGET_ARCH=$ANDROID_ARCH
break;
fi
done
if [ $TARGET_ARCH != "none" ]; then
DEVICE_GDBSERVER=/data/local/tmp/gdbserver
adb shell mkdir -p /data/local/tmp
adb push ${ANDROID_NDK_ROOT}/prebuilt/android-${TARGET_ARCH}/gdbserver/gdbserver \
$DEVICE_GDBSERVER
log "Push gdbserver in device"
else
echo "ERROR: Non-debuggable application installed on the target device."
echo " Please re-install the debuggable version!"
exit 1
fi
fi
log "Found device gdbserver: $DEVICE_GDBSERVER"
# Launch the activity if needed
if [ "$OPTION_START" = "yes" ] ; then
# If --launch is used, ignore --start, otherwise extract the first
# launchable activity name from the manifest and use it as if --launch=<name>
# was used instead.
#
if [ -z "$OPTION_LAUNCH" ] ; then
OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
if [ $? != 0 ] ; then
echo "ERROR: Could not extract name of launchable activity from manifest!"
echo " Try to use --launch=<name> directly instead as a work-around."
exit 1
fi
log "Found first launchable activity: $OPTION_LAUNCH"
if [ -z "$OPTION_LAUNCH" ] ; then
echo "ERROR: It seems that your Application does not have any launchable activity!"
echo " Please fix your manifest file and rebuild/re-install your application."
exit 1
fi
fi
fi
if [ -n "$OPTION_LAUNCH" ] ; then
log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
adb_var_shell2 DUMMY am start $OPTION_WAIT -n $PACKAGE_NAME/$OPTION_LAUNCH
if [ $? != 0 ] ; then
echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
echo " Use --launch-list to dump a list of valid values."
exit 1
fi
# Sleep a bit, it sometimes take one second to start properly
# Note that we use the 'sleep' command on the device here.
run adb_cmd shell sleep $DELAY
fi
# Find the PID of the application being run
PID=$(get_pid_of "$PACKAGE_NAME")
RET=$?
log "Found running PID: $PID"
if [ "$RET" != 0 -o "$PID" = "0" ] ; then
echo "ERROR: Could not extract PID of application on device/emulator."
if [ -n "$OPTION_LAUNCH" ] ; then
echo " Weird, this probably means one of these:"
echo ""
echo " - The installed package does not match your current manifest."
echo " - The application process was terminated."
echo ""
echo " Try using the --verbose option and look at its output for details."
else
echo " Are you sure the application is already started?"
echo " Consider using --start or --launch=<name> if not."
fi
exit 1
fi
# Check that there is no other instance of gdbserver running
GDBSERVER_PID=$(get_pid_of lib/gdbserver)
if [ "$GDBSERVER_PID" != "0" ]; then
if [ "$OPTION_FORCE" = "no" ] ; then
echo "ERROR: Another debug session running, Use --force to kill it."
exit 1
fi
log "Killing existing debugging session"
run adb_cmd shell kill -9 $GDBSERVER_PID
fi
# Launch gdbserver now
DEBUG_SOCKET=debug-socket
adb_var_shell2 DUMMY run-as $PACKAGE_NAME $DEVICE_GDBSERVER +$DEBUG_SOCKET --attach $PID &
if [ $? != 0 ] ; then
echo "ERROR: Could not launch gdbserver on the device?"
exit 1
fi
log "Launched gdbserver succesfully."
# Setup network redirection
log "Setup network redirection"
run adb_cmd forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
if [ $? != 0 ] ; then
echo "ERROR: Could not setup network redirection to gdbserver?"
echo " Maybe using --port=<port> to use a different TCP port might help?"
exit 1
fi
# If we are debugging 64-bit app, then we need to pull linker64,
# app_process64 and libc.so from lib64 directory
LINKER_NAME=linker
LIBDIR_NAME=lib
APP_PROCESS_NAME=app_process32
if [ "$COMPAT_ABI_BITS" = 64 ] ; then
LINKER_NAME=linker64
LIBDIR_NAME=lib64
APP_PROCESS_NAME=app_process64
else
# Old 32-bit devices do not have app_process32. Pull
# app_process in this case
adb_var_shell2 DUMMY test -e /system/bin/$APP_PROCESS_NAME
if [ $? != 0 ] ; then
APP_PROCESS_NAME=app_process
fi
fi
# Get the app_server binary from the device
APP_PROCESS=$APP_OUT/app_process
run adb_cmd pull /system/bin/$APP_PROCESS_NAME `native_path $APP_PROCESS`
log "Pulled $APP_PROCESS_NAME from device/emulator."
run adb_cmd pull /system/bin/$LINKER_NAME `native_path $APP_OUT/$LINKER_NAME`
log "Pulled $LINKER_NAME from device/emulator."
run adb_cmd pull /system/$LIBDIR_NAME/libc.so `native_path $APP_OUT/libc.so`
log "Pulled /system/$LIBDIR_NAME/libc.so from device/emulator."
# Setup JDB connection, for --start or --launch
if [ "$OPTION_START" = "yes" ] || [ -n "$OPTION_LAUNCH" ] ; then
if [ -n "$JDB_PORT" ]; then
log "Setup JDB connection"
run adb_cmd forward tcp:$JDB_PORT jdwp:$PID
sleep 1
$JDB_CMD -connect com.sun.jdi.SocketAttach:hostname=localhost,port=$JDB_PORT &
sleep 1
fi
fi
# If we are debugging UNKNOWN_ABI, download compiled *.so from device.
#
if [ "$COMPAT_ABI" = "$UNKNOWN_ABI" ]; then
for bc in $BCFILES; do
log "Pulled $(basename $bc .bc).so from device/emulator."
adb pull $DATA_DIR/lib/$(basename $bc .bc).so $PROJECT/obj/local/$UNKNOWN_ABI/
done
fi
# Now launch the appropriate gdb client with the right init commands
#
GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
GDBSETUP=$APP_OUT/gdb.setup
cp -f $GDBSETUP_INIT $GDBSETUP
#uncomment the following to debug the remote connection only
#echo "set debug remote 1" >> $GDBSETUP
echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
echo "target remote :$DEBUG_PORT" >> $GDBSETUP
if [ -n "$OPTION_EXEC" ] ; then
cat $OPTION_EXEC >> $GDBSETUP
fi
$GDBCLIENT -x `native_path $GDBSETUP`
set +x