]> granicus.if.org Git - icinga2/commitdiff
Move Addr2Line function to the Utility class.
authorGunnar Beutner <gunnar.beutner@netways.de>
Sun, 23 Mar 2014 18:39:25 +0000 (19:39 +0100)
committerGunnar Beutner <gunnar.beutner@netways.de>
Sun, 23 Mar 2014 18:39:25 +0000 (19:39 +0100)
Refs #5846

lib/base/stacktrace.cpp
lib/base/stacktrace.h
lib/base/utility.cpp
lib/base/utility.h

index 71ab6772c24ef5eb96217910432f2aa24af410d3..ba37166398892de78268c1b55b646e2b23f26a97 100644 (file)
@@ -22,7 +22,6 @@
 #include "base/utility.h"
 #include "base/convert.h"
 #include "base/application.h"
-#include <boost/algorithm/string/trim.hpp>
 
 #ifdef HAVE_BACKTRACE_SYMBOLS
 #      include <execinfo.h>
@@ -100,43 +99,6 @@ void StackTrace::Initialize(void)
 #endif /* _WIN32 */
 }
 
-/**
- * Looks up source file name and line number information for the specified
- * ELF executable and RVA.
- *
- * @param exe The ELF file.
- * @param rva The RVA.
- * @returns Source file and line number.
- */
-String StackTrace::Addr2Line(const String& exe, uintptr_t rva)
-{
-#ifndef _WIN32
-       std::ostringstream msgbuf;
-       msgbuf << "addr2line -s -e " << Application::GetExePath(exe) << " " << std::hex << rva << " 2>/dev/null";
-
-       String args = msgbuf.str();
-
-       FILE *fp = popen(args.CStr(), "r");
-
-       if (!fp)
-               return "RVA: " + Convert::ToString(rva);
-
-       char buffer[512] = {};
-       fgets(buffer, sizeof(buffer), fp);
-       fclose(fp);
-
-       String line = buffer;
-       boost::algorithm::trim_right(line);
-
-       if (line.GetLength() == 0)
-               return "RVA: " + Convert::ToString(rva);
-
-       return line;
-#else /* _WIN32 */
-       return String();
-#endif /* _WIN32 */
-}
-
 /**
  * Prints a stacktrace to the specified stream.
  *
@@ -176,15 +138,7 @@ void StackTrace::Print(std::ostream& fp, int ignoreFrames) const
                                        path = path.SubStr(slashp + 1);
 
                                message = path + ": " + sym_demangled + " (" + String(sym_end);
-
-#ifdef HAVE_DLADDR
-                               Dl_info dli;
-
-                               if (dladdr(m_Frames[i], &dli) > 0) {
-                                       uintptr_t rva = reinterpret_cast<uintptr_t>(m_Frames[i]) - reinterpret_cast<uintptr_t>(dli.dli_fbase);
-                                       message += " (" + Addr2Line(dli.dli_fname, rva) + ")";
-                               }
-#endif /* HAVE_DLADDR */
+                               message += " (" + Utility::GetSymbolSource(m_Frames[i]);
                        }
                }
 
index a262283832149737f8fc4017c78c54cac290d00f..cd6828ff8441d5d5acd70e682536a9ab347eee6f 100644 (file)
@@ -50,7 +50,6 @@ private:
        static boost::once_flag m_OnceFlag;
 
        static void Initialize(void);
-       static String Addr2Line(const String& exe, uintptr_t rva);
 };
 
 I2_BASE_API std::ostream& operator<<(std::ostream& stream, const StackTrace& trace);
index 44c5c1f4b7b4fb9d2f7a94e232649b1938205e51..189f77333bab8749a5fe3a27b168cf5d0d8c851c 100644 (file)
@@ -28,6 +28,7 @@
 #include <boost/foreach.hpp>
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/classification.hpp>
+#include <boost/algorithm/string/trim.hpp>
 
 #ifdef __FreeBSD__
 #      include <pthread_np.h>
@@ -81,6 +82,69 @@ String Utility::GetTypeName(const std::type_info& ti)
        return DemangleSymbolName(ti.name());
 }
 
+/**
+ * Looks up source file name and line number information for the specified
+ * ELF executable and RVA.
+ *
+ * @param exe The ELF file.
+ * @param rva The RVA.
+ * @returns Source file and line number.
+ */
+String Utility::Addr2Line(const String& exe, uintptr_t rva)
+{
+#ifndef _WIN32
+       std::ostringstream msgbuf;
+       msgbuf << "addr2line -s -e " << Application::GetExePath(exe) << " " << std::hex << rva << " 2>/dev/null";
+
+       String args = msgbuf.str();
+
+       FILE *fp = popen(args.CStr(), "r");
+
+       if (!fp)
+               return "RVA: " + Convert::ToString(rva);
+
+       char buffer[512] = {};
+       fgets(buffer, sizeof(buffer), fp);
+       fclose(fp);
+
+       String line = buffer;
+       boost::algorithm::trim_right(line);
+
+       if (line.GetLength() == 0)
+               return "RVA: " + Convert::ToString(rva);
+
+       return line;
+#else /* _WIN32 */
+       return String();
+#endif /* _WIN32 */
+}
+
+String Utility::GetSymbolName(const void *addr)
+{
+#ifdef HAVE_DLADDR
+       Dl_info dli;
+
+       if (dladdr(addr, &dli) > 0)
+               return dli.dli_sname;
+#endif /* HAVE_DLADDR */
+
+       return "";
+}
+
+String Utility::GetSymbolSource(const void *addr)
+{
+#ifdef HAVE_DLADDR
+       Dl_info dli;
+
+       if (dladdr(addr, &dli) > 0) {
+               uintptr_t rva = reinterpret_cast<uintptr_t>(addr) - reinterpret_cast<uintptr_t>(dli.dli_fbase);
+               return Addr2Line(dli.dli_fname, rva);
+       }
+#endif /* HAVE_DLADDR */
+
+       return "";
+}
+
 /**
  * Performs wildcard pattern matching.
  *
index 4a384acde5002997b8743d29aad5389290ad1824..dbea5a6f26b61d0408f21c9847ec9a02fcc6a6f2 100644 (file)
@@ -59,6 +59,9 @@ class I2_BASE_API Utility
 public:
        static String DemangleSymbolName(const String& sym);
        static String GetTypeName(const std::type_info& ti);
+       static String Addr2Line(const String& exe, uintptr_t rva);
+       static String GetSymbolName(const void *addr);
+       static String GetSymbolSource(const void *addr);
 
        static bool Match(const String& pattern, const String& text);