aboutsummaryrefslogtreecommitdiffstats
path: root/java/debug.sh
diff options
context:
space:
mode:
Diffstat (limited to 'java/debug.sh')
-rwxr-xr-xjava/debug.sh110
1 files changed, 74 insertions, 36 deletions
diff --git a/java/debug.sh b/java/debug.sh
index 3e3e3d9c281..aa80aeeebcd 100755
--- a/java/debug.sh
+++ b/java/debug.sh
@@ -31,6 +31,7 @@ gdb_port=5039
31jdb_port=64013 31jdb_port=64013
32jdb=no 32jdb=no
33attach_existing=no 33attach_existing=no
34gdbserver=
34 35
35while [ $# -gt 0 ]; do 36while [ $# -gt 0 ]; do
36 case "$1" in 37 case "$1" in
@@ -41,6 +42,7 @@ while [ $# -gt 0 ]; do
41 echo "You must specify an argument to --device" 42 echo "You must specify an argument to --device"
42 exit 1 43 exit 1
43 fi 44 fi
45 shift
44 ;; 46 ;;
45 "--help" ) 47 "--help" )
46 echo "Usage: $progname [options] -- [gdb options]" 48 echo "Usage: $progname [options] -- [gdb options]"
@@ -50,6 +52,7 @@ while [ $# -gt 0 ]; do
50 echo " --jdb-port PORT run the JDB server on a specific port" 52 echo " --jdb-port PORT run the JDB server on a specific port"
51 echo " --jdb run JDB instead of GDB" 53 echo " --jdb run JDB instead of GDB"
52 echo " --attach-existing attach to an existing process" 54 echo " --attach-existing attach to an existing process"
55 echo " --gdbserver BINARY upload and use the specified gdbserver binary"
53 echo " --help print this message" 56 echo " --help print this message"
54 echo "" 57 echo ""
55 echo "Available devices:" 58 echo "Available devices:"
@@ -62,9 +65,18 @@ while [ $# -gt 0 ]; do
62 "--jdb" ) 65 "--jdb" )
63 jdb=yes 66 jdb=yes
64 ;; 67 ;;
68 "--gdbserver" )
69 shift
70 gdbserver=$1
71 ;;
65 "--port" ) 72 "--port" )
73 shift
66 gdb_port=$1 74 gdb_port=$1
67 ;; 75 ;;
76 "--jdb-port" )
77 shift
78 jdb_port=$1
79 ;;
68 "--attach-existing" ) 80 "--attach-existing" )
69 attach_existing=yes 81 attach_existing=yes
70 ;; 82 ;;
@@ -170,46 +182,71 @@ elif [ -z $package_pids ]; then
170 exit 1 182 exit 1
171fi 183fi
172 184
173# Start JDB to make the wait dialog disappear. 185# This isn't necessary when attaching gdb to an existing process.
174echo "Attaching JDB to unblock the application." 186if [ "$jdb" = "yes" ] || [ "$attach_existing" != yes ]; then
175adb -s $device forward --remove-all 187 # Start JDB to make the wait dialog disappear.
176adb -s $device forward "tcp:$jdb_port" "jdwp:$pid" 188 echo "Attaching JDB to unblock the application."
189 adb -s $device forward --remove-all
190 adb -s $device forward "tcp:$jdb_port" "jdwp:$pid"
177 191
178if [ ! $? ]; then 192 if [ ! $? ]; then
179 echo "Failed to forward jdwp:$pid to $jdb_port!" 193 echo "Failed to forward jdwp:$pid to $jdb_port!"
180 echo "Perhaps you need to specify a different port with --port?" 194 echo "Perhaps you need to specify a different port with --port?"
181 exit 1; 195 exit 1;
182fi 196 fi
183 197
184jdb_command="jdb -connect \ 198 jdb_command="jdb -connect \
185 com.sun.jdi.SocketAttach:hostname=localhost,port=$jdb_port" 199 com.sun.jdi.SocketAttach:hostname=localhost,port=$jdb_port"
186 200
187if [ $jdb = "yes" ]; then 201 if [ $jdb = "yes" ]; then
188 # Just start JDB and then exit 202 # Just start JDB and then exit
189 $jdb_command 203 $jdb_command
190 exit 1 204 exit 1
191fi 205 fi
192 206
193exec 4<> /tmp/file-descriptor-stamp 207 exec 4<> /tmp/file-descriptor-stamp
194 208
195# Now run JDB with IO redirected to file descriptor 4 in a subprocess. 209 # Now run JDB with IO redirected to file descriptor 4 in a subprocess.
196$jdb_command <&4 >&4 & 210 $jdb_command <&4 >&4 &
197 211
198character= 212 character=
199# Next, wait until the prompt is found. 213 # Next, wait until the prompt is found.
200while read -n1 -u 4 character; do 214 while read -n1 -u 4 character; do
201 if [ "$character" = ">" ]; then 215 if [ "$character" = ">" ]; then
202 echo "JDB attached successfully" 216 echo "JDB attached successfully"
203 break; 217 break;
204 fi 218 fi
205done 219 done
220fi
221
222# See if gdbserver has to be uploaded
223if [ -z "$gdbserver" ]; then
224 gdbserver_bin=/system/bin/gdbserver
225else
226 gdbserver_bin=/data/local/tmp/gdbserver
227
228 # Upload the specified gdbserver binary to the device.
229 adb -s $device push "$gdbserver" "$gdbserver_bin"
230 adb -s $device shell chmod +x "$gdbserver_bin"
231fi
206 232
207# Now start gdbserver on the device asynchronously. 233# Now start gdbserver on the device asynchronously.
208 234
209echo "Attaching gdbserver to $pid on $device..." 235echo "Attaching gdbserver to $pid on $device..."
210exec 5<> /tmp/file-descriptor-stamp 236exec 5<> /tmp/file-descriptor-stamp
211adb -s $device shell run-as $package /system/bin/gdbserver --once \ 237
212 "+debug.$package_uid.socket" --attach $pid >&5 & 238if [ -z "$gdbserver" ]; then
239 adb -s $device shell run-as $package $gdbserver_bin --once \
240 "+debug.$package_uid.socket" --attach $pid >&5 &
241 gdb_socket="localfilesystem:$app_data_dir/debug.$package_uid.socket"
242else
243 # Normally the program cannot access $gdbserver_bin when it is
244 # placed in /data/local/tmp.
245 adb -s $device shell $gdbserver_bin --once \
246 "+/data/local/tmp/debug.$package_uid.socket" \
247 --attach $pid >&5 &
248 gdb_socket="localfilesystem:/data/local/tmp/debug.$package_uid.socket"
249fi
213 250
214# Wait until gdbserver successfully runs. 251# Wait until gdbserver successfully runs.
215line= 252line=
@@ -227,16 +264,17 @@ while read -u 5 line; do
227 esac 264 esac
228done 265done
229 266
230# Send EOF to JDB to make it go away. This will also cause Android to 267if [ "$attach_existing" != "yes" ]; then
231# allow Emacs to continue executing. 268 # Send EOF to JDB to make it go away. This will also cause
232echo "Making JDB go away..." 269 # Android to allow Emacs to continue executing.
233echo "exit" >&4 270 echo "Making JDB go away..."
234read -u 4 line 271 echo "exit" >&4
235echo "JDB has gone away with $line" 272 read -u 4 line
273 echo "JDB has gone away with $line"
274fi
236 275
237# Forward the gdb server port here. 276# Forward the gdb server port here.
238adb -s $device forward "tcp:$gdb_port" \ 277adb -s $device forward "tcp:$gdb_port" $gdb_socket
239 "localfilesystem:$app_data_dir/debug.$package_uid.socket"
240if [ ! $? ]; then 278if [ ! $? ]; then
241 echo "Failed to forward $app_data_dir/debug.$package_uid.socket" 279 echo "Failed to forward $app_data_dir/debug.$package_uid.socket"
242 echo "to $gdb_port! Perhaps you need to specify a different port" 280 echo "to $gdb_port! Perhaps you need to specify a different port"