Android: 使用gdb debug native进程

之前写过关于如何用gdb debug brillo系统中native进程的方法->@<-。Brillo系统的版本号如果按照android官网的说法,属于M之上的系统,使用的是python脚本,而android-5.1.1_15使用的是shell脚本。进行debug时所使用的命令也稍有些不同。

  • 设置debug环境

在设置好编译环境之后,我们就可以通过gdbclient命令进行调试了。需要注意的是调试设备所运行的系统必须是当然编译环境编译出来的系统image,因为调试native进程时需要用到symbol文件,而symbol文件在${OUT}/symbols目录下。如果我们调试的是m_e_arm模拟器,则相应的symbol文件在out/target/product/mini-emulator-armv7-a-neon/symbols下面。

当前的编译环境可以用printconfig命令查看(这个命令在build/envsetup.sh中定义):

$ printconfig 
============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=5.1.1
TARGET_PRODUCT=m_e_arm
TARGET_BUILD_VARIANT=userdebug
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
TARGET_2ND_ARCH=
TARGET_2ND_ARCH_VARIANT=
TARGET_2ND_CPU_VARIANT=
HOST_ARCH=x86_64
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-13.1.0-x86_64-i386-64bit
HOST_BUILD_TYPE=release
BUILD_ID=LMY48N
OUT_DIR=out
============================================
  • 调试system_server进程

主机系统使用的是ubuntu 14.04 x86_64, 使用mac os 10.9版本貌似有问题。

在ubuntu 14.04下执行gdbclient system_server时,会有如下信息:

$ gdbclient system_server
Starting gdbserver...
. adb forward for port=5039...
. starting gdbserver to attach to pid=334...
[1] 3497
. give it couple of seconds to start...
Attached; pid = 334
Listening on port 5039
. done
GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=x86_64-linux-gnu --target=arm-linux-android".
For bug reporting instructions, please see:
<http://source.android.com/source/report-bugs.html>...
Reading symbols from /local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/bin/app_process32...done.
Remote debugging from host 127.0.0.1
Fetch16 (offset=<optimized out>, this=<optimized out>) at art/runtime/dex_instruction.h:532
532	    return insns[offset];
Breakpoint 1 at 0xb4749890: file art/runtime/fault_handler.cc, line 74.
ART debugging mode is enabled.
If you are debugging a native only process, you need to
re-enable normal SIGSEGV handling using this command:
  handle SIGSEGV print stop
(gdb) 

NOTE: 当然你也可以使用pid号进行调试如gdbclient 334,语法如下:

gdbclient <pid|processname> [port number]

当gdb/gdbserver attch进程之后,你就可以输入bt查看调用栈了:

(gdb) bt
#0  Fetch16 (offset=<optimized out>, this=<optimized out>) at art/runtime/dex_instruction.h:532
#1  VRegB_23x (this=<optimized out>) at art/runtime/dex_instruction-inl.h:329
#2  art::interpreter::ExecuteGotoImpl<true, false> (self=0xb4827800, mh=..., code_item=0x6f834c54, shadow_frame=..., result_register=...) at art/runtime/interpreter/interpreter_goto_table_impl.cc:1636
#3  0xb466a790 in Execute (result_register=..., shadow_frame=..., code_item=0x6f834c54, mh=..., self=0xb4827800) at art/runtime/interpreter/interpreter.cc:392
#4  art::interpreter::EnterInterpreterFromStub (self=self@entry=0xb4827800, mh=..., code_item=code_item@entry=0x6f834c54, shadow_frame=...) at art/runtime/interpreter/interpreter.cc:536
#5  0xb478a614 in art::artQuickToInterpreterBridge (method=<optimized out>, self=<optimized out>, sp=<optimized out>) at art/runtime/entrypoints/quick/quick_trampoline_entrypoints.cc:531
#6  0xb45aff1e in art_quick_to_interpreter_bridge () at art/runtime/arch/arm/quick_entrypoints_arm.S:1093
#7  0xb46e6404 in art::mirror::ArtMethod::Invoke (this=0xb4827800, self=0xb45aff1f <art_quick_to_interpreter_bridge+15>, args=0xbea5baec, args_size=<optimized out>, result=0x0, shorty=0xb483b410 "1")
    at art/runtime/mirror/art_method.cc:316
