blob: a51decb83109e7b68f4666d4c6f04e33a96b6c95 [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:-}
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -070062DEVICE_SERIAL=
David 'Digit' Turnerdb092432011-11-02 12:44:20 +010063
64AWK_CMD=${AWK_CMD:-$(find_program awk)}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070065
66DEBUG_PORT=5039
67
David 'Digit' Turner5b656252010-10-08 00:43:32 +020068# Delay in seconds between launching the activity and attaching gdbserver on it.
69# This is needed because there is no way to know when the activity has really
70# started, and sometimes this takes a few seconds.
71DELAY=2
72
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070073PARAMETERS=
74OPTION_HELP=no
75OPTION_PROJECT=
76OPTION_FORCE=no
77OPTION_ADB=
78OPTION_EXEC=
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -070079OPTION_START=no
80OPTION_LAUNCH=
81OPTION_LAUNCH_LIST=no
David 'Digit' Turner5b656252010-10-08 00:43:32 +020082OPTION_DELAY=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -070083
84check_parameter ()
85{
86 if [ -z "$2" ]; then
87 echo "ERROR: Missing parameter after option '$1'"
88 exit 1
89 fi
90}
91
92check_adb_flags ()
93{
94 if [ -n "$ADB_FLAGS" ] ; then
95 echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
96 exit 1
97 fi
98}
99
100get_build_var ()
101{
102 if [ -z "$GNUMAKE" ] ; then
103 GNUMAKE=make
104 fi
105 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1
106}
107
108get_build_var_for_abi ()
109{
110 if [ -z "$GNUMAKE" ] ; then
111 GNUMAKE=make
112 fi
113 $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2
114}
115
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700116# Used to run an awk script on the manifest
117run_awk_manifest_script ()
118{
119 $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
120}
121
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700122if [ "$HOST_OS" = "cygwin" ] ; then
123# Return native path representation from cygwin one
124# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
125# Return: path in host windows representation, e.g. C:/some/thing
126#
127# We use mixed mode (i.e. / as the directory separator) because
128# all the tools we use recognize it properly, and it avoids lots
129# of escaping nonsense associated with "\"
130#
131native_path ()
132{
133 cygpath -m $1
134}
135else # HOST_OS != windows
136native_path ()
137{
138 echo "$1"
139}
140fi # HOST_OS != windows
141
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100142# We need to ensure the ANDROID_NDK_ROOT is absolute, otherwise calls
143# to get_build_var, get_build_var_for_abi and run_awk_manifest_script
144# might fail, e.g. when invoked with:
145#
146# cd $NDKROOT
147# ./ndk-gdb --project=/path/to/project
148#
149path_is_absolute ()
150{
151 local P P2
152 P=$1 # copy path
153 P2=${P#/} # remove / prefix, if any
154 [ "$P" != "$P2" ]
155}
156
157if ! path_is_absolute "$ANDROID_NDK_ROOT"; then
158 ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT
159fi
160
161
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700162VERBOSE=no
163while [ -n "$1" ]; do
164 opt="$1"
165 optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
166 case "$opt" in
167 --help|-h|-\?)
168 OPTION_HELP=yes
169 ;;
170 --verbose)
171 VERBOSE=yes
172 ;;
173 -s)
174 check_parameter $1 $2
175 check_adb_flags
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700176 ADB_FLAGS=" -s"
177 DEVICE_SERIAL=$2
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700178 shift
179 ;;
180 -s*)
181 check_adb_flags
182 optarg=`expr -- "$opt" : '-s\(.*\)'`
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700183 ADB_FLAGS=" -s"
184 DEVICE_SERIAL=$optarg
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700185 ;;
186 -p)
187 check_parameter $1 $2
188 OPTION_PROJECT="$2"
189 shift
190 ;;
191 -p*)
192 optarg=`expr -- "$opt" : '-p\(.*\)'`
193 OPTION_PROJECT="$optarg"
194 ;;
195 --exec=*)
196 OPTION_EXEC="$optarg"
197 ;;
198 -x)
199 check_parameter $1 $2
200 OPTION_EXEC="$2"
201 shift
202 ;;
203 -x*)
204 optarg=`expr -- "$opt" : '-x\(.*\)'`
205 OPTION_EXEC="$optarg"
206 ;;
207 -e)
208 check_adb_flags
209 ADB_FLAGS=" -e"
210 ;;
211 -d)
212 check_adb_flags
213 ADB_FLAGS=" -d"
214 ;;
215 --adb=*) # specify ADB command
216 OPTION_ADB="$optarg"
217 ;;
218 --awk=*)
219 AWK_CMD="$optarg"
220 ;;
221 --project=*)
222 OPTION_PROJECT="$optarg"
223 ;;
224 --port=*)
225 DEBUG_PORT="$optarg"
226 ;;
227 --force)
228 OPTION_FORCE="yes"
229 ;;
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700230 --launch-list)
231 OPTION_LAUNCH_LIST="yes"
232 ;;
233 --launch=*)
234 OPTION_LAUNCH="$optarg"
235 ;;
236 --start)
237 OPTION_START=yes
238 ;;
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200239 --delay=*)
240 OPTION_DELAY="$optarg"
241 ;;
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700242 -*) # unknown options
243 echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
244 exit 1
245 ;;
246 *) # Simply record parameter
247 if [ -z "$PARAMETERS" ] ; then
248 PARAMETERS="$opt"
249 else
250 PARAMETERS="$PARAMETERS $opt"
251 fi
252 ;;
253 esac
254 shift
255done
256
257if [ "$OPTION_HELP" = "yes" ] ; then
258 echo "Usage: $PROGNAME [options]"
259 echo ""
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700260 echo "Setup a gdb debugging session for your Android NDK application."
261 echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
262 echo ""
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700263 echo "Valid options:"
264 echo ""
265 echo " --help|-h|-? Print this help"
266 echo " --verbose Enable verbose mode"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700267 echo " --force Kill existing debug session if it exists"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700268 echo " --start Launch application instead of attaching to existing one"
269 echo " --launch=<name> Same as --start, but specify activity name (see below)"
270 echo " --launch-list List all launchable activity names from manifest"
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200271 echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700272 echo " --project=<path> Specify application project path"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700273 echo " -p <path> Same as --project=<path>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700274 echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700275 echo " --exec=<file> Execute gdb initialization commands in <file> after connection"
276 echo " -x <file> Same as --exec=<file>"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700277 echo " --adb=<file> Use specific adb command [$ADB_CMD]"
278 echo " --awk=<file> Use specific awk command [$AWK_CMD]"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700279 echo " -e Connect to single emulator instance"
280 echo " -d Connect to single target device"
281 echo " -s <serial> Connect to specific emulator or device"
282 echo ""
283 exit 0
284fi
285
286log "Android NDK installation path: $ANDROID_NDK_ROOT"
287
288if [ -n "$OPTION_EXEC" ] ; then
289 if [ ! -f "$OPTION_EXEC" ]; then
290 echo "ERROR: Invalid initialization file: $OPTION_EXEC"
291 exit 1
292 fi
293fi
294
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200295if [ -n "$OPTION_DELAY" ] ; then
296 DELAY="$OPTION_DELAY"
297fi
298
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700299# Check ADB tool version
300if [ -n "$OPTION_ADB" ] ; then
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100301 ADB_CMD=$OPTION_ADB
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700302 log "Using specific adb command: $ADB_CMD"
303else
304 if [ -z "$ADB_CMD" ] ; then
305 echo "ERROR: The 'adb' tool is not in your path."
306 echo " You can change your PATH variable, or use"
307 echo " --adb=<executable> to point to a valid one."
308 exit 1
309 fi
310 log "Using default adb command: $ADB_CMD"
311fi
312
David 'Digit' Turnerdb092432011-11-02 12:44:20 +0100313ADB_CMD=$(quote_spaces $ADB_CMD)
314ADB_VERSION=$("$ADB_CMD" version 2>/dev/null)
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700315if [ $? != 0 ] ; then
316 echo "ERROR: Could not run ADB with: $ADB_CMD"
317 exit 1
318fi
319log "ADB version found: $ADB_VERSION"
320
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700321if [ "x$DEVICE_SERIAL" = "x" ]; then
322 log "Using ADB flags: $ADB_FLAGS"
323else
324 log "Using ADB flags: $ADB_FLAGS" \"$DEVICE_SERIAL\"
325fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700326
Andrew Hsieh201486c2012-05-16 14:03:11 +0800327# Run an ADB command with the right ADB flags
328# $1+: adb command parameter
329adb_cmd ()
330{
Andrew Hsieh20f4e7e2012-06-29 14:57:28 -0700331 if [ "x$DEVICE_SERIAL" = "x" ]; then
332 "$ADB_CMD" $ADB_FLAGS "$@"
333 else
334 # NOTE: We escape $ADB_CMD and $DEVICE_SERIAL in case they contains spaces.
335 "$ADB_CMD" $ADB_FLAGS "$DEVICE_SERIAL" "$@"
336 fi
Andrew Hsieh201486c2012-05-16 14:03:11 +0800337}
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100338
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100339# Used internally by adb_var_shell and adb_var_shell2.
340# $1: 1 to redirect stderr to $1, 0 otherwise.
341# $2: Variable name that will contain the result
342# $3+: Command options
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100343_adb_var_shell ()
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700344{
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100345 # We need a temporary file to store the output of our command
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100346 local CMD_OUT RET OUTPUT VARNAME REDIRECT_STDERR
347 REDIRECT_STDERR=$1
348 VARNAME=$2
349 shift; shift;
350 CMD_OUT=`mktemp /tmp/ndk-gdb-cmdout-XXXXXX`
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100351 # Run the command, while storing the standard output to CMD_OUT
352 # and appending the exit code as the last line.
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100353 if [ "$REDIRECT_STDERR" != 0 ]; then
Andrew Hsieh201486c2012-05-16 14:03:11 +0800354 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100355 else
Andrew Hsieh201486c2012-05-16 14:03:11 +0800356 adb_cmd shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT
David 'Digit' Turnerb2e3ee72011-03-23 15:03:31 +0100357 fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100358 # Get last line in log, which contains the exit code from the command
359 RET=`sed -e '$!d' $CMD_OUT`
360 # Get output, which corresponds to everything except the last line
361 OUT=`sed -e '$d' $CMD_OUT`
362 rm -f $CMD_OUT
363 eval $VARNAME=\"\$OUT\"
364 return $RET
365}
366
367# Run a command through 'adb shell' and captures its standard output
368# into a variable. The function's exit code is the same than the command's.
369#
370# This is required because there is a bug where "adb shell" always returns
371# 0 on the host, even if the command fails on the device.
372#
373# $1: Variable name (e.g. FOO)
374# On exit, $FOO is set to the command's standard output
375#
376# The return status will be 0 (success) if the command succeeded
377# or 1 (failure) otherwise.
378adb_var_shell ()
379{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200380 _adb_var_shell 0 "$@"
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100381}
382
383# A variant of adb_var_shell that stores both stdout and stderr in the output
384# $1: Variable name
385adb_var_shell2 ()
386{
David 'Digit' Turnerec2b8dd2011-10-27 14:43:25 +0200387 _adb_var_shell 1 "$@"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700388}
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700389
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200390# Return the PID of a given package or program, or 0 if it doesn't run
391# $1: Package name ("com.example.hellojni") or program name ("/lib/gdbserver")
392# Out: PID number, or 0 if not running
393get_pid_of ()
394{
Andrew Hsieh201486c2012-05-16 14:03:11 +0800395 adb_cmd shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1"
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200396}
397
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700398# Check the awk tool
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700399AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
400AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700401if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700402 echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700403 exit 1
404fi
405if [ "$AWK_TEST" != "Pass" ] ; then
406 echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
407 exit 1
408fi
409
410# Name of the manifest file
411MANIFEST=AndroidManifest.xml
412
413# Find the root of the application project.
414if [ -n "$OPTION_PROJECT" ] ; then
415 PROJECT=$OPTION_PROJECT
416 log "Using specified project path: $PROJECT"
417 if [ ! -d "$PROJECT" ] ; then
418 echo "ERROR: Your --project option does not point to a directory!"
419 exit 1
420 fi
421 if [ ! -f "$PROJECT/$MANIFEST" ] ; then
422 echo "ERROR: Your --project does not point to an Android project path!"
423 echo " It is missing a $MANIFEST file."
424 exit 1
425 fi
426else
427 # Assume we are in the project directory
428 if [ -f "$MANIFEST" ] ; then
429 PROJECT=.
430 else
431 PROJECT=
432 CURDIR=`pwd`
433 while [ "$CURDIR" != "/" ] ; do
434 if [ -f "$CURDIR/$MANIFEST" ] ; then
435 PROJECT="$CURDIR"
436 break
437 fi
438 CURDIR=`dirname $CURDIR`
439 done
440 if [ -z "$PROJECT" ] ; then
441 echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
442 exit 1
443 fi
444 fi
445 log "Using auto-detected project path: $PROJECT"
446fi
447
448# Extract the package name from the manifest
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700449PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700450log "Found package name: $PACKAGE_NAME"
451if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
452 echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
453 echo " Please check that the file is well-formed!"
454 exit 1
455fi
456
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700457# If --launch-list is used, list all launchable activities, and be done with it
458if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
459 log "Extracting list of launchable activities from manifest:"
460 run_awk_manifest_script extract-launchable.awk
461 exit 0
462fi
463
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700464APP_ABIS=`get_build_var APP_ABI`
465log "ABIs targetted by application: $APP_ABIS"
466
467# Check the ADB command, and that we can connect to the device/emulator
Andrew Hsieh201486c2012-05-16 14:03:11 +0800468ADB_TEST=`adb_cmd shell ls`
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700469if [ $? != 0 ] ; then
470 echo "ERROR: Could not connect to device or emulator!"
471 echo " Please check that an emulator is running or a device is connected"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700472 echo " through USB to this machine. You can use -e, -d and -s <serial>"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700473 echo " in case of multiple ones."
474 exit 1
475fi
476
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700477# Check that the device is running Froyo (API Level 8) or higher
478#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100479adb_var_shell API_LEVEL getprop ro.build.version.sdk
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700480if [ $? != 0 -o -z "$API_LEVEL" ] ; then
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200481 echo "ERROR: Could not find target device's supported API level!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700482 echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
483 exit 1
484fi
David 'Digit' Turner54be4862010-06-03 11:57:49 -0700485log "Device API Level: $API_LEVEL"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700486if [ "$API_LEVEL" -lt "8" ] ; then
487 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 +0200488 echo "The target device is running API level $API_LEVEL!"
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700489 exit 1
490fi
491
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700492# Get the target device's supported ABI(s)
493# And check that they are supported by the application
494#
495COMPAT_ABI=none
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100496adb_var_shell CPU_ABI getprop ro.product.cpu.abi
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700497for ABI in $APP_ABIS; do
498 if [ "$ABI" = "$CPU_ABI" ] ; then
499 COMPAT_ABI=$CPU_ABI
500 break
501 fi
502done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700503
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100504adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2
505if [ $? != 0 -o -z "$CPU_ABI2" ] ; then
506 CPU_ABI2=
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700507 log "Device CPU ABI: $CPU_ABI"
508else
509 log "Device CPU ABIs: $CPU_ABI $CPU_ABI2"
David 'Digit' Turner47eff9f2010-06-03 11:57:49 -0700510 if [ "$COMPAT_ABI" = "none" ] ; then
511 for ABI in $APP_ABIS; do
512 if [ "$ABI" = "$CPU_ABI2" ] ; then
513 COMPAT_ABI=$CPU_ABI2
514 break
515 fi
516 done
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700517 fi
518fi
519if [ "$COMPAT_ABI" = none ] ; then
520 echo "ERROR: The device does not support the application's targetted CPU ABIs!"
521 if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then
522 CPU_ABI2=
523 fi
524 echo " Device supports: $CPU_ABI $CPU_ABI2"
525 echo " Package supports: $APP_ABIS"
526 exit 1
527fi
528log "Compatible device ABI: $COMPAT_ABI"
529
David 'Digit' Turnerff7cd042012-02-14 02:43:52 +0100530# Get information from the build system
531GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
532log "Using gdb setup init: $GDBSETUP_INIT"
533
534TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
535log "Using toolchain prefix: $TOOLCHAIN_PREFIX"
536
537APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
538log "Using app out directory: $APP_OUT"
539
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200540# Check that the application is debuggable, or nothing will work
541DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
542log "Found debuggable flag: $DEBUGGABLE"
543if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
544 # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
545 # ok to not have android:debuggable set to true in the original manifest.
546 # However, if this is not the case, then complain!!
547 if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
548 log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
549 else
550 echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
551 echo ""
552 echo " - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
553 echo ""
554 echo " - Modify your manifest to set android:debuggable attribute to \"true\","
555 echo " then rebuild normally."
556 echo ""
557 echo "After one of these, re-install to the device!"
558 exit 1
559 fi
560else
561 # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
562 # debuggable flag in the manifest without calling ndk-build afterwards.
563 if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
564 echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
565 echo " This usually means you modified your AndroidManifest.xml to set"
566 echo " the android:debuggable flag to 'true' but did not rebuild the"
567 echo " native binaries. Please call 'ndk-build' to do so,"
568 echo " *then* re-install to the device!"
569 exit 1
570 fi
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700571fi
572
573# Let's check that 'gdbserver' is properly installed on the device too. If this
574# is not the case, the user didn't install the proper package after rebuilding.
575#
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100576adb_var_shell2 DEVICE_GDBSERVER ls /data/data/$PACKAGE_NAME/lib/gdbserver
577if [ $? != 0 ]; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700578 echo "ERROR: Non-debuggable application installed on the target device."
David 'Digit' Turnerfd204372010-09-14 15:10:38 +0200579 echo " Please re-install the debuggable version!"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700580 exit 1
581fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100582log "Found device gdbserver: $DEVICE_GDBSERVER"
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700583
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700584# Find the <dataDir> of the package on the device
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100585adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd
David 'Digit' Turner4cff6502010-06-01 14:40:11 -0700586if [ $? != 0 -o -z "$DATA_DIR" ] ; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700587 echo "ERROR: Could not extract package's data directory. Are you sure that"
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700588 echo " your installed application is debuggable?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700589 exit 1
590fi
David 'Digit' Turnere3cfafb2011-03-15 15:43:12 +0100591log "Found data directory: '$DATA_DIR'"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700592
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700593# Launch the activity if needed
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200594if [ "$OPTION_START" = "yes" ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700595 # If --launch is used, ignore --start, otherwise extract the first
596 # launchable activity name from the manifest and use it as if --launch=<name>
597 # was used instead.
598 #
599 if [ -z "$OPTION_LAUNCH" ] ; then
600 OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
601 if [ $? != 0 ] ; then
602 echo "ERROR: Could not extract name of launchable activity from manifest!"
603 echo " Try to use --launch=<name> directly instead as a work-around."
604 exit 1
605 fi
606 log "Found first launchable activity: $OPTION_LAUNCH"
607 if [ -z "$OPTION_LAUNCH" ] ; then
608 echo "ERROR: It seems that your Application does not have any launchable activity!"
609 echo " Please fix your manifest file and rebuild/re-install your application."
610 exit 1
611 fi
612 fi
613fi
614
615if [ -n "$OPTION_LAUNCH" ] ; then
616 log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800617 run adb_cmd shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700618 if [ $? != 0 ] ; then
619 echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
620 echo " Use --launch-list to dump a list of valid values."
621 exit 1
622 fi
623 # Sleep a bit, it sometimes take one second to start properly
624 # Note that we use the 'sleep' command on the device here.
Andrew Hsieh201486c2012-05-16 14:03:11 +0800625 run adb_cmd shell sleep $DELAY
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700626fi
627
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700628# Find the PID of the application being run
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200629PID=$(get_pid_of "$PACKAGE_NAME")
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700630log "Found running PID: $PID"
631if [ $? != 0 -o "$PID" = "0" ] ; then
632 echo "ERROR: Could not extract PID of application on device/emulator."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700633 if [ -n "$OPTION_LAUNCH" ] ; then
David 'Digit' Turner2e063fe2010-05-06 15:31:34 -0700634 echo " Weird, this probably means one of these:"
635 echo ""
636 echo " - The installed package does not match your current manifest."
637 echo " - The application process was terminated."
638 echo ""
639 echo " Try using the --verbose option and look at its output for details."
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700640 else
641 echo " Are you sure the application is already started?"
642 echo " Consider using --start or --launch=<name> if not."
643 fi
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700644 exit 1
645fi
646
647# Check that there is no other instance of gdbserver running
David 'Digit' Turner9c10d882011-10-27 13:10:58 +0200648GDBSERVER_PID=$(get_pid_of lib/gdbserver)
649if [ "$GDBSERVER_PID" != "0" ]; then
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700650 if [ "$OPTION_FORCE" = "no" ] ; then
651 echo "ERROR: Another debug session running, Use --force to kill it."
652 exit 1
653 fi
654 log "Killing existing debugging session"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800655 run adb_cmd shell kill -9 $GDBSERVER_PID
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700656fi
657
658# Launch gdbserver now
659DEBUG_SOCKET=debug-socket
Andrew Hsieh201486c2012-05-16 14:03:11 +0800660run adb_cmd shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700661if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700662 echo "ERROR: Could not launch gdbserver on the device?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700663 exit 1
664fi
665log "Launched gdbserver succesfully."
666
667# Setup network redirection
668log "Setup network redirection"
Andrew Hsieh201486c2012-05-16 14:03:11 +0800669run adb_cmd forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700670if [ $? != 0 ] ; then
David 'Digit' Turner0b2676b2010-04-27 12:33:46 -0700671 echo "ERROR: Could not setup network redirection to gdbserver?"
672 echo " Maybe using --port=<port> to use a different TCP port might help?"
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700673 exit 1
674fi
675
676# Get the app_server binary from the device
677APP_PROCESS=$APP_OUT/app_process
Andrew Hsieh201486c2012-05-16 14:03:11 +0800678run adb_cmd pull /system/bin/app_process `native_path $APP_PROCESS`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200679log "Pulled app_process from device/emulator."
680
Andrew Hsieh201486c2012-05-16 14:03:11 +0800681run adb_cmd pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200682log "Pulled libc.so from device/emulator."
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700683
684# Now launch the appropriate gdb client with the right init commands
685#
686GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
687GDBSETUP=$APP_OUT/gdb.setup
688cp -f $GDBSETUP_INIT $GDBSETUP
David 'Digit' Turner5b656252010-10-08 00:43:32 +0200689#uncomment the following to debug the remote connection only
690#echo "set debug remote 1" >> $GDBSETUP
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200691echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
David 'Digit' Turnera08d6052010-04-16 12:45:33 -0700692echo "target remote :$DEBUG_PORT" >> $GDBSETUP
693if [ -n "$OPTION_EXEC" ] ; then
694 cat $OPTION_EXEC >> $GDBSETUP
695fi
David 'Digit' Turnercaf06192010-10-18 12:39:51 +0200696$GDBCLIENT -x `native_path $GDBSETUP`