blob: e4fbfffe194dab4af6d556ecc340587b53f404e8 [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
Dan Alberta624edc2015-02-12 11:11:30 -080052 local ROOT=$(gettop)
53 if [ -z "$ROOT" ]; then
54 # This is for the situation with downloaded symbols (from the build server)
55 # we check if they are available.
56 ROOT=`realpath .`
57 fi
58
Steve Kondik3dd5f532015-12-03 13:34:59 -080059
60 if [[ -z "$DEVICE" || ! -d "$ROOT/out/target/product/$DEVICE" ]]; then
61 if [ -z "$CM_BUILD" ]; then
62 echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
63 return -2
64 fi
65 DEVICE=$CM_BUILD
66 fi
67
68 if [ -n "$2" ]; then
69 PORT=$2
70 fi
71
Alex Light35dd4d62015-12-15 15:45:01 -080072 local SYS_OUT_ROOT=$(get_build_var OUT_DIR)
73 local OUT_ROOT="${SYS_OUT_ROOT:-${OUT_DIR:-$ROOT/out}}/target/product/$DEVICE"
Dan Alberta624edc2015-02-12 11:11:30 -080074 local SYMBOLS_DIR="$OUT_ROOT/symbols"
75 local IS_TAPAS_USER="$(get_build_var TARGET_BUILD_APPS)"
76 local TAPAS_SYMBOLS_DIR=
77
78 if [ $IS_TAPAS_USER ]; then
79 TAPAS_SYMBOLS_DIR=$(get_symbols_directory)
80 fi
81
82 if [ ! -d $SYMBOLS_DIR ]; then
83 if [ $IS_TAPAS_USER ]; then
84 mkdir -p $SYMBOLS_DIR/system/bin
85 else
86 echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
87 return -3
88 fi
89 fi
90
91 # let's figure out which executable we are about to debug
Dan Alberta624edc2015-02-12 11:11:30 -080092 # check if user specified a name -> resolve to pid
93 if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
94 PROCESS_NAME=$PID
Khalid Zubair9656b2c2016-06-14 17:36:35 -070095 PIDS=( $(adb shell "pidof $PROCESS_NAME 2> /dev/null" | tr -d '\r\n') )
96 if [[ ${#PIDS[@]} == 0 ]]; then
Dan Alberta624edc2015-02-12 11:11:30 -080097 echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
98 return -4
Khalid Zubair9656b2c2016-06-14 17:36:35 -070099 elif [[ ${#PIDS[@]} != 1 ]]; then
100 echo "Error: more than one 1 PID resolved by process name: $PROCESS_NAME"
101 echo " PIDs -> ${PIDS[@]}"
102 return -5
Dan Alberta624edc2015-02-12 11:11:30 -0800103 fi
Khalid Zubair9656b2c2016-06-14 17:36:35 -0700104 PID="${PIDS[0]}"
105 echo "Resolved pid for $PROCESS_NAME is [$PID]"
Dan Alberta624edc2015-02-12 11:11:30 -0800106 fi
107
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700108 local ID=`adb shell id -u`
109 if [ "$ID" != "0" ]; then
110 echo "Error: gdbclient only works if you've run 'adb root'"
111 return -4
112 fi
Dan Alberta624edc2015-02-12 11:11:30 -0800113
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700114 local EXE=`adb shell readlink /proc/$PID/exe | tr -d '\r\n'`
Dan Alberta624edc2015-02-12 11:11:30 -0800115 if [ -z "$EXE" ]; then
Elliott Hughesef3f1e22015-07-24 10:32:07 -0700116 echo "Error: couldn't find executable for pid $PID --- is the process still alive?"
Dan Alberta624edc2015-02-12 11:11:30 -0800117 return -4
118 fi
119
120 local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE
121
122 if [ ! -f $LOCAL_EXE_PATH ]; then
123 if [ $IS_TAPAS_USER ]; then
124 adb pull $EXE $LOCAL_EXE_PATH
125 else
126 echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
127 return -5
128 fi
129 fi
130
131 local USE64BIT=""
132
133 if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
134 USE64BIT="64"
135 fi
136
137 # and now linker for tapas users...
138 if [ -n "$IS_TAPAS_USER" -a ! -f "$SYMBOLS_DIR/system/bin/linker$USE64BIT" ]; then
139 adb pull /system/bin/linker$USE64BIT $SYMBOLS_DIR/system/bin/linker$USE64BIT
140 fi
141
Josh Gao409ab9f2015-12-17 12:06:21 -0800142 local GDB
143 case $(uname -s) in
144 Darwin)
145 GDB=$ANDROID_BUILD_TOP/prebuilts/gdb/darwin-x86/bin/gdb
146 ;;
147
148 Linux)
149 GDB=$ANDROID_BUILD_TOP/prebuilts/gdb/linux-x86/bin/gdb
150 ;;
151
152 *)
153 echo "Error: Unknown platform '$(uname -s)'"
154 return 1
155 ;;
156 esac
157
Christopher Ferris8981aee2015-05-20 19:36:54 -0700158 local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | tr -d '\r\n'`
Dan Alberta624edc2015-02-12 11:11:30 -0800159
160 # TODO: check if tracing process is gdbserver and not some random strace...
161 if [ "$(adb_get_traced_by $PID)" -eq 0 ]; then
162 # start gdbserver
163 echo "Starting gdbserver..."
164 # TODO: check if adb is already listening $PORT
165 # to avoid unnecessary calls
166 echo ". adb forward for port=$PORT..."
167 adb forward tcp:$PORT tcp:$PORT
168 echo ". starting gdbserver to attach to pid=$PID..."
169 adb shell gdbserver$USE64BIT :$PORT --attach $PID &
170 echo ". give it couple of seconds to start..."
171 sleep 2
172 echo ". done"
173 else
174 echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
175 adb forward tcp:$PORT tcp:$PORT
176 fi
177
178 local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
179 local TAPAS_OUT_SO_SYMBOLS=$TAPAS_SYMBOLS_DIR/system/lib$USE64BIT
180 local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
181 local ART_CMD=""
182
183 local SOLIB_SYSROOT=$SYMBOLS_DIR
184 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
185
186 if [ $IS_TAPAS_USER ]; then
187 SOLIB_SYSROOT=$TAPAS_SYMBOLS_DIR:$SOLIB_SYSROOT
188 SOLIB_SEARCHPATH=$TAPAS_OUT_SO_SYMBOLS:$SOLIB_SEARCHPATH
189 fi
190
191 echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SOLIB_SYSROOT"
192 echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $SOLIB_SEARCHPATH"
193 local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
194 if [ -f $DALVIK_GDB_SCRIPT ]; then
195 echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
196 ART_CMD="art-on"
197 else
198 echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
199 fi
200 echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
201 if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
202 echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
203 fi
204
205 echo >>"$OUT_ROOT/gdbclient.cmds" ""
206
Josh Gao409ab9f2015-12-17 12:06:21 -0800207 gdbwrapper $GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
Dan Alberta624edc2015-02-12 11:11:30 -0800208}
209
210gdbclient $*