I want to debug some android native c/c++ code, like
PROJECT_NAME/frameworks/av/media/libaudioclient/AudioTrack.cpp etc.
but when i use cmd logcat, I can't obtain some log level like D & I & V.I try to uncomment //#define LOG_NDEBUG 0 in top of AudioTrack.cpp file, but there are two questions for me,:
one is I want to open or close log dynamically, but in this method I have to comment/unconmment #define LOG_NDEBUG 0.
another one is though I uncomment #define LOG_NDEBUG 0 & endured a long time for make target-files-package -j12, It doesn't work as print out log level "Verbose" in terminal.
My question is if there are some way to capture android native c/c++ debug log (include verbose,Info,Debug level) by dynamincally (like read & edit xxx.xml)?
you can define cppFlags based on which you can toggle logs in native code
app build.gradle -
externalNativeBuild {
cmake {
cppFlags += "-DBUILD_DEBUG"
}
}
create log constants in native code(possibly in some header file) -
#ifdef BUILD_DEBUG
#include<android/log.h>
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "tag", __VA_ARGS__)
#else
#define LOGV(...)
#endif
finally use it like - LOGV("log to print")
Related
I need to debug native C code (ndk-build) on the Android platform.
Is it possible to debug it in Android Studio, and if so how can I do it ?
In your C source code, you could add logs with this function:
__android_log_print(ANDROID_LOG_INFO, "MyTag", "The value is %d", some_variable);
Need this define:
#define <android/log.h>
Logs will be print in LogCat of Android device.
You don't have "step by step" debugger as in Android application.
As the LaurentY response, In your header file, you could define some logs like this:
#define <android/log.h>
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__)
Once in your .c/.cpp code you set the TAG as:
#define LOG_TAG "sample.cpp"
And call your logs using:
LOGI("Yes!!, %s, %s", some, some2);
Open Android Studio
Connect your device and select it from the dropdown in the "Android" tab view in Studio
Go to shell/terminal/CLI and build your app using ndk-build, make or whatever
Set up your usual logcat filters in Studio, and debug away!
Works for me with the lastest Studio version (1.3 RC3). You don't have to debug via CLI / logcat.
In CPP code, for ex in camera HAL, ALOGD messages are not being printed.
Like in set_preview_window()
ALOGD("set_preview_window : X, rc %d", rc);
How to enable them?
try to add following statements in the files which you wanted to catch logs.
#define LOG_NDEBUG 0
#define LOG_NIDEBUG 0
#define LOG_NDDEBUG 0
After compiled, you can try to get logs through adb logcat.
These macros (ALOGX...) are defined in some system core C/C++ headers.
You can find an example in system/core/include/log/log.h (AOSP 6.0.0r1)
Sometimes you need to add liblog in LOCAL_SHARED_LIBRARIES of the corresponding Android.mk :
LOCAL_SHARED_LIBRARIES := ... liblog
Also, you might need to add two lines at the top of C/C++ concerned source files :
#define LOG_NDEBUG 0
#define LOG_TAG "LibName"
Don't forget LOG_NDEBUG 0 if you want ALOGV() logs.
After rebuilding the lib/module, you should be able to see logs in your logcat.
See the definition of ALOGD to get a hint.
In my case i had to do
setprop persist.testapp.debug.log 5
5 was debug level.
To get logs from CPP to Android you ca use the following
__android_log_print(ANDROID_LOG_ERROR, "TRACKERS", "%s", Str);
and to use this you need to import the following library
#include <android/log.h>
if you Want o get simple logs you ca use the following
LOG("Add description here");
I start getting funny errors with NDK with fairly complex program:
#include <stdio.h>
#include <iostream>
int main( int argc, char **argv ) {
return 0;
}
>call c:\android-ndk-r9\ndk-build.cmd
"Compile++ thumb : test <= test.cpp
In file included from C:/workspace/c++11_test//jni/test.cpp:11:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\iostream:43:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_istream.h:31:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_ostream.h:380:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_ostream.c:26:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_num_put.h:180:
In file included from c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_num_put.c:26:
c:/android-ndk-r9/sources/cxx-stl/stlport/stlport\stl/_limits.h:217:48: error: non-type template argument evaluates to -2147483648, which cannot be narrowed to type 'wchar_t'
[-Wc++11-narrowing]
: public _STLP_PRIV _Integer_limits<wchar_t, WCHAR_MIN, WCHAR_MAX, -1, true>
^
c:/android-ndk-r9/platforms/android-14/arch-arm/usr/include\../include/wchar.h:76:22: note: expanded from macro 'WCHAR_MIN'
#define WCHAR_MIN INT_MIN
^
c:/android-ndk-r9/platforms/android-14/arch-arm/usr/include\sys/limits.h:69:18: note: expanded from macro 'INT_MIN'
#define INT_MIN (-0x7fffffff-1) /* min value for an int */
^
1 error generated.
make: * [C:/workspace/c++11_test//obj/local/armeabi/objs/test/test.o] Error 1
It's ndk-r9. 4.8 complie it just fine, only clang stumbles on it.
Do I need to define something to make clang work?
I've tried to Google errors, but got nothing relevant...
Obviously, I can turn it off with -Wno-c++11-narrowing, but I don't want disable narrowing checks.
And it's only shows with stlport_static, there is NO error with gnustl_static
You appear to have enabled C++11, since you are getting this warning.
STLport support for C++11 is not very good. I suspect this is the reason for your woes.
A fix requires changes to STLport, or removing the "-std=c++11" switch from the clang command-line.
IMHO, I would recommend switching to the gnustl (aka. libstdc++), unless there are other reasons that prevent you from doing so (licensing: libstdc++ is GPL3 with some significant exceptions that may or may not be valid for clang - you should determine that for yourself), legacy support, size etc.). I have had very good experiences with that.
Using OpenSSL libcrypto in Android. Some OpenSSL functions output content to FILE*, actually many C functions are doing this, for example:
int X509_REQ_print_fp(FILE *bp,X509_REQ *req);
in this case "bp" can be stdout. My question is how I can get the output redirect to Android logcat? Or in more common way, to a char array or string?
Include android/log.h and define the macro as follows:
#include <android/log.h>
#define LOGV(...) __android_log_print(ANDROID_LOG_VERBOSE, "yourtag",__VA_ARGS__)
I am using Eclipse + gdbserver + ndk7. It seems that debugging through native code (called by Java ) takes ages to step through (~20sec each step), What might cause that? is this normal behaviour?
You can use logging for debugging. please look at this link.
Include log.h file into your Android NDK source file
#include <android/log.h>
Add the line below to your Android.mk make file.
LOCAL_LDLIBS := -llog
Now you can start logging, this two steps allows you to write logs in Eclipse from Android NDK. Write the line below in your Android NDK code and the log will bw appear in the Eclipse
__android_log_write(ANDROID_LOG_ERROR,"Tag","Message");
use following Flags to write logs in the column which you want.
typedef enum android_LogPriority {
ANDROID_LOG_UNKNOWN = 0,
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
ANDROID_LOG_VERBOSE,
ANDROID_LOG_DEBUG,
ANDROID_LOG_INFO,
ANDROID_LOG_WARN,
ANDROID_LOG_ERROR,
ANDROID_LOG_FATAL,
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority
For example if you want to write in Info column you must write
__android_log_write(ANDROID_LOG_INFO,"Tag","Message");