[mlpack-git] master: Add mlpack::backtrace (1404c1d)

gitdub at mlpack.org gitdub at mlpack.org
Thu Feb 25 10:50:04 EST 2016


Repository : https://github.com/mlpack/mlpack
On branch  : master
Link       : https://github.com/mlpack/mlpack/compare/fd84f3a2e53592d723a8e8a76dadb8aa76820913...1a9c41a86be1fa1dcc5c3b90dd959c2255c3bb8e

>---------------------------------------------------------------

commit 1404c1d41e140444690d2a91c37d3f886bb3aeb1
Author: Grzegorz Krajewski <krajekg at gmail.com>
Date:   Thu Feb 25 16:50:04 2016 +0100

    Add mlpack::backtrace
    
    Printing backtrace for Log::Assert.


>---------------------------------------------------------------

1404c1d41e140444690d2a91c37d3f886bb3aeb1
 src/mlpack/core/util/log.cpp | 74 +++-----------------------------------------
 1 file changed, 4 insertions(+), 70 deletions(-)

diff --git a/src/mlpack/core/util/log.cpp b/src/mlpack/core/util/log.cpp
index 2ff9cf8..155da34 100644
--- a/src/mlpack/core/util/log.cpp
+++ b/src/mlpack/core/util/log.cpp
@@ -4,12 +4,8 @@
  *
  * Implementation of the Log class.
  */
-#ifndef _WIN32
-  #include <cstddef>
-  #include <cxxabi.h>
-#endif
-
 #include "log.hpp"
+#include "backtrace.hpp"
 
 #ifdef BACKTRACE_FOUND
   #include BACKTRACE_HEADER
@@ -56,75 +52,13 @@ void Log::Assert(bool condition, const std::string& message)
 {
   if (!condition)
   {
-#ifdef BACKTRACE_FOUND
-    void* array[25];
-    size_t size = backtrace(array, sizeof(array) / sizeof(void*));
-    char** messages = backtrace_symbols(array, size);
-
-    // Skip first stack frame (points here).
-    for (size_t i = 1; i < size && messages != NULL; ++i)
-    {
-      char *mangledName = 0, *offsetBegin = 0, *offsetEnd = 0;
-
-      // Find parentheses and +address offset surrounding mangled name.
-      for (char *p = messages[i]; *p; ++p)
-      {
-        if (*p == '(')
-        {
-          mangledName = p;
-        }
-        else if (*p == '+')
-        {
-          offsetBegin = p;
-        }
-        else if (*p == ')')
-        {
-          offsetEnd = p;
-          break;
-        }
-      }
+#ifdef HAS_BFD_DL
+    Backtrace bt;
   
-      // If the line could be processed, attempt to demangle the symbol.
-      if (mangledName && offsetBegin && offsetEnd &&
-          mangledName < offsetBegin)
-      {
-        *mangledName++ = '\0';
-        *offsetBegin++ = '\0';
-        *offsetEnd++ = '\0';
-
-        int status;
-        char* realName = abi::__cxa_demangle(mangledName, 0, 0, &status);
-
-        // If demangling is successful, output the demangled function name.
-        if (status == 0)
-        {
-          Log::Debug << "[bt]: (" << i << ") " << messages[i] << " : "
-                    << realName << "+" << offsetBegin << offsetEnd
-                    << std::endl;
-
-        }
-        // Otherwise, output the mangled function name.
-        else
-        {
-          Log::Debug << "[bt]: (" << i << ") " << messages[i] << " : "
-                    << mangledName << "+" << offsetBegin << offsetEnd
-                    << std::endl;
-        }
-        free(realName);
-      }
-      // Otherwise, print the whole line.
-      else
-      {
-          Log::Debug << "[bt]: (" << i << ") " << messages[i] << std::endl;
-      }
-    }
+    Log::Debug << bt.ToString();
 #endif
     Log::Debug << message << std::endl;
 
-#ifdef BACKTRACE_FOUND
-    free(messages);
-#endif
-
     throw std::runtime_error("Log::Assert() failed:" + message);
   }
 }




More information about the mlpack-git mailing list