CFSection9's Studio.

打印BackTrace

字数统计: 208阅读时长: 1 min
2018/10/13 Share

Kernel code print backtrace

1
2
3
4
//must set following options in kernel config
//Kernel hacking -> Kernel debugging
//Kernel hacking -> Verbose kernel error messages
dump_stack();

Android C code print backtrace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//First add `LOCAL_SHARED_LIBRARIES += libcorkscrew` in Android.mk
#include <corkscrew/backtrace.h>
void print_backtrace()
{
   int i;
   char line[1024];
   backtrace_frame_t frames[256];
   ssize_t count = unwind_backtrace(frames, 2, sizeof(frames));
   backtrace_symbol_t symbols[count];
   get_backtrace_symbols(frames, count, symbols);
   for(i = 0; i < count; i++){
       memset(line, 0, 1024);
       format_backtrace_line(i, frames + i, symbols +i, line, 1024);
       ALOGE(" #%d %s\n", i,  line); /* use your logging here */
  }
   free_backtrace_symbols(symbols, count);
}

GNU C code print BackTrace

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <execinfo.h>
#include <stdio.h>
#include <stdlib.h>

void PrintTrace(int u8TraceSize)
{
       void *btArray[u8TraceSize];
       size_t btSize;
       char **strings;
       size_t i;
       btSize = backtrace(btArray, u8TraceSize);
       strings = backtrace_symbols(btArray, btSize);
       printf("\033[34;43m===BeckTrace Dump Start===\nObtained %zd stack frames.\n", btSize);
       for(i = 0; i < btSize; i++)
      {
               printf("#%d:%s\n", i, strings[i]);
      }
       printf("===BeckTrace Dump End===\033[0m\n");
       free(strings);

//超过堆栈高度,会自动跳过
PrintTrace(20);

Android Java print backtrace

1
2
Exception e = new Exception("This is a backtrace");
e.printStackTrace();
CATALOG
  1. 1. Kernel code print backtrace
  2. 2. Android C code print backtrace
  3. 3. GNU C code print BackTrace
  4. 4. Android Java print backtrace