Backtrace stopped: previous frame inner to this frame (corrupt stack?)

或者是使用info threads查看线程,以及thread <id>切换线程等,或者其他gdb调试用的命令。

  • 调试crash的进程

执行下面这段时代会出来native crash:

  1 #define LOG_TAG "hello-jni"
  2 
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 
  6 #include <jni.h>
  7 #include <JNIHelp.h>
  8 
  9 
 10 extern "C" void Java_com_android_hello_HelloActivity_sayHello(JNIEnv *, jobject)
 11 {
 12     __builtin_trap();
 13 }

NOTE:

1. 本来想用这一段代码来说明怎么在出现debuggerd log的时候,使用gdbclient去attach进程进行在线调试的,没想在在mac os中运行emulator的时候,会出现native crash,就顺道用了。

2. __builtin_trap()会调用abort()函数,使进程出现异常终止。

当native代码出现crash时,你可以在log中看到类似如下信息:

03-10 15:39:53.052   709   709 F libc    : Fatal signal 6 (SIGABRT), code -6 in tid 709 (main)
03-10 15:39:53.159    64    64 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-10 15:39:53.160    64    64 I DEBUG   : Build fingerprint: 'Android/m_e_arm/mini-emulator-armv7-a-neon:5.1.1/LMY48N/hello03102031:userdebug/test-keys'
03-10 15:39:53.160    64    64 I DEBUG   : Revision: '0'
03-10 15:39:53.160    64    64 I DEBUG   : ABI: 'arm'
03-10 15:39:53.161    64    64 I DEBUG   : pid: 709, tid: 709, name: main  >>> /system/bin/dex2oat <<<
03-10 15:39:53.161    64    64 I DEBUG   : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
03-10 15:39:53.244    64    64 I DEBUG   : Abort message: 'art/runtime/runtime.cc:836] Check failed: options->boot_class_path_ != nullptr '
03-10 15:39:53.244    64    64 I DEBUG   :     r0 00000000  r1 000002c5  r2 00000006  r3 00000000
03-10 15:39:53.244    64    64 I DEBUG   :     r4 b6f9de38  r5 00000006  r6 00000002  r7 0000010c
03-10 15:39:53.245    64    64 I DEBUG   :     r8 00000033  r9 b5c6f490  sl b5c27400  fp b5c70340
03-10 15:39:53.245    64    64 I DEBUG   :     ip 000002c5  sp beff2158  lr b6a2b745  pc b6a4fbd0  cpsr 60000010
03-10 15:39:53.247    64    64 I DEBUG   : 
03-10 15:39:53.247    64    64 I DEBUG   : backtrace:
03-10 15:39:53.247    64    64 I DEBUG   :     #00 pc 0003abd0  /system/lib/libc.so (tgkill+12)
03-10 15:39:53.247    64    64 I DEBUG   :     #01 pc 00016741  /system/lib/libc.so (pthread_kill+52)
03-10 15:39:53.247    64    64 I DEBUG   :     #02 pc 0001735f  /system/lib/libc.so (raise+10)
03-10 15:39:53.248    64    64 I DEBUG   :     #03 pc 00013b39  /system/lib/libc.so (__libc_android_abort+36)
03-10 15:39:53.248    64    64 I DEBUG   :     #04 pc 00012f18  /system/lib/libc.so (abort+4)
03-10 15:39:53.248    64    64 I DEBUG   :     #05 pc 00216f61  /system/lib/libart.so (art::Runtime::Abort()+160)
03-10 15:39:53.249    64    64 I DEBUG   :     #06 pc 000a7c17  /system/lib/libart.so (art::LogMessage::~LogMessage()+1066)
03-10 15:39:53.249    64    64 I DEBUG   :     #07 pc 002195e7  /system/lib/libart.so (art::Runtime::Init(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+4966)
03-10 15:39:53.249    64    64 I DEBUG   :     #08 pc 0021a217  /system/lib/libart.so (art::Runtime::Create(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+46)
03-10 15:39:53.249    64    64 I DEBUG   :     #09 pc 0000829f  /system/bin/dex2oat
03-10 15:39:53.249    64    64 I DEBUG   :     #10 pc 0000c12b  /system/bin/dex2oat
03-10 15:39:53.250    64    64 I DEBUG   :     #11 pc 00012dc9  /system/lib/libc.so (__libc_init+44)
03-10 15:39:53.250    64    64 I DEBUG   :     #12 pc 00004700  /system/bin/dex2oat
03-10 15:39:53.562    64    64 I DEBUG   : 
03-10 15:39:53.562    64    64 I DEBUG   : Tombstone written to: /data/tombstones/tombstone_08

