[PATCH] D85458: Adding functionality to Stack Tracing

Dibya Ranjan Mishra via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 6 11:21:19 PDT 2020


dibya001 created this revision.
dibya001 added reviewers: john.brawn, gbreynoo, aganea, vsk, kazu, shankare, bcain.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
dibya001 requested review of this revision.

1- Added optional Depth parameter to PrintStackTrace() to allow the client code to print stacktrace upto

  a particular level. If not provided, then the default behaviour is same asprevious i.e to print all the trace
  information.
      

2 -Added a default constructor in PrettyStackTraceProgram class and provide the functionality to set up the member

  variables through setArgs() member function.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D85458

Files:
  llvm/include/llvm/Support/PrettyStackTrace.h
  llvm/include/llvm/Support/Signals.h
  llvm/lib/Support/Unix/Signals.inc


Index: llvm/lib/Support/Unix/Signals.inc
===================================================================
--- llvm/lib/Support/Unix/Signals.inc
+++ llvm/lib/Support/Unix/Signals.inc
@@ -553,7 +553,7 @@
 //
 // On glibc systems we have the 'backtrace' function, which works nicely, but
 // doesn't demangle symbols.
-void llvm::sys::PrintStackTrace(raw_ostream &OS) {
+void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
 #if ENABLE_BACKTRACES
   static void *StackTrace[256];
   int depth = 0;
@@ -570,8 +570,12 @@
 #endif
   if (!depth)
     return;
-
-  if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS))
+  // If "Depth" value is not provided by the caller, assign
+  // return value of backtrace() i.e. "depth" to "Depth"
+  // which will be then used for printing symbolized stack trace.
+  if (!Depth)
+    Depth = depth;
+  if (printSymbolizedStackTrace(Argv0, StackTrace, Depth, OS))
     return;
 #if HAVE_DLFCN_H && HAVE_DLADDR
   int width = 0;
@@ -614,7 +618,7 @@
     OS << '\n';
   }
 #elif defined(HAVE_BACKTRACE)
-  backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
+  backtrace_symbols_fd(StackTrace, Depth, STDERR_FILENO);
 #endif
 #endif
 }
Index: llvm/include/llvm/Support/Signals.h
===================================================================
--- llvm/include/llvm/Support/Signals.h
+++ llvm/include/llvm/Support/Signals.h
@@ -50,7 +50,9 @@
   void DisableSystemDialogsOnCrash();
 
   /// Print the stack trace using the given \c raw_ostream object.
-  void PrintStackTrace(raw_ostream &OS);
+  /// optional Depth parameter refers to the number of stackframes
+  /// to print. If not specified, entire frame is printed.
+  void PrintStackTrace(raw_ostream &OS, int Depth = 0);
 
   // Run all registered signal handlers.
   void RunSignalHandlers();
Index: llvm/include/llvm/Support/PrettyStackTrace.h
===================================================================
--- llvm/include/llvm/Support/PrettyStackTrace.h
+++ llvm/include/llvm/Support/PrettyStackTrace.h
@@ -92,6 +92,14 @@
     int ArgC;
     const char *const *ArgV;
   public:
+    PrettyStackTraceProgram() : ArgC(0), ArgV(nullptr) {
+      EnablePrettyStackTrace();
+    }
+
+    void setArgs(int argC, const char *const *argV) {
+      this->ArgV = argV;
+      this->ArgC = argC;
+    }
     PrettyStackTraceProgram(int argc, const char * const*argv)
       : ArgC(argc), ArgV(argv) {
       EnablePrettyStackTrace();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85458.283677.patch
Type: text/x-patch
Size: 2445 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200806/1a5e9731/attachment.bin>


More information about the llvm-commits mailing list