Go Print Debug Stack method under Android

Source: Internet
Author: User
Tags throwable

http://blog.csdn.net/freshui/article/details/9456889

The printing stack is a common method of debugging, in general, when the system is abnormal, we can print out the stack of abnormal cases, so it is very convenient to find errors. There is actually another very useful feature: analyzing the behavior of the code. Android code is too large and complex, and full static analysis is often impossible, so it is necessary to analyze the dynamic of the print stack.

Android Print Stack method, simple collation

1. zygote Stack Dump

This can actually dump both the Java thread and the native thread stack, which is available for Java threads, the Java stack, and the native stack.

Using the method is straightforward, enter it directly in the ADB shell or in the string:

[Plain]View Plaincopy
    1. Kill-3 <pid>

The trace of the output is saved in the/data/anr/traces.txt file. This needs to be noted, if there is no/data/anr/this directory or/data/anr/traces.txt this file, you need to create it manually, and set up read and write permissions.

If you need to make it easier to control the timing of the stack's output in your code, you can get Zygote's core dump with the following command:

[Java]View Plaincopy
    1. Process.sendsignal (PID, process.signal_quit);

The principle is the same as the command line.

But there are two points to note:

    1. The ADB shell may not have permissions and requires root.
    2. The native Thread's stack print is turned off in Android 4.2, as described in the Dalvik/vm/thread.cpp Dumpnativethread method:
[CPP]View Plaincopy
    1. Dvmprintdebugmessage (Target,
    2. "\"%s\ "systid=%d nice=%d sched=%d/%d cgrp=%s\n",
    3. Name, Tid, getpriority (prio_process, Tid),
    4. Schedstats.policy, schedstats.priority, Schedstats.group);
    5. Dumpschedstat (target, TID);
    6. Temporarily disabled collecting native stacks from Non-dalvik
    7. threads because sometimes they misbehave.
    8. Dvmdumpnativestack (target, TID);

The print of the native stack is turned off! For most cases, however, you can open this comment directly.

2. Debuggerd Stack Dump

Debuggerd is a daemon process for Android that dumps the runtime information of a process for analysis when an error occurs. Debuggerd generated Coredump data is presented as text, saved in the/data/tombstone/directory (the name is also very image, Tombstone is the meaning of the tombstone), a total of 10 files can be saved, when more than 10, Overrides the oldest generated file. Starting with version 4.2, Debuggerd is also a utility: You can print the native stack of the current process without interrupting the execution of the process. Use this method:

[Plain]View Plaincopy
    1. Debuggerd-b <pid>

This can help us analyze the process execution behavior, but the most useful is that it can be very simple to navigate to the code location of the dead loop caused by the deadlock or error logic in the native process.

3. Print stacks in Java code

Java code Printing stack is relatively simple, stack information acquisition and output, can be achieved through the Throwable class method. It is now common practice to print the stack when the Java process has an exception that needs attention, and then decide to exit or save. The usual approach is to use the exception Printstacktrace () method:

[Java]View Plaincopy
    1. try {
    2. ...
    3. } catch (RemoteException e) {
    4. E.printstacktrace ();
    5. ...
    6. }

Of course, you can only print the stack does not exit, so it is convenient to analyze the dynamic operation of the code. The way to insert a stack in Java code is as follows:

[Java]View Plaincopy
    1. LOG.D (tag,log.getstacktracestring (new Throwable ()));

4. Print stacks in C + + code

C + + also supports exception handling, the exception handling library, already contains the interface to get BackTrace, Android also use this interface to print stack information. In Android C + +, a tool class callstack has been integrated, in libutils.so. How to use:

[CPP]View Plaincopy
    1. #include <utils/CallStack.h>
    2. ...
    3. CallStack stack;
    4. Stack.update ();
    5. Stack.dump ();

The use of the method is relatively simple. At present, the Andoid4.2 version of the relevant information has been resolved in place, symbol table lookup, Demangle, offset position correction are done.

[Plain]View Plaincopy

5. Print stacks in C code

C code, especially the underlying C library, would be cumbersome to see the stack information of the call. CallStack must not be used, one is because in fact, C + + write, need to re-encapsulation to use in C, the second is the underlying library sung the upper library functions, will cause the linker loop dependency and cannot link. However, there is no way to deal with the unwind calls and symbolic parsing functions used in the Android tool class CallStack implementation.

