1#!/bin/bash
2#
3# android_gdb_app: Pushes gdbserver, launches sampleApp, and connects
4# the debugging environment.
5
6SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
7source $SCRIPT_DIR/android_setup.sh
8
9APP_NAME=${APP_ARGS[0]}
10PORT=5039
11
12source $SCRIPT_DIR/utils/setup_adb.sh
13
14
15# Forward local to remote socket connection.
16$ADB $DEVICE_SERIAL forward "tcp:$PORT" "tcp:$PORT"
17
18# We kill all previous instances of gdbserver to rid all port overriding errors.
19if [ $(uname) == "Linux" ]; then
20    $ADB $DEVICE_SERIAL shell ps | grep gdbserver | awk '{print $2}' | xargs -r $ADB $DEVICE_SERIAL shell kill
21elif [ $(uname) == "Darwin" ]; then
22    $ADB $DEVICE_SERIAL shell ps | grep gdbserver | awk '{print $2}' | xargs $ADB $DEVICE_SERIAL shell kill
23else
24    echo "Could not automatically determine OS!"
25    exit 1;
26fi
27
28# We need the debug symbols from these files
29GDB_TMP_DIR=$(pwd)/android_gdb_tmp
30mkdir -p $GDB_TMP_DIR
31echo "Copying symbol files"
32adb_pull_if_needed /system/bin/app_process $GDB_TMP_DIR
33adb_pull_if_needed /system/lib/libc.so $GDB_TMP_DIR
34adb_pull_if_needed /data/data/com.skia/lib/libskia_android.so $GDB_TMP_DIR
35adb_pull_if_needed /data/data/com.skia/lib/libSampleApp.so $GDB_TMP_DIR
36
37echo "Pushing gdbserver..."
38adb_push_if_needed $ANDROID_TOOLCHAIN/../gdbserver /data/local/tmp
39
40# Launch the app
41echo "Launching the app..."
42$ADB $DEVICE_SERIAL shell am start -n com.skia/com.skia.SkiaSampleActivity
43
44# Wait for app process to initialize
45sleep 2
46
47# Attach gdbserver to the app process
48PID=$($ADB shell ps | grep com.skia | awk '{print $2}')
49echo "Attaching to pid: $PID"
50$ADB $DEVICE_SERIAL shell /data/local/tmp/gdbserver :$PORT --attach $PID &
51
52# Wait for gdbserver
53sleep 2
54
55# Set up gdb commands
56GDBSETUP=$GDB_TMP_DIR/gdb.setup
57echo "file $GDB_TMP_DIR/app_process" >> $GDBSETUP
58echo "target remote :$PORT" >> $GDBSETUP
59echo "set solib-absolute-prefix $GDB_TMP_DIR" >> $GDBSETUP
60echo "set solib-search-path $GDB_TMP_DIR" >> $GDBSETUP
61
62# Launch gdb client
63echo "Entering gdb client shell"
64GDB_COMMAND=$(command ls "$ANDROID_TOOLCHAIN"/*-gdb | head -n1)
65"$GDB_COMMAND" -x $GDBSETUP
66
67# Clean up:
68# We could 'rm -rf $GDB_TMP_DIR', but doing so would cause subsequent debugging
69# sessions to take longer than necessary. The tradeoff is to now force the user
70# to remove the directory when they are done debugging.
71rm $GDBSETUP
72
73
74