How to debug C++ code on Android by using Eclipse?

二次信任 提交于 2020-01-04 08:21:48

问题


I wrote some video streamer (C++) on PC and now I try to to implement the same cross platform section on Android. I "played" enough with NDK and know a bit about how to compile and integrate C code with Android (aka JNI).

The problem is that sometimes my application crashes and as you know Android doesn't point me where code failed (in C/C++ part).

Video Streamer code have a lot of libraries and, lets say, open source parts I used so to leave logs - not good idea. I use Eclipse, how can I debug C++ code or does Eclipse have any plugins to debug?

Thanks a lot,


回答1:


To log stuff and see it in the logcat from Android, I have a simple class for that:

#ifndef LOG_H_
#define LOG_H_

#include <android/log.h>

#define LOGD(LOG_TAG, ...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGI(LOG_TAG, ...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define LOGV(LOG_TAG, ...) __android_log_print(ANDROID_LOG_VERBOSE, LOG_TAG, __VA_ARGS__)
#define LOGW(LOG_TAG, ...) __android_log_print(ANDROID_LOG_WARN, LOG_TAG, __VA_ARGS__)
#define LOGE(LOG_TAG, ...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)

#endif /* LOG_H_ */

You can use it this way:

LOGI("YourClassTag","let's print some stuff: %i, %s", 4, "I'm a string");

To get more info for some crashes, you may try to enable JNICheck with adb:

If you have a regular device, you can use the following command:

adb shell setprop debug.checkjni 1

This won’t affect already-running apps, but any app launched from that point on will have CheckJNI enabled. (Change the property to any other value or simply rebooting will disable CheckJNI again.) In this case, you’ll see something like this in your logcat output the next time an app starts:

D Late-enabling CheckJNI




回答2:


GDB (GNU Debugger) for native code is available since Android 2.2 (SDK level 8).

Rebuild the NDK code with option NDK_DEBUG=1 in the command line. Start the app. Then invoke the "ndk-gdb" command from the Windows or Cygwin command line while the jni folder under the project is the current one. You'll get a GDB console. Type c and press Enter to continue program execution (it's paused when GDB connects).

It's not for the weak of spirit, though. GDB is known for its austere interface. But at the very least it'll give you a stack backtrace for the crash (the bt command) when the crash occurs.

You can connect GDB to processes running on earlier versions of Android, too, but the procedure is considerably trickier.

GDB reference is here.



来源:https://stackoverflow.com/questions/15347994/how-to-debug-c-code-on-android-by-using-eclipse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!