Skip to content

Instantly share code, notes, and snippets.

@Mufanc
Created August 18, 2023 08:00
Show Gist options
  • Save Mufanc/cde0a1238c4b5d53d92fe86c38f2f5ee to your computer and use it in GitHub Desktop.
Save Mufanc/cde0a1238c4b5d53d92fe86c38f2f5ee to your computer and use it in GitHub Desktop.
#include <cstdio>
#include <cxxabi.h>
#include <dlfcn.h>
#include <unwind.h>
void backtrace() {
int count = 0;
_Unwind_Trace_Fn cb = [] (_Unwind_Context *context, void *args) -> _Unwind_Reason_Code {
int *count = static_cast<int *>(args);
void *addr = reinterpret_cast<void *>(_Unwind_GetIP(context));
const char *symbol = nullptr;
Dl_info info;
if (dladdr(addr, &info) && info.dli_sname) {
symbol = info.dli_sname;
}
int status = 0;
char *symbol_d = __cxxabiv1::__cxa_demangle(symbol, nullptr, nullptr, &status);
printf("%03d: %p %s\n", (*count)++, addr, (symbol_d && status == 0) ? symbol_d : symbol);
delete symbol_d;
return _URC_NO_REASON;
};
_Unwind_Backtrace(cb, &count);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment