-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexample.cpp
70 lines (59 loc) · 1.51 KB
/
example.cpp
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
58
59
60
61
62
63
64
65
66
67
68
69
70
#define SW_IMPL
#include "../stackwalkerc.h"
#include <stdio.h>
static void symbol_init(const char* search_path, uint32_t sym_opts, void* userptr)
{
printf("Search path: %s\n", search_path);
}
static void load_module(const char* img, const char* module, uint64_t base_addr, uint32_t size, void* userptr)
{
printf("Load module:\n\timage: %s\n\tmodule: %s\n", img, module);
}
static void callstack_entry(const sw_callstack_entry* entry, void* userptr)
{
printf("\t%s(%d): %s\n", entry->line_filename, entry->line, entry->und_name);
}
static void callstack_begin(void* userptr)
{
puts("Callstack:");
}
static void callstack_end(void* userptr)
{
puts("EndCallstack");
}
static sw_context* g_stackwalk;
template <typename _T>
class Test
{
public:
void SomeFunc(_T c)
{
sw_show_callstack(g_stackwalk, NULL);
}
};
static void foo2(void)
{
Test<int> t;
t.SomeFunc(1);
}
static void foo(void)
{
foo2();
}
int main(int argc, char* argv[])
{
sw_callbacks callbacks;
callbacks.load_module = load_module;
callbacks.callstack_begin = callstack_begin;
callbacks.callstack_entry = callstack_entry;
callbacks.callstack_end = callstack_end;
callbacks.symbol_init = symbol_init;
g_stackwalk = sw_create_context_capture(SW_OPTIONS_ALL, callbacks, NULL);
if (!g_stackwalk) {
puts("ERROR: stackwalk init");
return -1;
}
foo(); // go through some functions and finally call show_callstack
sw_destroy_context(g_stackwalk);
return 1;
}