diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2015-05-27 13:48:45 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2015-05-27 13:48:45 +0100 |
| commit | 7245d2abe907b11bfa67c4ea0059ab00b34b5bc9 (patch) | |
| tree | adeaababe327ba7edaa275a17d9e7de6208e97e9 /src/backtrace.cc | |
| parent | 4a588d6ce5065c698f8a455f7e8025b56a23cd14 (diff) | |
Extract Backtrace out of SafePtr as a general utility
Diffstat (limited to 'src/backtrace.cc')
| -rw-r--r-- | src/backtrace.cc | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/backtrace.cc b/src/backtrace.cc new file mode 100644 index 00000000..f7be9f76 --- /dev/null +++ b/src/backtrace.cc @@ -0,0 +1,56 @@ +#include "backtrace.hh" + +#include <string.h> +#include <stdlib.h> + +#if defined(__linux__) || defined(__APPLE__) +# include <execinfo.h> +#elif defined(__CYGWIN__) +# include <windows.h> +#endif + +namespace Kakoune +{ + +Backtrace::Backtrace() +{ + #if defined(__linux__) || defined(__APPLE__) + num_frames = backtrace(stackframes, max_frames); + #elif defined(__CYGWIN__) + num_frames = CaptureStackBackTrace(0, max_frames, stackframes, nullptr); + #endif +} + +const char* Backtrace::desc() const +{ + #if defined(__linux__) || defined(__APPLE__) + char** symbols = backtrace_symbols(stackframes, num_frames); + int size = 0; + for (int i = 0; i < num_frames; ++i) + size += strlen(symbols[i]) + 1; + + char* res = (char*)malloc(size+1); + res[0] = 0; + for (int i = 0; i < num_frames; ++i) + { + strcat(res, symbols[i]); + strcat(res, "\n"); + } + free(symbols); + return res; + #elif defined(__CYGWIN__) + char* res = (char*)malloc(num_frames * 20); + res[0] = 0; + for (int i = 0; i < num_frames; ++i) + { + char addr[20]; + snprintf(addr, 20, "0x%p", stackframes[i]); + strcat(res, addr); + } + return res; + #else + return "<not implemented>"; + #endif +} + +} |
