ndk-gdb 18.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#!/bin/sh
#
# Copyright (C) 2010 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# This wrapper script is used to launch a native debugging session
# on a given NDK application. The application must be debuggable, i.e.
# its android:debuggable attribute must be set to 'true' in the
# <application> element of its manifest.
#
# See docs/NDK-GDB.TXT for usage description. Essentially, you just
# need to launch ndk-gdb from your application project directory
# after doing ndk-build && ant install && <start-application-on-device>
#
. `dirname $0`/build/core/ndk-common.sh

force_32bit_binaries

find_program ADB_CMD adb
ADB_FLAGS=

AWK_CMD=awk

DEBUG_PORT=5039

38 39 40 41 42
# Delay in seconds between launching the activity and attaching gdbserver on it.
# This is needed because there is no way to know when the activity has really
# started, and sometimes this takes a few seconds.
DELAY=2

43 44 45 46 47 48
PARAMETERS=
OPTION_HELP=no
OPTION_PROJECT=
OPTION_FORCE=no
OPTION_ADB=
OPTION_EXEC=
49 50 51
OPTION_START=no
OPTION_LAUNCH=
OPTION_LAUNCH_LIST=no
52
OPTION_DELAY=
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85

check_parameter ()
{
    if [ -z "$2" ]; then
        echo "ERROR: Missing parameter after option '$1'"
        exit 1
    fi
}

check_adb_flags ()
{
    if [ -n "$ADB_FLAGS" ] ; then
        echo "ERROR: Only one of -e, -d or -s <serial> can be used at the same time!"
        exit 1
    fi
}

get_build_var ()
{
    if [ -z "$GNUMAKE" ] ; then
        GNUMAKE=make
    fi
    $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1
}

get_build_var_for_abi ()
{
    if [ -z "$GNUMAKE" ] ; then
        GNUMAKE=make
    fi
    $GNUMAKE --no-print-dir -f $ANDROID_NDK_ROOT/build/core/build-local.mk -C $PROJECT DUMP_$1 APP_ABI=$2
}

86 87 88 89 90 91
# Used to run an awk script on the manifest
run_awk_manifest_script ()
{
    $AWK_CMD -f $AWK_SCRIPTS/$1 $PROJECT/$MANIFEST
}

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
if [ "$HOST_OS" = "cygwin" ] ; then
# Return native path representation from cygwin one
# $1: a cygwin-compatible path (e.g. /cygdrive/c/some/thing)
# Return: path in host windows representation, e.g. C:/some/thing
#
# We use mixed mode (i.e. / as the directory separator) because
# all the tools we use recognize it properly, and it avoids lots
# of escaping nonsense associated with "\"
#
native_path ()
{
    cygpath -m $1
}
else # HOST_OS != windows
native_path ()
{
    echo "$1"
}
fi # HOST_OS != windows

112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
VERBOSE=no
while [ -n "$1" ]; do
    opt="$1"
    optarg=`expr "x$opt" : 'x[^=]*=\(.*\)'`
    case "$opt" in
        --help|-h|-\?)
            OPTION_HELP=yes
            ;;
        --verbose)
            VERBOSE=yes
            ;;
        -s)
            check_parameter $1 $2
            check_adb_flags
            ADB_FLAGS=" -s $2"
            shift
            ;;
        -s*)
            check_adb_flags
            optarg=`expr -- "$opt" : '-s\(.*\)'`
            ADB_FLAGS=" -s $optarg"
            ;;
        -p)
            check_parameter $1 $2
            OPTION_PROJECT="$2"
            shift
            ;;
        -p*)
            optarg=`expr -- "$opt" : '-p\(.*\)'`
            OPTION_PROJECT="$optarg"
            ;;
        --exec=*)
            OPTION_EXEC="$optarg"
            ;;
        -x)
            check_parameter $1 $2
            OPTION_EXEC="$2"
            shift
            ;;
        -x*)
            optarg=`expr -- "$opt" : '-x\(.*\)'`
            OPTION_EXEC="$optarg"
            ;;
        -e)
            check_adb_flags
            ADB_FLAGS=" -e"
            ;;
        -d)
            check_adb_flags
            ADB_FLAGS=" -d"
            ;;
        --adb=*) # specify ADB command
            OPTION_ADB="$optarg"
            ;;
        --awk=*)
            AWK_CMD="$optarg"
            ;;
        --project=*)
            OPTION_PROJECT="$optarg"
            ;;
        --port=*)
            DEBUG_PORT="$optarg"
            ;;
        --force)
            OPTION_FORCE="yes"
            ;;