这时,你可以使用developments/scripts/stack命令,将log中的backtrace信息进行转换:

$ cat packages/apps/HelloNative/crash.txt | development/scripts/stack
Reading symbols from /Volumes/droid/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols
signal 6 (SIGABRT), code -6 in tid 709 (main)
Revision: '0'
ABI: 'arm'
pid: 709, tid: 709, name: main  >>> /system/bin/dex2oat <<<
signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
Abort message: 'art/runtime/runtime.cc:836] Check failed: options->boot_class_path_ != nullptr '
     r0 00000000  r1 000002c5  r2 00000006  r3 00000000
     r4 b6f9de38  r5 00000006  r6 00000002  r7 0000010c
     r8 00000033  r9 b5c6f490  sl b5c27400  fp b5c70340
     ip 000002c5  sp beff2158  lr b6a2b745  pc b6a4fbd0  cpsr 60000010
Using toolchain from: /Volumes/droid/android-5.1.1_r15/prebuilts/gcc/darwin-x86/arm/arm-linux-androideabi-4.8/bin

Stack Trace:
  RELADDR   FUNCTION                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        FILE:LINE
  0003abd0  tgkill+12                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       /Volumes/droid/android-5.1.1_r15/bionic/libc/arch-arm/syscalls/tgkill.S:9
  00016741  pthread_kill+52                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 /Volumes/droid/android-5.1.1_r15/bionic/libc/bionic/pthread_kill.cpp:49
  0001735f  raise+10                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        /Volumes/droid/android-5.1.1_r15/bionic/libc/bionic/raise.cpp:32
  00013b39  __libc_android_abort+36                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /Volumes/droid/android-5.1.1_r15/bionic/libc/bionic/abort.cpp:47
  00012f18  abort+4                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /Volumes/droid/android-5.1.1_r15/bionic/libc/arch-arm/bionic/abort_arm.S:43
  00216f61  art::Runtime::Abort()+160                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       /Volumes/droid/android-5.1.1_r15/art/runtime/runtime.cc:310
  000a7c17  art::LogMessage::~LogMessage()+1066                                                                                                                                                                                                                                                                                                                                                                                                                                                                             /Volumes/droid/android-5.1.1_r15/art/runtime/base/logging.cc:161
  002195e7  art::Runtime::Init(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+4966                                                                                                                                                                                           /Volumes/droid/android-5.1.1_r15/art/runtime/runtime.cc:836 (discriminator 1)
  0021a217  art::Runtime::Create(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+46                                                                                                                                                                                           /Volumes/droid/android-5.1.1_r15/art/runtime/runtime.cc:340
  v------>  art::Dex2Oat::CreateRuntime(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, art::InstructionSet)                                                                                                                                                                        /Volumes/droid/android-5.1.1_r15/art/dex2oat/dex2oat.cc:531
  0000829f  art::Dex2Oat::Create(art::Dex2Oat**, std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, art::CompilerOptions const&, art::Compiler::Kind, art::InstructionSet, art::InstructionSetFeatures, art::VerificationResults*, art::DexFileToMethodInlinerMap*, unsigned int)+106  /Volumes/droid/android-5.1.1_r15/art/dex2oat/dex2oat.cc:263
  0000c12b  art::dex2oat(int, char**)+3222                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /Volumes/droid/android-5.1.1_r15/art/dex2oat/dex2oat.cc:1372
  00012dc9  __libc_init+44                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  /Volumes/droid/android-5.1.1_r15/bionic/libc/bionic/libc_init_dynamic.cpp:113
  00004700  _start+96                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       sigchain.cc:?

当然,你也可以设置system property:

$ adb shell setprop debug.db.uid 999999                 # <= M
$ adb shell setprop debug.debuggerd.wait_for_gdb true   # > M

当出现native crash时,crash进程会等待gdb attach进行在线调试:

--------- beginning of crash
03-10 15:59:50.090   360   360 F libc    : Fatal signal 6 (SIGABRT), code -6 in tid 360 (main)
03-10 15:59:50.286    63    63 I DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
03-10 15:59:50.287    63    63 I DEBUG   : Build fingerprint: 'Android/m_e_arm/mini-emulator-armv7-a-neon:5.1.1/LMY48N/hello03102031:userdebug/test-keys'
03-10 15:59:50.287    63    63 I DEBUG   : Revision: '0'
03-10 15:59:50.287    63    63 I DEBUG   : ABI: 'arm'
03-10 15:59:50.288    63    63 I DEBUG   : pid: 360, tid: 360, name: main  >>> /system/bin/dex2oat <<<
03-10 15:59:50.288    63    63 I DEBUG   : signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------
03-10 15:59:50.437    63    63 I DEBUG   : Abort message: 'art/runtime/runtime.cc:836] Check failed: options->boot_class_path_ != nullptr '
03-10 15:59:50.437    63    63 I DEBUG   :     r0 00000000  r1 00000168  r2 00000006  r3 00000000
03-10 15:59:50.437    63    63 I DEBUG   :     r4 b6fb1e38  r5 00000006  r6 00000002  r7 0000010c
03-10 15:59:50.438    63    63 I DEBUG   :     r8 00000033  r9 b606f490  sl b6027400  fp b60702c0
03-10 15:59:50.438    63    63 I DEBUG   :     ip 00000168  sp bebba148  lr b6a3f745  pc b6a63bd0  cpsr 60000010
03-10 15:59:50.439    63    63 I DEBUG   : 
03-10 15:59:50.439    63    63 I DEBUG   : backtrace:
03-10 15:59:50.440    63    63 I DEBUG   :     #00 pc 0003abd0  /system/lib/libc.so (tgkill+12)
03-10 15:59:50.440    63    63 I DEBUG   :     #01 pc 00016741  /system/lib/libc.so (pthread_kill+52)
03-10 15:59:50.440    63    63 I DEBUG   :     #02 pc 0001735f  /system/lib/libc.so (raise+10)
03-10 15:59:50.440    63    63 I DEBUG   :     #03 pc 00013b39  /system/lib/libc.so (__libc_android_abort+36)
03-10 15:59:50.440    63    63 I DEBUG   :     #04 pc 00012f18  /system/lib/libc.so (abort+4)
03-10 15:59:50.440    63    63 I DEBUG   :     #05 pc 00216f61  /system/lib/libart.so (art::Runtime::Abort()+160)
03-10 15:59:50.440    63    63 I DEBUG   :     #06 pc 000a7c17  /system/lib/libart.so (art::LogMessage::~LogMessage()+1066)
03-10 15:59:50.447    63    63 I DEBUG   :     #07 pc 002195e7  /system/lib/libart.so (art::Runtime::Init(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+4966)
03-10 15:59:50.448    63    63 I DEBUG   :     #08 pc 0021a217  /system/lib/libart.so (art::Runtime::Create(std::__1::vector<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >, void const*> > > const&, bool)+46)
03-10 15:59:50.450    63    63 I DEBUG   :     #09 pc 0000829f  /system/bin/dex2oat
03-10 15:59:50.450    63    63 I DEBUG   :     #10 pc 0000c12b  /system/bin/dex2oat
03-10 15:59:50.451    63    63 I DEBUG   :     #11 pc 00012dc9  /system/lib/libc.so (__libc_init+44)
03-10 15:59:50.454    63    63 I DEBUG   :     #12 pc 00004700  /system/bin/dex2oat
03-10 15:59:50.686    63    63 I DEBUG   : 
03-10 15:59:50.686    63    63 I DEBUG   : Tombstone written to: /data/tombstones/tombstone_00
03-10 15:59:50.687    63    63 I         : ********************************************************
03-10 15:59:50.687    63    63 I         : * Process 360 has been suspended while crashing.
03-10 15:59:50.687    63    63 I         : * To attach gdbserver for a gdb connection on port 5039
03-10 15:59:50.687    63    63 I         : * and start gdbclient:
03-10 15:59:50.687    63    63 I         : *
03-10 15:59:50.687    63    63 I         : *     gdbclient /system/bin/dex2oat :5039 360
03-10 15:59:50.687    63    63 I         : *
03-10 15:59:50.687    63    63 I         : * Wait for gdb to start, then press the VOLUME DOWN key
03-10 15:59:50.687    63    63 I         : * to let the process continue crashing.
03-10 15:59:50.687    63    63 I         : ********************************************************
  • gdbclient相关的脚本

相关的脚本在build/envsetup.sh中:

function gdbclient() {
  # TODO:
  # 1. Check for ANDROID_SERIAL/multiple devices
  local PROCESS_NAME="n/a"
  local PID=$1
  local PORT=5039
  if [ -z "$PID" ]; then
    echo "Usage: gdbclient <pid|processname> [port number]"
    return -1
  fi
  local DEVICE=$(adb_get_product_device)

  if [ -z "$DEVICE" ]; then
    echo "Error: Unable to get device name. Please check if device is connected and ANDROID_SERIAL is set."
    return -2
  fi

  if [ -n "$2" ]; then
    PORT=$2
  fi

  local ROOT=$(gettop)
  if [ -z "$ROOT" ]; then
    # This is for the situation with downloaded symbols (from the build server)
    # we check if they are available.
    ROOT=`realpath .`
  fi

  local OUT_ROOT="$ROOT/out/target/product/$DEVICE"
  local SYMBOLS_DIR="$OUT_ROOT/symbols"

  if [ ! -d $SYMBOLS_DIR ]; then
    echo "Error: couldn't find symbols: $SYMBOLS_DIR does not exist or is not a directory."
    return -3
  fi

  # let's figure out which executable we are about to debug

  # check if user specified a name -> resolve to pid
  if [[ ! "$PID" =~ ^[0-9]+$ ]] ; then
    PROCESS_NAME=$PID
    PID=$(pid --exact $PROCESS_NAME)
    if [ -z "$PID" ]; then
      echo "Error: couldn't resolve pid by process name: $PROCESS_NAME"
      return -4
    fi
  fi

  local EXE=`adb shell readlink /proc/$PID/exe | sed s/.$//`
  # TODO: print error in case there is no such pid
  local LOCAL_EXE_PATH=$SYMBOLS_DIR$EXE

  if [ ! -f $LOCAL_EXE_PATH ]; then
    echo "Error: unable to find symbols for executable $EXE: file $LOCAL_EXE_PATH does not exist"
    return -5
  fi

  local USE64BIT=""

  if [[ "$(file $LOCAL_EXE_PATH)" =~ 64-bit ]]; then
    USE64BIT="64"
  fi

  local GDB=
  local GDB64=
  local CPU_ABI=`adb shell getprop ro.product.cpu.abilist | sed s/.$//`
  # TODO: we assume these are available via $PATH
  if [[ $CPU_ABI =~ (^|,)arm64 ]]; then
    GDB=arm-linux-androideabi-gdb
    GDB64=aarch64-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)arm ]]; then
    GDB=arm-linux-androideabi-gdb
  elif [[ $CPU_ABI =~ (^|,)x86_64 ]]; then
    GDB=x86_64-linux-androideabi-gdb
  elif [[ $CPU_ABI =~ (^|,)x86 ]]; then
    GDB=x86_64-linux-androideabi-gdb
  elif [[ $CPU_ABI =~ (^|,)mips64 ]]; then
    GDB=mipsel-linux-android-gdb
    GDB64=mips64el-linux-android-gdb
  elif [[ $CPU_ABI =~ (^|,)mips ]]; then
    GDB=mipsel-linux-android-gdb
  else
    echo "Error: unrecognized cpu.abilist: $CPU_ABI"
    return -6
  fi

  # TODO: check if tracing process is gdbserver and not some random strace...
  if [ $(adb_get_traced_by $PID) -eq 0 ]; then
    # start gdbserver
    echo "Starting gdbserver..."
    # TODO: check if adb is already listening $PORT
    # to avoid unnecessary calls
    echo ". adb forward for port=$PORT..."
    adb forward tcp:$PORT tcp:$PORT
    echo ". starting gdbserver to attach to pid=$PID..."
    adb shell gdbserver$USE64BIT :$PORT --attach $PID &
    echo ". give it couple of seconds to start..."
    sleep 2
    echo ". done"
  else
    echo "It looks like gdbserver is already attached to $PID (process is traced), trying to connect to it using local port=$PORT"
  fi

  local OUT_SO_SYMBOLS=$SYMBOLS_DIR/system/lib$USE64BIT
  local OUT_VENDOR_SO_SYMBOLS=$SYMBOLS_DIR/vendor/lib$USE64BIT
  local ART_CMD=""

  echo >|"$OUT_ROOT/gdbclient.cmds" "set solib-absolute-prefix $SYMBOLS_DIR"
  echo >>"$OUT_ROOT/gdbclient.cmds" "set solib-search-path $OUT_SO_SYMBOLS:$OUT_SO_SYMBOLS/hw:$OUT_SO_SYMBOLS/ssl/engines:$OUT_SO_SYMBOLS/drm:$OUT_SO_SYMBOLS/egl:$OUT_SO_SYMBOLS/soundfx:$OUT_VENDOR_SO_SYMBOLS:$OUT_VENDOR_SO_SYMBOLS/hw:$OUT_VENDOR_SO_SYMBOLS/egl"
  local DALVIK_GDB_SCRIPT=$ROOT/development/scripts/gdb/dalvik.gdb
  if [ -f $DALVIK_GDB_SCRIPT ]; then
    echo >>"$OUT_ROOT/gdbclient.cmds" "source $DALVIK_GDB_SCRIPT"
    ART_CMD="art-on"
  else
    echo "Warning: couldn't find $DALVIK_GDB_SCRIPT - ART debugging options will not be available"
  fi
  echo >>"$OUT_ROOT/gdbclient.cmds" "target remote :$PORT"
  if [[ $EXE =~ (^|/)(app_process|dalvikvm)(|32|64)$ ]]; then
    echo >> "$OUT_ROOT/gdbclient.cmds" $ART_CMD
  fi

  echo >>"$OUT_ROOT/gdbclient.cmds" ""

  local WHICH_GDB=$GDB

  if [ -n "$USE64BIT" -a -n "$GDB64" ]; then
    WHICH_GDB=$GDB64
  fi

  gdbwrapper $WHICH_GDB "$OUT_ROOT/gdbclient.cmds" "$LOCAL_EXE_PATH"
}

在执行gdbclient命令时,会在out/target/product/mini-emulator-armv7-a-neon/下创建gdbclient.cmds文件,内容如下:

set solib-absolute-prefix /local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols
set solib-search-path /local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib/hw:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib/ssl/engines:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib/drm:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib/egl:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/system/lib/soundfx:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/vendor/lib:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/vendor/lib/hw:/local/android-5.1.1_r15/out/target/product/mini-emulator-armv7-a-neon/symbols/vendor/lib/egl
source /local/android-5.1.1_r15/tiny/development/scripts/gdb/dalvik.gdb
target remote :5039
art-on

相关的参考文档:

  1. https://source.android.com/devices/tech/debug/

评论

Be the First to Comment!

提醒我
avatar

wpDiscuz