bombela / backward-cpp

A beautiful stack trace pretty printer for C++
MIT License
3.66k stars 466 forks source link

fixing this library #329

Open gitbruv opened 5 months ago

gitbruv commented 5 months ago
diff --git a/backward.hpp b/backward.hpp
index 670aa45..1f01e9f 100644
--- a/backward.hpp
+++ b/backward.hpp
@@ -81,12 +81,15 @@
 #include <cstdio>
 #include <cstdlib>
 #include <cstring>
+#include <filesystem>
 #include <fstream>
+#include <functional>
 #include <iomanip>
 #include <iostream>
 #include <limits>
 #include <new>
 #include <sstream>
+#include <stdexcept>
 #include <streambuf>
 #include <string>
 #include <vector>
@@ -741,24 +744,7 @@ public:

 protected:
   void load_thread_info() {
-#ifdef BACKWARD_SYSTEM_LINUX
-#ifndef __ANDROID__
-    _thread_id = static_cast<size_t>(syscall(SYS_gettid));
-#else
-    _thread_id = static_cast<size_t>(gettid());
-#endif
-    if (_thread_id == static_cast<size_t>(getpid())) {
-      // If the thread is the main one, let's hide that.
-      // I like to keep little secret sometimes.
-      _thread_id = 0;
-    }
-#elif defined(BACKWARD_SYSTEM_DARWIN)
-    _thread_id = reinterpret_cast<size_t>(pthread_self());
-    if (pthread_main_np() == 1) {
-      // If the thread is the main one, let's hide that.
-      _thread_id = 0;
-    }
-#endif
+    _thread_id = std::hash<std::thread::id>{}(std::this_thread::get_id());
   }

   void set_context(void *context) { _context = context; }
@@ -878,7 +864,6 @@ public:
     _stacktrace.resize(depth);
     size_t trace_cnt = details::unwind(callback(*this), depth);
     _stacktrace.resize(trace_cnt);
-    skip_n_firsts(0);
     return size();
   }
   size_t load_from(void *addr, size_t depth = 32, void *context = nullptr,
@@ -4076,11 +4061,7 @@ private:
   }

   void print_header(std::ostream &os, size_t thread_id) {
-    os << "Stack trace (most recent call last)";
-    if (thread_id) {
-      os << " in thread " << thread_id;
-    }
-    os << ":\n";
+    os << "Stack trace " << "(thread " << thread_id << ")" << ":\n";
   }

   void print_trace(std::ostream &os, const ResolvedTrace &trace,
@@ -4089,11 +4070,14 @@ private:
     bool already_indented = true;

     if (!trace.source.filename.size() || object) {
-      os << "   Object \"" << trace.object_filename << "\", at " << trace.addr
+      os << "   Object " << trace.object_filename << ", at " << trace.addr
          << ", in " << trace.object_function << "\n";
       already_indented = false;
     }

+    auto file = trace.source.filename;
+    bool skipsnippet = file.ends_with("invoke.h") ||  file.ends_with("std_function.h") ||  file.ends_with("std_thread.h");
+
     for (size_t inliner_idx = trace.inliners.size(); inliner_idx > 0;
          --inliner_idx) {
       if (!already_indented) {
@@ -4102,7 +4086,7 @@ private:
       const ResolvedTrace::SourceLoc &inliner_loc =
           trace.inliners[inliner_idx - 1];
       print_source_loc(os, " | ", inliner_loc);
-      if (snippet) {
+      if (snippet && !skipsnippet) {
         print_snippet(os, "    | ", inliner_loc, colorize, Color::purple,
                       inliner_context_size);
       }
@@ -4114,7 +4098,7 @@ private:
         os << "   ";
       }
       print_source_loc(os, "   ", trace.source, trace.addr);
-      if (snippet) {
+      if (snippet && !skipsnippet) {
         print_snippet(os, "      ", trace.source, colorize, Color::yellow,
                       trace_context_size);
       }
@@ -4148,8 +4132,9 @@ private:
   void print_source_loc(std::ostream &os, const char *indent,
                         const ResolvedTrace::SourceLoc &source_loc,
                         void *addr = nullptr) {
-    os << indent << "Source \"" << source_loc.filename << "\", line "
-       << source_loc.line << ", in " << source_loc.function;
+    auto filename = std::filesystem::weakly_canonical(source_loc.filename).string();
+    os << indent << "Source " << filename << ", line " << source_loc.line
+      << ", in " << source_loc.function;

     if (address && addr != nullptr) {
       os << " [" << addr << "]";
@@ -4158,6 +4143,15 @@ private:
   }
 };

+class traced_error : public std::runtime_error {
+public:
+  backward::StackTrace trace;
+  traced_error(const std::string &msg) : std::runtime_error(msg) {
+    trace.skip_n_firsts(3);
+    trace.load_here();
+  }
+};
+
 /*************** SIGNALS HANDLING ***************/

 #if defined(BACKWARD_SYSTEM_LINUX) || defined(BACKWARD_SYSTEM_DARWIN)
@@ -4294,13 +4288,7 @@ private:
   static void
   sig_handler(int signo, siginfo_t *info, void *_ctx) {
     handleSignal(signo, info, _ctx);
-
-    // try to forward the signal.
-    raise(info->si_signo);
-
-    // terminate the process immediately.
-    puts("watf? exit");
-    _exit(EXIT_FAILURE);
+    exit(EXIT_FAILURE);
   }
 };
lahwaacz commented 5 months ago

What is the issue? Why did you post just a diff rather than a pull request?

bombela commented 5 months ago

Looks like some sort of migration to the C++20 filesystem lib? We cannot really do that unconditionally anyways.

On Sat, Feb 3, 2024, 08:55 Jakub Klinkovský @.***> wrote:

What is the issue? Why did you post just a diff rather than a pull request?

— Reply to this email directly, view it on GitHub https://github.com/bombela/backward-cpp/issues/329#issuecomment-1925203923, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABUZDHUC2W5RHN2TVMYPIDYRXUNNAVCNFSM6AAAAABCVJHIU2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMRVGIYDGOJSGM . You are receiving this because you are subscribed to this thread.Message ID: @.***>