178 179 180 181 182 183 184 185 186
        --launch-list)
            OPTION_LAUNCH_LIST="yes"
            ;;
        --launch=*)
            OPTION_LAUNCH="$optarg"
            ;;
        --start)
            OPTION_START=yes
            ;;
187 188 189
        --delay=*)
            OPTION_DELAY="$optarg"
            ;;
190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        -*) # unknown options
            echo "ERROR: Unknown option '$opt', use --help for list of valid ones."
            exit 1
        ;;
        *)  # Simply record parameter
            if [ -z "$PARAMETERS" ] ; then
                PARAMETERS="$opt"
            else
                PARAMETERS="$PARAMETERS $opt"
            fi
            ;;
    esac
    shift
done

if [ "$OPTION_HELP" = "yes" ] ; then
    echo "Usage: $PROGNAME [options]"
    echo ""
208 209 210
    echo "Setup a gdb debugging session for your Android NDK application."
    echo "Read $$NDK/docs/NDK-GDB.TXT for complete usage instructions."
    echo ""
211 212 213 214 215
    echo "Valid options:"
    echo ""
    echo "    --help|-h|-?      Print this help"
    echo "    --verbose         Enable verbose mode"
    echo "    --force           Kill existing debug session if it exists"
216 217 218
    echo "    --start           Launch application instead of attaching to existing one"
    echo "    --launch=<name>   Same as --start, but specify activity name (see below)"
    echo "    --launch-list     List all launchable activity names from manifest"
219
    echo "    --delay=<secs>    Delay in seconds between activity start and gdbserver attach."
220
    echo "    --project=<path>  Specify application project path"
221
    echo "    -p <path>         Same as --project=<path>"
222
    echo "    --port=<port>     Use tcp:localhost:<port> to communicate with gdbserver [$DEBUG_PORT]"
223 224
    echo "    --exec=<file>     Execute gdb initialization commands in <file> after connection"
    echo "    -x <file>         Same as --exec=<file>"
225 226
    echo "    --adb=<file>      Use specific adb command [$ADB_CMD]"
    echo "    --awk=<file>      Use specific awk command [$AWK_CMD]"
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
    echo "    -e                Connect to single emulator instance"
    echo "    -d                Connect to single target device"
    echo "    -s <serial>       Connect to specific emulator or device"
    echo ""
    exit 0
fi

log "Android NDK installation path: $ANDROID_NDK_ROOT"

if [ -n "$OPTION_EXEC" ] ; then
    if [ ! -f "$OPTION_EXEC" ]; then
        echo "ERROR: Invalid initialization file: $OPTION_EXEC"
        exit 1
    fi
fi

243 244 245 246
if [ -n "$OPTION_DELAY" ] ; then
    DELAY="$OPTION_DELAY"
fi

247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
# Check ADB tool version
if [ -n "$OPTION_ADB" ] ; then
    ADB_CMD="$OPTION_ADB"
    log "Using specific adb command: $ADB_CMD"
else
    if [ -z "$ADB_CMD" ] ; then
        echo "ERROR: The 'adb' tool is not in your path."
        echo "       You can change your PATH variable, or use"
        echo "       --adb=<executable> to point to a valid one."
        exit 1
    fi
    log "Using default adb command: $ADB_CMD"
fi

ADB_VERSION=`$ADB_CMD version`
if [ $? != 0 ] ; then
    echo "ERROR: Could not run ADB with: $ADB_CMD"
    exit 1
fi
log "ADB version found: $ADB_VERSION"

ADB_CMD="${ADB_CMD}${ADB_FLAGS}"
log "Using final ADB command: '$ADB_CMD'"

271 272 273 274 275 276 277 278 279
adb_shell ()
{
    # Run an adb shell command and return its output.
    #
    # We need to filter weird control characters like \r that are
    # included in the output.
    #
    $ADB_CMD shell $@ | sed -e 's![[:cntrl:]]!!g'
}
280 281

# Check the awk tool
282 283
AWK_SCRIPTS=$ANDROID_NDK_ROOT/build/awk
AWK_TEST=`$AWK_CMD -f $AWK_SCRIPTS/check-awk.awk`
284
if [ $? != 0 ] ; then
285
    echo "ERROR: Could not run '$AWK_CMD' command. Do you have it installed properly?"
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    exit 1
fi
if [ "$AWK_TEST" != "Pass" ] ; then
    echo "ERROR: Your version of 'awk' is obsolete. Please use --awk=<file> to point to Nawk or Gawk!"
    exit 1
