blob: 80af28359359f722e48a930bc8c91e4f052346b5 [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#
27. `dirname $0`/build/core/ndk-common.sh
28
29force_32bit_binaries
30
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010031# Find if a given shell program is available.
32# We need to take care of the fact that the 'which <foo>' command
33# may return either an empty string (Linux) or something like
34# "no <foo> in ..." (Darwin). Also, we need to redirect stderr
35# to /dev/null for Cygwin
36#
37# $1: program name
38# Out: program path, or empty string
39# Return: 0 on success, != 0 on error
40#
41find_program ()
42{
43 local PROG RET
44 PROG=$(which "$1" 2>/dev/null)
45 RET=$?
46 if [ $RET != 0 ]; then
47 PROG=
48 fi
49 echo "$PROG"
50 return $RET
51}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070052
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010053quote_spaces ()
54{
55 echo "$@" | sed -e 's! !\ !g'
56}
57
58# If ADB_CMD is not defined, try to find a program named 'adb'
59# in our path.
60ADB_CMD=${ADB_CMD:-$(find_program adb)}
61ADB_FLAGS=${ADB_FLAGS:-}
62
63AWK_CMD=${AWK_CMD:-$(find_program awk)}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070064
65DEBUG_PORT=5039
66
David 'Digit' Turner5b656252010-10-08 00:43:32 +020067# Delay in seconds between launching the activity and attaching gdbserver on it.
68# This is needed because there is no way to know when the activity has really
69# started, and sometimes this takes a few seconds.
70DELAY=2
71
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070072PARAMETERS=
73OPTION_HELP=no
74OPTION_PROJECT=
75OPTION_FORCE=no
76OPTION_ADB=
77OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070078OPTION_START=no
79OPTION_LAUNCH=
80OPTION_LAUNCH_LIST=no
David 'Digit' Turner5b656252010-10-08 00:43:32 +020081OPTION_DELAY=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070082
83check_parameter ()
84{
85 if [ -z "$2" ]; then
86 echo "ERROR: Missing parameter after option '$1'"
87 exit 1
88 fi
89}
90
91check_adb_flags ()
92{
93 if [ -n "$ADB_FLAGS" ] ; then
94 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
95 exit 1
96 fi
97}
98
99get_build_var ()
100{
101 if [ -z "$GNUMAKE" ] ; then
102 GNUMAKE=make
103 fi
104 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1
105}
106
107get_build_var_for_abi ()
108{
109 if [ -z "$GNUMAKE" ] ; then
110 GNUMAKE=make
111 fi
112 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2
113}
114
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700115# Used to run an awk script on the manifest
116run_awk_manifest_script ()
117{
118 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
119}
120
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700121if [ "$HOST_OS" = "cygwin" ] ; then
122# Return native path representation from cygwin one
123# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
124# Return: path in host windows representation, e.g. C:/some/thing
125#
126# We use mixed mode (i.e. / as the directory separator) because
127# all the tools we use recognize it properly, and it avoids lots
128# of escaping nonsense associated with "\"
129#
130native_path ()
131{
132 cygpath -m $1
133}
134else # HOST_OS != windows
135native_path ()
136{
137 echo "$1"
138}
139fi # HOST_OS != windows
140
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100141# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
142# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
143# might fail, e.g. when invoked with:
144#
145# cd $NDKROOT
146# ./ndk-gdb --project=/path/to/project
147#
148path_is_absolute ()
149{
150 local P P2
151 P=$1 # copy path
152 P2=${P#/} # remove / prefix, if any
153 [ "$P" != "$P2" ]
154}
155
156if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
157 ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
158fi
159
160
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700161VERBOSE=no
162while [ -n "$1" ]; do
163 opt="$1"
164 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
165 case "$opt" in
166 --help|-h|-\?)
167 OPTION_HELP=yes
168 ;;
169 --verbose)
170 VERBOSE=yes
171 ;;
172 -s)
173 check_parameter $1 $2
174 check_adb_flags
175 ADB_FLAGS=" -s $2"
176 shift
177 ;;
178 -s*)
179 check_adb_flags
180 optarg=`expr -- "$opt" : '-s\(.*\)'`
181 ADB_FLAGS=" -s $optarg"
182 ;;
183 -p)
184 check_parameter $1 $2
185 OPTION_PROJECT="$2"
186 shift
187 ;;
188 -p*)
189 optarg=`expr -- "$opt" : '-p\(.*\)'`
190 OPTION_PROJECT="$optarg"
191 ;;
192 --exec=*)
193 OPTION_EXEC="$optarg"
194 ;;
195 -x)
196 check_parameter $1 $2
197 OPTION_EXEC="$2"
198 shift
199 ;;
200 -x*)
201 optarg=`expr -- "$opt" : '-x\(.*\)'`
202 OPTION_EXEC="$optarg"
203 ;;
204 -e)
205 check_adb_flags
206 ADB_FLAGS=" -e"
207 ;;
208 -d)
209 check_adb_flags
210 ADB_FLAGS=" -d"
211 ;;
212 --adb=*) # specify ADB command
213 OPTION_ADB="$optarg"
214 ;;
215 --awk=*)
216 AWK_CMD="$optarg"
217 ;;
218 --project=*)
219 OPTION_PROJECT="$optarg"
220 ;;
221 --port=*)
222 DEBUG_PORT="$optarg"
223 ;;
224 --force)
225 OPTION_FORCE="yes"
226 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700227 --launch-list)
228 OPTION_LAUNCH_LIST="yes"
229 ;;
230 --launch=*)
231 OPTION_LAUNCH="$optarg"
232 ;;
233 --start)
234 OPTION_START=yes
235 ;;
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200236 --delay=*)
237 OPTION_DELAY="$optarg"
238 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700239 -*) # unknown options
240 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
241 exit 1
242 ;;
243 *) # Simply record parameter
244 if [ -z "$PARAMETERS" ] ; then
245 PARAMETERS="$opt"
246 else
247 PARAMETERS="$PARAMETERS $opt"
248 fi
249 ;;
250 esac
251 shift
252done
253
254if [ "$OPTION_HELP" = "yes" ] ; then
255 echo "Usage: $PROGNAME [options]"
256 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700257 echo "Setup a gdb debugging session for your Android NDK application."
258 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
259 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700260 echo "Valid options:"
261 echo ""
262 echo " --help|-h|-? Print this help"
263 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700264 echo " --force Kill existing debug session if it exists"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700265 echo " --start Launch application instead of attaching to existing one"
266 echo " --launch=<name> Same as --start, but specify activity name (see below)"
267 echo " --launch-list List all launchable activity names from manifest"
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200268 echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700269 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700270 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700271 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700272 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
273 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700274 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
275 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700276 echo " -e Connect to single emulator instance"
277 echo " -d Connect to single target device"
278 echo " -s <serial> Connect to specific emulator or device"
279 echo ""
280 exit 0
281fi
282
283log "Android NDK installation path: $ANDROID_NDK_ROOT"
284
285if [ -n "$OPTION_EXEC" ] ; then
286 if [ ! -f "$OPTION_EXEC" ]; then
287 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
288 exit 1
289 fi
290fi
291
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200292if [ -n "$OPTION_DELAY" ] ; then
293 DELAY="$OPTION_DELAY"
294fi
295
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700296# Check ADB tool version
297if [ -n "$OPTION_ADB" ] ; then
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100298 ADB_CMD=$OPTION_ADB
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700299 log "Using specific adb command: $ADB_CMD"
300else
301 if [ -z "$ADB_CMD" ] ; then
302 echo "ERROR: The 'adb' tool is not in your path."
303 echo " You can change your PATH variable, or use"
304 echo " --adb=<executable> to point to a valid one."
305 exit 1
306 fi
307 log "Using default adb command: $ADB_CMD"
308fi
309
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100310ADB_CMD=$(quote_spaces $ADB_CMD)
311ADB_VERSION=$("$ADB_CMD" version 2>/dev/null)
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700312if [ $? != 0 ] ; then
313 echo "ERROR: Could not run ADB with: $ADB_CMD"
314 exit 1
315fi
316log "ADB version found: $ADB_VERSION"
317
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100318log "Using ADB flags: $ADB_FLAGS"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700319
Andrew Hsieh201486c2012-05-16 14:03:11 +0800320# Run an ADB command with the right ADB flags
321# $1+: adb command parameter
322adb_cmd ()
323{
324 # NOTE: We escape $ADB_CMD in case the command's path contains spaces.
325 "$ADB_CMD" $ADB_FLAGS "$@"
326}
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100327
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100328# Used internally by adb_var_shell and adb_var_shell2.
329# $1: 1 to redirect stderr to $1, 0 otherwise.
330# $2: Variable name that will contain the result
331# $3+: Command options
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100332_adb_var_shell ()
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700333{
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100334 # We need a temporary file to store the output of our command
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100335 local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
336 REDIRECT_STDERR=$1
337 VARNAME=$2
338 shift; shift;
339 CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100340 # Run the command, while storing the standard output to CMD_OUT
341 # and appending the exit code as the last line.
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100342 if [ "$REDIRECT_STDERR" != 0 ]; then
Andrew Hsieh201486c2012-05-16 14:03:11 +0800343 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100344 else
Andrew Hsieh201486c2012-05-16 14:03:11 +0800345 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100346 fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100347 # Get last line in log, which contains the exit code from the command
348 RET=`sed -e '$!d' $CMD_OUT`
349 # Get output, which corresponds to everything except the last line
350 OUT=`sed -e '$d' $CMD_OUT`
351 rm -f $CMD_OUT
352 eval $VARNAME=\"\$OUT\"
353 return $RET
354}
355
356# Run a command through 'adb shell' and captures its standard output
357# into a variable. The function's exit code is the same than the command's.
358#
359# This is required because there is a bug where "adb shell" always returns
360# 0 on the host, even if the command fails on the device.
361#
362# $1: Variable name (e.g. FOO)
363# On exit, $FOO is set to the command's standard output
364#
365# The return status will be 0 (success) if the command succeeded
366# or 1 (failure) otherwise.
367adb_var_shell ()
368{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200369 _adb_var_shell 0 "$@"
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100370}
371
372# A variant of adb_var_shell that stores both stdout and stderr in the output
373# $1: Variable name
374adb_var_shell2 ()
375{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200376 _adb_var_shell 1 "$@"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700377}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700378
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200379# Return the PID of a given package or program, or 0 if it doesn't run
380# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
381# Out: PID number, or 0 if not running
382get_pid_of ()
383{
Andrew Hsieh201486c2012-05-16 14:03:11 +0800384 adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200385}
386
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700387# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700388AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
389AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700390if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700391 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700392 exit 1
393fi
394if [ "$AWK_TEST" != "Pass" ] ; then
395 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
396 exit 1
397fi
398
399# Name of the manifest file
400MANIFEST=AndroidManifest.xml
401
402# Find the root of the application project.
403if [ -n "$OPTION_PROJECT" ] ; then
404 PROJECT=$OPTION_PROJECT
405 log "Using specified project path: $PROJECT"
406 if [ ! -d "$PROJECT" ] ; then
407 echo "ERROR: Your --project option does not point to a directory!"
408 exit 1
409 fi
410 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
411 echo "ERROR: Your --project does not point to an Android project path!"
412 echo " It is missing a $MANIFEST file."
413 exit 1
414 fi
415else
416 # Assume we are in the project directory
417 if [ -f "$MANIFEST" ] ; then
418 PROJECT=.
419 else
420 PROJECT=
421 CURDIR=`pwd`
422 while [ "$CURDIR" != "/" ] ; do
423 if [ -f "$CURDIR/$MANIFEST" ] ; then
424 PROJECT="$CURDIR"
425 break
426 fi
427 CURDIR=`dirname $CURDIR`
428 done
429 if [ -z "$PROJECT" ] ; then
430 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
431 exit 1
432 fi
433 fi
434 log "Using auto-detected project path: $PROJECT"
435fi
436
437# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700438PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700439log "Found package name: $PACKAGE_NAME"
440if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
441 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
442 echo " Please check that the file is well-formed!"
443 exit 1
444fi
445
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700446# If --launch-list is used, list all launchable activities, and be done with it
447if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
448 log "Extracting list of launchable activities from manifest:"
449 run_awk_manifest_script extract-launchable.awk
450 exit 0
451fi
452
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700453APP_ABIS=`get_build_var APP_ABI`
454log "ABIs targetted by application: $APP_ABIS"
455
456# Check the ADB command, and that we can connect to the device/emulator
Andrew Hsieh201486c2012-05-16 14:03:11 +0800457ADB_TEST=`adb_cmd shell ls`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700458if [ $? != 0 ] ; then
459 echo "ERROR: Could not connect to device or emulator!"
460 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700461 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700462 echo " in case of multiple ones."
463 exit 1
464fi
465
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700466# Check that the device is running Froyo (API Level 8) or higher
467#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100468adb_var_shell API_LEVEL getprop ro.build.version.sdk
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700469if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200470 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700471 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
472 exit 1
473fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700474log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700475if [ "$API_LEVEL" -lt "8" ] ; then
476 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 +0200477 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700478 exit 1
479fi
480
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700481# Get the target device's supported ABI(s)
482# And check that they are supported by the application
483#
484COMPAT_ABI=none
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100485adb_var_shell CPU_ABI getprop ro.product.cpu.abi
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700486for ABI in $APP_ABIS; do
487 if [ "$ABI" = "$CPU_ABI" ] ; then
488 COMPAT_ABI=$CPU_ABI
489 break
490 fi
491done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700492
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100493adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
494if [ $? != 0 -o -z "$CPU_ABI2" ] ; then
495 CPU_ABI2=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700496 log "Device CPU ABI: $CPU_ABI"
497else
498 log "Device CPU ABIs: $CPU_ABI $CPU_ABI2"
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700499 if [ "$COMPAT_ABI" = "none" ] ; then
500 for ABI in $APP_ABIS; do
501 if [ "$ABI" = "$CPU_ABI2" ] ; then
502 COMPAT_ABI=$CPU_ABI2
503 break
504 fi
505 done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700506 fi
507fi
508if [ "$COMPAT_ABI" = none ] ; then
509 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
510 if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then
511 CPU_ABI2=
512 fi
513 echo " Device supports: $CPU_ABI $CPU_ABI2"
514 echo " Package supports: $APP_ABIS"
515 exit 1
516fi
517log "Compatible device ABI: $COMPAT_ABI"
518
David 'Digit' Turnerff7cd042012-02-14 02:43:52 +0100519# Get information from the build system
520GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
521log "Using gdb setup init: $GDBSETUP_INIT"
522
523TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
524log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
525
526APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
527log "Using app out directory: $APP_OUT"
528
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200529# Check that the application is debuggable, or nothing will work
530DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
531log "Found debuggable flag: $DEBUGGABLE"
532if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
533 # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
534 # ok to not have android:debuggable set to true in the original manifest.
535 # However, if this is not the case, then complain!!
536 if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
537 log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
538 else
539 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
540 echo ""
541 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
542 echo ""
543 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
544 echo " then rebuild normally."
545 echo ""
546 echo "After one of these, re-install to the device!"
547 exit 1
548 fi
549else
550 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
551 # debuggable flag in the manifest without calling ndk-build afterwards.
552 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
553 echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
554 echo " This usually means you modified your AndroidManifest.xml to set"
555 echo " the android:debuggable flag to 'true' but did not rebuild the"
556 echo " native binaries. Please call 'ndk-build' to do so,"
557 echo " *then* re-install to the device!"
558 exit 1
559 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700560fi
561
562# Let's check that 'gdbserver' is properly installed on the device too. If this
563# is not the case, the user didn't install the proper package after rebuilding.
564#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100565adb_var_shell2 DEVICE_GDBSERVER ls /data/data/$PACKAGE_NAME/lib/gdbserver
566if [ $? != 0 ]; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700567 echo "ERROR: Non-debuggable application installed on the target device."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200568 echo " Please re-install the debuggable version!"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700569 exit 1
570fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100571log "Found device gdbserver: $DEVICE_GDBSERVER"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700572
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700573# Find the <dataDir> of the package on the device
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100574adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700575if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700576 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700577 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700578 exit 1
579fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100580log "Found data directory: '$DATA_DIR'"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700581
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700582# Launch the activity if needed
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200583if [ "$OPTION_START" = "yes" ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700584 # If --launch is used, ignore --start, otherwise extract the first
585 # launchable activity name from the manifest and use it as if --launch=<name>
586 # was used instead.
587 #
588 if [ -z "$OPTION_LAUNCH" ] ; then
589 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
590 if [ $? != 0 ] ; then
591 echo "ERROR: Could not extract name of launchable activity from manifest!"
592 echo " Try to use --launch=<name> directly instead as a work-around."
593 exit 1
594 fi
595 log "Found first launchable activity: $OPTION_LAUNCH"
596 if [ -z "$OPTION_LAUNCH" ] ; then
597 echo "ERROR: It seems that your Application does not have any launchable activity!"
598 echo " Please fix your manifest file and rebuild/re-install your application."
599 exit 1
600 fi
601 fi
602fi
603
604if [ -n "$OPTION_LAUNCH" ] ; then
605 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800606 run adb_cmd shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700607 if [ $? != 0 ] ; then
608 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
609 echo " Use --launch-list to dump a list of valid values."
610 exit 1
611 fi
612 # Sleep a bit, it sometimes take one second to start properly
613 # Note that we use the 'sleep' command on the device here.
Andrew Hsieh201486c2012-05-16 14:03:11 +0800614 run adb_cmd shell sleep $DELAY
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700615fi
616
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700617# Find the PID of the application being run
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200618PID=$(get_pid_of "$PACKAGE_NAME")
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700619log "Found running PID: $PID"
620if [ $? != 0 -o "$PID" = "0" ] ; then
621 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700622 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700623 echo " Weird, this probably means one of these:"
624 echo ""
625 echo " - The installed package does not match your current manifest."
626 echo " - The application process was terminated."
627 echo ""
628 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700629 else
630 echo " Are you sure the application is already started?"
631 echo " Consider using --start or --launch=<name> if not."
632 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700633 exit 1
634fi
635
636# Check that there is no other instance of gdbserver running
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200637GDBSERVER_PID=$(get_pid_of lib/gdbserver)
638if [ "$GDBSERVER_PID" != "0" ]; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700639 if [ "$OPTION_FORCE" = "no" ] ; then
640 echo "ERROR: Another debug session running, Use --force to kill it."
641 exit 1
642 fi
643 log "Killing existing debugging session"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800644 run adb_cmd shell kill -9 $GDBSERVER_PID
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700645fi
646
647# Launch gdbserver now
648DEBUG_SOCKET=debug-socket
Andrew Hsieh201486c2012-05-16 14:03:11 +0800649run adb_cmd shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700650if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700651 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700652 exit 1
653fi
654log "Launched gdbserver succesfully."
655
656# Setup network redirection
657log "Setup network redirection"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800658run adb_cmd forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700659if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700660 echo "ERROR: Could not setup network redirection to gdbserver?"
661 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700662 exit 1
663fi
664
665# Get the app_server binary from the device
666APP_PROCESS=$APP_OUT/app_process
Andrew Hsieh201486c2012-05-16 14:03:11 +0800667run adb_cmd pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200668log "Pulled app_process from device/emulator."
669
Andrew Hsieh201486c2012-05-16 14:03:11 +0800670run adb_cmd pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200671log "Pulled libc.so from device/emulator."
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700672
673# Now launch the appropriate gdb client with the right init commands
674#
675GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
676GDBSETUP=$APP_OUT/gdb.setup
677cp -f $GDBSETUP_INIT $GDBSETUP
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200678#uncomment the following to debug the remote connection only
679#echo "set debug remote 1" >> $GDBSETUP
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200680echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700681echo "target remote :$DEBUG_PORT" >> $GDBSETUP
682if [ -n "$OPTION_EXEC" ] ; then
683 cat $OPTION_EXEC >> $GDBSETUP
684fi
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200685$GDBCLIENT -x `native_path $GDBSETUP`