David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 1 | #!/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 | |
| 29 | force_32bit_binaries |
| 30 | |
| 31 | find_program ADB_CMD adb |
| 32 | ADB_FLAGS= |
| 33 | |
| 34 | AWK_CMD=awk |
| 35 | |
| 36 | DEBUG_PORT=5039 |
| 37 | |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 38 | # 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. |
| 41 | DELAY=2 |
| 42 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 43 | PARAMETERS= |
| 44 | OPTION_HELP=no |
| 45 | OPTION_PROJECT= |
| 46 | OPTION_FORCE=no |
| 47 | OPTION_ADB= |
| 48 | OPTION_EXEC= |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 49 | OPTION_START=no |
| 50 | OPTION_LAUNCH= |
| 51 | OPTION_LAUNCH_LIST=no |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 52 | OPTION_DELAY= |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 53 | |
| 54 | check_parameter () |
| 55 | { |
| 56 | if [ -z "$2" ]; then |
| 57 | echo "ERROR: Missing parameter after option '$1'" |
| 58 | exit 1 |
| 59 | fi |
| 60 | } |
| 61 | |
| 62 | check_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 | |
| 70 | get_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 | |
| 78 | get_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' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 86 | # Used to run an awk script on the manifest |
| 87 | run_awk_manifest_script () |
| 88 | { |
| 89 | $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST |
| 90 | } |
| 91 | |
David 'Digit' Turner | 47eff9f | 2010-06-03 11:57:49 -0700 | [diff] [blame] | 92 | if [ "$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 | # |
| 101 | native_path () |
| 102 | { |
| 103 | cygpath -m $1 |
| 104 | } |
| 105 | else # HOST_OS != windows |
| 106 | native_path () |
| 107 | { |
| 108 | echo "$1" |
| 109 | } |
| 110 | fi # HOST_OS != windows |
| 111 | |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 112 | # 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 | # |
| 119 | path_is_absolute () |
| 120 | { |
| 121 | local P P2 |
| 122 | P=$1 # copy path |
| 123 | P2=${P#/} # remove / prefix, if any |
| 124 | [ "$P" != "$P2" ] |
| 125 | } |
| 126 | |
| 127 | if ! path_is_absolute "$ANDROID_NDK_ROOT"; then |
| 128 | ANDROID_NDK_ROOT=$(pwd)/$ANDROID_NDK_ROOT |
| 129 | fi |
| 130 | |
| 131 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 132 | VERBOSE=no |
| 133 | while [ -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' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 198 | --launch-list) |
| 199 | OPTION_LAUNCH_LIST="yes" |
| 200 | ;; |
| 201 | --launch=*) |
| 202 | OPTION_LAUNCH="$optarg" |
| 203 | ;; |
| 204 | --start) |
| 205 | OPTION_START=yes |
| 206 | ;; |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 207 | --delay=*) |
| 208 | OPTION_DELAY="$optarg" |
| 209 | ;; |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 210 | -*) # 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 |
| 223 | done |
| 224 | |
| 225 | if [ "$OPTION_HELP" = "yes" ] ; then |
| 226 | echo "Usage: $PROGNAME [options]" |
| 227 | echo "" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 228 | 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' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 231 | echo "Valid options:" |
| 232 | echo "" |
| 233 | echo " --help|-h|-? Print this help" |
| 234 | echo " --verbose Enable verbose mode" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 235 | echo " --force Kill existing debug session if it exists" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 236 | 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' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 239 | echo " --delay=<secs> Delay in seconds between activity start and gdbserver attach." |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 240 | echo " --project=<path> Specify application project path" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 241 | echo " -p <path> Same as --project=<path>" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 242 | echo " --port=<port> Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 243 | echo " --exec=<file> Execute gdb initialization commands in <file> after connection" |
| 244 | echo " -x <file> Same as --exec=<file>" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 245 | echo " --adb=<file> Use specific adb command [$ADB_CMD]" |
| 246 | echo " --awk=<file> Use specific awk command [$AWK_CMD]" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 247 | 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 |
| 252 | fi |
| 253 | |
| 254 | log "Android NDK installation path: $ANDROID_NDK_ROOT" |
| 255 | |
| 256 | if [ -n "$OPTION_EXEC" ] ; then |
| 257 | if [ ! -f "$OPTION_EXEC" ]; then |
| 258 | echo "ERROR: Invalid initialization file: $OPTION_EXEC" |
| 259 | exit 1 |
| 260 | fi |
| 261 | fi |
| 262 | |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 263 | if [ -n "$OPTION_DELAY" ] ; then |
| 264 | DELAY="$OPTION_DELAY" |
| 265 | fi |
| 266 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 267 | # Check ADB tool version |
| 268 | if [ -n "$OPTION_ADB" ] ; then |
| 269 | ADB_CMD="$OPTION_ADB" |
| 270 | log "Using specific adb command: $ADB_CMD" |
| 271 | else |
| 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" |
| 279 | fi |
| 280 | |
| 281 | ADB_VERSION=`$ADB_CMD version` |
| 282 | if [ $? != 0 ] ; then |
| 283 | echo "ERROR: Could not run ADB with: $ADB_CMD" |
| 284 | exit 1 |
| 285 | fi |
| 286 | log "ADB version found: $ADB_VERSION" |
| 287 | |
| 288 | ADB_CMD="${ADB_CMD}${ADB_FLAGS}" |
| 289 | log "Using final ADB command: '$ADB_CMD'" |
| 290 | |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 291 | |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 292 | # 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' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 296 | _adb_var_shell () |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 297 | { |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 298 | # We need a temporary file to store the output of our command |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 299 | 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' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 304 | # Run the command, while storing the standard output to CMD_OUT |
| 305 | # and appending the exit code as the last line. |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 306 | if [ "$REDIRECT_STDERR" != 0 ]; then |
David 'Digit' Turner | ec2b8dd | 2011-10-27 14:43:25 +0200 | [diff] [blame^] | 307 | $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT 2>&1 |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 308 | else |
David 'Digit' Turner | ec2b8dd | 2011-10-27 14:43:25 +0200 | [diff] [blame^] | 309 | $ADB_CMD shell "$@" ";" echo \$? | sed -e 's![[:cntrl:]]!!g' > $CMD_OUT |
David 'Digit' Turner | b2e3ee7 | 2011-03-23 15:03:31 +0100 | [diff] [blame] | 310 | fi |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 311 | # 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. |
| 331 | adb_var_shell () |
| 332 | { |
David 'Digit' Turner | ec2b8dd | 2011-10-27 14:43:25 +0200 | [diff] [blame^] | 333 | _adb_var_shell 0 "$@" |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | # A variant of adb_var_shell that stores both stdout and stderr in the output |
| 337 | # $1: Variable name |
| 338 | adb_var_shell2 () |
| 339 | { |
David 'Digit' Turner | ec2b8dd | 2011-10-27 14:43:25 +0200 | [diff] [blame^] | 340 | _adb_var_shell 1 "$@" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 341 | } |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 342 | |
David 'Digit' Turner | 9c10d88 | 2011-10-27 13:10:58 +0200 | [diff] [blame] | 343 | # 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 |
| 346 | get_pid_of () |
| 347 | { |
| 348 | $ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE="$1" |
| 349 | } |
| 350 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 351 | # Check the awk tool |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 352 | AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk |
| 353 | AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 354 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 355 | echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 356 | exit 1 |
| 357 | fi |
| 358 | if [ "$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 |
| 361 | fi |
| 362 | |
| 363 | # Name of the manifest file |
| 364 | MANIFEST=AndroidManifest.xml |
| 365 | |
| 366 | # Find the root of the application project. |
| 367 | if [ -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 |
| 379 | else |
| 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" |
| 399 | fi |
| 400 | |
| 401 | # Extract the package name from the manifest |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 402 | PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 403 | log "Found package name: $PACKAGE_NAME" |
| 404 | if [ $? != 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 |
| 408 | fi |
| 409 | |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 410 | # If --launch-list is used, list all launchable activities, and be done with it |
| 411 | if [ "$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 |
| 415 | fi |
| 416 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 417 | APP_ABIS=`get_build_var APP_ABI` |
| 418 | log "ABIs targetted by application: $APP_ABIS" |
| 419 | |
| 420 | # Check the ADB command, and that we can connect to the device/emulator |
| 421 | ADB_TEST=`$ADB_CMD shell ls` |
| 422 | if [ $? != 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' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 425 | echo " through USB to this machine. You can use -e, -d and -s <serial>" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 426 | echo " in case of multiple ones." |
| 427 | exit 1 |
| 428 | fi |
| 429 | |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 430 | # Check that the device is running Froyo (API Level 8) or higher |
| 431 | # |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 432 | adb_var_shell API_LEVEL getprop ro.build.version.sdk |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 433 | if [ $? != 0 -o -z "$API_LEVEL" ] ; then |
David 'Digit' Turner | fd20437 | 2010-09-14 15:10:38 +0200 | [diff] [blame] | 434 | echo "ERROR: Could not find target device's supported API level!" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 435 | echo "ndk-gdb will only work if your device is running Android 2.2 or higher." |
| 436 | exit 1 |
| 437 | fi |
David 'Digit' Turner | 54be486 | 2010-06-03 11:57:49 -0700 | [diff] [blame] | 438 | log "Device API Level: $API_LEVEL" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 439 | if [ "$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' Turner | fd20437 | 2010-09-14 15:10:38 +0200 | [diff] [blame] | 441 | echo "The target device is running API level $API_LEVEL!" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 442 | exit 1 |
| 443 | fi |
| 444 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 445 | # Get the target device's supported ABI(s) |
| 446 | # And check that they are supported by the application |
| 447 | # |
| 448 | COMPAT_ABI=none |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 449 | adb_var_shell CPU_ABI getprop ro.product.cpu.abi |
David 'Digit' Turner | 47eff9f | 2010-06-03 11:57:49 -0700 | [diff] [blame] | 450 | for ABI in $APP_ABIS; do |
| 451 | if [ "$ABI" = "$CPU_ABI" ] ; then |
| 452 | COMPAT_ABI=$CPU_ABI |
| 453 | break |
| 454 | fi |
| 455 | done |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 456 | |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 457 | adb_var_shell CPU_ABI2 getprop ro.product.cpu.abi2 |
| 458 | if [ $? != 0 -o -z "$CPU_ABI2" ] ; then |
| 459 | CPU_ABI2= |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 460 | log "Device CPU ABI: $CPU_ABI" |
| 461 | else |
| 462 | log "Device CPU ABIs: $CPU_ABI $CPU_ABI2" |
David 'Digit' Turner | 47eff9f | 2010-06-03 11:57:49 -0700 | [diff] [blame] | 463 | 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' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 470 | fi |
| 471 | fi |
| 472 | if [ "$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 |
| 480 | fi |
| 481 | log "Compatible device ABI: $COMPAT_ABI" |
| 482 | |
David 'Digit' Turner | fd20437 | 2010-09-14 15:10:38 +0200 | [diff] [blame] | 483 | # Check that the application is debuggable, or nothing will work |
| 484 | DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk` |
| 485 | log "Found debuggable flag: $DEBUGGABLE" |
| 486 | if [ $? != 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 |
| 503 | else |
| 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' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 514 | fi |
| 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' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 519 | adb_var_shell2 DEVICE_GDBSERVER ls /data/data/$PACKAGE_NAME/lib/gdbserver |
| 520 | if [ $? != 0 ]; then |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 521 | echo "ERROR: Non-debuggable application installed on the target device." |
David 'Digit' Turner | fd20437 | 2010-09-14 15:10:38 +0200 | [diff] [blame] | 522 | echo " Please re-install the debuggable version!" |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 523 | exit 1 |
| 524 | fi |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 525 | log "Found device gdbserver: $DEVICE_GDBSERVER" |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 526 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 527 | # Get information from the build system |
| 528 | GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI` |
| 529 | log "Using gdb setup init: $GDBSETUP_INIT" |
| 530 | |
| 531 | TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI` |
| 532 | log "Using toolchain prefix: $TOOLCHAIN_PREFIX" |
| 533 | |
| 534 | APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI` |
| 535 | log "Using app out directory: $APP_OUT" |
| 536 | |
| 537 | # Find the <dataDir> of the package on the device |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 538 | adb_var_shell2 DATA_DIR run-as $PACKAGE_NAME /system/bin/sh -c pwd |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 539 | if [ $? != 0 -o -z "$DATA_DIR" ] ; then |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 540 | echo "ERROR: Could not extract package's data directory. Are you sure that" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 541 | echo " your installed application is debuggable?" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 542 | exit 1 |
| 543 | fi |
David 'Digit' Turner | e3cfafb | 2011-03-15 15:43:12 +0100 | [diff] [blame] | 544 | log "Found data directory: '$DATA_DIR'" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 545 | |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 546 | # Launch the activity if needed |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 547 | if [ "$OPTION_START" = "yes" ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 548 | # 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 |
| 566 | fi |
| 567 | |
| 568 | if [ -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' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 578 | run $ADB_CMD shell sleep $DELAY |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 579 | fi |
| 580 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 581 | # Find the PID of the application being run |
David 'Digit' Turner | 9c10d88 | 2011-10-27 13:10:58 +0200 | [diff] [blame] | 582 | PID=$(get_pid_of "$PACKAGE_NAME") |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 583 | log "Found running PID: $PID" |
| 584 | if [ $? != 0 -o "$PID" = "0" ] ; then |
| 585 | echo "ERROR: Could not extract PID of application on device/emulator." |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 586 | if [ -n "$OPTION_LAUNCH" ] ; then |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 587 | 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' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 593 | 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' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 597 | exit 1 |
| 598 | fi |
| 599 | |
| 600 | # Check that there is no other instance of gdbserver running |
David 'Digit' Turner | 9c10d88 | 2011-10-27 13:10:58 +0200 | [diff] [blame] | 601 | GDBSERVER_PID=$(get_pid_of lib/gdbserver) |
| 602 | if [ "$GDBSERVER_PID" != "0" ]; then |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 603 | 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' Turner | 9c10d88 | 2011-10-27 13:10:58 +0200 | [diff] [blame] | 608 | run $ADB_CMD shell kill -9 $GDBSERVER_PID |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 609 | fi |
| 610 | |
| 611 | # Launch gdbserver now |
| 612 | DEBUG_SOCKET=debug-socket |
| 613 | run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID & |
| 614 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 615 | echo "ERROR: Could not launch gdbserver on the device?" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 616 | exit 1 |
| 617 | fi |
| 618 | log "Launched gdbserver succesfully." |
| 619 | |
| 620 | # Setup network redirection |
| 621 | log "Setup network redirection" |
| 622 | run $ADB_CMD forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET |
| 623 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 624 | 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' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 626 | exit 1 |
| 627 | fi |
| 628 | |
| 629 | # Get the app_server binary from the device |
| 630 | APP_PROCESS=$APP_OUT/app_process |
David 'Digit' Turner | 47eff9f | 2010-06-03 11:57:49 -0700 | [diff] [blame] | 631 | run $ADB_CMD pull /system/bin/app_process `native_path $APP_PROCESS` |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 632 | log "Pulled app_process from device/emulator." |
| 633 | |
| 634 | run $ADB_CMD pull /system/lib/libc.so `native_path $APP_OUT/libc.so` |
| 635 | log "Pulled libc.so from device/emulator." |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 636 | |
| 637 | # Now launch the appropriate gdb client with the right init commands |
| 638 | # |
| 639 | GDBCLIENT=${TOOLCHAIN_PREFIX}gdb |
| 640 | GDBSETUP=$APP_OUT/gdb.setup |
| 641 | cp -f $GDBSETUP_INIT $GDBSETUP |
David 'Digit' Turner | 5b65625 | 2010-10-08 00:43:32 +0200 | [diff] [blame] | 642 | #uncomment the following to debug the remote connection only |
| 643 | #echo "set debug remote 1" >> $GDBSETUP |
David 'Digit' Turner | caf0619 | 2010-10-18 12:39:51 +0200 | [diff] [blame] | 644 | echo "file `native_path $APP_PROCESS`" >> $GDBSETUP |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 645 | echo "target remote :$DEBUG_PORT" >> $GDBSETUP |
| 646 | if [ -n "$OPTION_EXEC" ] ; then |
| 647 | cat $OPTION_EXEC >> $GDBSETUP |
| 648 | fi |
David 'Digit' Turner | caf0619 | 2010-10-18 12:39:51 +0200 | [diff] [blame] | 649 | $GDBCLIENT -x `native_path $GDBSETUP` |