Debugging Native Android Platform Code

来自:

http://source.android.com/devices/tech/debug/index.html

This page contains a summary of useful tools and related commands for debugging, tracing, and profiling native Android platform code. The pages within this section contain detailed information on other debugging tools for use during development of platform-level features.

For example, you may learn how to explore system services with Dumpsys and evaluate network and RAM use. See the subpages for tools and methods not described below.

debuggerd



When a dynamically-linked executable starts, several signal handlers are registered that connect to debuggerd (or debuggerd64) in the event that signal is sent to the process. The debuggerd process dumps registers and unwinds the stack. Here is example output (with timestamps and extraneous information removed):

*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***Build fingerprint: ‘Android/aosp_flounder/flounder:5.1.51/AOSP/enh08201009:eng/test-keys‘Revision: ‘0‘ABI: ‘arm‘pid: 1656, tid: 1656, name: crasher  >>> crasher <<<signal 6 (SIGABRT), code -6 (SI_TKILL), fault addr --------Abort message: ‘some_file.c:123: some_function: assertion "false" failed‘    r0 00000000  r1 00000678  r2 00000006  r3 f70b6dc8    r4 f70b6dd0  r5 f70b6d80  r6 00000002  r7 0000010c    r8 ffffffed  r9 00000000  sl 00000000  fp ff96ae1c    ip 00000006  sp ff96ad18  lr f700ced5  pc f700dc98  cpsr 400b0010backtrace:    #00 pc 00042c98  /system/lib/libc.so (tgkill+12)    #01 pc 00041ed1  /system/lib/libc.so (pthread_kill+32)    #02 pc 0001bb87  /system/lib/libc.so (raise+10)    #03 pc 00018cad  /system/lib/libc.so (__libc_android_abort+34)    #04 pc 000168e8  /system/lib/libc.so (abort+4)    #05 pc 0001a78f  /system/lib/libc.so (__libc_fatal+16)    #06 pc 00018d35  /system/lib/libc.so (__assert2+20)    #07 pc 00000f21  /system/xbin/crasher    #08 pc 00016795  /system/lib/libc.so (__libc_init+44)    #09 pc 00000abc  /system/xbin/crasherTombstone written to: /data/tombstones/tombstone_06

This can be pasted into development/scripts/stack to get a more detailed unwind with line number information (assuming the unstripped binaries can be found).

Some libraries on the system are built with LOCAL_STRIP_MODULE := keep_symbols to provide usable backtraces directly from debuggerd. This makes your library or executable slightly larger, but not nearly as large as an unstripped version.

Note also the last line of debuggerd output --- in addition to dumping a summary to the log, debuggerd writes a full “tombstone” to disk. This contains a lot of extra information that can be helpful in debugging a crash, in particular the stack traces for all the threads in the crashing process (not just the thread that caught the signal) and a full memory map.

Native Debugging with GDB


Debugging a running app

To connect to an already-running app or native daemon, use gdbclient.

Current versions of gdbclient just require the process ID (PID). So to debug a process with PID 1234, simply run:

$ gdbclient 1234

The script will set up port forwarding, start the appropriate gdbserver on the device, start the appropriate gdb on the host, configure gdb to find symbols, and connect gdb to the remote gdbserver.

Debugging a native process as it starts

If you want to debug a process as it starts, you’ll need to use gdbserver or gdbserver64 manually, but that’s easy too:

$ adb shell gdbserver :5039 /system/bin/my_test_appProcess my_test_app created; pid = 3460Listening on port 5039

Identify the app’s PID from the gdbserver output, and then in another window:

$ gdbclient <app pid>

Then enter continue at the gdb prompt.

Note that to debug a 64-bit process, you‘ll need to use gdbserver64. The error messages from gdb if you made the wrong choice are unhelpful (along the lines of Reply contains invalid hex digit 59).

Debugging processes that crash

If you want debuggerd to suspend crashed processes so you can attach gdb, set the appropriate property:

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

At the end of the usual crash output, debuggerd will give you instructions on how to connect gdb using the typical command:

$ gdbclient <pid>

Debugging without symbols

If you don’t have symbols, sometimes gdb will get confused about the instruction set it is disassembling (ARM or Thumb). The instruction set that is chosen as the default when symbol information is missing can be switched between ARM or Thumb like so:

$ set arm fallback-mode arm   # or ‘thumb‘

Other tools


Valgrind

