blob: 1bb42342ff290fe28b7afeb3f6e09203e67b9a1d [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`
Andrew Hsiehd8276e92013-11-12 14:33:49 +080028PROGDIR=`cd $PROGDIR && pwd -P`
Andrew Hsieh2c1bb7c2012-09-20 17:52:00 -070029
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
Cole Wang65f49a52013-11-14 11:44:27 +080038NDK_BUILDTOOLS_PATH=$PROGDIR/build/tools
39. $PROGDIR/build/tools/prebuilt-common.sh
David 'Digit' Turnerbd7544d2013-06-10 10:17:33 +020040. $PROGDIR/build/tools/ndk-common.sh
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070041
42force_32bit_binaries
43
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010044# Find if a given shell program is available.
45# We need to take care of the fact that the 'which <foo>' command
46# may return either an empty string (Linux) or something like
47# "no <foo> in ..." (Darwin). Also, we need to redirect stderr
48# to /dev/null for Cygwin
49#
50# $1: program name
51# Out: program path, or empty string
52# Return: 0 on success, != 0 on error
53#
54find_program ()
55{
56 local PROG RET
57 PROG=$(which "$1" 2>/dev/null)
58 RET=$?
59 if [ $RET != 0 ]; then
60 PROG=
61 fi
62 echo "$PROG"
63 return $RET
64}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070065
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010066quote_spaces ()
67{
68 echo "$@" | sed -e 's! !\ !g'
69}
70
71# If ADB_CMD is not defined, try to find a program named 'adb'
72# in our path.
73ADB_CMD=${ADB_CMD:-$(find_program adb)}
74ADB_FLAGS=${ADB_FLAGS:-}
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -070075DEVICE_SERIAL=
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010076
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -060077JDB_CMD=${JDB_CMD:-$(find_program jdb)}
78
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010079AWK_CMD=${AWK_CMD:-$(find_program awk)}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070080
81DEBUG_PORT=5039
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -060082JDB_PORT=65534
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070083
Cole Wang65f49a52013-11-14 11:44:27 +080084UNKNOWN_ABI=$(find_ndk_unknown_archs)
85
David 'Digit' Turner5b656252010-10-08 00:43:32 +020086# Delay in seconds between launching the activity and attaching gdbserver on it.
87# This is needed because there is no way to know when the activity has really
88# started, and sometimes this takes a few seconds.
89DELAY=2
90
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070091PARAMETERS=
92OPTION_HELP=no
93OPTION_PROJECT=
94OPTION_FORCE=no
95OPTION_ADB=
96OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070097OPTION_START=no
98OPTION_LAUNCH=
99OPTION_LAUNCH_LIST=no
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200100OPTION_DELAY=
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600101OPTION_WAIT="-D"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700102
103check_parameter ()
104{
105 if [ -z "$2" ]; then
106 echo "ERROR: Missing parameter after option '$1'"
107 exit 1
108 fi
109}
110
111check_adb_flags ()
112{
113 if [ -n "$ADB_FLAGS" ] ; then
114 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
115 exit 1
116 fi
117}
118
119get_build_var ()
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 | tail -1
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700125}
126
127get_build_var_for_abi ()
128{
129 if [ -z "$GNUMAKE" ] ; then
130 GNUMAKE=make
131 fi
David 'Digit' Turner7bbc2112012-08-27 14:23:19 +0200132 $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 -0700133}
134
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700135# Used to run an awk script on the manifest
136run_awk_manifest_script ()
137{
138 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
139}
140
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700141if [ "$HOST_OS" = "cygwin" ] ; then
142# Return native path representation from cygwin one
143# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
144# Return: path in host windows representation, e.g. C:/some/thing
145#
146# We use mixed mode (i.e. / as the directory separator) because
147# all the tools we use recognize it properly, and it avoids lots
148# of escaping nonsense associated with "\"
149#
150native_path ()
151{
152 cygpath -m $1
153}
154else # HOST_OS != windows
155native_path ()
156{
157 echo "$1"
158}
159fi # HOST_OS != windows
160
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100161# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
162# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
163# might fail, e.g. when invoked with:
164#
165# cd $NDKROOT
166# ./ndk-gdb --project=/path/to/project
167#
168path_is_absolute ()
169{
170 local P P2
171 P=$1 # copy path
172 P2=${P#/} # remove / prefix, if any
173 [ "$P" != "$P2" ]
174}
175
176if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
177 ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
178fi
179
180
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700181VERBOSE=no
182while [ -n "$1" ]; do
183 opt="$1"
184 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
185 case "$opt" in
186 --help|-h|-\?)
187 OPTION_HELP=yes
188 ;;
189 --verbose)
190 VERBOSE=yes
191 ;;
192 -s)
193 check_parameter $1 $2
194 check_adb_flags
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700195 ADB_FLAGS=" -s"
196 DEVICE_SERIAL=$2
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700197 shift
198 ;;
199 -s*)
200 check_adb_flags
201 optarg=`expr -- "$opt" : '-s\(.*\)'`
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700202 ADB_FLAGS=" -s"
203 DEVICE_SERIAL=$optarg
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700204 ;;
205 -p)
206 check_parameter $1 $2
207 OPTION_PROJECT="$2"
208 shift
209 ;;
210 -p*)
211 optarg=`expr -- "$opt" : '-p\(.*\)'`
212 OPTION_PROJECT="$optarg"
213 ;;
214 --exec=*)
215 OPTION_EXEC="$optarg"
216 ;;
217 -x)
218 check_parameter $1 $2
219 OPTION_EXEC="$2"
220 shift
221 ;;
222 -x*)
223 optarg=`expr -- "$opt" : '-x\(.*\)'`
224 OPTION_EXEC="$optarg"
225 ;;
226 -e)
227 check_adb_flags
228 ADB_FLAGS=" -e"
229 ;;
230 -d)
231 check_adb_flags
232 ADB_FLAGS=" -d"
233 ;;
234 --adb=*) # specify ADB command
235 OPTION_ADB="$optarg"
236 ;;
237 --awk=*)
238 AWK_CMD="$optarg"
239 ;;
240 --project=*)
241 OPTION_PROJECT="$optarg"
242 ;;
243 --port=*)
244 DEBUG_PORT="$optarg"
245 ;;
246 --force)
247 OPTION_FORCE="yes"
248 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700249 --launch-list)
250 OPTION_LAUNCH_LIST="yes"
251 ;;
252 --launch=*)
253 OPTION_LAUNCH="$optarg"
254 ;;
255 --start)
256 OPTION_START=yes
257 ;;
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200258 --delay=*)
259 OPTION_DELAY="$optarg"
260 ;;
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600261 --nowait)
262 JDB_PORT=
263 OPTION_WAIT=
264 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700265 -*) # unknown options
266 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
267 exit 1
268 ;;
269 *) # Simply record parameter
270 if [ -z "$PARAMETERS" ] ; then
271 PARAMETERS="$opt"
272 else
273 PARAMETERS="$PARAMETERS $opt"
274 fi
275 ;;
276 esac
277 shift
278done
279
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600280if [ -z "$JDB_CMD" ] && [ -n "$OPTION_WAIT" ]; then
281 echo "ERROR: 'jdb' not found; you must either install the JDK, or specify --nowait"
282 exit 1
283fi
284if [ -n "$JDB_PORT" ] && [ "$JDB_PORT" = "$DEBUG_PORT" ]; then
285 echo "ERROR: --port specified cannot be $JDB_PORT without --nowait"
286 exit 1
287fi
288
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700289if [ "$OPTION_HELP" = "yes" ] ; then
290 echo "Usage: $PROGNAME [options]"
291 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700292 echo "Setup a gdb debugging session for your Android NDK application."
293 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
294 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700295 echo "Valid options:"
296 echo ""
297 echo " --help|-h|-? Print this help"
298 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700299 echo " --force Kill existing debug session if it exists"
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600300 echo " --nowait Don't have application wait for debugger to attach"
301 echo " (This might cause you to miss some early JNI breakpoints)"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700302 echo " --start Launch application instead of attaching to existing one"
303 echo " --launch=<name> Same as --start, but specify activity name (see below)"
304 echo " --launch-list List all launchable activity names from manifest"
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200305 echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700306 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700307 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700308 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700309 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
310 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700311 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
312 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700313 echo " -e Connect to single emulator instance"
314 echo " -d Connect to single target device"
315 echo " -s <serial> Connect to specific emulator or device"
316 echo ""
317 exit 0
318fi
319
320log "Android NDK installation path: $ANDROID_NDK_ROOT"
321
322if [ -n "$OPTION_EXEC" ] ; then
323 if [ ! -f "$OPTION_EXEC" ]; then
324 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
325 exit 1
326 fi
327fi
328
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200329if [ -n "$OPTION_DELAY" ] ; then
330 DELAY="$OPTION_DELAY"
331fi
332
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700333# Check ADB tool version
334if [ -n "$OPTION_ADB" ] ; then
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100335 ADB_CMD=$OPTION_ADB
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700336 log "Using specific adb command: $ADB_CMD"
337else
338 if [ -z "$ADB_CMD" ] ; then
339 echo "ERROR: The 'adb' tool is not in your path."
340 echo " You can change your PATH variable, or use"
341 echo " --adb=<executable> to point to a valid one."
342 exit 1
343 fi
344 log "Using default adb command: $ADB_CMD"
345fi
346
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100347ADB_CMD=$(quote_spaces $ADB_CMD)
348ADB_VERSION=$("$ADB_CMD" version 2>/dev/null)
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700349if [ $? != 0 ] ; then
350 echo "ERROR: Could not run ADB with: $ADB_CMD"
351 exit 1
352fi
353log "ADB version found: $ADB_VERSION"
354
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700355if [ "x$DEVICE_SERIAL" = "x" ]; then
356 log "Using ADB flags: $ADB_FLAGS"
357else
358 log "Using ADB flags: $ADB_FLAGS" \"$DEVICE_SERIAL\"
359fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700360
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600361JDB_CMD=$(quote_spaces $JDB_CMD)
362log "Using JDB command: $JDB_CMD"
363
Andrew Hsieh201486c2012-05-16 14:03:11 +0800364# Run an ADB command with the right ADB flags
365# $1+: adb command parameter
366adb_cmd ()
367{
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700368 if [ "x$DEVICE_SERIAL" = "x" ]; then
369 "$ADB_CMD" $ADB_FLAGS "$@"
370 else
371 # NOTE: We escape $ADB_CMD and $DEVICE_SERIAL in case they contains spaces.
372 "$ADB_CMD" $ADB_FLAGS "$DEVICE_SERIAL" "$@"
373 fi
Andrew Hsieh201486c2012-05-16 14:03:11 +0800374}
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100375
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100376# Used internally by adb_var_shell and adb_var_shell2.
377# $1: 1 to redirect stderr to $1, 0 otherwise.
378# $2: Variable name that will contain the result
379# $3+: Command options
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100380_adb_var_shell ()
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700381{
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100382 # We need a temporary file to store the output of our command
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100383 local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
384 REDIRECT_STDERR=$1
385 VARNAME=$2
386 shift; shift;
387 CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100388 # Run the command, while storing the standard output to CMD_OUT
389 # and appending the exit code as the last line.
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100390 if [ "$REDIRECT_STDERR" != 0 ]; then
Andrew Hsieh201486c2012-05-16 14:03:11 +0800391 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100392 else
Andrew Hsieh201486c2012-05-16 14:03:11 +0800393 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100394 fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100395 # Get last line in log, which contains the exit code from the command
396 RET=`sed -e '$!d' $CMD_OUT`
397 # Get output, which corresponds to everything except the last line
398 OUT=`sed -e '$d' $CMD_OUT`
399 rm -f $CMD_OUT
400 eval $VARNAME=\"\$OUT\"
401 return $RET
402}
403
404# Run a command through 'adb shell' and captures its standard output
405# into a variable. The function's exit code is the same than the command's.
406#
407# This is required because there is a bug where "adb shell" always returns
408# 0 on the host, even if the command fails on the device.
409#
410# $1: Variable name (e.g. FOO)
411# On exit, $FOO is set to the command's standard output
412#
413# The return status will be 0 (success) if the command succeeded
414# or 1 (failure) otherwise.
415adb_var_shell ()
416{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200417 _adb_var_shell 0 "$@"
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100418}
419
420# A variant of adb_var_shell that stores both stdout and stderr in the output
421# $1: Variable name
422adb_var_shell2 ()
423{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200424 _adb_var_shell 1 "$@"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700425}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700426
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200427# Return the PID of a given package or program, or 0 if it doesn't run
428# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
429# Out: PID number, or 0 if not running
430get_pid_of ()
431{
Andrew Hsieh201486c2012-05-16 14:03:11 +0800432 adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200433}
434
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700435# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700436AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
437AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700438if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700439 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700440 exit 1
441fi
442if [ "$AWK_TEST" != "Pass" ] ; then
443 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
444 exit 1
445fi
446
447# Name of the manifest file
448MANIFEST=AndroidManifest.xml
449
450# Find the root of the application project.
451if [ -n "$OPTION_PROJECT" ] ; then
452 PROJECT=$OPTION_PROJECT
453 log "Using specified project path: $PROJECT"
454 if [ ! -d "$PROJECT" ] ; then
455 echo "ERROR: Your --project option does not point to a directory!"
456 exit 1
457 fi
458 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
459 echo "ERROR: Your --project does not point to an Android project path!"
460 echo " It is missing a $MANIFEST file."
461 exit 1
462 fi
463else
464 # Assume we are in the project directory
465 if [ -f "$MANIFEST" ] ; then
466 PROJECT=.
467 else
468 PROJECT=
469 CURDIR=`pwd`
470 while [ "$CURDIR" != "/" ] ; do
471 if [ -f "$CURDIR/$MANIFEST" ] ; then
472 PROJECT="$CURDIR"
473 break
474 fi
475 CURDIR=`dirname $CURDIR`
476 done
477 if [ -z "$PROJECT" ] ; then
478 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
479 exit 1
480 fi
481 fi
482 log "Using auto-detected project path: $PROJECT"
483fi
484
485# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700486PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700487log "Found package name: $PACKAGE_NAME"
488if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
489 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
490 echo " Please check that the file is well-formed!"
491 exit 1
492fi
493
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700494# If --launch-list is used, list all launchable activities, and be done with it
495if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
496 log "Extracting list of launchable activities from manifest:"
497 run_awk_manifest_script extract-launchable.awk
498 exit 0
499fi
500
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700501APP_ABIS=`get_build_var APP_ABI`
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700502if [ "$APP_ABIS" != "${APP_ABIS%%all*}" ] ; then
503# replace first "all" with all available ABIs
504 ALL_ABIS=`get_build_var NDK_ALL_ABIS`
505 APP_ABIS_FRONT="${APP_ABIS%%all*}"
506 APP_ABIS_BACK="${APP_ABIS#*all}"
507 APP_ABIS="${APP_ABIS_FRONT}${ALL_ABIS}${APP_ABIS_BACK}"
508fi
Andrew Hsieh748416e2014-01-22 18:21:18 -0800509# replace "armeabi-v7a-hard" with "armeabi-v7a"
510APP_ABIS=`echo $APP_ABIS | sed -e 's/armeabi-v7a-hard/armeabi-v7a/g'`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700511log "ABIs targetted by application: $APP_ABIS"
512
513# Check the ADB command, and that we can connect to the device/emulator
Andrew Hsieh201486c2012-05-16 14:03:11 +0800514ADB_TEST=`adb_cmd shell ls`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700515if [ $? != 0 ] ; then
516 echo "ERROR: Could not connect to device or emulator!"
517 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700518 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700519 echo " in case of multiple ones."
520 exit 1
521fi
522
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700523# Check that the device is running Froyo (API Level 8) or higher
524#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100525adb_var_shell API_LEVEL getprop ro.build.version.sdk
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700526if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200527 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700528 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
529 exit 1
530fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700531log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700532if [ "$API_LEVEL" -lt "8" ] ; then
533 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 +0200534 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700535 exit 1
536fi
537
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700538# Get the target device's supported ABI(s)
539# And check that they are supported by the application
540#
541COMPAT_ABI=none
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700542adb_var_shell CPU_ABI1 getprop ro.product.cpu.abi
543adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
544
545# Both CPU_ABI1 and CPU_ABI2 may contain multiple comma-delimited abis.
546# Concatanate CPU_ABI1 and CPU_ABI2 and replace all ',' with space.
547# Add trailing space to ease whole-word matching of APP_ABI.
Andrew Hsieh5f2f1102013-04-08 14:22:19 +0800548CPU_ABIS="$CPU_ABI1,$CPU_ABI2"
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700549CPU_ABIS=$(echo $CPU_ABIS | tr ',' ' ')
550log "Device CPU ABIs: $CPU_ABIS"
551
Andrew Hsieh5f2f1102013-04-08 14:22:19 +0800552APP_ABIS=$APP_ABIS" "
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700553
Cole Wang65f49a52013-11-14 11:44:27 +0800554adb_var_shell BCFILES run-as $PACKAGE_NAME /system/bin/sh -c "ls lib/*.bc"
555if [ $? == 0 ]; then
556 COMPAT_ABI="$UNKNOWN_ABI"
557else
558 for CPU_ABI in $CPU_ABIS; do
559 if [ "$APP_ABIS" != "${APP_ABIS%$CPU_ABI *}" ] ; then
560 COMPAT_ABI=$CPU_ABI
561 break
562 fi
563 done
564fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700565if [ "$COMPAT_ABI" = none ] ; then
566 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
Andrew Hsieh3c3ce152012-08-03 10:44:09 -0700567 echo " Device supports: $CPU_ABIS"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700568 echo " Package supports: $APP_ABIS"
569 exit 1
570fi
571log "Compatible device ABI: $COMPAT_ABI"
572
David 'Digit' Turnerff7cd042012-02-14 02:43:52 +0100573# Get information from the build system
574GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
575log "Using gdb setup init: $GDBSETUP_INIT"
576
Cole Wang65f49a52013-11-14 11:44:27 +0800577# Find the prefix for gdb-client
578if [ "$COMPAT_ABI" != "$UNKNOWN_ABI" ]; then
579 TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
580else
581 TOOLCHAIN_ABI=$(echo $CPU_ABIS | awk '{print $NF}')
582 TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $TOOLCHAIN_ABI`
583fi
David 'Digit' Turnerff7cd042012-02-14 02:43:52 +0100584log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
585
586APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
587log "Using app out directory: $APP_OUT"
588
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200589# Check that the application is debuggable, or nothing will work
590DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
591log "Found debuggable flag: $DEBUGGABLE"
592if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
Cole Wanga02bea02013-10-31 14:11:30 +0800593 # If gdb.setup exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200594 # ok to not have android:debuggable set to true in the original manifest.
595 # However, if this is not the case, then complain!!
Cole Wanga02bea02013-10-31 14:11:30 +0800596 if [ -f $PROJECT/libs/$COMPAT_ABI/gdb.setup ] ; then
597 log "Found gdb.setup under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200598 else
599 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
600 echo ""
601 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
602 echo ""
603 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
604 echo " then rebuild normally."
605 echo ""
606 echo "After one of these, re-install to the device!"
607 exit 1
608 fi
609else
610 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
611 # debuggable flag in the manifest without calling ndk-build afterwards.
Cole Wanga02bea02013-10-31 14:11:30 +0800612 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdb.setup ] ; then
613 echo "ERROR: Could not find gdb.setup under $PROJECT/libs/$COMPAT_ABI"
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200614 echo " This usually means you modified your AndroidManifest.xml to set"
615 echo " the android:debuggable flag to 'true' but did not rebuild the"
616 echo " native binaries. Please call 'ndk-build' to do so,"
617 echo " *then* re-install to the device!"
618 exit 1
619 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700620fi
621
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700622# Find the <dataDir> of the package on the device
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100623adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700624if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700625 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700626 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700627 exit 1
628fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100629log "Found data directory: '$DATA_DIR'"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700630
Cole Wanga02bea02013-10-31 14:11:30 +0800631# Let's check that 'gdbserver' is properly installed on the device too. If 'gdbserver'
632# is not there, push 'gdbserver' found in prebuilt.
633#
634DEVICE_GDBSERVER=$DATA_DIR/lib/gdbserver
635adb_var_shell2 GDBSERVER_RESULT ls $DEVICE_GDBSERVER
636if [ $? != 0 ]; then
637
638 # Figure out what's the target-arch and find gdbserver in prebuilt.
639 TARGET_ARCH=none
640
641 for ANDROID_ARCH in $ANDROID_NDK_ROOT/prebuilt/android-*; do
642 ANDROID_ARCH=${ANDROID_ARCH#*android-}
643 if [ x"$CPU_ABIS" != x"${CPU_ABIS#$ANDROID_ARCH*}" ]; then
644 TARGET_ARCH=$ANDROID_ARCH
645 break;
646 fi
647 done
648
649 if [ $TARGET_ARCH != "none" ]; then
650 DEVICE_GDBSERVER=/data/local/tmp/gdbserver
651
652 adb shell mkdir -p /data/local/tmp
653 adb push ${ANDROID_NDK_ROOT}/prebuilt/android-${TARGET_ARCH}/gdbserver/gdbserver \
654 $DEVICE_GDBSERVER
655 log "Push gdbserver in device"
656 else
657 echo "ERROR: Non-debuggable application installed on the target device."
658 echo " Please re-install the debuggable version!"
659 exit 1
660 fi
661fi
662log "Found device gdbserver: $DEVICE_GDBSERVER"
663
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700664# Launch the activity if needed
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200665if [ "$OPTION_START" = "yes" ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700666 # If --launch is used, ignore --start, otherwise extract the first
667 # launchable activity name from the manifest and use it as if --launch=<name>
668 # was used instead.
669 #
670 if [ -z "$OPTION_LAUNCH" ] ; then
671 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
672 if [ $? != 0 ] ; then
673 echo "ERROR: Could not extract name of launchable activity from manifest!"
674 echo " Try to use --launch=<name> directly instead as a work-around."
675 exit 1
676 fi
677 log "Found first launchable activity: $OPTION_LAUNCH"
678 if [ -z "$OPTION_LAUNCH" ] ; then
679 echo "ERROR: It seems that your Application does not have any launchable activity!"
680 echo " Please fix your manifest file and rebuild/re-install your application."
681 exit 1
682 fi
683 fi
684fi
685
686if [ -n "$OPTION_LAUNCH" ] ; then
687 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600688 run adb_cmd shell am start $OPTION_WAIT -n $PACKAGE_NAME/$OPTION_LAUNCH
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700689 if [ $? != 0 ] ; then
690 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
691 echo " Use --launch-list to dump a list of valid values."
692 exit 1
693 fi
694 # Sleep a bit, it sometimes take one second to start properly
695 # Note that we use the 'sleep' command on the device here.
Andrew Hsieh201486c2012-05-16 14:03:11 +0800696 run adb_cmd shell sleep $DELAY
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700697fi
698
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700699# Find the PID of the application being run
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200700PID=$(get_pid_of "$PACKAGE_NAME")
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700701log "Found running PID: $PID"
702if [ $? != 0 -o "$PID" = "0" ] ; then
703 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700704 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700705 echo " Weird, this probably means one of these:"
706 echo ""
707 echo " - The installed package does not match your current manifest."
708 echo " - The application process was terminated."
709 echo ""
710 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700711 else
712 echo " Are you sure the application is already started?"
713 echo " Consider using --start or --launch=<name> if not."
714 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700715 exit 1
716fi
717
718# Check that there is no other instance of gdbserver running
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200719GDBSERVER_PID=$(get_pid_of lib/gdbserver)
720if [ "$GDBSERVER_PID" != "0" ]; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700721 if [ "$OPTION_FORCE" = "no" ] ; then
722 echo "ERROR: Another debug session running, Use --force to kill it."
723 exit 1
724 fi
725 log "Killing existing debugging session"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800726 run adb_cmd shell kill -9 $GDBSERVER_PID
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700727fi
728
729# Launch gdbserver now
730DEBUG_SOCKET=debug-socket
Cole Wanga02bea02013-10-31 14:11:30 +0800731run adb_cmd shell run-as $PACKAGE_NAME $DEVICE_GDBSERVER +$DEBUG_SOCKET --attach $PID &
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700732if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700733 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700734 exit 1
735fi
736log "Launched gdbserver succesfully."
737
738# Setup network redirection
739log "Setup network redirection"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800740run adb_cmd forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700741if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700742 echo "ERROR: Could not setup network redirection to gdbserver?"
743 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700744 exit 1
745fi
746
747# Get the app_server binary from the device
748APP_PROCESS=$APP_OUT/app_process
Andrew Hsieh201486c2012-05-16 14:03:11 +0800749run adb_cmd pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200750log "Pulled app_process from device/emulator."
751
Andrew Hsieh07a6a662012-07-02 16:38:19 -0700752run adb_cmd pull /system/bin/linker `native_path $APP_OUT/linker`
753log "Pulled linker from device/emulator."
754
Andrew Hsieh201486c2012-05-16 14:03:11 +0800755run adb_cmd pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200756log "Pulled libc.so from device/emulator."
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700757
Ryan V. Bissell5cbc03f2012-12-11 18:05:12 -0600758# Setup JDB connection, for --start or --launch
759if [ "$OPTION_START" = "yes" ] || [ -n "$OPTION_LAUNCH" ] ; then
760 if [ -n "$JDB_PORT" ]; then
761 log "Setup JDB connection"
762 run adb_cmd forward tcp:$JDB_PORT jdwp:$PID
763 sleep 1
764 $JDB_CMD -connect com.sun.jdi.SocketAttach:hostname=localhost,port=$JDB_PORT &
765 sleep 1
766 fi
767fi
768
Cole Wang65f49a52013-11-14 11:44:27 +0800769# If we are debugging UNKNOWN_ABI, download compiled *.so from device.
770#
771if [ $COMPAT_ABI == $UNKNOWN_ABI ]; then
772 for bc in $BCFILES; do
773 log "Pulled $(basename $bc .bc).so from device/emulator."
774 adb pull $DATA_DIR/lib/$(basename $bc .bc).so $PROJECT/obj/local/$UNKNOWN_ABI/
775 done
776fi
777
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700778# Now launch the appropriate gdb client with the right init commands
779#
780GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
781GDBSETUP=$APP_OUT/gdb.setup
782cp -f $GDBSETUP_INIT $GDBSETUP
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200783#uncomment the following to debug the remote connection only
784#echo "set debug remote 1" >> $GDBSETUP
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200785echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700786echo "target remote :$DEBUG_PORT" >> $GDBSETUP
787if [ -n "$OPTION_EXEC" ] ; then
788 cat $OPTION_EXEC >> $GDBSETUP
789fi
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200790$GDBCLIENT -x `native_path $GDBSETUP`