java 如何在 Eclipse 中有效地调试用 JNI 包装的 C 代码?(安卓开发)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4846080/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:17:52  来源:igfitidea点击:

How can I effectively debug C code that's wrapped with JNI in Eclipse? (Android Dev)

javaandroidcjava-native-interfaceandroid-ndk

提问by Brian D

I've got a segfault but I have absolutely no idea how to locate it.

我有段错误,但我完全不知道如何找到它。

Tips? enter image description here

尖端? 在此处输入图片说明

采纳答案by richq

You can get the location of the C function that caused the crash using the Android NDK Stacktrace Analyzer.

您可以使用Android NDK Stacktrace Analyzer获取导致崩溃的 C 函数的位置。

The steps are on the wiki, but basically you need to get the stack trace from logcat into a file (adb logcat > mycrash.log), then dump your library to a text file, then run the program on the two of them. Here's the shell script I use to do the lot:

这些步骤在wiki 上,但基本上您需要将 logcat 中的堆栈跟踪获取到一个文件 ( adb logcat > mycrash.log) 中,然后将您的库转储到一个文本文件中,然后在其中两个上运行该程序。这是我用来做很多事情的shell脚本:

#!/bin/sh
if test $# -lt 2 ; then
    echo "Extract readable stack trace from Android logcat crash"
    echo "Usage ##代码## lib.so crash.log"
    exit 1
fi
of=$(mktemp)
echo "Disassemble "
~/tools/android-ndk-r5/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/bin/arm-linux-androideabi-objdump -S  > $of
echo "Parse stack trace in "
~/bin/parse_stack.py $of 

Change the paths to the Android NDK and parse_stack.py file as you need.

根据需要更改 Android NDK 和 parse_stack.py 文件的路径。

回答by Chris

How to Effectively Debug Android JNI C/C++ Code in Eclipse:

如何在 Eclipse 中有效调试 Android JNI C/C++ 代码:

  1. Setup your project to be a mixed Java, C, and C++ project:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/

  2. Setup your project to enable debugging:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-debugging/#more-23

  1. 将您的项目设置为混合 Java、C 和 C++ 项目:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-development/

  2. 设置您的项目以启用调试:

    http://mhandroid.wordpress.com/2011/01/23/using-eclipse-for-android-cc-debugging/#more-23

Note: The author of the site used Eclipse(Galileo) on Ubuntu. I found some differences when using Eclipse(Helios) on MacOS (especially with the debug setup). Once things are set up, Eclipse works very well as an IDE for JNI development.

注意:该站点的作者在 Ubuntu 上使用了 Eclipse(Galileo)。我在 MacOS 上使用 Eclipse(Helios) 时发现了一些差异(尤其是调试设置)。一旦设置好,Eclipse 就可以很好地用作 JNI 开发的 IDE。