2018-04-26 19:17:34 +02:00
|
|
|
#include <cstdio>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
|
|
|
|
#include "../stack_walker/stack_walker.hpp"
|
|
|
|
|
2018-04-27 11:06:25 +02:00
|
|
|
void fill_my_stack1(volatile int&);
|
|
|
|
void fill_my_stack2(volatile int&);
|
|
|
|
|
|
|
|
void stack_filled() {
|
2018-04-27 17:32:49 +02:00
|
|
|
int frame_id = 0;
|
2018-04-27 11:06:25 +02:00
|
|
|
walk_stack([&frame_id](const unwind_context_t& ctx) {
|
|
|
|
Dl_info func_info;
|
|
|
|
int dl_rc = dladdr((void*) ctx.rip, &func_info);
|
2018-04-27 17:32:49 +02:00
|
|
|
printf("#%d - %s — %%rip = 0x%lx, %%rbp = 0x%lx, %%rsp = 0x%lx\n",
|
|
|
|
++frame_id,
|
2018-04-27 11:06:25 +02:00
|
|
|
(dl_rc != 0) ? func_info.dli_sname : "[Unknown func]",
|
2018-05-18 11:33:39 +02:00
|
|
|
get_register(ctx, SW_REG_RIP),
|
|
|
|
get_register(ctx, SW_REG_RBP),
|
|
|
|
get_register(ctx, SW_REG_RSP));
|
2018-04-27 11:06:25 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_my_stack2(volatile int& depth) {
|
|
|
|
if(depth == 0)
|
|
|
|
stack_filled();
|
|
|
|
else {
|
|
|
|
--depth;
|
|
|
|
fill_my_stack1(depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void fill_my_stack1(volatile int& depth) {
|
|
|
|
if(depth == 0)
|
|
|
|
stack_filled();
|
|
|
|
else {
|
|
|
|
--depth;
|
|
|
|
fill_my_stack2(depth);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:17:34 +02:00
|
|
|
int main() {
|
|
|
|
Dl_info main_info;
|
|
|
|
int rc = dladdr((void*)&main, &main_info);
|
|
|
|
|
|
|
|
printf("I'm in %s!\n", (rc != 0) ? main_info.dli_sname : "[No data]");
|
|
|
|
|
|
|
|
stack_walker_init();
|
|
|
|
|
2018-04-27 11:06:25 +02:00
|
|
|
printf("Stack walker init'd!\n");
|
|
|
|
volatile int depth = 10;
|
|
|
|
fill_my_stack1(depth);
|
|
|
|
|
2018-04-26 19:17:34 +02:00
|
|
|
return 0;
|
|
|
|
}
|