It is important to note that, in order to solve the link problem, it is better to use the Dlopen method to find the interface that needs to be used and then call directly, this will be relatively simple. Here is the relevant implementation code, just insert this part of the code in the file you want to print, and then call Getcallstack (), without having to include too many header files and modifying the Android.mk file:

[CPP]View Plaincopy
  1. #define MAX_DEPTH 31
  2. #define MAX_BACKTRACE_LINE_LENGTH 800
  3. #define PATH "/system/lib/libcorkscrew.so"
  4. typedef ssize_t (*UNWINDFN) (backtrace_frame_t*, size_t, size_t);
  5. typedef VOID (*UNWINDSYMBFN) (const backtrace_frame_t*, size_t, backtrace_symbol_t*);
  6. typedef VOID (*UNWINDSYMBFREEFN) (backtrace_symbol_t*, size_t);
  7. static void *ghandle = NULL;
  8. static int getcallstack (void) {
  9. ssize_t i = 0;
  10. ssize_t result = 0;
  11. ssize_t count;
  12. backtrace_frame_t Mstack[max_depth];
  13. backtrace_symbol_t Symbols[max_depth];
  14. UNWINDFN unwind_backtrace = NULL;
  15. UNWINDSYMBFN get_backtrace_symbols = NULL;
  16. Unwindsymbfreefn free_backtrace_symbols = NULL;
  17. //Open the.
  18. if (Ghandle = = NULL) Ghandle = Dlopen (PATH, Rtld_now);
  19. //Get the interface for unwind and symbol analyse
  20. if (ghandle! = NULL) Unwind_backtrace = (UNWINDFN) dlsym (Ghandle, "Unwind_backtrace");
  21. if (ghandle! = NULL) Get_backtrace_symbols = (UNWINDSYMBFN) dlsym (Ghandle, "Get_backtrace_symbols");
  22. if (ghandle! = NULL) Free_backtrace_symbols = (unwindsymbfreefn) dlsym (Ghandle, "Free_backtrace_symbols");
  23. if (!ghandle | |! Unwind_backtrace | |! Get_backtrace_symbols | | !free_backtrace_symbols) {
  24. Aloge ("error! Cannot get unwind info:handle:%p%p%p%p ",
  25. Ghandle, Unwind_backtrace, Get_backtrace_symbols, Free_backtrace_symbols);
  26. return result;
  27. }
  28. Count= Unwind_backtrace (Mstack, 1, max_depth);
  29. Get_backtrace_symbols (Mstack, count, symbols);
  30. For (i = 0; i < count; i++) {
  31. Char Line[max_backtrace_line_length];
  32. Const char* mapName = symbols[i].map_name? Symbols[i].map_name: "<unknown>";
  33. Const char* symbolname =symbols[i].demangled_name symbols[i].demangled_name:symbols[i].symbol_name;
  34. size_t fieldwidth = (max_backtrace_line_length-80)/2;
  35. if (symbolname) {
  36. uint32_t pc_offset = symbols[i].relative_pc-symbols[i].relative_symbol_addr;
  37. if (pc_offset) {
  38. snprintf (line, Max_backtrace_line_length, "#%02d pc%08x%.*s (%.*s+%u)",
  39. I, SYMBOLS[I].RELATIVE_PC, Fieldwidth, MapName,
  40. Fieldwidth, Symbolname, Pc_offset);
  41. } Else {
  42. snprintf (line, Max_backtrace_line_length, "#%02d pc%08x%.*s (%.*s)",
  43. I, SYMBOLS[I].RELATIVE_PC, Fieldwidth, MapName,
  44. Fieldwidth, Symbolname);
  45. }
  46. } Else {
  47. snprintf (line, Max_backtrace_line_length, "#%02d pc%08x%.*s",
  48. I, SYMBOLS[I].RELATIVE_PC, Fieldwidth, mapName);
  49. }
  50. ALOGD ("%s", line);
  51. }
  52. Free_backtrace_symbols (symbols, count);
  53. return result;
  54. }

The SCHED_POLICY.C stack call analysis is as follows, pay attention to the specific whether to print, where to print, but also through the PID, UID, property, etc. to control, so that will not be drowned in the sea of trace.

