在Eclipse中调试Android NDK C/ c++代码——断点不会被击中

时间:2023-01-18 08:59:17

I downloaded Android SDK Bundle for Linux and Android NDK. ADT was installed, I installed CDT.

我下载了Linux和Android NDK的Android SDK包。安装了ADT,安装了CDT。

I created a Android project and added native support (jni). Then I wrote native function in java-code which exporting in c++ code. In c++ code I defined this function.

我创建了一个Android项目并添加了本机支持(jni)。然后,我在java代码中编写了本机函数,该函数以c++代码导出。在c++代码中,我定义了这个函数。

Java-code:

java代码:

static {
    System.loadLibrary("test");
}

private native String get_text_from_cpp();

c++ code (h):

c++代码(h):

extern "C"{
   JNIEXPORT jstring JNICALL Java_com_example_test_MainActivity_get_1text_1from_1cpp(JNIEnv *, jobject);
}

c++ code (cpp):

c++代码(cpp):

JNIEXPORT jstring JNICALL Java_com_example_test_MainActivity_get_1text_1from_1cpp(JNIEnv * env, jobject){
    return env->NewStringUTF( "hello from C++" );
}

Code works without errors. But when I set breakpoint in c++ code it is not hit.

代码没有错误。但是当我在c++代码中设置断点时,它不会被击中。

build-nkd NDK_DEBUG = 1 - are included

包含build-nkd NDK_DEBUG = 1 -

I followed this instructions http://tools.android.com/recent/usingthendkplugin

我按照下面的说明:http://tools.android.com/recent/usingthendkplugin

Android.mk in jni/ has LOCAL_CFLAGS := -g

Android。在jni/中有LOCAL_CFLAGS:= -g

I have read very much information but I could't customized Eclipse. Please, help anybody.

我已经阅读了很多信息,但是我无法定制Eclipse。请帮助任何人。

PS: I am sorry for my English is not my native language. I have difficulty in writing.

PS:对不起,我的英语不是我的母语。我写作有困难。

Add: Also during debug in console shows: "warning: Could not load shared library symbols for 95 libraries, e.g. /system/bin/linker. Use the "info sharedlibrary" command to see the complete listing. Do you need "set solib-search-path" or "set sysroot"? warning: Unable to find dynamic linker breakpoint function. GDB will retry eventurally. Meanwhile, it is likely that GDB is unable to debug shared library initializers or resolve pending breakpoints after dlopen()."

添加:在控制台的调试过程中还显示:“警告:不能为95个库加载共享库符号,例如/system/bin/linker。使用“info sharedlibrary”命令查看完整的列表。你需要“设置solib搜索路径”还是“设置sysroot”?警告:无法找到动态链接器断点函数。GDB将重试eventurally。与此同时,GDB很可能无法调试共享库初始化程序,或者在dlopen()之后解决挂起的断点。

6 个解决方案

#1


12  

The trick I use is to put a usleep call as the very first native line in my debug code.

我使用的技巧是将usleep调用作为调试代码中的第一个本地行。

This makes your thread sleep and gives the debugger a chance to be ready for you.

这使您的线程睡眠,并给调试器一个为您准备就绪的机会。

#include <unistd.h>

.
.
.

#ifndef NDEBUG
usleep(5000 * 1000);
#endif

#2


6  

After struggling a lot to debug on Eclipse this is my recipe:

在Eclipse上调试了很多之后,这是我的秘诀:

Do the usual steps:

通常的步骤:

  • Add android.os.Debug.waitForDebugger(); before loading your native library. This might help.
  • 添加android.os.Debug.waitForDebugger();在加载本地库之前。这可能会有所帮助。
  • Add APP_OPTIM := debug in Application.mk
  • 在Application.mk中添加APP_OPTIM:= debug
  • Build with ndk-build NDK_DEBUG=1
  • 构建与ndk-build NDK_DEBUG = 1

