简体   繁体   中英

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

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,

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM