error handling - In C++, how can I get the current thread's call stack? -
i'm writing error handler code i'm working in, in c++. able make sort of reference whatever have on stack, without being explicitly passed me. specifically, let's want print names of functions on call stack, in order. trivial in managed runtime environments jvm, not trivial 'simple' compiled code. can this?
notes:
- assume simplicity compile code debugging information , no optimization.
- i want write either platform-independent or multi-platform. prefer former.
- if think i'm trying reinvent wheel, link source of relevant wheel , i'll there.
update:
i can't believe how need bend on backwards this... makes me pine language shall not mentioned.
there way back-trace in c++, though not portable. cannot speak windows, on unix-like systems there backtrace api consists of following functions:
int backtrace(void** array, int size);
char** backtrace_symbols(void* const* array, int size);
void backtrace_symbols_fd(void* const* array, int size, int fd);
you can find date documentation , examples on gnu website here. there other sources, this manual page os x, etc.
keep in mind there few problems getting backtrace using api. firstly, there no file names , no line numbers. secondly, cannot backtrace in situations if frame pointer omitted entirely (default behavior of recent gcc compilers x86_64 platforms). or maybe binary doesn't have debug symbols whatsoever. on systems, have specify -rdynamic
flag when compiling binary (which has other, possible undesirable, effects).
Comments
Post a Comment