The following steps show you how to use Valgrind on Android. This tool suite contains a number of tools including Memcheck for detecting memory-related errors in C and C++.

  1. To install Valgrind, run:

    $ mmm -j6 external/valgrind
  2. Push Valgrind to the device:
    $ adb remount$ adb sync
  3. Set up the temporary directory:
    $ adb shell mkdir /data/local/tmp$ adb shell chmod 777 /data/local/tmp
  4. Run the system server with Valgrind:
    $ adb root$ adb shell setprop wrap.system_server "logwrapper valgrind"$ adb shell stop && adb shell start
  5. For debug symbols, push unstripped libraries to /data/local/symbols:
    $ adb shell mkdir /data/local/symbols$ adb push $OUT/symbols /data/local/symbols
  6. To use Valgrind during boot up, edit out/target/product/XXXX/root/init.rc and change:
    service example /system/bin/foo --arg1 --arg2
    to:
    service example /system/bin/logwrapper /system/bin/valgrind /system/bin/foo --arg1 --arg2
    To see the effects, you need to create a boot.img and reflash the device.

Systrace

See Systrace on
developer.android.com
for deriving execution times of applications and
other Android system processes.

时间: 2024-10-14 06:07:47

Debugging Native Android Platform Code的相关文章

React Native Android Gradle 编译流程浅析

[工匠若水 http://blog.csdn.net/yanbober 未经允许严禁转载,请尊重作者劳动成果.私信联系我] 1 背景 前面已经发车了一篇<React Native Android 从学车到补胎和成功发车经历>,接着就该好好琢磨一下 React Native 周边了,没看第一篇的可以先去看看:这里我们先从 React Native 的 Android 编译来简单揭晓一下 React Native 在集成的过程中到底干了哪些不可告人的坏事:由于我们项目准备以 Gradle 形式接入

React Native Android 源码框架浅析(主流程及 Java 与 JS 双边通信)

[工匠若水 http://blog.csdn.net/yanbober 未经允许严禁转载,请尊重作者劳动成果.私信联系我] 1 背景 有了前面<React Native Android 从学车到补胎和成功发车经历>和<React Native Android Gradle 编译流程浅析>两篇文章的学习我们 React Native 已经能够基本接入处理一些事情了,那接下来的事情就是渐渐理解 RN 框架的一些东西,以便裁剪和对 RN 有个更深入的认识,所以本篇总结了我这段时间阅读源码

通过Chrome浏览器进行android调试/Remote Debugging on Android with Chrome

The way your web content behaves on mobile can be dramatically different from the desktop experience. Remote debugging with Chrome DevTools lets you debug live content on your Android device from your development machine. Remote debugging on Android

android platform下载地址

查看需要下载的android platform包的地址: https://dl-ssl.google.com/android/repository/repository-7.xml 1.  Android 2.2 platform : http://dl-ssl.google.com/android/repository/android-2.2_r02-linux.zip 2. Android 3.2 platform: http://dl-ssl.google.com/android/repo

React Native Android原生模块开发实战|教程|心得|如何创建React Native Android原生模块

尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 前言 一直想写一下我在React Native原生模块封装方面的一些经验和心得,来分享给大家,但实在抽不开身,今天看了一下日历发现马上就春节了,所以就赶在春节之前将这篇博文写好并发布(其实是两篇:要看iOS篇的点这里<React Native iOS原生模块开发>). 我平时在用React Native开发App时会

react-native —— 在Mac上配置React Native Android开发环境排坑总结

配置React Native Android开发环境总结 1.卸载Android Studio,在终端(terminal)执行以下命令: rm -Rf /Applications/Android\ Studio.app rm -Rf ~/Library/Preferences/AndroidStudio* rm ~/Library/Preferences/com.google.android.studio.plist rm -Rf ~/Library/Application\ Support/A

React Native Android原生模块开发实战|教程|心得|怎样创建React Native Android原生模块

尊重版权,未经授权不得转载 本文出自:贾鹏辉的技术博客(http://blog.csdn.net/fengyuzhengfan/article/details/54691503) 告诉大家一个好消息.为大家精心准备的React Native视频教程公布了,大家现能够看视频学React Native了. 前言 一直想写一下我在React Native原生模块封装方面的一些经验和心得.来分享给大家,但实在抽不开身.今天看了一下日历发现立即就春节了.所以就赶在春节之前将这篇博文写好并公布(事实上是两篇

React native android 最常见的10个问题

这里逐条记录下最容易遇到的React native android 相关case: 1. app启动后,红色界面,unable load jsbundle : 解决办法:一般来说就是,你是用dev-serve方式,且你的server没有正确匹配上,如果是用手机跑的话,需要pc和手机在同一个wifi下,且通过menu键设置menu-ip为pc的ip,如果是模拟器,则不需要手动设置ip,设置的话,反倒会出错 2. app启动后,红色界面,unRegisteredProject 提示提示什么,你的ap

Android Secret Code

我们很多人应该都做过这样的操作,打开拨号键盘输入*#*#4636#*#*等字符就会弹出一个界面显示手机相关的一些信息,这个功能在Android中被称为android secret code,除了这些系统预置的secret code,我们也可以实现自己的secret code,而且实现起来非常简单. 要实现自己的secret code,只需要向系统注册一个Broadcast Receiver,不需要任何权限,如下所示: <receiver android:name=".SecretRecei