Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 #!/bin/bash | 1 #!/bin/bash |
| 2 # | 2 # |
| 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 3 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 4 # Use of this source code is governed by a BSD-style license that can be | 4 # Use of this source code is governed by a BSD-style license that can be |
| 5 # found in the LICENSE file. | 5 # found in the LICENSE file. |
| 6 # | 6 # |
| 7 # Attach gdb to a running android application. Similar to ndk-gdb. | 7 # Attach gdb to a running android application. Similar to ndk-gdb. |
| 8 # Run with --annotate=3 if running under emacs (M-x gdb). | 8 # Run with --annotate=3 if running under emacs (M-x gdb). |
| 9 # | 9 # |
| 10 # By default it is used to debug content shell, if it is used to | 10 # By default it is used to debug content shell, if it is used to |
| 11 # debug other piceces, '-p' and '-l' options are needed. | 11 # debug other piceces, '-p' and '-l' options are needed. |
| 12 # For *unittests_apk (like base_unittests_apk), run with: | 12 # For *unittests_apk (like base_unittests_apk), run with: |
| 13 # "gdb_apk -p org.chromium.native_test -l out/Release/lib.target -r" | 13 # "gdb_apk -p org.chromium.native_test -l out/Release/lib.target -r" |
| 14 | 14 |
| 15 # Run a command through adb shell, strip the extra \r from the output | |
| 16 # and return the correct status code to detect failures. This assumes | |
| 17 # that the adb shell command prints a final \n to stdout. | |
| 18 # $1+: command to run | |
| 19 # Out: command's stdout | |
| 20 # Return: command's status | |
|
John Grabowski
2012/07/19 18:19:26
Doc is a little awkward; implies "return" is an ar
michaelbai
2012/07/20 18:52:44
Done.
| |
| 21 # Note: the command's stderr is lost | |
| 22 adb_shell () | |
|
John Grabowski
2012/07/19 18:19:26
looks like convention for this file would be
adb
michaelbai
2012/07/20 18:52:44
Done.
| |
| 23 { | |
| 24 local TMPOUT="$(mktemp)" | |
| 25 local LASTLINE RET | |
| 26 local ADB=${ADB:-adb} | |
| 27 | |
| 28 # The weird sed rule is to strip the final \r on each output line | |
| 29 # Since 'adb shell' never returns the command's proper exit/status code, | |
| 30 # we force it to print it as '%%<status>' in the temporary output file, | |
| 31 # which we will later strip from it. | |
| 32 $ADB shell $@ ";" echo "%%\$?" 2>/dev/null | sed -e 's![[:cntrl:]]!!g' > $TMPO UT | |
| 33 # Get last line in log, which contains the exit code from the command | |
| 34 LASTLINE=$(sed -e '$!d' $TMPOUT) | |
| 35 # Extract the status code from the end of the line, which must be '%%<code>' | |
| 36 RET=$(echo "$LASTLINE" | awk '{ if (match($0, "%%[0-9]+$")) { print substr($0, RSTART+2); } }') | |
| 37 # Remove the status code from the last line. Note that this may result in an e mpty line | |
| 38 LASTLINE=$(echo "$LASTLINE" | awk '{ if (match($0, "%%[0-9]+$")) { print subst r($0,1,RSTART-1); } }') | |
| 39 # The output itself: all lines except the status code | |
| 40 sed -e '$d' $TMPOUT && echo -n "$LASTLINE" | |
| 41 # Remove temp file | |
| 42 rm -f $TMPOUT | |
| 43 # Exit with the appropriate status | |
| 44 return $RET | |
| 45 } | |
| 46 | |
| 15 adb=$(which adb) | 47 adb=$(which adb) |
| 16 if [[ "$adb" = "" ]] ; then | 48 if [[ "$adb" = "" ]] ; then |
| 17 echo "Need adb in your path" | 49 echo "Need adb in your path" |
| 18 exit 1 | 50 exit 1 |
| 19 fi | 51 fi |
| 20 | 52 |
| 21 usage() { | 53 usage() { |
| 22 echo "usage: ${0##*/} [-p package_name] [-l shared_lib_dir] [-g gdb] [-r]" | 54 echo "usage: ${0##*/} [-p package_name] [-l shared_lib_dir] [-g gdb] [-r]" |
| 23 echo "-p package_name the android APK package to be debugged" | 55 echo "-p package_name the android APK package to be debugged" |
| 24 echo "-l shared_lib_dir directory containes native shared library" | 56 echo "-l shared_lib_dir directory containes native shared library" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 79 # Kill any running gdbserver | 111 # Kill any running gdbserver |
| 80 pid=$(adb shell ps | awk '/gdbserver/ {print $2}') | 112 pid=$(adb shell ps | awk '/gdbserver/ {print $2}') |
| 81 if [[ "$pid" != "" ]] ; then | 113 if [[ "$pid" != "" ]] ; then |
| 82 if [[ $rooted_phone -eq 1 ]] ; then | 114 if [[ $rooted_phone -eq 1 ]] ; then |
| 83 adb shell kill $pid | 115 adb shell kill $pid |
| 84 else | 116 else |
| 85 adb shell run-as $package_name kill $pid | 117 adb shell run-as $package_name kill $pid |
| 86 fi | 118 fi |
| 87 fi | 119 fi |
| 88 | 120 |
| 89 pid=$(adb shell ps | awk "/$package_name/ {print \$2}") | 121 pid=$(adb_shell ps | awk "/$package_name$/ {print \$2}") |
| 90 if [[ "$pid" = "" ]] ; then | 122 if [[ "$pid" = "" ]] ; then |
| 91 echo "No $package_name running?" | 123 echo "No $package_name running?" |
| 92 echo "Try this: adb shell am start -a android.intent.action.VIEW " \ | 124 echo "Try this: adb shell am start -a android.intent.action.VIEW " \ |
| 93 "-n $package_name/.SomethingActivity (Something might be ContentShell)" | 125 "-n $package_name/.SomethingActivity (Something might be ContentShell)" |
| 94 exit 2 | 126 exit 2 |
| 95 fi | 127 fi |
| 96 | 128 |
| 97 no_gdb_server=$(adb shell ls $gdb_server_on_device | grep 'No such file') | 129 no_gdb_server=$(adb shell ls $gdb_server_on_device | grep 'No such file') |
| 98 if [[ "$no_gdb_server" != "" ]] ; then | 130 if [[ "$no_gdb_server" != "" ]] ; then |
| 99 echo "No gdb server on device at $gdb_server_on_device" | 131 echo "No gdb server on device at $gdb_server_on_device" |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 130 if [[ ! -f ${gdb} ]] ; then | 162 if [[ ! -f ${gdb} ]] ; then |
| 131 echo "Wow no gdb in env var ANDROID_TOOLCHAIN which is $ANDROID_TOOLCHAIN" | 163 echo "Wow no gdb in env var ANDROID_TOOLCHAIN which is $ANDROID_TOOLCHAIN" |
| 132 exit 4 | 164 exit 4 |
| 133 else | 165 else |
| 134 echo Using $gdb | 166 echo Using $gdb |
| 135 fi | 167 fi |
| 136 | 168 |
| 137 # ${gdb} -x $cmdfile $* $app_process | 169 # ${gdb} -x $cmdfile $* $app_process |
| 138 ${gdb} -x $cmdfile $gdb_args | 170 ${gdb} -x $cmdfile $gdb_args |
| 139 rm $cmdfile | 171 rm $cmdfile |
| OLD | NEW |