Android NDK Debug
Posted oncealong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android NDK Debug相关的知识,希望对你有一定的参考价值。
前言:说真的android NDK debug还是推荐lldb,gdb经常莫名其妙的不成功。不过下面的这个流程是谷歌官方建议的,还是有参考价值的。尤其是在App启动时Debug的流程。
lldb debug的文章推荐: https://fucknmb.com/2019/12/06/Flutter-Engine-C-%E6%BA%90%E7%A0%81%E8%B0%83%E8%AF%95%E5%88%9D%E6%8E%A2/
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:
- Go to Settings > Developer options > Select debug app and choose your app from the list, then click Wait for debugger.
- 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
- Wait for the app to load and a dialog to appear telling you the app is waiting for a debugger.
- 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:
gdbclient.py -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:
- Ensure that all build artifacts (such as symbols) required to run
gdbclient.py
are present. - Run the following command:
gdbclient.py -p pid | -n proc-name | -r ... --setup-forwarding vscode ANY_OTHER_FLAGS
This prints a JSON object and
gdbclient.py
continues running. This is expected; don't kill thegdbclient.py
program. - 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. - Delete the newly added debugger configuration.
- Copy the JSON object printed by
gdbclient.py
and paste it into the object you just deleted. Save the changes. - To reload the window to refresh the debugger list, press Ctrl+Shift+P and type
reload window
. - Select the new debugger configuration and press run. The debugger should connect after 10 to 30 seconds.
- When you're done debugging, go to the terminal running
gdbclient.py
and press Enter to end thegdbclient.py
program.
After setting up the debugger configuration for the first time, you can skip steps 3 through 6.
---
https://source.android.com/devices/tech/debug/gdb
以上是关于Android NDK Debug的主要内容,如果未能解决你的问题,请参考以下文章
android NDK开发中,用Cygwin调试本地代码时报错“Another debug session running,Use --force to kill it”原因及解决的方法