Android NDK Debug

前言:说真的Android NDK debug还是推荐lldb,gdb经常莫名其妙的不成功。不过下面的这个流程是谷歌官方建议的,还是有参考价值的。尤其是在App启动时Debug的流程。

lldb debug的文章推荐:

1. Debugging app startup

Sometimes you want to debug an app as it starts, such as when there's a crash and you want to step through code to see what happened before the crash. Attaching works in some cases, but in other cases is impossible because the app crashes before you can attach. The logwrapper approach (used for strace) doesn't always work because the app might not have permissions to open a port, and gdbserver inherits that restriction.

To debug app startup, use the developer options in Settings to instruct the app to wait for a Java debugger to attach:

  1. Go to Settings > Developer options > Select debug app and choose your app from the list, then click Wait for debugger.
  2. Start the app, either from the launcher or by using the command line to run: adb shell am start -a android.intent.action.MAIN -n APP_NAME/.APP_ACTIVITY
  3. Wait for the app to load and a dialog to appear telling you the app is waiting for a debugger.
  4. Attach gdbserver/gdbclient normally, set breakpoints, then continue the process.

To let the app run, attach a Java Debug Wire Protocol (JDWP) debugger such as Java Debugger (jdb):

adb forward tcp:12345 jdwp:XXX  # (Where XXX is the PID
of the debugged process.)
jdb -attach localhost:12345

2. Debugging apps or processes that crash

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

  • Android 7.0 Nougat and higher adb shell setprop debug.debuggerd.wait_for_gdb true
  • Android 6.0 Marshmallow and lower adb shell setprop debug.db.uid 999999

At the end of the usual crash output, debuggerd provides instructions on how to connect gdb using the command: -p PID

3. Debugging without symbols

For 32-bit ARM, if you don’t have symbols, gdb can't determine which instruction set it's disassembling (ARM or Thumb). To specify the instruction set chosen as the default when symbol information is missing, set the following property:

set arm fallback-mode arm  # or thumb

4. Debugging with VS Code

GDB supports debugging platform code on Visual Studio Code. You can use the VS Code debugger frontend instead of the GDB CLI interface to control and debug native code running on devices.

Before using VS Code for debugging, install the C/C++ extension.

To debug code using VS Code:

  1. Ensure that all build artifacts (such as symbols) required to run are present.
  2. Run the following command: -p pid | -n proc-name | -r ... --setup-forwarding vscode ANY_OTHER_FLAGS This prints a JSON object and continues running. This is expected; don't kill the program.
  3. In the debugging tab in VS Code, select add configuration, then select C/C++ gdb attach. This opens a launch.json file and adds a new JSON object to a list.
  4. Delete the newly added debugger configuration.
  5. Copy the JSON object printed by and paste it into the object you just deleted. Save the changes.
  6. To reload the window to refresh the debugger list, press Ctrl+Shift+P and type reload window.
  7. Select the new debugger configuration and press run. The debugger should connect after 10 to 30 seconds.
  8. When you're done debugging, go to the terminal running and press Enter to end the program.

After setting up the debugger configuration for the first time, you can skip steps 3 through 6.




0 条评论
登录 后参与评论


  • Linux命令行下编译Android NDK的示例代码

    这几天琢磨写一个Android的Runtime用来加速HTML5 Canvas,让GameBuilder+CanTK 不但开发速度快,运行速度也能接近原生应用。...

  • Android NDK R19 交叉编译 thrift C++ 库

    在NDK下编译thrift C++库,先要要编译android版的boost,这个不是本文讨论的内容,关于编译android 版本的boost,参见这个开源项目...

  • 使用AndroidStudio编译NDK的方法及错误解决方案

    参考资料: 【android ndk】macos环境下Android Studio中利用gradle编译jni模块及配置:http://demo.netfouc...

  • 深入理解使用CMake编译 NDK 程序

    Android Studio 2.2 及以后的版本默认使用CMake进行 NDK 编译, 其中最吸引人的地方是,在开发NDK程序时可以进行联机调试,这真是大在的...

  • Android NDK 开发中快速定位 Crash 问题

    在 Android NDK 开发中,排查问题遇到的最熟悉的关键字非 backtrace 莫属,Linux 系统中进程 crash 后通过 backtrace 输...

  • Android studio 使用Debugger问题(代码中含有ndk)

    NDK 是 Native Developmentit的缩写,是Google在Android开发中提供的一套用于快速创建native工程的一个工具。 使用这个工...

  • ffmpeg编译


  • "CMake Error: CMake was unable to find a build program corresponding Ninja"

    前两天, 没修改什么, 打开Android Studio编so, 忽然就不成功了.

  • 升级Android Studio 导致的 `Error:Failed to notify project evaluation listener.`

    最近升级Android Studio和sdk后, gradle同步项目时忽然报错, Error:Failed to notify project evalu...