https://gist.github.com/TomTasche/9690186

ndk-gdb of NDK r9d modified to *always* debug the ":remote"-process of your app
 
 
#!/bin/sh
#
# Copyright (C) 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 $`
PROGDIR=`cd $PROGDIR && pwd -P` # Check if absolute NDK path contain space
#
case $PROGDIR in
*\ *) echo "ERROR: NDK path cannot contain space"
exit
;;
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
#
# $: program name
# Out: program path, or empty string
# Return: on success, != on error
#
find_program ()
{
local PROG RET
PROG=$(which "$1" >/dev/null)
RET=$?
if [ $RET != ]; 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=
JDB_PORT= 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= 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" check_parameter ()
{
if [ -z "$2" ]; then
echo "ERROR: Missing parameter after option '$1'"
exit
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
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_$ | tail -
} 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_$ APP_ABI=$ | tail -
} # Used to run an awk script on the manifest
run_awk_manifest_script ()
{
$AWK_CMD -f $AWK_SCRIPTS/$ $PROJECT/$MANIFEST
} if [ "$HOST_OS" = "cygwin" ] ; then
# Return native path representation from cygwin one
# $: 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 $
}
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=$ # 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 $ $
check_adb_flags
ADB_FLAGS=" -s"
DEVICE_SERIAL=$
shift
;;
-s*)
check_adb_flags
optarg=`expr -- "$opt" : '-s\(.*\)'`
ADB_FLAGS=" -s"
DEVICE_SERIAL=$optarg
;;
-p)
check_parameter $ $
OPTION_PROJECT="$2"
shift
;;
-p*)
optarg=`expr -- "$opt" : '-p\(.*\)'`
OPTION_PROJECT="$optarg"
;;
--exec=*)
OPTION_EXEC="$optarg"
;;
-x)
check_parameter $ $
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=
;;
-*) # unknown options
echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
exit
;;
*) # 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
fi
if [ -n "$JDB_PORT" ] && [ "$JDB_PORT" = "$DEBUG_PORT" ]; then
echo "ERROR: --port specified cannot be $JDB_PORT without --nowait"
exit
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 " --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
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
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
fi
log "Using default adb command: $ADB_CMD"
fi ADB_CMD=$(quote_spaces $ADB_CMD)
ADB_VERSION=$("$ADB_CMD" version >/dev/null)
if [ $? != ] ; then
echo "ERROR: Could not run ADB with: $ADB_CMD"
exit
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
# $+: 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.
# $: to redirect stderr to $, otherwise.
# $: Variable name that will contain the result
# $+: 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=$
VARNAME=$
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" != ]; then
adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT >&
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
# on the host, even if the command fails on the device.
#
# $: Variable name (e.g. FOO)
# On exit, $FOO is set to the command's standard output
#
# The return status will be (success) if the command succeeded
# or (failure) otherwise.
adb_var_shell ()
{
_adb_var_shell "$@"
} # A variant of adb_var_shell that stores both stdout and stderr in the output
# $: Variable name
adb_var_shell2 ()
{
_adb_var_shell "$@"
} # Return the PID of a given package or program, or if it doesn't run
# $: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
# Out: PID number, or if not running
get_pid_of ()
{
adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1:remote"
} # Check the awk tool
AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
if [ $? != ] ; then
echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
exit
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
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
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
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
fi
fi
log "Using auto-detected project path: $PROJECT"
fi # Extract the package name from the manifest
PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
log "Found package name: $PACKAGE_NAME"
if [ $? != -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
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
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 [ $? != ] ; 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
fi # Check that the device is running Froyo (API Level ) or higher
#
adb_var_shell API_LEVEL getprop ro.build.version.sdk
if [ $? != -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
fi
log "Device API Level: $API_LEVEL"
if [ "$API_LEVEL" -lt "" ] ; 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
fi # Get the target device's supported ABI(s)
# And check that they are supported by the application
#
COMPAT_ABI=none
adb_var_shell CPU_ABI1 getprop ro.product.cpu.abi
adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2 # Both CPU_ABI1 and CPU_ABI2 may contain multiple comma-delimited abis.
# Concatanate CPU_ABI1 and CPU_ABI2 and replace all ',' with space.
# Add trailing space to ease whole-word matching of APP_ABI.
CPU_ABIS="$CPU_ABI1,$CPU_ABI2"
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 [ $? == ]; then
COMPAT_ABI="$UNKNOWN_ABI"
else
for CPU_ABI in $CPU_ABIS; do
if [ "$APP_ABIS" != "${APP_ABIS%$CPU_ABI *}" ] ; then
COMPAT_ABI=$CPU_ABI
break
fi
done
fi
if [ "$COMPAT_ABI" = none ] ; then
echo "ERROR: The device does not support the application's targetted CPU ABIs!"
echo " Device supports: $CPU_ABIS"
echo " Package supports: $APP_ABIS"
exit
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`
log "Found debuggable flag: $DEBUGGABLE"
if [ $? != -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
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
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 [ $? != -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
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 ls $DEVICE_GDBSERVER
if [ $? != ]; 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-}
if [ x"$CPU_ABIS" != x"${CPU_ABIS#$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
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 [ $? != ] ; 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
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
fi
fi
fi if [ -n "$OPTION_LAUNCH" ] ; then
log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
run adb_cmd shell am start $OPTION_WAIT -n $PACKAGE_NAME/$OPTION_LAUNCH
if [ $? != ] ; then
echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
echo " Use --launch-list to dump a list of valid values."
exit
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")
log "Found running PID: $PID"
if [ $? != -o "$PID" = "" ] ; 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
fi # Check that there is no other instance of gdbserver running
GDBSERVER_PID=$(get_pid_of lib/gdbserver)
if [ "$GDBSERVER_PID" != "" ]; then
if [ "$OPTION_FORCE" = "no" ] ; then
echo "ERROR: Another debug session running, Use --force to kill it."
exit
fi
log "Killing existing debugging session"
run adb_cmd shell kill - $GDBSERVER_PID
fi # Launch gdbserver now
DEBUG_SOCKET=debug-socket
run adb_cmd shell run-as $PACKAGE_NAME $DEVICE_GDBSERVER +$DEBUG_SOCKET --attach $PID &
if [ $? != ] ; then
echo "ERROR: Could not launch gdbserver on the device?"
exit
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 [ $? != ] ; then
echo "ERROR: Could not setup network redirection to gdbserver?"
echo " Maybe using --port=<port> to use a different TCP port might help?"
exit
fi # Get the app_server binary from the device
APP_PROCESS=$APP_OUT/app_process
run adb_cmd pull /system/bin/app_process `native_path $APP_PROCESS`
log "Pulled app_process from device/emulator." run adb_cmd pull /system/bin/linker `native_path $APP_OUT/linker`
log "Pulled linker from device/emulator." run adb_cmd pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
log "Pulled 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
$JDB_CMD -connect com.sun.jdi.SocketAttach:hostname=localhost,port=$JDB_PORT &
sleep
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`

ndk-gdb of NDK r9d modified to *always* debug the ":remote"-process of your app的更多相关文章

  1. NDK学习二: NDK目录结构

    NDK目录结构   NDK下载好之后目录结构如下:         目录名 描述 build   存放和编译相关的脚本文件,最外面的ndk-build就是调用该目录下的makefile文件,其中mak ...

  2. ndk学习6: 使用gdb调试ndk程序一

    生成debug版程序 方法一: 使用ndk-build编译时,加上如下参数NDK_DEBUG=1,之后生成so文件之外,还会生成gdbobserver,gdb.setup调式文件   方法二: 修改A ...

  3. NDK GDB 中打印vector , vector<vector <> >

    在android上进行native开发的时候,我们需要用NDK-GDB 对native code进行调试,其中很麻烦的是,我使用的NDK版本是4.0,该版本还不支持用NDK-GDB直接打印vector ...

  4. NDK(17)让ndk支持完整C++,exception,rtti,

    C++ Support The Android platform provides a very minimal C++ runtime support library (/system/lib/li ...

  5. android studio ndk配置和ndk开发

    配置开发环境: 1:下载ndk,导入android studio中. 2:在项目中引入NDK   3:在计算机path变量中导入NDK路径,在编译.h文件的时候会用到. 一:建立java的native ...

  6. ndk学习5: ndk中使用c++

    默认情况下ndk不支持标准C++库,异常, rtti等   在ndk文档有关于C++ support的详细介绍   一. 使用C++标准库 介绍: 默认是使用最小额度的C++运行时库, 在Applic ...

  7. NDK(1)配置ndk,含eclipse,Android Studio1.5.1

    现在的ndk配置已经非常简单,如果看到要cygwin的请关闭. 1,Eclipse 添加ndk linux,mac ,windows 相似, a.下载 ndk并解压, b.在eclipse的andro ...

  8. Android JNI和NDK学习(04)--NDK调试方法(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3092812.html 本文主要介绍在ndk中添加log的方法.然后,我们就可 ...

  9. NDK开发和NDK双进程守护

    https://www.jianshu.com/p/433b2c93c6a7 NDK进程守护 https://blog.csdn.net/k393393/article/details/7895435 ...

随机推荐

  1. 【Bean】 这才是bean,一直没仔细看

    EJB是Enterprise Java Bean的缩写,一个Bean扮演着应用程序素材的角色.它包含有一个functional interface,一个life-cycle interface,以及一 ...

  2. 一步步学习ASP.NET MVC3 (8)——EmptyResult,ContentResult,RedirectResult

    请注明转载地址:http://www.cnblogs.com/arhat 上一章,我们阐述了Controller,Action和ActionResult所代表的含义及使用,本章继续研究ActionRe ...

  3. iOS新上线注意事项

    上传不出现构建版本 现在苹果要求先上传版本,然后在提交审核,但是现在经常上传完应用后,不出现构建版本,等待很久很久,也不出现,那么怎么解决,我告诉你~~尼玛的苹果是自己数据丢包了,结果就造成你不出现构 ...

  4. Android 虚拟机Dalvik、Android各种java包功能、Android相关文件类型、应用程序结构分析、ADB

    Android虚拟机Dalvik Dalvik冲击 随着Google 的AndroidSDK 的发布,关于它的API 以及在移动电话领域所带来的预期影响这些方面的讨论不胜枚举.不过,其中的一个话题在J ...

  5. gcc和g++的区别

    参考What is the difference between g++ and gcc? 1.The actual compiler is "cc1" for C and &qu ...

  6. 实用make最佳实践

    http://www.cnblogs.com/ggjucheng/archive/2011/12/14/2288055.html 一.前言 Make工具最主要也是最基本的功能就是通过makefile文 ...

  7. Design Tutorial: Inverse the Problem

    Codeforces Round #270 D:http://codeforces.com/contest/472/problem/D 题意:给以一张图,用邻接矩阵表示,现在问你这张图能不能够是一棵树 ...

  8. loadrunner_analysis技巧_average 和 90% percent

    “90% Percent Time” 表示90%的事务response time 都维持在某个值附近,不是 average response time * 90%;  “Average Time” 平 ...

  9. QReadWriteLock 读写锁的区别

    QReadWriteLock 这个允许多个进程同时读,但是只有一个写.而且写读不能同时进行. 文档里语焉不详,这是我自己的理解: lockForWrite 为写而锁,就是要修改数据,外人连想进来读数据 ...

  10. 学习笔记-[Maven实战]-第一章:Maven简介

    Maven简介: Maven 可翻译为:知识的积累,也可以翻译为"专家"或"内行". Maven 是一个跨平台的项目管理工具,是Apache组织中一个很成功的开 ...