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_r409012477
 
 

 ##########
 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);
-  /**
-   * we can skip the signal handler, call to pull_trace, and the first entry 
for backtrace_symbols
-   */
+  /* We can skip the signal handler, call to pull_trace, and the first entry 
for backtrace_symbols */
   for (int i = frames_to_skip; i < trace_size; i++) {
-    char *start_parenthetical = nullptr;
-    char *functor = nullptr;
-    char *stop_parenthetical = nullptr;
-
-    for (char *p = symboltable[i]; *p; ++p) {
-      if (*p == '(') {
-        start_parenthetical = p;
-      } else if (*p == '+') {
-        functor = p;
-      } else if (*p == ')' && functor) {
-        stop_parenthetical = p;
-        break;
+    const char* file_name = "???";
+    const char* symbol_name = nullptr;
+    uintptr_t symbol_offset = 0;
+
+    /* Translate the address to symbolic information */
+    Dl_info dl_info{};
+#ifdef __linux__
 
 Review comment:
   This business logic is based on glibc's, uClibc's and macOS's Libc's 
`backtrace_symbols` implementation.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to