fi

# Name of the manifest file
MANIFEST=AndroidManifest.xml

# Find the root of the application project.
if [ -n "$OPTION_PROJECT" ] ; then
    PROJECT=$OPTION_PROJECT
    log "Using specified project path: $PROJECT"
    if [ ! -d "$PROJECT" ] ; then
        echo "ERROR: Your --project option does not point to a directory!"
        exit 1
    fi
    if [ ! -f "$PROJECT/$MANIFEST" ] ; then
        echo "ERROR: Your --project does not point to an Android project path!"
        echo "       It is missing a $MANIFEST file."
        exit 1
    fi
else
    # Assume we are in the project directory
    if [ -f "$MANIFEST" ] ; then
        PROJECT=.
    else
        PROJECT=
        CURDIR=`pwd`
        while [ "$CURDIR" != "/" ] ; do
            if [ -f "$CURDIR/$MANIFEST" ] ; then
                PROJECT="$CURDIR"
                break
            fi
            CURDIR=`dirname $CURDIR`
        done
        if [ -z "$PROJECT" ] ; then
            echo "ERROR: Launch this script from an application project directory, or use --project=<path>."
            exit 1
        fi
    fi
    log "Using auto-detected project path: $PROJECT"
fi

# Extract the package name from the manifest
332
PACKAGE_NAME=`run_awk_manifest_script extract-package-name.awk`
333 334 335 336 337 338 339
log "Found package name: $PACKAGE_NAME"
if [ $? != 0 -o "$PACKAGE_NAME" = "<none>" ] ; then
    echo "ERROR: Could not extract package name from $PROJECT/$MANIFEST."
    echo "       Please check that the file is well-formed!"
    exit 1
fi

340 341 342 343 344 345 346
# If --launch-list is used, list all launchable activities, and be done with it
if [ "$OPTION_LAUNCH_LIST" = "yes" ] ; then
    log "Extracting list of launchable activities from manifest:"
    run_awk_manifest_script extract-launchable.awk
    exit 0
fi

347 348 349 350 351 352 353 354
APP_ABIS=`get_build_var APP_ABI`
log "ABIs targetted by application: $APP_ABIS"

# Check the ADB command, and that we can connect to the device/emulator
ADB_TEST=`$ADB_CMD shell ls`
if [ $? != 0 ] ; then
    echo "ERROR: Could not connect to device or emulator!"
    echo "       Please check that an emulator is running or a device is connected"
355
    echo "       through USB to this machine. You can use -e, -d and -s <serial>"
356 357 358 359
    echo "       in case of multiple ones."
    exit 1
fi

360 361 362 363
# Check that the device is running Froyo (API Level 8) or higher
#
API_LEVEL=`adb_shell getprop ro.build.version.sdk`
if [ $? != 0 -o -z "$API_LEVEL" ] ; then
364
    echo "ERROR: Could not find target device's supported API level!"
365 366 367
    echo "ndk-gdb will only work if your device is running Android 2.2 or higher."
    exit 1
fi
368
log "Device API Level: $API_LEVEL"
369 370
if [ "$API_LEVEL" -lt "8" ] ; then
    echo "ERROR: ndk-gdb requires a target device running Android 2.2 (API level 8) or higher."
371
    echo "The target device is running API level $API_LEVEL!"
372 373 374
    exit 1
fi

375 376 377 378
# Get the target device's supported ABI(s)
# And check that they are supported by the application
#
COMPAT_ABI=none
379
CPU_ABI=`adb_shell getprop ro.product.cpu.abi`
380 381 382 383 384 385
for ABI in $APP_ABIS; do
    if [ "$ABI" = "$CPU_ABI" ] ; then
        COMPAT_ABI=$CPU_ABI
        break
    fi
done
386

387
CPU_ABI2=`adb_shell getprop ro.product.cpu.abi2`
388 389 390 391
if [ -z "$CPU_ABI2" ] ; then
    log "Device CPU ABI: $CPU_ABI"
else
    log "Device CPU ABIs: $CPU_ABI $CPU_ABI2"
392 393 394 395 396 397 398
    if [ "$COMPAT_ABI" = "none" ] ; then
        for ABI in $APP_ABIS; do
            if [ "$ABI" = "$CPU_ABI2" ] ; then
                COMPAT_ABI=$CPU_ABI2
                break
            fi
        done
399 400 401 402 403 404 405 406 407 408 409 410 411
    fi