Then, what I found different:

然后,我发现了不同的地方:

  • Open a console and run:
  • 打开控制台并运行:

adb pull /system/bin/linker <your_project_base_dir>/obj/local/armeabi/linker

亚行拉/系统/ bin /链接< your_project_base_dir > / obj /地方/ armeabi /链接器

Depending on your device you might have to write armeabi or armeabi-v7a. You only need to do it once (I noticed that running ndk-gdb manually was doing that. Executing that command manually the breakpoints started to work)

根据您的设备,您可能需要编写armeabi或armeabi-v7a。您只需要做一次(我注意到手动运行ndk-gdb就是这样做的。手动执行该命令断点开始工作)

  • Finally, to debug use the menu "Run -> Debug As -> Android Native Application"
  • 最后,调试使用菜单“Run -> debug As -> Android Native Application”

#3


5  

You can use DS-5 CE Android Debug tool provided by ARM as a plugin to your eclipse. It works really well and provides a very good and easy UI for debugging. From my personal experience it is lots better than the traditional way of debugging the ndk app.

您可以使用ARM提供的DS-5 CE Android调试工具作为eclipse的插件。它工作得非常好,为调试提供了一个非常好的、非常简单的UI。根据我的个人经验,它比调试ndk应用程序的传统方式要好得多。

Please refer the below link which will provide you with the details of how to use the DS-5 debugger:

请参阅以下连结,提供有关如何使用DS-5除错器的详情:

https://developer.arm.com/products/software-development-tools/ds-5-development-studio/resources/tutorials/android-native-app-debug-tutorial

https://developer.arm.com/products/software-development-tools/ds-5-development-studio/resources/tutorials/android-native-app-debug-tutorial

#4


3  

Your app executes the JNI function very early, so that the debugger is not ready yet. Unfortunately, it takes a while for gdb to establish the remote connection, see http://visualgdb.com/documentation/appstartup

您的应用程序非常早地执行JNI函数,因此调试器还没有准备好。不幸的是,gdb需要一段时间来建立远程连接,请参见http://visualgdb.com/documentation/appstartup

Instead of fighting the windmills, add a button to your activity, and call the same native method onClick() of that button - it will be easier to catch the breakpoint.

不要与风车战斗,在您的活动中添加一个按钮,并调用该按钮的相同的本机方法onClick()——将更容易捕获断点。

BTW, the warning about 95 libraries is perfectly normal. These are the system libraries which you don't want to debug, and don't have sources for.

顺便说一句,关于95个图书馆的警告是完全正常的。这些是您不想调试的系统库,并且没有源代码。

#5


1  

consider adding:

考虑添加:

android.os.Debug.waitForDebugger();

android.os.Debug.waitForDebugger();

before your native call, this makes your app wait until the debugger attaches, could help you avoid sleeping / using a button.

在您的本机调用之前,这将使您的应用程序等待调试器连接,可以帮助您避免睡眠/使用按钮。

#6


0  

I had this issue, and the only thing that worked for me was putting

我有这个问题,唯一对我起作用的是放

Thread.sleep(2000);

before the JNI library is loaded. This gave enough time for the debugger to attach before the call to System.loadLibrary crashed the app. Helped me to locate the problem C++ code.

在加载JNI库之前。这为调试器在调用系统之前附加足够的时间。loadLibrary崩溃了应用程序,帮助我找到问题c++代码。

#1


12  

The trick I use is to put a usleep call as the very first native line in my debug code.

我使用的技巧是将usleep调用作为调试代码中的第一个本地行。

This makes your thread sleep and gives the debugger a chance to be ready for you.

这使您的线程睡眠,并给调试器一个为您准备就绪的机会。

#include <unistd.h>

.
.
.

#ifndef NDEBUG
usleep(5000 * 1000);
#endif

#2


6  

After struggling a lot to debug on Eclipse this is my recipe:

