How to play Android program call stack trace

Source: Internet
Author: User

Find out the call stack of the program trace can know who called this interface, but also quickly learn the process of invoking the program, very practical. However, it is important to note that it cannot be used in formal code, only for debugging purposes, and this very expensive source can also cause log flooding.

Here's how to print out the program call Trace in the Android java/c++/c program, and if you need to use it in another environment, the C++/C section needs to be ported to the Corkscrew library.

Java

It is very simple to create an Throwable object and you can get the current stack trace. The following example is a foobar() trace that invokes a function:

1 Private void Foobar () {2     New throwable (); 3     LOG.D (TAG, "stack trace is", T); 4 }
C++

The

is also relatively simple, using the Utils/callstack class. The header file is located in frameworks/native/include/utils/callstack.h and is typically used without modification android.mk. The following example is a trace that calls the Foo::bar () function:

1 2 3 void Foo::bar () {4     // callstack::callstack (const char* LogTag, int32_t ignoredepth, int32_t maxDepth) 5     New CallStack ("Trace"1); 6     Delete t; 7 }
C

A little bit more trouble, need to call directly corkscrew/backtrace . In fact, C + + utils/Callstack is also used corkscrew/backtrace , just to make the package easier to use. We can refer to the CallStack.cpp code inside. The following example is a foobar() trace that invokes a function:

Note: C cannot invoke C + + code directly unless you add the corresponding C wrapper in the C + + class, or dynamically load through Dlsym.

1#include <corkscrew/backtrace.h>2 3 voidDumpstacktrace (Const Char* LogTag, int32_t ignoredepth, int32_t maxDepth)Const {4     Static Const intMax_depth = to;5     Static Const intMax_backtrace_line_length = -;6 7     if(MaxDepth >max_depth) {8MaxDepth =max_depth;9     }Ten backtrace_frame_t mstack[max_depth]; Onessize_t count = Unwind_backtrace (mstack, ignoredepth +1, maxDepth); A     if(Count <=0) { -LOGE ("Can not get stack trace"); -         return; the     } -      - backtrace_symbol_t Symbols[count]; -  + Get_backtrace_symbols (Mstack, count, symbols); -      for(size_t i =0; I < count; i++) { +         CharLine[max_backtrace_line_length]; AFormat_backtrace_line (i, &mstack[i], &Symbols[i], at Line , max_backtrace_line_length); -ALOG (Log_debug, LogTag,"%s%s", -                 "", - Line ); -     } - free_backtrace_symbols (symbols, count); in } -  to voidFoobar () { +Dumpstacktrace ("Trace",1, -); -}

The header file is located system/core/include/corkscrew/backtrace.h , and you need to add it in ANDROID.MK:

1 local_shared_libraries + = Libcorkscrew

How to play Android program call stack trace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.