Add stackdump source
This commit is contained in:
commit
df88f56450
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
/stackdump
|
||||||
2
Makefile
Normal file
2
Makefile
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
stackdump: stackdump.c Makefile
|
||||||
|
clang -o $@ -Wall -g stackdump.c -lunwind
|
||||||
54
stackdump.c
Normal file
54
stackdump.c
Normal file
@ -0,0 +1,54 @@
|
|||||||
|
#define UNW_LOCAL_ONLY
|
||||||
|
#include <libunwind.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
// Function to print the current stack trace
|
||||||
|
void show_backtrace(void)
|
||||||
|
{
|
||||||
|
unw_cursor_t cursor;
|
||||||
|
unw_context_t context;
|
||||||
|
unw_word_t ip, sp;
|
||||||
|
char sym[256];
|
||||||
|
unw_word_t offset;
|
||||||
|
|
||||||
|
// Initialize cursor to current frame for local unwinding
|
||||||
|
unw_getcontext(&context);
|
||||||
|
unw_init_local(&cursor, &context);
|
||||||
|
|
||||||
|
printf("--- Stack Trace ---\n");
|
||||||
|
// Unwind frames one by one
|
||||||
|
while (unw_step(&cursor) > 0) {
|
||||||
|
unw_get_reg(&cursor, UNW_REG_IP, &ip);
|
||||||
|
unw_get_reg(&cursor, UNW_REG_SP, &sp);
|
||||||
|
|
||||||
|
// Try to get the function name
|
||||||
|
if (unw_get_proc_name(&cursor, sym, sizeof(sym), &offset) == 0) {
|
||||||
|
printf("0x%lx: (%s+0x%lx)\n", (long)ip, sym, (long)offset);
|
||||||
|
} else {
|
||||||
|
printf("0x%lx: -- no symbol name found --\n", (long)ip);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
printf("-------------------\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
void function_c(void)
|
||||||
|
{
|
||||||
|
show_backtrace();
|
||||||
|
}
|
||||||
|
|
||||||
|
void function_b(void)
|
||||||
|
{
|
||||||
|
function_c();
|
||||||
|
}
|
||||||
|
|
||||||
|
void function_a(void)
|
||||||
|
{
|
||||||
|
function_b();
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(int argc, char **argv)
|
||||||
|
{
|
||||||
|
function_a();
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user