How to print log messages with in Android framework - android

I am trying to print log messages within core Android framework files. For example, I tried logging messages within MediaRecorderClient.cpp under frameworks\base\media\libmediaplayerservice\. I've tried LOGV, LOGE, LOGD, printf, and __android_log_print, without any success.
Which command should I use to print log messages?

Log should be used, but it will print to logcat not system print.
Example:
Log.d("filter", example text); // filter is any tag you want to use as filter
You can open logcat in eclipse from window-show view -> other -> android -> logcat

What kind of error do you receive? If it does not compile, make sure you've included <android/log.h>, also in Android.mk:
LOCAL_LDLIBS := -llog
If it compiles but does not produce any output, then, like #Warpzit has said, you have to look at logcat to see your messages.

It also seems that you can only pass a char* to the JNI LOG methods. So if you have numbers in your debug string, you have to put them into a char buffer (with sprintf).

/system/core/include/cutils/log.h defines the macros for logging in the Android Framework.
So you uncomment the line that says #define LOG_NDEBUG 0 at the top of the source file and it will enable VERBOSE logging. You will need to recompile that entire library and put it into your android system.img build.
I believe the macro calls are ALOGV, ALOGE and so on.
It is similar to this other answer:
How to get Verbose logging in logcat for a specific module

First declare string
Private String Tag = getClass().getsimplename();
and than in your method
Log.d(Tag,"This is my oncreate method");

Related

Strange Makefile, need explanations

I'm trying to compile a kernel but can't figure out how its Makefile work : https://github.com/LineageOS/android_kernel_sony_msm8994/blob/cm-14.1/scripts/Makefile.build
As I have this error when building : Build of a custom Linux/Android/LineageOS kernel in C doesn't work , I'm for now trying to understand up until the line 44.
Why are there several Makefile with extensions like .build .clean etc rather than these actions being "targets" within the main Makefile?
How can I figure out what the very first $(obj) var refers to ?
Is the mathematical syntax := "equals by definition" instead of = specific to the developper? I do saw this on mathematics notes or symbolic languages such as Wolfram/Mathematica if I'm right, but never within a program.
Why does PHONY := is a variable and not a "type of action" as in the doc ? It should be written .PHONY: as on the very last line of the file. I didn't get this trick.
Why are there 2 underscores before __build the value of PHONY ?
By thanking you for your precisions
Because the person who wrote the makefile wanted to break those out into separate files. Maybe they're included in multiple other files, or they just wanted the top-level makefile to be more clean to read.
You can run make with the -p option and it will print all the values of all the variables in the makefile.
I don't quite understand the question: the := operator in a makefile is used for simply-expanded variable assignments. See the GNU make manual for more info.
That sets the variable PHONY to contain some contents. It is just a normal variable assignment, there's nothing fancy here. Presumably somewhere else in the makefile will appear a line: .PHONY: $(PHONY) and that will make all the targets in the PHONY variable phony.
Because the person who wrote the makefile wanted to use two underscores.

Getting error while setting bitrate in CAN interface for ICS

I am trying to set the bit-rate in i.Mx6 processor in android.
I am using iproute2 utility to set bitrate for CAN controller. The command used to set the bitrate is given below:
#ip link set can0 type can bitrate 125000
While I am trying to set the bitrate in android using below command, I am getting error message.
The error message is given below:
Garbage instead of arguments \"bitrate ...\". " "Try \"ip link help\""
I analysed and debugged inside the source code of this utility and compared with the Linux utility source. I found that the error was occurred in the system call dlsym().
l = dlsym(dlh, buf);
if (l == NULL)
return NULL;
This function suppose to return some valid address. But in my case, its returning the NULL.
Add the following line to external/iproute2/ip/Android.mk
+LOCAL_LDFLAGS := -Wl,-export-dynamic -Wl,--no-gc-sections
include $(BUILD_EXECUTABLE)
Compile again, and it should work.
(1) (Android Source Code)/external/iproute2/ip/iplink.c
#define LIBDIR "/usr/lib/"
to
#define LIBDIR "/usr/lib"
(2) (Android Source Code)/external/iproute2/ip/Android.mk
+LOCAL_LDFLAGS := -Wl,-export-dynamic -Wl,--no-gc-sections
include $(BUILD_EXECUTABLE)
PS. This bug only on Android ICS(4.0.4).

How to use CallStack (in CallStack.tpp) in a executable on android platform?

