blob: d307f24c0fd1f29be8dc9ee7b0ee24e8d235500a [file] [log] [blame]
David 'Digit' Turnera08d6052010-04-16 12:45:33 -07001#!/bin/sh
2#
3# Copyright (C) 2010 The Android Open Source Project
4#
5# Licensed under the Apache License, Version 2.0 (the "License");
6# you may not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# http://www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an "AS IS" BASIS,
13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
16#
17
18# This wrapper script is used to launch a native debugging session
19# on a given NDK application. The application must be debuggable, i.e.
20# its android:debuggable attribute must be set to 'true' in the
21# <application> element of its manifest.
22#
23# See docs/NDK-GDB.TXT for usage description. Essentially, you just
24# need to launch ndk-gdb from your application project directory
25# after doing ndk-build && ant install && <start-application-on-device>
26#
Andrew Hsieh2c1bb7c2012-09-20 17:52:00 -070027PROGDIR=`dirname $0`
28PROGDIR=`cd $PROGDIR && pwd`
29
30# Check if absolute NDK path contain space
31#
32case $PROGDIR in
33 *\ *) echo "ERROR: NDK path cannot contain space"
34 exit 1
35 ;;
36esac
37
38. $PROGDIR/build/core/ndk-common.sh
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070039
40force_32bit_binaries
41
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010042# Find if a given shell program is available.
43# We need to take care of the fact that the 'which <foo>' command
44# may return either an empty string (Linux) or something like
45# "no <foo> in ..." (Darwin). Also, we need to redirect stderr
46# to /dev/null for Cygwin
47#
48# $1: program name
49# Out: program path, or empty string
50# Return: 0 on success, != 0 on error
51#
52find_program ()
53{
54 local PROG RET
55 PROG=$(which "$1" 2>/dev/null)
56 RET=$?
57 if [ $RET != 0 ]; then
58 PROG=
59 fi
60 echo "$PROG"
61 return $RET
62}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070063
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010064quote_spaces ()
65{
66 echo "$@" | sed -e 's! !\ !g'
67}
68
69# If ADB_CMD is not defined, try to find a program named 'adb'
70# in our path.
71ADB_CMD=${ADB_CMD:-$(find_program adb)}
72ADB_FLAGS=${ADB_FLAGS:-}
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -070073DEVICE_SERIAL=
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010074
75AWK_CMD=${AWK_CMD:-$(find_program awk)}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070076
77DEBUG_PORT=5039
78
David 'Digit' Turner5b656252010-10-08 00:43:32 +020079# Delay in seconds between launching the activity and attaching gdbserver on it.
80# This is needed because there is no way to know when the activity has really
81# started, and sometimes this takes a few seconds.
82DELAY=2
83
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070084PARAMETERS=
85OPTION_HELP=no
86OPTION_PROJECT=
87OPTION_FORCE=no
88OPTION_ADB=
89OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070090OPTION_START=no
91OPTION_LAUNCH=
92OPTION_LAUNCH_LIST=no
David 'Digit' Turner5b656252010-10-08 00:43:32 +020093OPTION_DELAY=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070094
95check_parameter ()
96{
97 if [ -z "$2" ]; then
98 echo "ERROR: Missing parameter after option '$1'"
99 exit 1
100 fi
101}
102
103check_adb_flags ()
104{
105 if [ -n "$ADB_FLAGS" ] ; then
106 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
107 exit 1
108 fi
109}
110
111get_build_var ()
112{
113 if [ -z "$GNUMAKE" ] ; then
114 GNUMAKE=make
115 fi
David 'Digit' Turner7bbc2112012-08-27 14:23:19 +0200116 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 | tail -1
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700117}
118
119get_build_var_for_abi ()
120{
121 if [ -z "$GNUMAKE" ] ; then
122 GNUMAKE=make
123 fi
David 'Digit' Turner7bbc2112012-08-27 14:23:19 +0200124 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2 | tail -1
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700125}
126
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700127# Used to run an awk script on the manifest
128run_awk_manifest_script ()
129{
130 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
131}
132
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700133if [ "$HOST_OS" = "cygwin" ] ; then
134# Return native path representation from cygwin one
135# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
136# Return: path in host windows representation, e.g. C:/some/thing
137#
138# We use mixed mode (i.e. / as the directory separator) because
139# all the tools we use recognize it properly, and it avoids lots
140# of escaping nonsense associated with "\"
141#
142native_path ()
143{
144 cygpath -m $1
145}
146else # HOST_OS != windows
147native_path ()
148{
149 echo "$1"
150}
151fi # HOST_OS != windows
152
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100153# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
154# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
155# might fail, e.g. when invoked with:
156#
157# cd $NDKROOT
158# ./ndk-gdb --project=/path/to/project
159#
160path_is_absolute ()
161{
162 local P P2
163 P=$1 # copy path
164 P2=${P#/} # remove / prefix, if any
165 [ "$P" != "$P2" ]
166}
167
168if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
169 ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
170fi
171
172
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700173VERBOSE=no
174while [ -n "$1" ]; do
175 opt="$1"
176 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
177 case "$opt" in
178 --help|-h|-\?)
179 OPTION_HELP=yes
180 ;;
181 --verbose)
182 VERBOSE=yes
183 ;;
184 -s)
185 check_parameter $1 $2
186 check_adb_flags
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700187 ADB_FLAGS=" -s"
188 DEVICE_SERIAL=$2
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700189 shift
190 ;;
191 -s*)
192 check_adb_flags
193 optarg=`expr -- "$opt" : '-s\(.*\)'`
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700194 ADB_FLAGS=" -s"
195 DEVICE_SERIAL=$optarg
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700196 ;;
197 -p)
198 check_parameter $1 $2
199 OPTION_PROJECT="$2"
200 shift
201 ;;
202 -p*)
203 optarg=`expr -- "$opt" : '-p\(.*\)'`
204 OPTION_PROJECT="$optarg"
205 ;;
206 --exec=*)
207 OPTION_EXEC="$optarg"
208 ;;
209 -x)
210 check_parameter $1 $2
211 OPTION_EXEC="$2"
212 shift
213 ;;
214 -x*)
215 optarg=`expr -- "$opt" : '-x\(.*\)'`
216 OPTION_EXEC="$optarg"
217 ;;
218 -e)
219 check_adb_flags
220 ADB_FLAGS=" -e"
221 ;;
222 -d)
223 check_adb_flags
224 ADB_FLAGS=" -d"
225 ;;
226 --adb=*) # specify ADB command
227 OPTION_ADB="$optarg"
228 ;;
229 --awk=*)
230 AWK_CMD="$optarg"
231 ;;
232 --project=*)
233 OPTION_PROJECT="$optarg"
234 ;;
235 --port=*)
236 DEBUG_PORT="$optarg"
237 ;;
238 --force)
239 OPTION_FORCE="yes"
240 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700241 --launch-list)
242 OPTION_LAUNCH_LIST="yes"
243 ;;
244 --launch=*)
245 OPTION_LAUNCH="$optarg"
246 ;;
247 --start)
248 OPTION_START=yes
249 ;;
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200250 --delay=*)
251 OPTION_DELAY="$optarg"
252 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700253 -*) # unknown options
254 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
255 exit 1
256 ;;
257 *) # Simply record parameter
258 if [ -z "$PARAMETERS" ] ; then
259 PARAMETERS="$opt"
260 else
261 PARAMETERS="$PARAMETERS $opt"
262 fi
263 ;;
264 esac
265 shift
266done
267
268if [ "$OPTION_HELP" = "yes" ] ; then
269 echo "Usage: $PROGNAME [options]"
270 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700271 echo "Setup a gdb debugging session for your Android NDK application."
272 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
273 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700274 echo "Valid options:"
275 echo ""
276 echo " --help|-h|-? Print this help"
277 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700278 echo " --force Kill existing debug session if it exists"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700279 echo " --start Launch application instead of attaching to existing one"
280 echo " --launch=<name> Same as --start, but specify activity name (see below)"
281 echo " --launch-list List all launchable activity names from manifest"
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200282 echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700283 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700284 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700285 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700286 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
287 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700288 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
289 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700290 echo " -e Connect to single emulator instance"
291 echo " -d Connect to single target device"
292 echo " -s <serial> Connect to specific emulator or device"
293 echo ""
294 exit 0
295fi
296
297log "Android NDK installation path: $ANDROID_NDK_ROOT"
298
299if [ -n "$OPTION_EXEC" ] ; then
300 if [ ! -f "$OPTION_EXEC" ]; then
301 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
302 exit 1
303 fi
304fi
305
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200306if [ -n "$OPTION_DELAY" ] ; then
307 DELAY="$OPTION_DELAY"
308fi
309
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700310# Check ADB tool version
311if [ -n "$OPTION_ADB" ] ; then
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100312 ADB_CMD=$OPTION_ADB
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700313 log "Using specific adb command: $ADB_CMD"
314else
315 if [ -z "$ADB_CMD" ] ; then
316 echo "ERROR: The 'adb' tool is not in your path."
317 echo " You can change your PATH variable, or use"
318 echo " --adb=<executable> to point to a valid one."
319 exit 1
320 fi
321 log "Using default adb command: $ADB_CMD"
322fi
323
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100324ADB_CMD=$(quote_spaces $ADB_CMD)
325ADB_VERSION=$("$ADB_CMD" version 2>/dev/null)
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700326if [ $? != 0 ] ; then
327 echo "ERROR: Could not run ADB with: $ADB_CMD"
328 exit 1
329fi
330log "ADB version found: $ADB_VERSION"
331
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700332if [ "x$DEVICE_SERIAL" = "x" ]; then
333 log "Using ADB flags: $ADB_FLAGS"
334else
335 log "Using ADB flags: $ADB_FLAGS" \"$DEVICE_SERIAL\"
336fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700337
Andrew Hsieh201486c2012-05-16 14:03:11 +0800338# Run an ADB command with the right ADB flags
339# $1+: adb command parameter
340adb_cmd ()
341{
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700342 if [ "x$DEVICE_SERIAL" = "x" ]; then
343 "$ADB_CMD" $ADB_FLAGS "$@"
344 else
345 # NOTE: We escape $ADB_CMD and $DEVICE_SERIAL in case they contains spaces.
346 "$ADB_CMD" $ADB_FLAGS "$DEVICE_SERIAL" "$@"
347 fi
Andrew Hsieh201486c2012-05-16 14:03:11 +0800348}
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100349
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100350# Used internally by adb_var_shell and adb_var_shell2.
351# $1: 1 to redirect stderr to $1, 0 otherwise.
352# $2: Variable name that will contain the result
353# $3+: Command options
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100354_adb_var_shell ()
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700355{
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100356 # We need a temporary file to store the output of our command
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100357 local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
358 REDIRECT_STDERR=$1
359 VARNAME=$2
360 shift; shift;
361 CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100362 # Run the command, while storing the standard output to CMD_OUT
363 # and appending the exit code as the last line.
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100364 if [ "$REDIRECT_STDERR" != 0 ]; then
Andrew Hsieh201486c2012-05-16 14:03:11 +0800365 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100366 else
Andrew Hsieh201486c2012-05-16 14:03:11 +0800367 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100368 fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100369 # Get last line in log, which contains the exit code from the command
370 RET=`sed -e '$!d' $CMD_OUT`
371 # Get output, which corresponds to everything except the last line
372 OUT=`sed -e '$d' $CMD_OUT`
373 rm -f $CMD_OUT
374 eval $VARNAME=\"\$OUT\"
375 return $RET
376}
377
378# Run a command through 'adb shell' and captures its standard output
379# into a variable. The function's exit code is the same than the command's.
380#
381# This is required because there is a bug where "adb shell" always returns
382# 0 on the host, even if the command fails on the device.
383#
384# $1: Variable name (e.g. FOO)
385# On exit, $FOO is set to the command's standard output
386#
387# The return status will be 0 (success) if the command succeeded
388# or 1 (failure) otherwise.
389adb_var_shell ()
390{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200391 _adb_var_shell 0 "$@"
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100392}
393
394# A variant of adb_var_shell that stores both stdout and stderr in the output
395# $1: Variable name
396adb_var_shell2 ()
397{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200398 _adb_var_shell 1 "$@"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700399}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700400
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200401# Return the PID of a given package or program, or 0 if it doesn't run
402# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
403# Out: PID number, or 0 if not running
404get_pid_of ()
405{
Andrew Hsieh201486c2012-05-16 14:03:11 +0800406 adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200407}
408
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700409# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700410AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
411AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700412if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700413 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700414 exit 1
415fi
416if [ "$AWK_TEST" != "Pass" ] ; then
417 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
418 exit 1
419fi
420
421# Name of the manifest file
422MANIFEST=AndroidManifest.xml
423
424# Find the root of the application project.
425if [ -n "$OPTION_PROJECT" ] ; then
426 PROJECT=$OPTION_PROJECT
427 log "Using specified project path: $PROJECT"
428 if [ ! -d "$PROJECT" ] ; then
429 echo "ERROR: Your --project option does not point to a directory!"
430 exit 1
431 fi
432 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
433 echo "ERROR: Your --project does not point to an Android project path!"
434 echo " It is missing a $MANIFEST file."
435 exit 1
436 fi
437else
438 # Assume we are in the project directory
439 if [ -f "$MANIFEST" ] ; then
440 PROJECT=.
441 else
442 PROJECT=
443 CURDIR=`pwd`
444 while [ "$CURDIR" != "/" ] ; do
445 if [ -f "$CURDIR/$MANIFEST" ] ; then
446 PROJECT="$CURDIR"
447 break
448 fi
449 CURDIR=`dirname $CURDIR`
450 done
451 if [ -z "$PROJECT" ] ; then
452 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
453 exit 1
454 fi
455 fi
456 log "Using auto-detected project path: $PROJECT"
457fi
458
459# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700460PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700461log "Found package name: $PACKAGE_NAME"
462if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
463 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
464 echo " Please check that the file is well-formed!"
465 exit 1
466fi
467
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700468# If --launch-list is used, list all launchable activities, and be done with it
469if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
470 log "Extracting list of launchable activities from manifest:"
471 run_awk_manifest_script extract-launchable.awk
472 exit 0
473fi
474
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700475APP_ABIS=`get_build_var APP_ABI`
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700476if [ "$APP_ABIS" != "${APP_ABIS%%all*}" ] ; then
477# replace first "all" with all available ABIs
478 ALL_ABIS=`get_build_var NDK_ALL_ABIS`
479 APP_ABIS_FRONT="${APP_ABIS%%all*}"
480 APP_ABIS_BACK="${APP_ABIS#*all}"
481 APP_ABIS="${APP_ABIS_FRONT}${ALL_ABIS}${APP_ABIS_BACK}"
482fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700483log "ABIs targetted by application: $APP_ABIS"
484
485# Check the ADB command, and that we can connect to the device/emulator
Andrew Hsieh201486c2012-05-16 14:03:11 +0800486ADB_TEST=`adb_cmd shell ls`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700487if [ $? != 0 ] ; then
488 echo "ERROR: Could not connect to device or emulator!"
489 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700490 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700491 echo " in case of multiple ones."
492 exit 1
493fi
494
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700495# Check that the device is running Froyo (API Level 8) or higher
496#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100497adb_var_shell API_LEVEL getprop ro.build.version.sdk
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700498if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200499 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700500 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
501 exit 1
502fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700503log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700504if [ "$API_LEVEL" -lt "8" ] ; then
505 echo "ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200506 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700507 exit 1
508fi
509
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700510# Get the target device's supported ABI(s)
511# And check that they are supported by the application
512#
513COMPAT_ABI=none
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700514adb_var_shell CPU_ABI1 getprop ro.product.cpu.abi
515adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
516
517# Both CPU_ABI1 and CPU_ABI2 may contain multiple comma-delimited abis.
518# Concatanate CPU_ABI1 and CPU_ABI2 and replace all ',' with space.
519# Add trailing space to ease whole-word matching of APP_ABI.
520CPU_ABIS="$CPU_ABI1,$CPU_ABI2,"
521CPU_ABIS=$(echo $CPU_ABIS | tr ',' ' ')
522log "Device CPU ABIs: $CPU_ABIS"
523
524for APP_ABI in $APP_ABIS; do
525 if [ "$CPU_ABIS" != "${CPU_ABIS%$APP_ABI *}" ] ; then
526 COMPAT_ABI=$APP_ABI
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700527 break
528 fi
529done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700530
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700531if [ "$COMPAT_ABI" = none ] ; then
532 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700533 echo " Device supports: $CPU_ABIS"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700534 echo " Package supports: $APP_ABIS"
535 exit 1
536fi
537log "Compatible device ABI: $COMPAT_ABI"
538
David 'Digit' Turnerff7cd042012-02-14 02:43:52 +0100539# Get information from the build system
540GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
541log "Using gdb setup init: $GDBSETUP_INIT"
542
543TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
544log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
545
546APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
547log "Using app out directory: $APP_OUT"
548
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200549# Check that the application is debuggable, or nothing will work
550DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
551log "Found debuggable flag: $DEBUGGABLE"
552if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
553 # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
554 # ok to not have android:debuggable set to true in the original manifest.
555 # However, if this is not the case, then complain!!
556 if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
557 log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
558 else
559 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
560 echo ""
561 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
562 echo ""
563 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
564 echo " then rebuild normally."
565 echo ""
566 echo "After one of these, re-install to the device!"
567 exit 1
568 fi
569else
570 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
571 # debuggable flag in the manifest without calling ndk-build afterwards.
572 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
573 echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
574 echo " This usually means you modified your AndroidManifest.xml to set"
575 echo " the android:debuggable flag to 'true' but did not rebuild the"
576 echo " native binaries. Please call 'ndk-build' to do so,"
577 echo " *then* re-install to the device!"
578 exit 1
579 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700580fi
581
582# Let's check that 'gdbserver' is properly installed on the device too. If this
583# is not the case, the user didn't install the proper package after rebuilding.
584#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100585adb_var_shell2 DEVICE_GDBSERVER ls /data/data/$PACKAGE_NAME/lib/gdbserver
586if [ $? != 0 ]; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700587 echo "ERROR: Non-debuggable application installed on the target device."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200588 echo " Please re-install the debuggable version!"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700589 exit 1
590fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100591log "Found device gdbserver: $DEVICE_GDBSERVER"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700592
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700593# Find the <dataDir> of the package on the device
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100594adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700595if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700596 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700597 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700598 exit 1
599fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100600log "Found data directory: '$DATA_DIR'"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700601
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700602# Launch the activity if needed
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200603if [ "$OPTION_START" = "yes" ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700604 # If --launch is used, ignore --start, otherwise extract the first
605 # launchable activity name from the manifest and use it as if --launch=<name>
606 # was used instead.
607 #
608 if [ -z "$OPTION_LAUNCH" ] ; then
609 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
610 if [ $? != 0 ] ; then
611 echo "ERROR: Could not extract name of launchable activity from manifest!"
612 echo " Try to use --launch=<name> directly instead as a work-around."
613 exit 1
614 fi
615 log "Found first launchable activity: $OPTION_LAUNCH"
616 if [ -z "$OPTION_LAUNCH" ] ; then
617 echo "ERROR: It seems that your Application does not have any launchable activity!"
618 echo " Please fix your manifest file and rebuild/re-install your application."
619 exit 1
620 fi
621 fi
622fi
623
624if [ -n "$OPTION_LAUNCH" ] ; then
625 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800626 run adb_cmd shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700627 if [ $? != 0 ] ; then
628 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
629 echo " Use --launch-list to dump a list of valid values."
630 exit 1
631 fi
632 # Sleep a bit, it sometimes take one second to start properly
633 # Note that we use the 'sleep' command on the device here.
Andrew Hsieh201486c2012-05-16 14:03:11 +0800634 run adb_cmd shell sleep $DELAY
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700635fi
636
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700637# Find the PID of the application being run
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200638PID=$(get_pid_of "$PACKAGE_NAME")
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700639log "Found running PID: $PID"
640if [ $? != 0 -o "$PID" = "0" ] ; then
641 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700642 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700643 echo " Weird, this probably means one of these:"
644 echo ""
645 echo " - The installed package does not match your current manifest."
646 echo " - The application process was terminated."
647 echo ""
648 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700649 else
650 echo " Are you sure the application is already started?"
651 echo " Consider using --start or --launch=<name> if not."
652 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700653 exit 1
654fi
655
656# Check that there is no other instance of gdbserver running
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200657GDBSERVER_PID=$(get_pid_of lib/gdbserver)
658if [ "$GDBSERVER_PID" != "0" ]; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700659 if [ "$OPTION_FORCE" = "no" ] ; then
660 echo "ERROR: Another debug session running, Use --force to kill it."
661 exit 1
662 fi
663 log "Killing existing debugging session"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800664 run adb_cmd shell kill -9 $GDBSERVER_PID
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700665fi
666
667# Launch gdbserver now
668DEBUG_SOCKET=debug-socket
Andrew Hsieh201486c2012-05-16 14:03:11 +0800669run adb_cmd shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700670if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700671 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700672 exit 1
673fi
674log "Launched gdbserver succesfully."
675
676# Setup network redirection
677log "Setup network redirection"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800678run adb_cmd forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700679if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700680 echo "ERROR: Could not setup network redirection to gdbserver?"
681 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700682 exit 1
683fi
684
685# Get the app_server binary from the device
686APP_PROCESS=$APP_OUT/app_process
Andrew Hsieh201486c2012-05-16 14:03:11 +0800687run adb_cmd pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200688log "Pulled app_process from device/emulator."
689
Andrew Hsieh07a6a662012-07-02 16:38:19 -0700690run adb_cmd pull /system/bin/linker `native_path $APP_OUT/linker`
691log "Pulled linker from device/emulator."
692
Andrew Hsieh201486c2012-05-16 14:03:11 +0800693run adb_cmd pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200694log "Pulled libc.so from device/emulator."
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700695
696# Now launch the appropriate gdb client with the right init commands
697#
698GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
699GDBSETUP=$APP_OUT/gdb.setup
700cp -f $GDBSETUP_INIT $GDBSETUP
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200701#uncomment the following to debug the remote connection only
702#echo "set debug remote 1" >> $GDBSETUP
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200703echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700704echo "target remote :$DEBUG_PORT" >> $GDBSETUP
705if [ -n "$OPTION_EXEC" ] ; then
706 cat $OPTION_EXEC >> $GDBSETUP
707fi
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200708$GDBCLIENT -x `native_path $GDBSETUP`