gdb_apk 5.0 KB

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