在Eclipse上调试了很多之后,这是我的秘诀:

Do the usual steps:

通常的步骤:

  • Add android.os.Debug.waitForDebugger(); before loading your native library. This might help.
  • 添加android.os.Debug.waitForDebugger();在加载本地库之前。这可能会有所帮助。
  • Add APP_OPTIM := debug in Application.mk
  • 在Application.mk中添加APP_OPTIM:= debug
  • Build with ndk-build NDK_DEBUG=1
  • 构建与ndk-build NDK_DEBUG = 1

Then, what I found different:

然后,我发现了不同的地方:

  • Open a console and run:
  • 打开控制台并运行:

adb pull /system/bin/linker <your_project_base_dir>/obj/local/armeabi/linker

亚行拉/系统/ bin /链接< your_project_base_dir > / obj /地方/ armeabi /链接器

Depending on your device you might have to write armeabi or armeabi-v7a. You only need to do it once (I noticed that running ndk-gdb manually was doing that. Executing that command manually the breakpoints started to work)

根据您的设备,您可能需要编写armeabi或armeabi-v7a。您只需要做一次(我注意到手动运行ndk-gdb就是这样做的。手动执行该命令断点开始工作)

  • Finally, to debug use the menu "Run -> Debug As -> Android Native Application"
  • 最后,调试使用菜单“Run -> debug As -> Android Native Application”

#3


5  

You can use DS-5 CE Android Debug tool provided by ARM as a plugin to your eclipse. It works really well and provides a very good and easy UI for debugging. From my personal experience it is lots better than the traditional way of debugging the ndk app.

您可以使用ARM提供的DS-5 CE Android调试工具作为eclipse的插件。它工作得非常好,为调试提供了一个非常好的、非常简单的UI。根据我的个人经验,它比调试ndk应用程序的传统方式要好得多。

Please refer the below link which will provide you with the details of how to use the DS-5 debugger:

请参阅以下连结,提供有关如何使用DS-5除错器的详情:

https://developer.arm.com/products/software-development-tools/ds-5-development-studio/resources/tutorials/android-native-app-debug-tutorial

https://developer.arm.com/products/software-development-tools/ds-5-development-studio/resources/tutorials/android-native-app-debug-tutorial

#4


3  

Your app executes the JNI function very early, so that the debugger is not ready yet. Unfortunately, it takes a while for gdb to establish the remote connection, see http://visualgdb.com/documentation/appstartup

您的应用程序非常早地执行JNI函数,因此调试器还没有准备好。不幸的是,gdb需要一段时间来建立远程连接,请参见http://visualgdb.com/documentation/appstartup

Instead of fighting the windmills, add a button to your activity, and call the same native method onClick() of that button - it will be easier to catch the breakpoint.

不要与风车战斗,在您的活动中添加一个按钮,并调用该按钮的相同的本机方法onClick()——将更容易捕获断点。

BTW, the warning about 95 libraries is perfectly normal. These are the system libraries which you don't want to debug, and don't have sources for.

顺便说一句,关于95个图书馆的警告是完全正常的。这些是您不想调试的系统库,并且没有源代码。

#5


1  

consider adding:

考虑添加:

android.os.Debug.waitForDebugger();

android.os.Debug.waitForDebugger();

before your native call, this makes your app wait until the debugger attaches, could help you avoid sleeping / using a button.

在您的本机调用之前,这将使您的应用程序等待调试器连接,可以帮助您避免睡眠/使用按钮。

#6


0  

I had this issue, and the only thing that worked for me was putting

我有这个问题,唯一对我起作用的是放

Thread.sleep(2000);

before the JNI library is loaded. This gave enough time for the debugger to attach before the call to System.loadLibrary crashed the app. Helped me to locate the problem C++ code.

在加载JNI库之前。这为调试器在调用系统之前附加足够的时间。loadLibrary崩溃了应用程序,帮助我找到问题c++代码。