blob: efd96803662f5a864c0d8a35432b4d1b56eafda4 [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
31find_program ADB_CMD adb
32ADB_FLAGS=
33
34AWK_CMD=awk
35
36DEBUG_PORT=5039
37
David 'Digit' Turner5b656252010-10-08 00:43:32 +020038# Delay in seconds between launching the activity and attaching gdbserver on it.
39# This is needed because there is no way to know when the activity has really
40# started, and sometimes this takes a few seconds.
41DELAY=2
42
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070043PARAMETERS=
44OPTION_HELP=no
45OPTION_PROJECT=
46OPTION_FORCE=no
47OPTION_ADB=
48OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070049OPTION_START=no
50OPTION_LAUNCH=
51OPTION_LAUNCH_LIST=no
David 'Digit' Turner5b656252010-10-08 00:43:32 +020052OPTION_DELAY=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070053
54check_parameter ()
55{
56 if [ -z "$2" ]; then
57 echo "ERROR: Missing parameter after option '$1'"
58 exit 1
59 fi
60}
61
62check_adb_flags ()
63{
64 if [ -n "$ADB_FLAGS" ] ; then
65 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
66 exit 1
67 fi
68}
69
70get_build_var ()
71{
72 if [ -z "$GNUMAKE" ] ; then
73 GNUMAKE=make
74 fi
75 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1
76}
77
78get_build_var_for_abi ()
79{
80 if [ -z "$GNUMAKE" ] ; then
81 GNUMAKE=make
82 fi
83 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2
84}
85
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070086# Used to run an awk script on the manifest
87run_awk_manifest_script ()
88{
89 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
90}
91
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -070092if [ "$HOST_OS" = "cygwin" ] ; then
93# Return native path representation from cygwin one
94# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
95# Return: path in host windows representation, e.g. C:/some/thing
96#
97# We use mixed mode (i.e. / as the directory separator) because
98# all the tools we use recognize it properly, and it avoids lots
99# of escaping nonsense associated with "\"
100#
101native_path ()
102{
103 cygpath -m $1
104}
105else # HOST_OS != windows
106native_path ()
107{
108 echo "$1"
109}
110fi # HOST_OS != windows
111
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100112# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
113# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
114# might fail, e.g. when invoked with:
115#
116# cd $NDKROOT
117# ./ndk-gdb --project=/path/to/project
118#
119path_is_absolute ()
120{
121 local P P2
122 P=$1 # copy path
123 P2=${P#/} # remove / prefix, if any
124 [ "$P" != "$P2" ]
125}
126
127if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
128 ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
129fi
130
131
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700132VERBOSE=no
133while [ -n "$1" ]; do
134 opt="$1"
135 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
136 case "$opt" in
137 --help|-h|-\?)
138 OPTION_HELP=yes
139 ;;
140 --verbose)
141 VERBOSE=yes
142 ;;
143 -s)
144 check_parameter $1 $2
145 check_adb_flags
146 ADB_FLAGS=" -s $2"
147 shift
148 ;;
149 -s*)
150 check_adb_flags
151 optarg=`expr -- "$opt" : '-s\(.*\)'`
152 ADB_FLAGS=" -s $optarg"
153 ;;
154 -p)
155 check_parameter $1 $2
156 OPTION_PROJECT="$2"
157 shift
158 ;;
159 -p*)
160 optarg=`expr -- "$opt" : '-p\(.*\)'`
161 OPTION_PROJECT="$optarg"
162 ;;
163 --exec=*)
164 OPTION_EXEC="$optarg"
165 ;;
166 -x)
167 check_parameter $1 $2
168 OPTION_EXEC="$2"
169 shift
170 ;;
171 -x*)
172 optarg=`expr -- "$opt" : '-x\(.*\)'`
173 OPTION_EXEC="$optarg"
174 ;;
175 -e)
176 check_adb_flags
177 ADB_FLAGS=" -e"
178 ;;
179 -d)
180 check_adb_flags
181 ADB_FLAGS=" -d"
182 ;;
183 --adb=*) # specify ADB command
184 OPTION_ADB="$optarg"
185 ;;
186 --awk=*)
187 AWK_CMD="$optarg"
188 ;;
189 --project=*)
190 OPTION_PROJECT="$optarg"
191 ;;
192 --port=*)
193 DEBUG_PORT="$optarg"
194 ;;
195 --force)
196 OPTION_FORCE="yes"
197 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700198 --launch-list)
199 OPTION_LAUNCH_LIST="yes"
200 ;;
201 --launch=*)
202 OPTION_LAUNCH="$optarg"
203 ;;
204 --start)
205 OPTION_START=yes
206 ;;
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200207 --delay=*)
208 OPTION_DELAY="$optarg"
209 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700210 -*) # unknown options
211 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
212 exit 1
213 ;;
214 *) # Simply record parameter
215 if [ -z "$PARAMETERS" ] ; then
216 PARAMETERS="$opt"
217 else
218 PARAMETERS="$PARAMETERS $opt"
219 fi
220 ;;
221 esac
222 shift
223done
224
225if [ "$OPTION_HELP" = "yes" ] ; then
226 echo "Usage: $PROGNAME [options]"
227 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700228 echo "Setup a gdb debugging session for your Android NDK application."
229 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
230 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700231 echo "Valid options:"
232 echo ""
233 echo " --help|-h|-? Print this help"
234 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700235 echo " --force Kill existing debug session if it exists"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700236 echo " --start Launch application instead of attaching to existing one"
237 echo " --launch=<name> Same as --start, but specify activity name (see below)"
238 echo " --launch-list List all launchable activity names from manifest"
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200239 echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700240 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700241 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700242 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700243 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
244 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700245 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
246 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700247 echo " -e Connect to single emulator instance"
248 echo " -d Connect to single target device"
249 echo " -s <serial> Connect to specific emulator or device"
250 echo ""
251 exit 0
252fi
253
254log "Android NDK installation path: $ANDROID_NDK_ROOT"
255
256if [ -n "$OPTION_EXEC" ] ; then
257 if [ ! -f "$OPTION_EXEC" ]; then
258 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
259 exit 1
260 fi
261fi
262
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200263if [ -n "$OPTION_DELAY" ] ; then
264 DELAY="$OPTION_DELAY"
265fi
266
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700267# Check ADB tool version
268if [ -n "$OPTION_ADB" ] ; then
269 ADB_CMD="$OPTION_ADB"
270 log "Using specific adb command: $ADB_CMD"
271else
272 if [ -z "$ADB_CMD" ] ; then
273 echo "ERROR: The 'adb' tool is not in your path."
274 echo " You can change your PATH variable, or use"
275 echo " --adb=<executable> to point to a valid one."
276 exit 1
277 fi
278 log "Using default adb command: $ADB_CMD"
279fi
280
281ADB_VERSION=`$ADB_CMD version`
282if [ $? != 0 ] ; then
283 echo "ERROR: Could not run ADB with: $ADB_CMD"
284 exit 1
285fi
286log "ADB version found: $ADB_VERSION"
287
288ADB_CMD="${ADB_CMD}${ADB_FLAGS}"
289log "Using final ADB command: '$ADB_CMD'"
290
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100291
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100292# Used internally by adb_var_shell and adb_var_shell2.
293# $1: 1 to redirect stderr to $1, 0 otherwise.
294# $2: Variable name that will contain the result
295# $3+: Command options
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100296_adb_var_shell ()
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700297{
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100298 # We need a temporary file to store the output of our command
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100299 local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
300 REDIRECT_STDERR=$1
301 VARNAME=$2
302 shift; shift;
303 CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100304 # Run the command, while storing the standard output to CMD_OUT
305 # and appending the exit code as the last line.
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100306 if [ "$REDIRECT_STDERR" != 0 ]; then
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200307 $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100308 else
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200309 $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100310 fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100311 # Get last line in log, which contains the exit code from the command
312 RET=`sed -e '$!d' $CMD_OUT`
313 # Get output, which corresponds to everything except the last line
314 OUT=`sed -e '$d' $CMD_OUT`
315 rm -f $CMD_OUT
316 eval $VARNAME=\"\$OUT\"
317 return $RET
318}
319
320# Run a command through 'adb shell' and captures its standard output
321# into a variable. The function's exit code is the same than the command's.
322#
323# This is required because there is a bug where "adb shell" always returns
324# 0 on the host, even if the command fails on the device.
325#
326# $1: Variable name (e.g. FOO)
327# On exit, $FOO is set to the command's standard output
328#
329# The return status will be 0 (success) if the command succeeded
330# or 1 (failure) otherwise.
331adb_var_shell ()
332{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200333 _adb_var_shell 0 "$@"
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100334}
335
336# A variant of adb_var_shell that stores both stdout and stderr in the output
337# $1: Variable name
338adb_var_shell2 ()
339{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200340 _adb_var_shell 1 "$@"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700341}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700342
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200343# Return the PID of a given package or program, or 0 if it doesn't run
344# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
345# Out: PID number, or 0 if not running
346get_pid_of ()
347{
348 $ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
349}
350
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700351# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700352AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
353AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700354if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700355 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700356 exit 1
357fi
358if [ "$AWK_TEST" != "Pass" ] ; then
359 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
360 exit 1
361fi
362
363# Name of the manifest file
364MANIFEST=AndroidManifest.xml
365
366# Find the root of the application project.
367if [ -n "$OPTION_PROJECT" ] ; then
368 PROJECT=$OPTION_PROJECT
369 log "Using specified project path: $PROJECT"
370 if [ ! -d "$PROJECT" ] ; then
371 echo "ERROR: Your --project option does not point to a directory!"
372 exit 1
373 fi
374 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
375 echo "ERROR: Your --project does not point to an Android project path!"
376 echo " It is missing a $MANIFEST file."
377 exit 1
378 fi
379else
380 # Assume we are in the project directory
381 if [ -f "$MANIFEST" ] ; then
382 PROJECT=.
383 else
384 PROJECT=
385 CURDIR=`pwd`
386 while [ "$CURDIR" != "/" ] ; do
387 if [ -f "$CURDIR/$MANIFEST" ] ; then
388 PROJECT="$CURDIR"
389 break
390 fi
391 CURDIR=`dirname $CURDIR`
392 done
393 if [ -z "$PROJECT" ] ; then
394 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
395 exit 1
396 fi
397 fi
398 log "Using auto-detected project path: $PROJECT"
399fi
400
401# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700402PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700403log "Found package name: $PACKAGE_NAME"
404if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
405 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
406 echo " Please check that the file is well-formed!"
407 exit 1
408fi
409
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700410# If --launch-list is used, list all launchable activities, and be done with it
411if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
412 log "Extracting list of launchable activities from manifest:"
413 run_awk_manifest_script extract-launchable.awk
414 exit 0
415fi
416
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700417APP_ABIS=`get_build_var APP_ABI`
418log "ABIs targetted by application: $APP_ABIS"
419
420# Check the ADB command, and that we can connect to the device/emulator
421ADB_TEST=`$ADB_CMD shell ls`
422if [ $? != 0 ] ; then
423 echo "ERROR: Could not connect to device or emulator!"
424 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700425 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700426 echo " in case of multiple ones."
427 exit 1
428fi
429
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700430# Check that the device is running Froyo (API Level 8) or higher
431#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100432adb_var_shell API_LEVEL getprop ro.build.version.sdk
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700433if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200434 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700435 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
436 exit 1
437fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700438log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700439if [ "$API_LEVEL" -lt "8" ] ; then
440 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 +0200441 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700442 exit 1
443fi
444
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700445# Get the target device's supported ABI(s)
446# And check that they are supported by the application
447#
448COMPAT_ABI=none
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100449adb_var_shell CPU_ABI getprop ro.product.cpu.abi
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700450for ABI in $APP_ABIS; do
451 if [ "$ABI" = "$CPU_ABI" ] ; then
452 COMPAT_ABI=$CPU_ABI
453 break
454 fi
455done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700456
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100457adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
458if [ $? != 0 -o -z "$CPU_ABI2" ] ; then
459 CPU_ABI2=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700460 log "Device CPU ABI: $CPU_ABI"
461else
462 log "Device CPU ABIs: $CPU_ABI $CPU_ABI2"
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700463 if [ "$COMPAT_ABI" = "none" ] ; then
464 for ABI in $APP_ABIS; do
465 if [ "$ABI" = "$CPU_ABI2" ] ; then
466 COMPAT_ABI=$CPU_ABI2
467 break
468 fi
469 done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700470 fi
471fi
472if [ "$COMPAT_ABI" = none ] ; then
473 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
474 if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then
475 CPU_ABI2=
476 fi
477 echo " Device supports: $CPU_ABI $CPU_ABI2"
478 echo " Package supports: $APP_ABIS"
479 exit 1
480fi
481log "Compatible device ABI: $COMPAT_ABI"
482
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200483# Check that the application is debuggable, or nothing will work
484DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
485log "Found debuggable flag: $DEBUGGABLE"
486if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
487 # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
488 # ok to not have android:debuggable set to true in the original manifest.
489 # However, if this is not the case, then complain!!
490 if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
491 log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
492 else
493 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
494 echo ""
495 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
496 echo ""
497 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
498 echo " then rebuild normally."
499 echo ""
500 echo "After one of these, re-install to the device!"
501 exit 1
502 fi
503else
504 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
505 # debuggable flag in the manifest without calling ndk-build afterwards.
506 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
507 echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
508 echo " This usually means you modified your AndroidManifest.xml to set"
509 echo " the android:debuggable flag to 'true' but did not rebuild the"
510 echo " native binaries. Please call 'ndk-build' to do so,"
511 echo " *then* re-install to the device!"
512 exit 1
513 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700514fi
515
516# Let's check that 'gdbserver' is properly installed on the device too. If this
517# is not the case, the user didn't install the proper package after rebuilding.
518#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100519adb_var_shell2 DEVICE_GDBSERVER ls /data/data/$PACKAGE_NAME/lib/gdbserver
520if [ $? != 0 ]; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700521 echo "ERROR: Non-debuggable application installed on the target device."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200522 echo " Please re-install the debuggable version!"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700523 exit 1
524fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100525log "Found device gdbserver: $DEVICE_GDBSERVER"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700526
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700527# Get information from the build system
528GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
529log "Using gdb setup init: $GDBSETUP_INIT"
530
531TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
532log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
533
534APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
535log "Using app out directory: $APP_OUT"
536
537# Find the <dataDir> of the package on the device
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100538adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700539if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700540 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700541 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700542 exit 1
543fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100544log "Found data directory: '$DATA_DIR'"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700545
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700546# Launch the activity if needed
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200547if [ "$OPTION_START" = "yes" ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700548 # If --launch is used, ignore --start, otherwise extract the first
549 # launchable activity name from the manifest and use it as if --launch=<name>
550 # was used instead.
551 #
552 if [ -z "$OPTION_LAUNCH" ] ; then
553 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
554 if [ $? != 0 ] ; then
555 echo "ERROR: Could not extract name of launchable activity from manifest!"
556 echo " Try to use --launch=<name> directly instead as a work-around."
557 exit 1
558 fi
559 log "Found first launchable activity: $OPTION_LAUNCH"
560 if [ -z "$OPTION_LAUNCH" ] ; then
561 echo "ERROR: It seems that your Application does not have any launchable activity!"
562 echo " Please fix your manifest file and rebuild/re-install your application."
563 exit 1
564 fi
565 fi
566fi
567
568if [ -n "$OPTION_LAUNCH" ] ; then
569 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
570 run $ADB_CMD shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
571 if [ $? != 0 ] ; then
572 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
573 echo " Use --launch-list to dump a list of valid values."
574 exit 1
575 fi
576 # Sleep a bit, it sometimes take one second to start properly
577 # Note that we use the 'sleep' command on the device here.
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200578 run $ADB_CMD shell sleep $DELAY
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700579fi
580
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700581# Find the PID of the application being run
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200582PID=$(get_pid_of "$PACKAGE_NAME")
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700583log "Found running PID: $PID"
584if [ $? != 0 -o "$PID" = "0" ] ; then
585 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700586 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700587 echo " Weird, this probably means one of these:"
588 echo ""
589 echo " - The installed package does not match your current manifest."
590 echo " - The application process was terminated."
591 echo ""
592 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700593 else
594 echo " Are you sure the application is already started?"
595 echo " Consider using --start or --launch=<name> if not."
596 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700597 exit 1
598fi
599
600# Check that there is no other instance of gdbserver running
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200601GDBSERVER_PID=$(get_pid_of lib/gdbserver)
602if [ "$GDBSERVER_PID" != "0" ]; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700603 if [ "$OPTION_FORCE" = "no" ] ; then
604 echo "ERROR: Another debug session running, Use --force to kill it."
605 exit 1
606 fi
607 log "Killing existing debugging session"
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200608 run $ADB_CMD shell kill -9 $GDBSERVER_PID
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700609fi
610
611# Launch gdbserver now
612DEBUG_SOCKET=debug-socket
613run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
614if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700615 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700616 exit 1
617fi
618log "Launched gdbserver succesfully."
619
620# Setup network redirection
621log "Setup network redirection"
622run $ADB_CMD forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
623if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700624 echo "ERROR: Could not setup network redirection to gdbserver?"
625 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700626 exit 1
627fi
628
629# Get the app_server binary from the device
630APP_PROCESS=$APP_OUT/app_process
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700631run $ADB_CMD pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200632log "Pulled app_process from device/emulator."
633
634run $ADB_CMD pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
635log "Pulled libc.so from device/emulator."
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700636
637# Now launch the appropriate gdb client with the right init commands
638#
639GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
640GDBSETUP=$APP_OUT/gdb.setup
641cp -f $GDBSETUP_INIT $GDBSETUP
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200642#uncomment the following to debug the remote connection only
643#echo "set debug remote 1" >> $GDBSETUP
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200644echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700645echo "target remote :$DEBUG_PORT" >> $GDBSETUP
646if [ -n "$OPTION_EXEC" ] ; then
647 cat $OPTION_EXEC >> $GDBSETUP
648fi
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200649$GDBCLIENT -x `native_path $GDBSETUP`