carl
carl

Reputation: 305

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

Upvotes: 3

Views: 7306

Answers (4)

eggfly
eggfly

Reputation: 31

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.

Upvotes: 0

Verdigrass
Verdigrass

Reputation: 1301

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/

Upvotes: 0

user3211341
user3211341

Reputation: 21

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__);

Upvotes: 2

carl
carl

Reputation: 305

my final target is to dump user space stack.

after google so many information from internet, I found there are 4 ways:

  1. ptracehttp://en.wikipedia.org/wiki/Ptrace

    It is really hard to use ptrace, and we need to stop the thread before using ptrace to attach

  2. _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

  3. 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

  4. 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

Upvotes: 6

Related Questions