fi
if [ "$COMPAT_ABI" = none ] ; then
    echo "ERROR: The device does not support the application's targetted CPU ABIs!"
    if [ "$CPU_ABI2" = "$CPU_ABI" ] ; then
        CPU_ABI2=
    fi
    echo "       Device supports:  $CPU_ABI $CPU_ABI2"
    echo "       Package supports: $APP_ABIS"
    exit 1
fi
log "Compatible device ABI: $COMPAT_ABI"

412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
# Check that the application is debuggable, or nothing will work
DEBUGGABLE=`run_awk_manifest_script extract-debuggable.awk`
log "Found debuggable flag: $DEBUGGABLE"
if [ $? != 0 -o "$DEBUGGABLE" != "true" ] ; then
    # If gdbserver exists, then we built with 'ndk-build NDK_DEBUG=1' and it's
    # ok to not have android:debuggable set to true in the original manifest.
    # However, if this is not the case, then complain!!
    if [ -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
        log "Found gdbserver under libs/$COMPAT_ABI, assuming app was built with NDK_DEBUG=1"
    else
        echo "ERROR: Package $PACKAGE_NAME is not debuggable ! You can fix that in two ways:"
        echo ""
        echo "  - Rebuilt with the NDK_DEBUG=1 option when calling 'ndk-build'."
        echo ""
        echo "  - Modify your manifest to set android:debuggable attribute to \"true\","
        echo "    then rebuild normally."
        echo ""
        echo "After one of these, re-install to the device!"
        exit 1
    fi
else
    # DEBUGGABLE is true in the manifest. Let's check that the user didn't change the
    # debuggable flag in the manifest without calling ndk-build afterwards.
    if [ ! -f $PROJECT/libs/$COMPAT_ABI/gdbserver ] ; then
        echo "ERROR: Could not find gdbserver binary under $PROJECT/libs/$COMPAT_ABI"
        echo "       This usually means you modified your AndroidManifest.xml to set"
        echo "       the android:debuggable flag to 'true' but did not rebuild the"
        echo "       native binaries. Please call 'ndk-build' to do so,"
        echo "       *then* re-install to the device!"
        exit 1
    fi
443 444 445 446 447
fi

# Let's check that 'gdbserver' is properly installed on the device too. If this
# is not the case, the user didn't install the proper package after rebuilding.
#
448
DEVICE_GDBSERVER=`adb_shell ls /data/data/$PACKAGE_NAME/lib/gdbserver`
449 450 451
log "Found device gdbserver: $DEVICE_GDBSERVER"
if pattern_match "No such file or directory" "$DEVICE_GDBSERVER" ] ; then
    echo "ERROR: Non-debuggable application installed on the target device."
452
    echo "       Please re-install the debuggable version!"
453 454 455
    exit 1
fi

456 457 458 459 460 461 462 463 464 465 466
# Get information from the build system
GDBSETUP_INIT=`get_build_var_for_abi NDK_APP_GDBSETUP $COMPAT_ABI`
log "Using gdb setup init: $GDBSETUP_INIT"

TOOLCHAIN_PREFIX=`get_build_var_for_abi TOOLCHAIN_PREFIX $COMPAT_ABI`
log "Using toolchain prefix: $TOOLCHAIN_PREFIX"

APP_OUT=`get_build_var_for_abi TARGET_OUT $COMPAT_ABI`
log "Using app out directory: $APP_OUT"

# Find the <dataDir> of the package on the device
467
DATA_DIR=`adb_shell run-as $PACKAGE_NAME /system/bin/sh -c pwd`
468
log "Found data directory: '$DATA_DIR'"
469
if [ $? != 0 -o -z "$DATA_DIR" ] ; then
470
    echo "ERROR: Could not extract package's data directory. Are you sure that"
471
    echo "       your installed application is debuggable?"
472 473 474
    exit 1
fi

475
# Launch the activity if needed
476
if [ "$OPTION_START" = "yes" ] ; then
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
    # If --launch is used, ignore --start, otherwise extract the first
    # launchable activity name from the manifest and use it as if --launch=<name>
    # was used instead.
    #
    if [ -z "$OPTION_LAUNCH" ] ; then
        OPTION_LAUNCH=`run_awk_manifest_script extract-launchable.awk | sed 2q`
        if [ $? != 0 ] ; then
            echo "ERROR: Could not extract name of launchable activity from manifest!"
            echo "       Try to use --launch=<name> directly instead as a work-around."
            exit 1
        fi
        log "Found first launchable activity: $OPTION_LAUNCH"
        if [ -z "$OPTION_LAUNCH" ] ; then
            echo "ERROR: It seems that your Application does not have any launchable activity!"
            echo "       Please fix your manifest file and rebuild/re-install your application."
            exit 1
        fi
    fi
