| OLD | NEW | 
|---|
|  | (Empty) | 
| 1 #!/bin/bash |  | 
| 2 # |  | 
| 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 |  | 
| 5 # found in the LICENSE file. |  | 
| 6 # |  | 
| 7 # Attach gdb to a running android application.  Similar to ndk-gdb. |  | 
| 8 # Run with --annotate=3 if running under emacs (M-x gdb). |  | 
| 9 # |  | 
| 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. |  | 
| 12 # For *unittests_apk (like base_unittests_apk), run with: |  | 
| 13 #  "gdb_apk -p org.chromium.native_test -l out/Release/lib.target -r" |  | 
| 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 # args: command to run |  | 
| 19 # Prints the command's stdout on stdout |  | 
| 20 # Returns the command's status |  | 
| 21 # Note: the command's stderr is lost |  | 
| 22 adb_shell () { |  | 
| 23   local TMPOUT="$(mktemp)" |  | 
| 24   local LASTLINE RET |  | 
| 25   local ADB=${ADB:-adb} |  | 
| 26 |  | 
| 27   # The weird sed rule is to strip the final \r on each output line |  | 
| 28   # Since 'adb shell' never returns the command's proper exit/status code, |  | 
| 29   # we force it to print it as '%%<status>' in the temporary output file, |  | 
| 30   # which we will later strip from it. |  | 
| 31   $ADB shell $@ ";" echo "%%\$?" 2>/dev/null | sed -e 's![[:cntrl:]]!!g' > $TMPO
     UT |  | 
| 32   # Get last line in log, which contains the exit code from the command |  | 
| 33   LASTLINE=$(sed -e '$!d' $TMPOUT) |  | 
| 34   # Extract the status code from the end of the line, which must be '%%<code>' |  | 
| 35   RET=$(echo "$LASTLINE" | awk '{ if (match($0, "%%[0-9]+$")) { print substr($0,
     RSTART+2); } }') |  | 
| 36   # Remove the status code from the last line. Note that this may result in an e
     mpty line |  | 
| 37   LASTLINE=$(echo "$LASTLINE" | awk '{ if (match($0, "%%[0-9]+$")) { print subst
     r($0,1,RSTART-1); } }') |  | 
