Android GDB 调试

原文地址:https://github.com/mapbox/mapbox-gl-native/wiki/Android-debugging-with-remote-GDB

Android debugging with remote GDB

Leith Bade edited this page on Sep 15, 2015 · 17 revisions

Pages 18

Clone this wiki locally

Clone in Desktop

Preparation

  1. Install Android Studio, SDK and NDK, including build tools and platform tools.
  2. Ensure android-sdk/tools and android-sdk/platform-tools are on your PATH
  3. Build the app with: BUILDTYPE=Debug make android
  4. Connect your device via USB
  5. Check you can connect to the device by running adb shell. You should get the terminal from your device.
  6. Exit with exit

Extract system binaries from device

(You will need to do this for every device and every Android OS version)

  1. Create a folder to hold the Android system binaries locally e.g. ~/android
  2. cd into the folder
  3. Create a system_lib and vendor_lib folder
  4. cd system_lib
  5. adb pull /system/lib
  6. cd ../vendor_lib
  7. adb pull /vendor/lib. If you get permissions error you will need to get a list of each file and folder in adb shell then copy each file one at a time with adb pull /vendor/lib/file
  8. cd ..
  9. adb pull /system/bin/app_process (or on 64 bit phones adb pull /system/bin/app_process32 and adb pull /system/bin/app_process64)
  10. adb pull /system/bin/linker (and on 64 bit phones adb pull /system/bin/linker64)

Install GDB server

  1. Go to the NDK folder.
  2. Copy gdbserver from android-ndk/prebuilt/android-arm/gdbserver/gdbserver to mapbox-gl-native/android/java/MapboxGLAndroidSDK/src/main/jniLibs/armeabi-v7a/gdbserver.soIMPORTANT it must be renamed a .so file
  3. Build and run the app in Android Studio
  4. Android studio will copy and install the APK with gdbserver in it to your device

Start the app paused

  1. Open the project in Android Studio
  2. Place a breakpoint in Java class NativeMapView constructor on nativeCreate line
  3. Start app with Run -> Debug
  4. Wait for app to start and hit breakpoint
  5. Open up logcat and look for output from app
  6. Note the process ID e.g. in 11-08 19:25:52.957 31834-31834/com.mapbox.mapboxgl.app V/FragmentActivity﹕ onCreate it is 31834

Start gdbserver

  1. Open a terminal
  2. Run adb forward tcp:5039 localfilesystem:/data/data/com.mapbox.mapboxgl.testapp/debug-pipe
  3. Run adb shell run-as com.mapbox.mapboxgl.testapp /data/data/com.mapbox.mapboxgl.testapp/lib/gdbserver.so +debug-pipe --attach 31834. Replace 31834 with the process ID from earlier. (You will need to do this each time you restart the app as the PID will change)

If you get the error `then you have a version of Android not compatible withadb run-as`. See [this page] for information. It appears to affect Android updates in 4.2.2, 4.3, 5.0, 5.1.1. Workaround appears to be to load a fresh image.

You should see: Attached; pid = 31834 Listening on sockaddr socket debug-socket

  1. Leave the terminal open

Start gdb

  1. Open another terminal
  2. Go to the NDK folder android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin
  3. On OSX use android-ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/darwin-x86_64/bin
  4. Run ./arm-linux-androideabi-gdb ~/android/app_process
  5. target remote :5039
  6. In GDB: set solib-search-path ~/android/:~/android/system_lib:~/android/vendor_lib:~/android/vendor_lib/egl:~/path/to/mapbox-gl-native/build/android-arm-v7/Debug/lib.target/
  7. Check that all the debug symbols were loaded with info sharedlibrary
  8. Check each .so has Yes (*) except for the last libmapbox-gl.so which must have only Yes i.e. (no star). If not double check your solib-search-path
  9. b jni.cpp:183 (the first line of nativeCreate)
  10. c
  11. Switch to Android Studio
  12. Click Run -> Resume Program
  13. Switch back to GDB. It should be paused at nativeCreate
  14. GDB now has control, so c will continue execution (set breakpoints first) Note: If you encounter this crash:
Program received signal SIGILL, Illegal instruction.
0x7956a3a8 in _armv7_tick () from /home/leith/dev/mapbox-gl-native-mason/build/android/out/Debug/lib.target/libmapbox-gl.so
(gdb) bt
#0  0x7956a3a8 in _armv7_tick () from /home/leith/dev/mapbox-gl-native-mason/build/android/out/Debug/lib.target/libmapbox-gl.so
#1  0x795d1ccc in OPENSSL_cpuid_setup () from /home/leith/dev/mapbox-gl-native-mason/build/android/out/Debug/lib.target/libmapbox-gl.so
#2  0x400bd9c6 in ?? () from /home/leith/dev/android/linker
#3  0x400bda9e in ?? () from /home/leith/dev/android/linker
#4  0x400bdbf0 in ?? () from /home/leith/dev/android/linker
#5  0x400bdc6e in ?? () from /home/leith/dev/android/linker
#6  0x400bc1a6 in _start () from /home/leith/dev/android/linker
#7  0x41643c86 in dvmLoadNativeCode(char const*, Object*, char**) () from /home/leith/dev/android/system_lib/libdvm.so
#8  0x416600f4 in ?? () from /home/leith/dev/android/system_lib/libdvm.so
#9  0x41613ee8 in dvmJitToInterpNoChain () from /home/leith/dev/android/system_lib/libdvm.so
#10 0x41613ee8 in dvmJitToInterpNoChain () from /home/leith/dev/android/system_lib/libdvm.so
Backtrace stopped: previous frame identical to this frame (corrupt stack?)