A question from https://stackoverflow.com/a/11467040/1442443
my final target is to dump user space stack.
I try to build a cpp file as below to a executable on android platform. Therefore, by calling tryToGetStack(), I can get call stack of my executable in run time.
#include <utils/CallStack.h>
namespace android
{
extern "C" void tryToGetStack()
{
CallStack stack;
stack.update();
stack.dump("");
}
}
and add lib setting to to Android.mak since CallStack.tpp is in libutils
LOCAL_SHARED_LIBRARIES += libutils
but I always get error with message:
error: undefined reference to 'android::CallStack::CallStack()'
error: undefined reference to 'android::CallStack::update(int, int)'
...
It seems the executable resolve the symbols at link time rather than load the .so file in runtime?
Do I missing something or there is some limit in Android build system?
I know it is a simple question, but I really need help...
update1
I try to add the code to another executable. The result is same... Does anyone knows the rule of android build system?
update2
There are some key words in my console "target StaticExecutable: ...", I think is is the answer.
http://en.wikipedia.org/wiki/Static_executable
my final target is to dump user space stack.
after google so many information from internet, I found there are 4 ways:
ptraceļ¼š http://en.wikipedia.org/wiki/Ptrace
It is really hard to use ptrace, and we need to stop the thread before using ptrace to attach
_unwind_backtrace: the way used by CallStack (CallStack class in CallStack.cpp)
example: http://git.stlinux.com/?p=stm/uclibc.git;a=blob;f=libubacktrace/sysdeps/sh/backtrace.c;h=18b91b1bb3fa26344a521927c631553a410fcf56;hb=d6a3d9ece5922a337800a8e2ed4db7e226f9ccb3
It is work with a drawback: if you use it as the thread is processing signal, it would dump signal stack rather than dump thread stack
The same problen: How to get fullstacktrace using _Unwind_Backtrace on SIGSEGV
backtrace: http://www.gnu.org/software/libc/manual/html_node/Backtraces.html
a GNU extension function, not be implemented in Bionic libc used by Android
reference: https://stackoverflow.com/a/8295238/1442443
reference: http://lists.puredata.info/pipermail/pd-list/2012-02/094258.html
a patch to dump user space thread stack: http://www.gossamer-threads.com/lists/linux/kernel/1525096
but only be implemented in X86 architecture... orz
I try to port it to android, no it only shows the first frame of stack since arm does not use frame pointer.
So... 2 is the answer.
However, I wonder if someone can resolve the problem : How to get fullstacktrace using _Unwind_Backtrace on SIGSEGV
update:
if you can use cross compiler to use glic to compile your code, maybe you can use 3. backtrace !
http://communities.mentor.com/community/cs/archives/arm-gnu/msg02514.html
update2
a good article
http://codingrelic.geekhold.com/2009/05/pre-mortem-backtracing.html
Since this is such an important question here is my answer that worked for me. My code is in C so it has to call a C++ function that can access android::CallStack.
stackdump.cpp:
#include < utils/CallStack.h >
extern "C" void dumpCallStack( char *label)
{
android::CallStack cs;
cs.update();
cs.dump(label);
}
my main code (foo.c):
void dumpCallStack(char *label);
...
dumpCallStack(\__FUNCTION__);
I have had the same problem once. And it is hard to interpret.
The syntax is of course correct and reasonable!
I have tried many methods but it did not work.
Finally, I got an idea that, the lib reference "LOCAL_SHARED_LIBRARIES += libutils" should be put into a makefile generating a dynamic library rather than into a makefile generating a static library. This is the final reason.
Reference:
http://yongbingchen.github.io/blog/2013/05/09/dump-stack-in-android-native-c-code/
I also received this error, but I added:
LOCAL_STATIC_LIBRARIES += libutils
before line of LOCAL_MODULE := xxx in vm/Android.m of three targets and added
LOCAL_SHARED_LIBRARIES += libcorkscrew
in vm/Android.mk
and libdex/Android.mk, and same for the dexlist/Android.mk, dexdump/Android.mk
After all these done, it works for me.

Android NDK assert.h problems

First one - is what NDEBUG somehow already defined by default, so asserts don't work until you #undef it.
Second one - they do they work, but i receive no logging in DDMS.
If there is some android specific one assert.h?
Or i just do something wrong?
If you want to compile your code with asserts then you can do it in three ways:
use NDK_DEBUG=1 argument in ndk-build commandline
add android:debuggable="true" to < application > tag in AndroidManifest.xml
add APP_OPTIM := debug to your Application.mk file - this will also disable optimizations and will compile with debug symbols
Usually program will crash due to SIGSEGV signal after assert() is called, by default NDEBUG is define, you may turn off by add the flag (LOCAL_CFLAGS += -UNDEBUG) during compilation but not work for my case.
I found another solution is using __android_log_assert, simply define as below and replace assert() with assert3:
#define assert3(e...) __android_log_assert(e, "TAG", #e)

Retrieve Logcat data through native (C) code

I was wondering if there is any way to access the logcat logging data from native code ?
Also if someone knows what kind of shell can be accessed on the native linux system directly on the device itself (so NOT by starting a shell through adb on a client pc !!!) ?
Thanks in advance
You can either read the raw data fom /dev/log/main or you can run the built-in shell command logcat and pipe the result to a file descriptor as usual. The logcat command is usually preferably because then the printout is easier to filter and format.
As for the built-in shell, it is called toolbox and the source can be found in the Android open source project. The shell is rather similar to bash. Toolbox contains lots more functionality than just a shell. It is very similar to Busybox, but released under another license.
You can set it up using a log tag in your source file and then include the library as so:
#define LOG_TAG "some.unique.identifier" //I usually use the class name
#include <utils/Log.h>
Then in the Android.mk file you will need to add the liblog library dependency to your LOCAL_SHARED_LIBRARIES.
LOCAL_SHARED_LIBRARIES += liblog
Also, take note that logcat looks for the LOG_TAG definition to include as part of the tagging so it makes searching for your logs easier. Then you can log as such:
int my_int = 0;
ALOGI("some logs.... print my int: %d", my_int);
ALOGI is for info, you can also use ALOGE, ALOGD, ALOGV and ALOGW for error, debug, verbose and warning logging, respectively.
ALOG* is analogous to printf. I interchange them at times if I need to debug on different platforms, say Linux.

Categories

Resources