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 | |
| 38 | PARAMETERS= |
| 39 | OPTION_HELP=no |
| 40 | OPTION_PROJECT= |
| 41 | OPTION_FORCE=no |
| 42 | OPTION_ADB= |
| 43 | OPTION_EXEC= |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 44 | OPTION_START=no |
| 45 | OPTION_LAUNCH= |
| 46 | OPTION_LAUNCH_LIST=no |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 47 | |
| 48 | check_parameter () |
| 49 | { |
| 50 | if [ -z "$2" ]; then |
| 51 | echo "ERROR: Missing parameter after option '$1'" |
| 52 | exit 1 |
| 53 | fi |
| 54 | } |
| 55 | |
| 56 | check_adb_flags () |
| 57 | { |
| 58 | if [ -n "$ADB_FLAGS" ] ; then |
| 59 | echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!" |
| 60 | exit 1 |
| 61 | fi |
| 62 | } |
| 63 | |
| 64 | get_build_var () |
| 65 | { |
| 66 | if [ -z "$GNUMAKE" ] ; then |
| 67 | GNUMAKE=make |
| 68 | fi |
| 69 | $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 |
| 70 | } |
| 71 | |
| 72 | get_build_var_for_abi () |
| 73 | { |
| 74 | if [ -z "$GNUMAKE" ] ; then |
| 75 | GNUMAKE=make |
| 76 | fi |
| 77 | $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2 |
| 78 | } |
| 79 | |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 80 | # Used to run an awk script on the manifest |
| 81 | run_awk_manifest_script () |
| 82 | { |
| 83 | $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST |
| 84 | } |
| 85 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 86 | VERBOSE=no |
| 87 | while [ -n "$1" ]; do |
| 88 | opt="$1" |
| 89 | optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'` |
| 90 | case "$opt" in |
| 91 | --help|-h|-\?) |
| 92 | OPTION_HELP=yes |
| 93 | ;; |
| 94 | --verbose) |
| 95 | VERBOSE=yes |
| 96 | ;; |
| 97 | -s) |
| 98 | check_parameter $1 $2 |
| 99 | check_adb_flags |
| 100 | ADB_FLAGS=" -s $2" |
| 101 | shift |
| 102 | ;; |
| 103 | -s*) |
| 104 | check_adb_flags |
| 105 | optarg=`expr -- "$opt" : '-s\(.*\)'` |
| 106 | ADB_FLAGS=" -s $optarg" |
| 107 | ;; |
| 108 | -p) |
| 109 | check_parameter $1 $2 |
| 110 | OPTION_PROJECT="$2" |
| 111 | shift |
| 112 | ;; |
| 113 | -p*) |
| 114 | optarg=`expr -- "$opt" : '-p\(.*\)'` |
| 115 | OPTION_PROJECT="$optarg" |
| 116 | ;; |
| 117 | --exec=*) |
| 118 | OPTION_EXEC="$optarg" |
| 119 | ;; |
| 120 | -x) |
| 121 | check_parameter $1 $2 |
| 122 | OPTION_EXEC="$2" |
| 123 | shift |
| 124 | ;; |
| 125 | -x*) |
| 126 | optarg=`expr -- "$opt" : '-x\(.*\)'` |
| 127 | OPTION_EXEC="$optarg" |
| 128 | ;; |
| 129 | -e) |
| 130 | check_adb_flags |
| 131 | ADB_FLAGS=" -e" |
| 132 | ;; |
| 133 | -d) |
| 134 | check_adb_flags |
| 135 | ADB_FLAGS=" -d" |
| 136 | ;; |
| 137 | --adb=*) # specify ADB command |
| 138 | OPTION_ADB="$optarg" |
| 139 | ;; |
| 140 | --awk=*) |
| 141 | AWK_CMD="$optarg" |
| 142 | ;; |
| 143 | --project=*) |
| 144 | OPTION_PROJECT="$optarg" |
| 145 | ;; |
| 146 | --port=*) |
| 147 | DEBUG_PORT="$optarg" |
| 148 | ;; |
| 149 | --force) |
| 150 | OPTION_FORCE="yes" |
| 151 | ;; |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 152 | --launch-list) |
| 153 | OPTION_LAUNCH_LIST="yes" |
| 154 | ;; |
| 155 | --launch=*) |
| 156 | OPTION_LAUNCH="$optarg" |
| 157 | ;; |
| 158 | --start) |
| 159 | OPTION_START=yes |
| 160 | ;; |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 161 | -*) # unknown options |
| 162 | echo "ERROR: Unknown option '$opt', use --help for list of valid ones." |
| 163 | exit 1 |
| 164 | ;; |
| 165 | *) # Simply record parameter |
| 166 | if [ -z "$PARAMETERS" ] ; then |
| 167 | PARAMETERS="$opt" |
| 168 | else |
| 169 | PARAMETERS="$PARAMETERS $opt" |
| 170 | fi |
| 171 | ;; |
| 172 | esac |
| 173 | shift |
| 174 | done |
| 175 | |
| 176 | if [ "$OPTION_HELP" = "yes" ] ; then |
| 177 | echo "Usage: $PROGNAME [options]" |
| 178 | echo "" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 179 | echo "Setup a gdb debugging session for your Android NDK application." |
| 180 | echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions." |
| 181 | echo "" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 182 | echo "Valid options:" |
| 183 | echo "" |
| 184 | echo " --help|-h|-? Print this help" |
| 185 | echo " --verbose Enable verbose mode" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 186 | echo " --force Kill existing debug session if it exists" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 187 | echo " --start Launch application instead of attaching to existing one" |
| 188 | echo " --launch=<name> Same as --start, but specify activity name (see below)" |
| 189 | echo " --launch-list List all launchable activity names from manifest" |
| 190 | echo " --project=<path> Specify application project path" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 191 | echo " -p <path> Same as --project=<path>" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 192 | 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] | 193 | echo " --exec=<file> Execute gdb initialization commands in <file> after connection" |
| 194 | echo " -x <file> Same as --exec=<file>" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 195 | echo " --adb=<file> Use specific adb command [$ADB_CMD]" |
| 196 | echo " --awk=<file> Use specific awk command [$AWK_CMD]" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 197 | echo " -e Connect to single emulator instance" |
| 198 | echo " -d Connect to single target device" |
| 199 | echo " -s <serial> Connect to specific emulator or device" |
| 200 | echo "" |
| 201 | exit 0 |
| 202 | fi |
| 203 | |
| 204 | log "Android NDK installation path: $ANDROID_NDK_ROOT" |
| 205 | |
| 206 | if [ -n "$OPTION_EXEC" ] ; then |
| 207 | if [ ! -f "$OPTION_EXEC" ]; then |
| 208 | echo "ERROR: Invalid initialization file: $OPTION_EXEC" |
| 209 | exit 1 |
| 210 | fi |
| 211 | fi |
| 212 | |
| 213 | # Check ADB tool version |
| 214 | if [ -n "$OPTION_ADB" ] ; then |
| 215 | ADB_CMD="$OPTION_ADB" |
| 216 | log "Using specific adb command: $ADB_CMD" |
| 217 | else |
| 218 | if [ -z "$ADB_CMD" ] ; then |
| 219 | echo "ERROR: The 'adb' tool is not in your path." |
| 220 | echo " You can change your PATH variable, or use" |
| 221 | echo " --adb=<executable> to point to a valid one." |
| 222 | exit 1 |
| 223 | fi |
| 224 | log "Using default adb command: $ADB_CMD" |
| 225 | fi |
| 226 | |
| 227 | ADB_VERSION=`$ADB_CMD version` |
| 228 | if [ $? != 0 ] ; then |
| 229 | echo "ERROR: Could not run ADB with: $ADB_CMD" |
| 230 | exit 1 |
| 231 | fi |
| 232 | log "ADB version found: $ADB_VERSION" |
| 233 | |
| 234 | ADB_CMD="${ADB_CMD}${ADB_FLAGS}" |
| 235 | log "Using final ADB command: '$ADB_CMD'" |
| 236 | |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 237 | adb_shell () |
| 238 | { |
| 239 | # Run an adb shell command and return its output. |
| 240 | # |
| 241 | # We need to filter weird control characters like \r that are |
| 242 | # included in the output. |
| 243 | # |
| 244 | $ADB_CMD shell $@ | sed -e 's![[:cntrl:]]!!g' |
| 245 | } |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 246 | |
| 247 | # Check the awk tool |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 248 | AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk |
| 249 | AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 250 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 251 | 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] | 252 | exit 1 |
| 253 | fi |
| 254 | if [ "$AWK_TEST" != "Pass" ] ; then |
| 255 | echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!" |
| 256 | exit 1 |
| 257 | fi |
| 258 | |
| 259 | # Name of the manifest file |
| 260 | MANIFEST=AndroidManifest.xml |
| 261 | |
| 262 | # Find the root of the application project. |
| 263 | if [ -n "$OPTION_PROJECT" ] ; then |
| 264 | PROJECT=$OPTION_PROJECT |
| 265 | log "Using specified project path: $PROJECT" |
| 266 | if [ ! -d "$PROJECT" ] ; then |
| 267 | echo "ERROR: Your --project option does not point to a directory!" |
| 268 | exit 1 |
| 269 | fi |
| 270 | if [ ! -f "$PROJECT/$MANIFEST" ] ; then |
| 271 | echo "ERROR: Your --project does not point to an Android project path!" |
| 272 | echo " It is missing a $MANIFEST file." |
| 273 | exit 1 |
| 274 | fi |
| 275 | else |
| 276 | # Assume we are in the project directory |
| 277 | if [ -f "$MANIFEST" ] ; then |
| 278 | PROJECT=. |
| 279 | else |
| 280 | PROJECT= |
| 281 | CURDIR=`pwd` |
| 282 | while [ "$CURDIR" != "/" ] ; do |
| 283 | if [ -f "$CURDIR/$MANIFEST" ] ; then |
| 284 | PROJECT="$CURDIR" |
| 285 | break |
| 286 | fi |
| 287 | CURDIR=`dirname $CURDIR` |
| 288 | done |
| 289 | if [ -z "$PROJECT" ] ; then |
| 290 | echo "ERROR: Launch this script from an application project directory, or use --project=<path>." |
| 291 | exit 1 |
| 292 | fi |
| 293 | fi |
| 294 | log "Using auto-detected project path: $PROJECT" |
| 295 | fi |
| 296 | |
| 297 | # Extract the package name from the manifest |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 298 | PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 299 | log "Found package name: $PACKAGE_NAME" |
| 300 | if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then |
| 301 | echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST." |
| 302 | echo " Please check that the file is well-formed!" |
| 303 | exit 1 |
| 304 | fi |
| 305 | |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 306 | # If --launch-list is used, list all launchable activities, and be done with it |
| 307 | if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then |
| 308 | log "Extracting list of launchable activities from manifest:" |
| 309 | run_awk_manifest_script extract-launchable.awk |
| 310 | exit 0 |
| 311 | fi |
| 312 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 313 | # Check that the application is debuggable, or nothing will work |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 314 | DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 315 | log "Found debuggable flag: $DEBUGGABLE" |
| 316 | if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then |
| 317 | echo "ERROR: Package $PACKAGE_NAME is not debuggable ! Please fix your manifest," |
| 318 | echo " rebuild your application and re-install it to fix this." |
| 319 | exit 1 |
| 320 | fi |
| 321 | |
| 322 | APP_ABIS=`get_build_var APP_ABI` |
| 323 | log "ABIs targetted by application: $APP_ABIS" |
| 324 | |
| 325 | # Check the ADB command, and that we can connect to the device/emulator |
| 326 | ADB_TEST=`$ADB_CMD shell ls` |
| 327 | if [ $? != 0 ] ; then |
| 328 | echo "ERROR: Could not connect to device or emulator!" |
| 329 | 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] | 330 | 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] | 331 | echo " in case of multiple ones." |
| 332 | exit 1 |
| 333 | fi |
| 334 | |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 335 | # Check that the device is running Froyo (API Level 8) or higher |
| 336 | # |
| 337 | API_LEVEL=`adb_shell getprop ro.build.version.sdk` |
| 338 | if [ $? != 0 -o -z "$API_LEVEL" ] ; then |
| 339 | echo "ERROR: Could not find target device's supported API level !" |
| 340 | echo "ndk-gdb will only work if your device is running Android 2.2 or higher." |
| 341 | exit 1 |
| 342 | fi |
David 'Digit' Turner | 54be486 | 2010-06-03 11:57:49 -0700 | [diff] [blame^] | 343 | log "Device API Level: $API_LEVEL" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 344 | if [ "$API_LEVEL" -lt "8" ] ; then |
| 345 | echo "ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher." |
| 346 | echo "The target device is running API level $API_LEVEL !" |
| 347 | exit 1 |
| 348 | fi |
| 349 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 350 | # Get the target device's supported ABI(s) |
| 351 | # And check that they are supported by the application |
| 352 | # |
| 353 | COMPAT_ABI=none |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 354 | CPU_ABI=`adb_shell getprop ro.product.cpu.abi` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 355 | echo "$APP_ABIS" | grep -q -F "$CPU_ABI" |
| 356 | if [ $? = 0 ] ; then |
| 357 | COMPAT_ABI=$CPU_ABI |
| 358 | fi |
| 359 | |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 360 | CPU_ABI2=`adb_shell getprop ro.product.cpu.abi2` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 361 | if [ -z "$CPU_ABI2" ] ; then |
| 362 | log "Device CPU ABI: $CPU_ABI" |
| 363 | else |
| 364 | log "Device CPU ABIs: $CPU_ABI $CPU_ABI2" |
| 365 | echo "$APP_ABIS" | grep -q -F "$CPU_ABI2" |
| 366 | if [ $? = 0 ] ; then |
| 367 | COMPAT_ABI=$CPU_ABI2 |
| 368 | fi |
| 369 | fi |
| 370 | if [ "$COMPAT_ABI" = none ] ; then |
| 371 | echo "ERROR: The device does not support the application's targetted CPU ABIs!" |
| 372 | if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then |
| 373 | CPU_ABI2= |
| 374 | fi |
| 375 | echo " Device supports: $CPU_ABI $CPU_ABI2" |
| 376 | echo " Package supports: $APP_ABIS" |
| 377 | exit 1 |
| 378 | fi |
| 379 | log "Compatible device ABI: $COMPAT_ABI" |
| 380 | |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 381 | # Let's check that the user didn't change the debuggable flag in |
| 382 | # the manifest without calling ndk-build afterwards. |
| 383 | if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then |
| 384 | echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI" |
| 385 | echo " This usually means you modified your AndroidManifest.xml to set" |
| 386 | echo " the android:debuggable flag to 'true' but did not rebuild the" |
| 387 | echo " native binaries. Please call 'ndk-build' to do so," |
| 388 | echo " *then* re-install to the device !" |
| 389 | exit 1 |
| 390 | fi |
| 391 | |
| 392 | # Let's check that 'gdbserver' is properly installed on the device too. If this |
| 393 | # is not the case, the user didn't install the proper package after rebuilding. |
| 394 | # |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 395 | DEVICE_GDBSERVER=`adb_shell ls /data/data/$PACKAGE_NAME/lib/gdbserver` |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 396 | log "Found device gdbserver: $DEVICE_GDBSERVER" |
| 397 | if pattern_match "No such file or directory" "$DEVICE_GDBSERVER" ] ; then |
| 398 | echo "ERROR: Non-debuggable application installed on the target device." |
| 399 | echo " Please re-install the debuggable version !" |
| 400 | exit 1 |
| 401 | fi |
| 402 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 403 | # Get information from the build system |
| 404 | GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI` |
| 405 | log "Using gdb setup init: $GDBSETUP_INIT" |
| 406 | |
| 407 | TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI` |
| 408 | log "Using toolchain prefix: $TOOLCHAIN_PREFIX" |
| 409 | |
| 410 | APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI` |
| 411 | log "Using app out directory: $APP_OUT" |
| 412 | |
| 413 | # Find the <dataDir> of the package on the device |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 414 | DATA_DIR=`adb_shell run-as $PACKAGE_NAME /system/bin/sh -c pwd` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 415 | log "Found data directory: '$DATA_DIR'" |
David 'Digit' Turner | 4cff650 | 2010-06-01 14:40:11 -0700 | [diff] [blame] | 416 | if [ $? != 0 -o -z "$DATA_DIR" ] ; then |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 417 | 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] | 418 | echo " your installed application is debuggable?" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 419 | exit 1 |
| 420 | fi |
| 421 | |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 422 | # Launch the activity if needed |
| 423 | if [ -n "$OPTION_START" ] ; then |
| 424 | # If --launch is used, ignore --start, otherwise extract the first |
| 425 | # launchable activity name from the manifest and use it as if --launch=<name> |
| 426 | # was used instead. |
| 427 | # |
| 428 | if [ -z "$OPTION_LAUNCH" ] ; then |
| 429 | OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q` |
| 430 | if [ $? != 0 ] ; then |
| 431 | echo "ERROR: Could not extract name of launchable activity from manifest!" |
| 432 | echo " Try to use --launch=<name> directly instead as a work-around." |
| 433 | exit 1 |
| 434 | fi |
| 435 | log "Found first launchable activity: $OPTION_LAUNCH" |
| 436 | if [ -z "$OPTION_LAUNCH" ] ; then |
| 437 | echo "ERROR: It seems that your Application does not have any launchable activity!" |
| 438 | echo " Please fix your manifest file and rebuild/re-install your application." |
| 439 | exit 1 |
| 440 | fi |
| 441 | fi |
| 442 | fi |
| 443 | |
| 444 | if [ -n "$OPTION_LAUNCH" ] ; then |
| 445 | log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH" |
| 446 | run $ADB_CMD shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH |
| 447 | if [ $? != 0 ] ; then |
| 448 | echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH" |
| 449 | echo " Use --launch-list to dump a list of valid values." |
| 450 | exit 1 |
| 451 | fi |
| 452 | # Sleep a bit, it sometimes take one second to start properly |
| 453 | # Note that we use the 'sleep' command on the device here. |
| 454 | run $ADB_CMD shell sleep 1 |
| 455 | fi |
| 456 | |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 457 | # Find the PID of the application being run |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 458 | PID=`$ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=$PACKAGE_NAME` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 459 | log "Found running PID: $PID" |
| 460 | if [ $? != 0 -o "$PID" = "0" ] ; then |
| 461 | echo "ERROR: Could not extract PID of application on device/emulator." |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 462 | if [ -n "$OPTION_LAUNCH" ] ; then |
David 'Digit' Turner | 2e063fe | 2010-05-06 15:31:34 -0700 | [diff] [blame] | 463 | echo " Weird, this probably means one of these:" |
| 464 | echo "" |
| 465 | echo " - The installed package does not match your current manifest." |
| 466 | echo " - The application process was terminated." |
| 467 | echo "" |
| 468 | 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] | 469 | else |
| 470 | echo " Are you sure the application is already started?" |
| 471 | echo " Consider using --start or --launch=<name> if not." |
| 472 | fi |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 473 | exit 1 |
| 474 | fi |
| 475 | |
| 476 | # Check that there is no other instance of gdbserver running |
| 477 | GDBSERVER_PS=`$ADB_CMD shell ps | grep lib/gdbserver` |
| 478 | if [ -n "$GDBSERVER_PS" ] ; then |
| 479 | if [ "$OPTION_FORCE" = "no" ] ; then |
| 480 | echo "ERROR: Another debug session running, Use --force to kill it." |
| 481 | exit 1 |
| 482 | fi |
| 483 | log "Killing existing debugging session" |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 484 | GDBSERVER_PID=`echo $GDBSERVER_PS | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=lib/gdbserver` |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 485 | if [ $GDBSERVER_PID != 0 ] ; then |
| 486 | run $ADB_CMD shell kill -9 $GDBSERVER_PID |
| 487 | fi |
| 488 | fi |
| 489 | |
| 490 | # Launch gdbserver now |
| 491 | DEBUG_SOCKET=debug-socket |
| 492 | run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID & |
| 493 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 494 | echo "ERROR: Could not launch gdbserver on the device?" |
David 'Digit' Turner | a08d605 | 2010-04-16 12:45:33 -0700 | [diff] [blame] | 495 | exit 1 |
| 496 | fi |
| 497 | log "Launched gdbserver succesfully." |
| 498 | |
| 499 | # Setup network redirection |
| 500 | log "Setup network redirection" |
| 501 | run $ADB_CMD forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET |
| 502 | if [ $? != 0 ] ; then |
David 'Digit' Turner | 0b2676b | 2010-04-27 12:33:46 -0700 | [diff] [blame] | 503 | echo "ERROR: Could not setup network redirection to gdbserver?" |
| 504 | 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] | 505 | exit 1 |
| 506 | fi |
| 507 | |
| 508 | # Get the app_server binary from the device |
| 509 | APP_PROCESS=$APP_OUT/app_process |
| 510 | run adb pull /system/bin/app_process $APP_PROCESS |
| 511 | log "Pulled $APP_BINARY from device/emulator." |
| 512 | |
| 513 | # Now launch the appropriate gdb client with the right init commands |
| 514 | # |
| 515 | GDBCLIENT=${TOOLCHAIN_PREFIX}gdb |
| 516 | GDBSETUP=$APP_OUT/gdb.setup |
| 517 | cp -f $GDBSETUP_INIT $GDBSETUP |
| 518 | echo "target remote :$DEBUG_PORT" >> $GDBSETUP |
| 519 | if [ -n "$OPTION_EXEC" ] ; then |
| 520 | cat $OPTION_EXEC >> $GDBSETUP |
| 521 | fi |
| 522 | $GDBCLIENT -x $GDBSETUP -e $APP_PROCESS |