Debug

Debugging in the Kernel

Use printk(“Message you want to display”); in the code.
And then you can see those logs with the following command:

$ adb shell dmesg

Note that this command will not work like a regular terminal, as it will essentially print out every message displayed by the kernel since the time the system booted.
So you may want to save the output of this command to a file, to better interpret the information:

$ adb shell dmesg > kernelOutput.out

Alternatively you can just read the output of your emulator's terminal window.


Debugging in User Space Native Code

There are two ways you can redirect your native code prints to logcat.

Method A

The first was covered here, and if you want to use this approach you better follow that page in terms of build.gradle file configurations.
Essentially in our code you must:

Include the appropriate header files
 #include <string.h>
 #include <android/log.h>
Define your macro function
 #define APPNAME "YourAppName"
 #define  printToLogcat(...)  __android_log_print(ANDROID_LOG_VERBOSE, APPNAME, __VA_ARGS__)
Use your print function freely throughout the code
 printToLogcat("Message you want to display to Java #%d\n", 10);

Method B

The second way you can redirect your native prints to logcat is perhaps simpler.
After launching your emulator, open a terminal and follow these commands:

 $ adb shell stop
 $ adb shell setprop log.redirect-stdio true
 $ adb shell start

Your emulator should then restart and then you can use traditional prints on your code that will then appear on logcat:

 printf("Message you want to display\n");
 fflush(stdout);

If you want to use printf, you need to use the flush function after.
Alternatively you can write to stderr:

 fprintf(stderr, "Message you want to display in case of an error");

http://stackoverflow.com/questions/4455941/undefined-reference-to-android-log-print
http://stackoverflow.com/questions/4629308/any-simple-or-easy-way-to-debug-android-ndk-code