-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbacktrace_test_unwind.c
57 lines (44 loc) · 981 Bytes
/
backtrace_test_unwind.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// gcc backtrace_test_unwind.c -rdynamic -lunwind -ldl
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#ifndef __USE_GNU
#define __USE_GNU
#endif
#include <dlfcn.h>
#define UNW_LOCAL_ONLY
#include <libunwind.h>
#include <execinfo.h>
void my_backtrace (void) {
unw_cursor_t cursor;
unw_context_t context;
unw_word_t offset;
unw_word_t pc;
char fname [64] = {0};
int nCnt = 0;
unw_getcontext (&context);
unw_init_local (&cursor, &context);
while (unw_step (&cursor) > 0) {
unw_get_reg (&cursor, UNW_REG_IP, &pc);
fname[0] = 0x00;
(void) unw_get_proc_name (&cursor, fname, sizeof(fname), &offset);
Dl_info info;
dladdr ((void*)pc, &info);
fprintf (stdout, "%s : backtrace [%d] %s(%s+0x%lx) [%p]\n",
__func__, nCnt, info.dli_fname, fname, offset, (void*)pc);
nCnt ++;
}
}
void hoge2 () {
my_backtrace ();
}
void hoge1 () {
hoge2 ();
}
int main (void)
{
hoge1 ();
exit (EXIT_SUCCESS);
}