fi

if [ -n "$OPTION_LAUNCH" ] ; then
    log "Launching activity: $PACKAGE_NAME/$OPTION_LAUNCH"
    run $ADB_CMD shell am start -n $PACKAGE_NAME/$OPTION_LAUNCH
    if [ $? != 0 ] ; then
        echo "ERROR: Could not launch specified activity: $OPTION_LAUNCH"
        echo "       Use --launch-list to dump a list of valid values."
        exit 1
    fi
    # Sleep a bit, it sometimes take one second to start properly
    # Note that we use the 'sleep' command on the device here.
507
    run $ADB_CMD shell sleep $DELAY
508 509
fi

510
# Find the PID of the application being run
511
PID=`$ADB_CMD shell ps | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=$PACKAGE_NAME`
512 513 514
log "Found running PID: $PID"
if [ $? != 0 -o "$PID" = "0" ] ; then
    echo "ERROR: Could not extract PID of application on device/emulator."
515
    if [ -n "$OPTION_LAUNCH" ] ; then
516 517 518 519 520 521
        echo "       Weird, this probably means one of these:"
        echo ""
        echo "         - The installed package does not match your current manifest."
        echo "         - The application process was terminated."
        echo ""
        echo "       Try using the --verbose option and look at its output for details."
522 523 524 525
    else
        echo "       Are you sure the application is already started?"
        echo "       Consider using --start or --launch=<name> if not."
    fi
526 527 528 529 530 531 532 533 534 535 536
    exit 1
fi

# Check that there is no other instance of gdbserver running
GDBSERVER_PS=`$ADB_CMD shell ps | grep lib/gdbserver`
if [ -n "$GDBSERVER_PS" ] ; then
    if [ "$OPTION_FORCE" = "no" ] ; then
        echo "ERROR: Another debug session running, Use --force to kill it."
        exit 1
    fi
    log "Killing existing debugging session"
537
    GDBSERVER_PID=`echo $GDBSERVER_PS | $AWK_CMD -f $AWK_SCRIPTS/extract-pid.awk -v PACKAGE=lib/gdbserver`
538 539 540 541 542 543 544 545 546
    if [ $GDBSERVER_PID != 0 ] ; then
        run $ADB_CMD shell kill -9 $GDBSERVER_PID
    fi
fi

# Launch gdbserver now
DEBUG_SOCKET=debug-socket
run $ADB_CMD shell run-as $PACKAGE_NAME lib/gdbserver +$DEBUG_SOCKET --attach $PID &
if [ $? != 0 ] ; then
547
    echo "ERROR: Could not launch gdbserver on the device?"
548 549 550 551 552 553 554 555
    exit 1
fi
log "Launched gdbserver succesfully."

# Setup network redirection
log "Setup network redirection"
run $ADB_CMD forward tcp:$DEBUG_PORT localfilesystem:$DATA_DIR/$DEBUG_SOCKET
if [ $? != 0 ] ; then
556 557
    echo "ERROR: Could not setup network redirection to gdbserver?"
    echo "       Maybe using --port=<port> to use a different TCP port might help?"
558 559 560 561 562
    exit 1
fi

# Get the app_server binary from the device
APP_PROCESS=$APP_OUT/app_process
563
run $ADB_CMD pull /system/bin/app_process `native_path $APP_PROCESS`
564 565 566 567
log "Pulled app_process from device/emulator."

run $ADB_CMD pull /system/lib/libc.so `native_path $APP_OUT/libc.so`
log "Pulled libc.so from device/emulator."
568 569 570 571 572 573

# Now launch the appropriate gdb client with the right init commands
#
GDBCLIENT=${TOOLCHAIN_PREFIX}gdb
GDBSETUP=$APP_OUT/gdb.setup
cp -f $GDBSETUP_INIT $GDBSETUP
574 575
#uncomment the following to debug the remote connection only
#echo "set debug remote 1" >> $GDBSETUP
576
echo "file `native_path $APP_PROCESS`" >> $GDBSETUP
577 578 579 580
echo "target remote :$DEBUG_PORT" >> $GDBSETUP
if [ -n "$OPTION_EXEC" ] ; then
    cat $OPTION_EXEC >> $GDBSETUP
fi
581
$GDBCLIENT -x `native_path $GDBSETUP`