aboutsummaryrefslogtreecommitdiffstats
path: root/java/debug.sh
diff options
context:
space:
mode:
Diffstat (limited to 'java/debug.sh')
-rwxr-xr-xjava/debug.sh155
1 files changed, 99 insertions, 56 deletions
diff --git a/java/debug.sh b/java/debug.sh
index aa80aeeebcd..7008664c049 100755
--- a/java/debug.sh
+++ b/java/debug.sh
@@ -93,7 +93,7 @@ while [ $# -gt 0 ]; do
93 shift 93 shift
94done 94done
95 95
96if [ -z $devices ]; then 96if [ -z "$devices" ]; then
97 echo "No devices are available." 97 echo "No devices are available."
98 exit 1 98 exit 1
99fi 99fi
@@ -117,25 +117,43 @@ if [ -z $app_data_dir ]; then
117 echo "Is it installed?" 117 echo "Is it installed?"
118fi 118fi
119 119
120echo "Found application data directory at $app_data_dir..." 120echo "Found application data directory at" "$app_data_dir"
121 121
122# Find which PIDs are associated with org.gnu.emacs 122# Generate an awk script to extract PIDs from Android ps output. It
123package_uid=`adb -s $device shell run-as $package id -u` 123# is enough to run `ps' as the package user on newer versions of
124 124# Android, but that doesn't work on Android 2.3.
125if [ -z $package_uid ]; then 125cat << EOF > tmp.awk
126 echo "Failed to obtain UID of packages named $package" 126BEGIN {
127 exit 1 127 pid = 0;
128fi 128 pid_column = 2;
129 129}
130# First, run ps -u $package_uid -o PID,CMD to fetch the list of 130
131# process IDs. 131{
132package_pids=`adb -s $device shell run-as $package ps -u $package_uid -o PID,CMD` 132 # Remove any trailing carriage return from the input line.
133 133 gsub ("\r", "", \$NF)
134# Next, remove lines matching "ps" itself. 134
135package_pids=`awk -- '{ 135 # If this is line 1, figure out which column contains the PID.
136 if (!match ($0, /(PID|ps)/)) 136 if (NR == 1)
137 print $1 137 {
138}' <<< $package_pids` 138 for (n = 1; n <= NF; ++n)
139 {
140 if (\$n == "PID")
141 pid_column=n;
142 }
143 }
144 else if (\$NF == "$package")
145 print \$pid_column
146}
147EOF
148
149# Make sure that file disappears once this script exits.
150trap "rm -f $(pwd)/tmp.awk" 0
151
152# First, run ps to fetch the list of process IDs.
153package_pids=`adb -s $device shell ps`
154
155# Next, extract the list of PIDs currently running.
156package_pids=`awk -f tmp.awk <<< $package_pids`
139 157
140if [ "$attach_existing" != "yes" ]; then 158if [ "$attach_existing" != "yes" ]; then
141 # Finally, kill each existing process. 159 # Finally, kill each existing process.
@@ -149,19 +167,20 @@ if [ "$attach_existing" != "yes" ]; then
149 echo "Starting activity $activity and attaching debugger" 167 echo "Starting activity $activity and attaching debugger"
150 168
151 # Exit if the activity could not be started. 169 # Exit if the activity could not be started.
152 adb -s $device shell am start -D "$package/$activity" 170 adb -s $device shell am start -D -n "$package/$activity"
153 if [ ! $? ]; then 171 if [ ! $? ]; then
154 exit 1; 172 exit 1;
155 fi 173 fi
156 174
175 # Sleep for a bit. Otherwise, the process may not have started
176 # yet.
177 sleep 1
178
157 # Now look for processes matching the package again. 179 # Now look for processes matching the package again.
158 package_pids=`adb -s $device shell run-as $package ps -u $package_uid -o PID,CMD` 180 package_pids=`adb -s $device shell ps`
159 181
160 # Next, remove lines matching "ps" itself. 182 # Next, remove lines matching "ps" itself.
161 package_pids=`awk -- '{ 183 package_pids=`awk -f tmp.awk <<< $package_pids`
162 if (!match ($0, /(PID|ps)/))
163 print $1
164}' <<< $package_pids`
165fi 184fi
166 185
167pid=$package_pids 186pid=$package_pids
@@ -170,10 +189,10 @@ num_pids=`wc -w <<< "$package_pids"`
170if [ $num_pids -gt 1 ]; then 189if [ $num_pids -gt 1 ]; then
171 echo "More than one process was started:" 190 echo "More than one process was started:"
172 echo "" 191 echo ""
173 adb -s $device shell run-as $package ps -u $package_uid | awk -- '{ 192 adb -s $device shell run-as $package ps | awk -- "{
174 if (!match ($0, /ps/)) 193 if (!match (\$0, /ps/) && match (\$0, /$package/))
175 print $0 194 print \$0
176 }' 195 }"
177 echo "" 196 echo ""
178 printf "Which one do you want to attach to? " 197 printf "Which one do you want to attach to? "
179 read pid 198 read pid
@@ -182,10 +201,12 @@ elif [ -z $package_pids ]; then
182 exit 1 201 exit 1
183fi 202fi
184 203
185# This isn't necessary when attaching gdb to an existing process. 204# If either --jdb was specified or debug.sh is not connecting to an
205# existing process, then store a suitable JDB invocation in
206# jdb_command. GDB will then run JDB to unblock the application from
207# the wait dialog after startup.
208
186if [ "$jdb" = "yes" ] || [ "$attach_existing" != yes ]; then 209if [ "$jdb" = "yes" ] || [ "$attach_existing" != yes ]; then
187 # Start JDB to make the wait dialog disappear.
188 echo "Attaching JDB to unblock the application."
189 adb -s $device forward --remove-all 210 adb -s $device forward --remove-all
190 adb -s $device forward "tcp:$jdb_port" "jdwp:$pid" 211 adb -s $device forward "tcp:$jdb_port" "jdwp:$pid"
191 212
@@ -203,20 +224,42 @@ if [ "$jdb" = "yes" ] || [ "$attach_existing" != yes ]; then
203 $jdb_command 224 $jdb_command
204 exit 1 225 exit 1
205 fi 226 fi
227fi
206 228
207 exec 4<> /tmp/file-descriptor-stamp 229if [ -n "$jdb_command" ]; then
230 echo "Starting JDB to unblock application."
208 231
209 # Now run JDB with IO redirected to file descriptor 4 in a subprocess. 232 # Start JDB to unblock the application.
210 $jdb_command <&4 >&4 & 233 coproc JDB { $jdb_command; }
211 234
212 character= 235 # Tell JDB to first suspend all threads.
213 # Next, wait until the prompt is found. 236 echo "suspend" >&${JDB[1]}
214 while read -n1 -u 4 character; do 237
215 if [ "$character" = ">" ]; then 238 # Tell JDB to print a magic string once the program is
216 echo "JDB attached successfully" 239 # initialized.
217 break; 240 echo "print \"__verify_jdb_has_started__\"" >&${JDB[1]}
241
242 # Now wait for JDB to give the string back.
243 line=
244 while :; do
245 read -u ${JDB[0]} line
246 if [ ! $? ]; then
247 echo "Failed to read JDB output"
248 exit 1
218 fi 249 fi
250
251 case "$line" in
252 *__verify_jdb_has_started__*)
253 # Android only polls for a Java debugger every 200ms, so
254 # the debugger must be connected for at least that long.
255 echo "Pausing 1 second for the program to continue."
256 sleep 1
257 break
258 ;;
259 esac
219 done 260 done
261
262 # Note that JDB does not exit until GDB is fully attached!
220fi 263fi
221 264
222# See if gdbserver has to be uploaded 265# See if gdbserver has to be uploaded
@@ -234,18 +277,19 @@ fi
234 277
235echo "Attaching gdbserver to $pid on $device..." 278echo "Attaching gdbserver to $pid on $device..."
236exec 5<> /tmp/file-descriptor-stamp 279exec 5<> /tmp/file-descriptor-stamp
280rm -f /tmp/file-descriptor-stamp
237 281
238if [ -z "$gdbserver" ]; then 282if [ -z "$gdbserver" ]; then
239 adb -s $device shell run-as $package $gdbserver_bin --once \ 283 adb -s $device shell run-as $package $gdbserver_bin --once \
240 "+debug.$package_uid.socket" --attach $pid >&5 & 284 "+debug.$package.socket" --attach $pid >&5 &
241 gdb_socket="localfilesystem:$app_data_dir/debug.$package_uid.socket" 285 gdb_socket="localfilesystem:$app_data_dir/debug.$package.socket"
242else 286else
243 # Normally the program cannot access $gdbserver_bin when it is 287 # Normally the program cannot access $gdbserver_bin when it is
244 # placed in /data/local/tmp. 288 # placed in /data/local/tmp.
245 adb -s $device shell $gdbserver_bin --once \ 289 adb -s $device shell $gdbserver_bin --once \
246 "+/data/local/tmp/debug.$package_uid.socket" \ 290 "+/data/local/tmp/debug.$package.socket" \
247 --attach $pid >&5 & 291 --attach $pid >&5 &
248 gdb_socket="localfilesystem:/data/local/tmp/debug.$package_uid.socket" 292 gdb_socket="localfilesystem:/data/local/tmp/debug.$package.socket"
249fi 293fi
250 294
251# Wait until gdbserver successfully runs. 295# Wait until gdbserver successfully runs.
@@ -256,7 +300,7 @@ while read -u 5 line; do
256 break; 300 break;
257 ;; 301 ;;
258 *error* | *Error* | failed ) 302 *error* | *Error* | failed )
259 echo $line 303 echo "GDB error:" $line
260 exit 1 304 exit 1
261 ;; 305 ;;
262 * ) 306 * )
@@ -264,19 +308,18 @@ while read -u 5 line; do
264 esac 308 esac
265done 309done
266 310
267if [ "$attach_existing" != "yes" ]; then 311# Now that GDB is attached, tell the Java debugger to resume execution
268 # Send EOF to JDB to make it go away. This will also cause 312# and then exit.
269 # Android to allow Emacs to continue executing. 313
270 echo "Making JDB go away..." 314if [ -n "$jdb_command" ]; then
271 echo "exit" >&4 315 echo "resume" >&${JDB[1]}
272 read -u 4 line 316 echo "exit" >&${JDB[1]}
273 echo "JDB has gone away with $line"
274fi 317fi
275 318
276# Forward the gdb server port here. 319# Forward the gdb server port here.
277adb -s $device forward "tcp:$gdb_port" $gdb_socket 320adb -s $device forward "tcp:$gdb_port" $gdb_socket
278if [ ! $? ]; then 321if [ ! $? ]; then
279 echo "Failed to forward $app_data_dir/debug.$package_uid.socket" 322 echo "Failed to forward $app_data_dir/debug.$package.socket"
280 echo "to $gdb_port! Perhaps you need to specify a different port" 323 echo "to $gdb_port! Perhaps you need to specify a different port"
281 echo "with --port?" 324 echo "with --port?"
282 exit 1; 325 exit 1;
@@ -284,4 +327,4 @@ fi
284 327
285# Finally, start gdb with any extra arguments needed. 328# Finally, start gdb with any extra arguments needed.
286cd "$oldpwd" 329cd "$oldpwd"
287gdb --eval-command "" --eval-command "target remote localhost:$gdb_port" $gdbargs 330gdb --eval-command "target remote localhost:$gdb_port" $gdbargs