blob: 1d6b6f69f1a52ad9b48331ca294e805530e427ae [file] [log] [blame]
Dan Alberta624edc2015-02-12 11:11:30 -08001#!/bin/bash
2# TODO:
3# 1. Check for ANDROID_SERIAL/multiple devices
4
5if [ -z "$ANDROID_BUILD_TOP" ]; then
6 >&2 echo '$ANDROID_BUILD_TOP is not set. Source build/envsetup.sh.'
7 exit 1
8fi
9
10# We can use environment variables (like ANDROID_BUILD_TOP) from the user's
11# shell, but not functions (like gettop), so we need to source envsetup in here
12# as well.
13source $ANDROID_BUILD_TOP/build/envsetup.sh
Elliott Hughesef3f1e22015-07-24 10:32:07 -070014echo
Dan Alberta624edc2015-02-12 11:11:30 -080015
Nikola Veljkovic4efdec62015-07-09 11:24:10 +020016function adb_get_product_device() {
17 local candidate=`adb shell getprop ro.hardware | tr -d '\r\n'`
18 if [[ "$candidate" =~ ^(goldfish|ranchu)$ ]]; then
19 # Emulator builds use product.device for OUT folder
20 candidate=`adb shell getprop ro.product.device | tr -d '\r\n'`
21 fi
22 echo $candidate
23}
24
Dan Alberta624edc2015-02-12 11:11:30 -080025# returns 0 when process is not traced
26function adb_get_traced_by() {
Christopher Ferris8981aee2015-05-20 19:36:54 -070027 echo `adb shell cat /proc/$1/status | grep -e "^TracerPid:" | sed "s/^TracerPid:\t//" | tr -d '\r\n'`
Dan Alberta624edc2015-02-12 11:11:30 -080028}
29
30function get_symbols_directory()
31{
32 echo $(get_abs_build_var TARGET_OUT_UNSTRIPPED)
33}
34
35function gdbwrapper()
36{
37 local GDB_CMD="$1"
38 shift 1
39 $GDB_CMD -x "$@"
40}
41
42function gdbclient() {
43 local PROCESS_NAME="n/a"
44 local PID=$1
45 local PORT=5039
46 if [ -z "$PID" ]; then
47 echo "Usage: gdbclient <pid|processname> [port number]"
48 return -1
49 fi
Nikola Veljkovic4efdec62015-07-09 11:24:10 +020050 local DEVICE=$(adb_get_product_device)
Dan Alberta624edc2015-02-12 11:11:30 -080051
52 if [ -z "$DEVICE" ]; then
53 echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
54 return -2
55 fi
56
57 if [ -n "$2" ]; then
58 PORT=$2
59 fi
60
61 local ROOT=$(gettop)
62 if [ -z "$ROOT" ]; then
63 # This is for the situation with downloaded symbols (from the build server)
64 # we check if they are available.
65 ROOT=`realpath .`
66 fi
67
68 local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
69 local SYMBOLS_DIR="$OUT_ROOT/symbols"
70 local IS_TAPAS_USER="$(get_build_var TARGET_BUILD_APPS)"
71 local TAPAS_SYMBOLS_DIR=
72
73 if [ $IS_TAPAS_USER ]; then
74 TAPAS_SYMBOLS_DIR=$(get_symbols_directory)
75 fi
76
77 if [ ! -d $SYMBOLS_DIR ]; then
78 if [ $IS_TAPAS_USER ]; then
79 mkdir -p $SYMBOLS_DIR/system/bin
80 else
81 echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
82 return -3
83 fi
84 fi
85
86 # let's figure out which executable we are about to debug
87
88 # check if user specified a name -> resolve to pid
89 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
90 PROCESS_NAME=$PID
91 PID=$(pid --exact $PROCESS_NAME)
92 if [ -z "$PID" ]; then
93 echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
94 return -4
95 else
96 echo "Resolved pid for $PROCESS_NAME is $PID"
97 fi
98 fi
99
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700100 local ID=`adb shell id -u`
101 if [ "$ID" != "0" ]; then
102 echo "Error: gdbclient only works if you've run 'adb root'"
103 return -4
104 fi
Dan Alberta624edc2015-02-12 11:11:30 -0800105
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700106 local EXE=`adb shell readlink /proc/$PID/exe | tr -d '\r\n'`
Dan Alberta624edc2015-02-12 11:11:30 -0800107 if [ -z "$EXE" ]; then
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700108 echo "Error: couldn't find executable for pid $PID --- is the process still alive?"
Dan Alberta624edc2015-02-12 11:11:30 -0800109 return -4
110 fi
111
112 local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE
113
114 if [ ! -f $LOCAL_EXE_PATH ]; then
115 if [ $IS_TAPAS_USER ]; then
116 adb pull $EXE $LOCAL_EXE_PATH
117 else
118 echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
119 return -5
120 fi
121 fi
122
123 local USE64BIT=""
124
125 if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
126 USE64BIT="64"
127 fi
128
129 # and now linker for tapas users...
130 if [ -n "$IS_TAPAS_USER" -a ! -f "$SYMBOLS_DIR/system/bin/linker$USE64BIT" ]; then
131 adb pull /system/bin/linker$USE64BIT $SYMBOLS_DIR/system/bin/linker$USE64BIT
132 fi
133
Josh Gao409ab9f2015-12-17 12:06:21 -0800134 local GDB
135 case $(uname -s) in
136 Darwin)
137 GDB=$ANDROID_BUILD_TOP/prebuilts/gdb/darwin-x86/bin/gdb
138 ;;
139
140 Linux)
141 GDB=$ANDROID_BUILD_TOP/prebuilts/gdb/linux-x86/bin/gdb
142 ;;
143
144 *)
145 echo "Error: Unknown platform '$(uname -s)'"
146 return 1
147 ;;
148 esac
149
Christopher Ferris8981aee2015-05-20 19:36:54 -0700150 local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | tr -d '\r\n'`
Dan Alberta624edc2015-02-12 11:11:30 -0800151
152 # TODO: check if tracing process is gdbserver and not some random strace...
153 if [ "$(adb_get_traced_by $PID)" -eq 0 ]; then
154 # start gdbserver
155 echo "Starting gdbserver..."
156 # TODO: check if adb is already listening $PORT
157 # to avoid unnecessary calls
158 echo ". adb forward for port=$PORT..."
159 adb forward tcp:$PORT tcp:$PORT
160 echo ". starting gdbserver to attach to pid=$PID..."
161 adb shell gdbserver$USE64BIT :$PORT --attach $PID &
162 echo ". give it couple of seconds to start..."
163 sleep 2
164 echo ". done"
165 else
166 echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
167 adb forward tcp:$PORT tcp:$PORT
168 fi
169
170 local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
171 local TAPAS_OUT_SO_SYMBOLS=$TAPAS_SYMBOLS_DIR/system/lib$USE64BIT
172 local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
173 local ART_CMD=""
174
175 local SOLIB_SYSROOT=$SYMBOLS_DIR
176 local SOLIB_SEARCHPATH=$OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl
177
178 if [ $IS_TAPAS_USER ]; then
179 SOLIB_SYSROOT=$TAPAS_SYMBOLS_DIR:$SOLIB_SYSROOT
180 SOLIB_SEARCHPATH=$TAPAS_OUT_SO_SYMBOLS:$SOLIB_SEARCHPATH
181 fi
182
183 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SOLIB_SYSROOT"
184 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $SOLIB_SEARCHPATH"
185 local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
186 if [ -f $DALVIK_GDB_SCRIPT ]; then
187 echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
188 ART_CMD="art-on"
189 else
190 echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
191 fi
192 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
193 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
194 echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
195 fi
196
197 echo >>"$OUT_ROOT/gdbclient.cmds" ""
198
Josh Gao409ab9f2015-12-17 12:06:21 -0800199 gdbwrapper $GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
Dan Alberta624edc2015-02-12 11:11:30 -0800200}
201
202gdbclient $*