You just need to c past it to the real crash. From https://bugs.launchpad.net/raspbian/+bug/1154042:

Afaict openssl probes the capabilities of the user‘s CPU by trying to do things and trapping the illegal instruction errors. So a couple of sigills during startup is normal. When using a debugger in order to find the real failure in your application you must continue past the startup sigills. 14. Use GDB commands to debug

Read http://condor.depaul.edu/glancast/373class/docs/gdb.html for GDB commands

时间: 2024-10-29 19:13:43

Android GDB 调试的相关文章

使用GDB调试Android NDK native(C/C++)程序

使用GDB调试Android NDK native(C/C++)程序 先说明下,这里所谓的ndk native程序跟Android上层java应用没有什么关系,也不需要涉及jni来封装native接口,通俗来讲,就是把编译好的纯C/C++程序,push到Android设备或者仿真器上,然后在设备上运行该程序.而调试则是通过attach到gdbserver来实现.推荐在Ubuntu或者mac osx下来进行,windows下要安装cygwin来模拟posix环境,速度很慢的说.具体操作如下: 0.

使用GDB调试Android NDK native(C/C++)程序-转

最 近写了些需要跨平台兼容的C++库,Android是其中需要兼容的平台之一.区别于Windows,Mac中功能强大的IDE环境,官方并没有为 Android ndk提供太多的支持.因此,尝试了下通过一些配置使用gdb来调试Android ndk的C++程序,感觉还算方便,记录下来跟大家分享. 先 说明下,这里所谓的ndk native程序跟Android上层java应用没有什么关系,也不需要涉及jni来封装native接口,通俗来讲,就是把编译好的纯C/C++程 序,push到Android设

Eclipse+CDT+GDB调试android NDK程序(转)

Eclipse+CDT+gdb调试android ndk程序 先介绍一下开发环境,在这个环境下,up主保证是没有问题的. ubuntu 11.10 eclipse 3.7(indego) for java jdk 6 android sdk 2.2 andrid ndk r7 当然,在windows环境下通过cygwin等工具也是可以实现gdb调试的,我也确实实现过.但是性能实在太低,卡的根本没法用.Linux下直接用gdb调试本地方法是很流畅的. 再确定安装并配置好开发环境之后,就可以开始了.

Android APP 调试过程中遇到的问题。

调试过过程中APP安装完启动后有的时候会异常退出,报这个错误.有的时候可以直接启动.查找不到原因.网上说把commit方法替换成commitAllowingStateLoss() 也无效. Android APP 调试过程中遇到的问题. >> android 这个答案描述的挺清楚的:http://www.goodpm.net/postreply/android/1010000007192169/AndroidAPP调试过程中遇到的问题.html

gdb调试

[前言]使用gdb调试前,在编译程序时,要加 -g 选项,否则你将看不见程序的函数名.变量名,所代替的全是运行时的内存地址. 1.开始调试 a.  gdb <program> program也就是你的执行文件,一般在当前目录下. b. gdb <program> core 用gdb同时调试一个运行程序和core文件,core是程序非法执行后core dump后产生的文件. 2.[列出源码],从第n行开始(编译时要加 -g 选项) l n 3.[设置断点]在第N行加断点 break

gdb调试命令

本篇摘自互联网,纯属自己学习笔记,然分享给看到我的博客的人们. 用GDB调试程序 GDB是一个强大的命令行调试工具.大家知道命令行的强大就是在于,其可以形成执行序列,形成脚本.UNIX下的软件全是命令行的,这给程序开发提代供了极大的便利,命令行软件的优势在于,它们可以非常容易的集成在一起,使用几个简单的已有工具的命令,就可以做出一个非常强大的功能. 于是UNIX下的软件比Windows下的软件更能有机地结合,各自发挥各自的长处,组合成更为强劲的功能.而Windows下的图形软件基本上是各自为营,

Go语言gdb调试踩坑

整个是一个docker环境 docker版本: 1.12.1,镜像是我自己做的基于ubuntu:14.04.05. 容器操作系统版本: Ubuntu 14.04.5 LTS go版本: 1.6.3 在gdb中执行run命令出错! 错误输出: warning:Error disabling address space randomization: Operation not permitted 环境:docker 解决办法: warning:Error disabling address spac

GDB调试汇编堆栈

GDB调试汇编堆栈 准备工作 终端编译工具: 编译64位Linux版本32位的二进制文件,需要安装一个库,使用指令sudo apt-get install libc6-dev-i386 测试代码: test.c 分析过程 1.生成汇编代码:gcc -g gdbtest.c -o gdbtest -m32 2.调试:gdb test 3.设置断点,因为目的是分析而不是调试bug,所以我们将断点设置在main函数 4.开始gdb调试:r(un),如若想获取此时的汇编代码,可用指令:disassemb

GDB调试汇编分析

GDB调试汇编分析 代码 本次实践我参照了许多先做了的同学的博客,有卢肖明,高其,张梓靖同学.代码借用的是卢肖明同学的代码进行调试运行. GCC编译 使用gcc -g gdbtest.c -o gdbtest -m32命令在64位的机器上产生32位汇编代码 在使用gdb进行调试运行时,有cgdb和gdb两种工具,我建议大家使用张梓靖同学使用的cgdb工具,因为使用时可以随时看到自己的源代码,看到我们的断点在哪里,每一步返回值到了哪行,更加直观. 分析过程 使用b main指令在main函数处设置