| 38   # The output itself: all lines except the status code |  | 
| 39   sed -e '$d' $TMPOUT && echo -n "$LASTLINE" |  | 
| 40   # Remove temp file |  | 
| 41   rm -f $TMPOUT |  | 
| 42   # Exit with the appropriate status |  | 
| 43   return $RET |  | 
| 44 } |  | 
| 45 |  | 
| 46 adb=$(which adb) |  | 
| 47 if [[ "$adb" = "" ]] ; then |  | 
| 48   echo "Need adb in your path" |  | 
| 49   exit 1 |  | 
| 50 fi |  | 
| 51 |  | 
| 52 usage() { |  | 
| 53   echo "usage: ${0##*/} [-p package_name] [-l shared_lib_dir] [-g gdb] [-r]" |  | 
| 54   echo "-p package_name     the android APK package to be debugged" |  | 
| 55   echo "-l shared_lib_dir   directory containes native shared library" |  | 
| 56   echo "-g gdb_args         agruments for gdb, eg: -g '-n -write'" |  | 
| 57   echo "-r                  the target device is rooted" |  | 
| 58 } |  | 
| 59 |  | 
| 60 process_options() { |  | 
| 61   local OPTNAME OPTIND OPTERR OPTARG |  | 
| 62   while getopts ":p:l:g:r" OPTNAME; do |  | 
| 63     case "$OPTNAME" in |  | 
| 64       p) |  | 
| 65         package_name="$OPTARG" |  | 
| 66         ;; |  | 
| 67       l) |  | 
| 68         shared_lib_dir="$OPTARG" |  | 
| 69         ;; |  | 
| 70       g) |  | 
| 71         gdb_args="$OPTARG" |  | 
| 72                 ;; |  | 
| 73       r) |  | 
| 74         rooted_phone=1 |  | 
| 75         ;; |  | 
| 76       \:) |  | 
| 77         echo "'-$OPTARG' needs an argument." |  | 
| 78         usage |  | 
| 79         exit 1 |  | 
| 80         ;; |  | 
| 81       *) |  | 
| 82         echo "invalid command line option: $OPTARG" |  | 
| 83         usage |  | 
| 84         exit 1 |  | 
| 85         ;; |  | 
| 86     esac |  | 
| 87   done |  | 
| 88 |  | 
| 89   if [ $# -ge ${OPTIND} ]; then |  | 
| 90     eval echo "Unexpected command line argument: \${${OPTIND}}" |  | 
| 91     usage |  | 
| 92     exit 1 |  | 
| 93   fi |  | 
| 94 } |  | 
| 95 |  | 
| 96 rooted_phone=0 |  | 
| 97 |  | 
| 98 root=$(dirname $0)/../.. |  | 
| 99 package_name=org.chromium.content_shell_apk |  | 
| 100 shared_lib_dir=$root/out/${BUILDTYPE:-Debug}/lib.target |  | 
| 101 gdb_args='' |  | 
| 102 |  | 
| 103 #process options |  | 
| 104 process_options "$@" |  | 
| 105 echo "Debug package $package_name" |  | 
| 106 echo "Assume native shared library is under $shared_lib_dir" |  | 
| 107 |  | 
| 108 data_dir=/data/data/$package_name |  | 
| 109 gdb_server_on_device=$data_dir/lib/gdbserver |  | 
| 110 |  | 
| 111 # Kill any running gdbserver |  | 
| 112 pid=$(adb shell ps | awk '/gdbserver/ {print $2}') |  | 
| 113 if [[ "$pid" != "" ]] ; then |  | 
| 114   if [[ $rooted_phone -eq 1 ]] ; then |  | 
| 115     adb shell kill $pid |  | 
| 116   else |  | 
| 117     adb shell run-as $package_name kill $pid |  | 
| 118   fi |  | 
| 119 fi |  | 
| 120 |  | 
| 121 pid=$(adb_shell ps | awk "/$package_name$/ {print \$2}") |  | 
| 122 if [[ "$pid" = "" ]] ; then |  | 
| 123   echo "No $package_name running?" |  | 
| 124   echo "Try this: adb shell am start -a android.intent.action.VIEW " \ |  | 
| 125     "-n $package_name/.SomethingActivity (Something might be ContentShell)" |  | 
| 126   exit 2 |  | 
| 127 fi |  | 
| 128 |  | 
| 129 no_gdb_server=$(adb shell ls $gdb_server_on_device | grep 'No such file') |  | 
| 130 if [[ "$no_gdb_server" != "" ]] ; then |  | 
| 131   echo "No gdb server on device at $gdb_server_on_device" |  | 
| 132   echo "Please install a debug build." |  | 
| 133   exit 3 |  | 
| 134 fi |  | 
| 135 |  | 
| 136 if [[ $rooted_phone -eq 1 ]] ; then |  | 
| 137   adb shell $gdb_server_on_device :4321 --attach $pid & |  | 
| 138   adb forward tcp:4321 tcp:4321 |  | 
| 139 else |  | 
| 140   adb shell run-as $package_name lib/gdbserver +debug-socket --attach $pid & |  | 
| 141   adb forward tcp:4321 localfilesystem:$data_dir/debug-socket |  | 
| 142 fi |  | 
| 143 sleep 2 |  | 
| 144 |  | 
| 145 # Pull app_process and C libraries from device if needed |  | 
| 146 app_process=${shared_lib_dir}/app_process |  | 
| 147 if [[ ! -f ${app_process} ]] ; then |  | 
| 148   adb pull /system/bin/app_process ${app_process} |  | 
| 149   adb pull /system/lib/libc.so ${shared_lib_dir} |  | 
| 150 fi |  | 
| 151 |  | 
| 152 # gdb commands |  | 
| 153 cmdfile=$(mktemp /tmp/gdb_android_XXXXXXXX) |  | 
| 154 cat >$cmdfile<<EOF |  | 
| 155 # set solib-absolute-prefix null |  | 
| 156 set solib-search-path ${shared_lib_dir} |  | 
| 157 file ${app_process} |  | 
| 158 target remote :4321 |  | 
| 159 EOF |  | 
| 160 |  | 
| 161 gdb=$(echo $ANDROID_TOOLCHAIN/*gdb) |  | 
| 162 if [[ ! -f ${gdb} ]] ; then |  | 
| 163   echo "Wow no gdb in env var ANDROID_TOOLCHAIN which is $ANDROID_TOOLCHAIN" |  | 
| 164   exit 4 |  | 
| 165 else |  | 
| 166   echo Using $gdb |  | 
| 167 fi |  | 
| 168 |  | 
| 169 # ${gdb} -x $cmdfile $* $app_process |  | 
| 170 ${gdb} -x $cmdfile $gdb_args |  | 
| 171 rm $cmdfile |  | 
| OLD | NEW | 
|---|