From 21cc733161280fa7f8fea8113a40bcbd6c61f8d1 Mon Sep 17 00:00:00 2001 From: Milian Wolff Date: Sun, 22 Apr 2018 12:39:59 +0200 Subject: [PATCH] Print backtraces when encountering a write error --- src/track/libheaptrack.cpp | 34 +++++++++++++++++++++++++++++++ src/util/libunwind_config.h.cmake | 4 ++++ 2 files changed, 38 insertions(+) diff --git a/src/track/libheaptrack.cpp b/src/track/libheaptrack.cpp index a50417f2..5c469a39 100644 --- a/src/track/libheaptrack.cpp +++ b/src/track/libheaptrack.cpp @@ -123,6 +123,39 @@ inline void debugLog(const char fmt[], Args... args) } } +void printBacktrace() +{ + if (s_debugVerbosity == NoDebugOutput) + return; + +#if LIBUNWIND_HAS_UNW_GETCONTEXT && LIBUNWIND_HAS_UNW_INIT_LOCAL + RecursionGuard guard; + + unw_context_t context; + unw_getcontext(&context); + + unw_cursor_t cursor; + unw_init_local(&cursor, &context); + + int frameNr = 0; + while (unw_step(&cursor)) { + ++frameNr; + unw_word_t ip = 0; + unw_get_reg(&cursor, UNW_REG_IP, &ip); + + unw_word_t sp = 0; + unw_get_reg(&cursor, UNW_REG_SP, &sp); + + char symbol[256] = {""}; + unw_word_t offset = 0; + unw_get_proc_name(&cursor, symbol, sizeof(symbol), &offset); + + fprintf(stderr, "#%-2d 0x%016" PRIxPTR " sp=0x%016" PRIxPTR " %s + 0x%" PRIxPTR "\n", frameNr, + static_cast(ip), static_cast(sp), symbol, static_cast(offset)); + } +#endif +} + /** * Set to true in an atexit handler. In such conditions, the stop callback * will not be called. @@ -486,6 +519,7 @@ class HeapTrack void writeError() { debugLog("write error %d/%s", errno, strerror(errno)); + printBacktrace(); s_data->out = nullptr; shutdown(); } diff --git a/src/util/libunwind_config.h.cmake b/src/util/libunwind_config.h.cmake index 25da2a16..ce2de11d 100644 --- a/src/util/libunwind_config.h.cmake +++ b/src/util/libunwind_config.h.cmake @@ -23,5 +23,9 @@ #cmakedefine01 LIBUNWIND_HAS_UNW_BACKTRACE_SKIP +#cmakedefine01 LIBUNWIND_HAS_UNW_GETCONTEXT + +#cmakedefine01 LIBUNWIND_HAS_UNW_INIT_LOCAL + #endif // LIBUNWIND_CONFIG_H