bakaid commented on a change in pull request #758: MINIFICPP-1192 - Add macOS support and in-function offsets to backtrace URL: https://github.com/apache/nifi-minifi-cpp/pull/758#discussion_r409011356
########## File path: libminifi/src/utils/BackTrace.cpp ########## @@ -16,63 +16,113 @@ #include "utils/BackTrace.h" #ifdef HAS_EXECINFO #include <execinfo.h> +#ifndef _GNU_SOURCE +#define _GNU_SOURCE +#endif +#include <dlfcn.h> +#ifdef __linux__ +#include <link.h> +#endif #include <iostream> #include <utility> +#include <cstring> #include <cxxabi.h> #endif -void pull_trace(const uint8_t frames_to_skip) { +namespace { + /** + * Demangles a symbol name using the cxx abi. + * @param symbol_name the mangled name of the symbol + * @return the demangled name on success, empty string on failure + */ + std::string demangle_symbol(const char* symbol_name) { + int status; + char* demangled = abi::__cxa_demangle(symbol_name, nullptr, nullptr, &status); + if (status == 0) { + std::string demangled_name = demangled; + free(demangled); + return demangled_name; + } else { + return ""; + } + } +} // namespace + +void pull_trace(uint8_t frames_to_skip /* = 1 */) { #ifdef HAS_EXECINFO - void *stackBuffer[TRACE_BUFFER_SIZE + 1]; + void* stack_buffer[TRACE_BUFFER_SIZE + 1]; - // retrieve current stack addresses - int trace_size = backtrace(stackBuffer, TRACE_BUFFER_SIZE); + /* Get the backtrace of the current thread */ + int trace_size = backtrace(stack_buffer, TRACE_BUFFER_SIZE); - char **symboltable = backtrace_symbols(stackBuffer, trace_size); Review comment: `backtrace_symbols` is implemented by the C library and just calls `dladdr` (or `dladdr1`) internally, and creates a string from the fields provided by it. We then tried to parse these fields back from the formatted string. Unfortunately, this string is platform-specific, so it only worked on Linux, and even there only in some cases. Also, from the information provided by `dladdr` we can actually add the offset of the actual stack frame within the identified function, which makes debugging much easier. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services