[Plain]View Plaincopy
  1. D/schedpolicy (1350): #00 pc 0000676c/system/lib/libcutils.so
  2. D/schedpolicy (1350): #01 pc 00006b3a/system/lib/libcutils.so (set_sched_policy+49)
  3. D/schedpolicy (1350): #02 pc 00010e82/system/lib/libutils.so (androidsetthreadpriority+61)
  4. D/schedpolicy (1350): #03 pc 00068104/system/lib/libandroid_runtime.so (Android_os_process_setthreadpriority (_ jnienv*, _jobject*, int, int) +7)
  5. D/schedpolicy (1350): #04 pc 0001e510/system/lib/libdvm.so (dvmplatforminvoke+112)
  6. D/schedpolicy (1350): #05 pc 0004d6aa/system/lib/libdvm.so (dvmcalljnimethod (unsigned int const*, jvalue*, Method const *, thread*) +417)
  7. D/schedpolicy (1350): #06 pc 00027920/system/lib/libdvm.so
  8. D/schedpolicy (1350): #07 pc 0002b7fc/system/lib/libdvm.so (Dvminterpret (thread*, Method const*, jvalue*) +184)
  9. D/schedpolicy (1350): #08 pc 00060c30/system/lib/libdvm.so (DVMCALLMETHODV (thread*, Method const*, object*, BOOL, JValu e*, std::__va_list) +271)
  10. D/schedpolicy (1350): #09 pc 0004cd34/system/lib/libdvm.so
  11. D/schedpolicy (1350): #10 pc 00049382/system/lib/libandroid_runtime.so
  12. D/schedpolicy (1350): #11 pc 00065e52/system/lib/libandroid_runtime.so
  13. D/schedpolicy (1350): #12 pc 0001435e/system/lib/libbinder.so (android::bbinder::transact (unsigned int, Android:: Parcel Const&, Android::P arcel*, unsigned int) +57)
  14. D/schedpolicy (1350): #13 pc 00016f5a/system/lib/libbinder.so (android::ipcthreadstate::executecommand (int) +513)
  15. D/schedpolicy (1350): #14 pc 00017380/system/lib/libbinder.so (Android::ipcthreadstate::jointhreadpool (bool) +183)
  16. D/schedpolicy (1350): #15 pc 0001b160/system/lib/libbinder.so
  17. D/schedpolicy (1350): #16 pc 00011264/system/lib/libutils.so (Android::thread::_threadloop (void*) +111)
  18. D/schedpolicy (1350): #17 pc 000469bc/system/lib/libandroid_runtime.so (Android::androidruntime::javathreadshell ( void*) +63)
  19. D/schedpolicy (1350): #18 pc 00010dca/system/lib/libutils.so
  20. D/schedpolicy (1350): #19 pc 0000e3d8/system/lib/libc.so (__thread_entry+72)
  21. D/schedpolicy (1350): #20 pc 0000dac4/system/lib/libc.so (pthread_create+160)
  22. D/schedpolicy (1350): #00 pc 0000676c/system/lib/libcutils.so
  23. D/schedpolicy (1350): #01 pc 00006b3a/system/lib/libcutils.so (set_sched_policy+49)
  24. D/schedpolicy (1350): #02 pc 00016f26/system/lib/libbinder.so (android::ipcthreadstate::executecommand (int) +461)
  25. D/schedpolicy (1350): #03 pc 00017380/system/lib/libbinder.so (Android::ipcthreadstate::jointhreadpool (bool) +183)
  26. D/schedpolicy (1350): #04 pc 0001b160/system/lib/libbinder.so
  27. D/schedpolicy (1350): #05 pc 00011264/system/lib/libutils.so (Android::thread::_threadloop (void*) +111)
  28. D/schedpolicy (1350): #06 pc 000469bc/system/lib/libandroid_runtime.so (Android::androidruntime::javathreadshell ( void*) +63)
  29. D/schedpolicy (1350): #07 pc 00010dca/system/lib/libutils.so
  30. D/schedpolicy (1350): #08 pc 0000e3d8/system/lib/libc.so (__thread_entry+72)
  31. D/schedpolicy (1350): #09 pc 0000dac4/system/lib/libc.so (pthread_create+160)

6. Other stack information query

Go Print Debug Stack method under Android

Related Article

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.