[PATCH] D85458: Adding functionality to Stack Tracing

Dibya Ranjan Mishra via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 11 06:22:35 PDT 2020


dibya001 updated this revision to Diff 284685.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D85458/new/

https://reviews.llvm.org/D85458

Files:
  llvm/include/llvm/Support/Signals.h
  llvm/lib/Support/Unix/Signals.inc
  llvm/lib/Support/Windows/Signals.inc
  llvm/unittests/Support/CrashRecoveryTest.cpp


Index: llvm/unittests/Support/CrashRecoveryTest.cpp
===================================================================
--- llvm/unittests/Support/CrashRecoveryTest.cpp
+++ llvm/unittests/Support/CrashRecoveryTest.cpp
@@ -10,6 +10,7 @@
 #include "llvm/Support/CrashRecoveryContext.h"
 #include "llvm/Support/FileSystem.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/raw_ostream.h"
 #include "gtest/gtest.h"
 
 #ifdef _WIN32
@@ -90,6 +91,16 @@
   llvm::CrashRecoveryContext::Disable();
 }
 
+#if !defined(_WIN32)
+TEST(CrashRecoveryTest, LimitedStackTrace) {
+  std::string res;
+  llvm::raw_string_ostream RawStream(res);
+  PrintStackTrace(RawStream, 1);
+  std::string str = RawStream.str();
+  EXPECT_EQ(std::string::npos, str.find("#1"));
+}
+#endif
+
 #ifdef _WIN32
 static void raiseIt() {
   RaiseException(123, EXCEPTION_NONCONTINUABLE, 0, NULL);
Index: llvm/lib/Support/Windows/Signals.inc
===================================================================
--- llvm/lib/Support/Windows/Signals.inc
+++ llvm/lib/Support/Windows/Signals.inc
@@ -552,7 +552,8 @@
                            StackFrame, C);
 }
 
-void llvm::sys::PrintStackTrace(raw_ostream &OS) {
+void llvm::sys::PrintStackTrace(raw_ostream &OS, int Depth) {
+  // FIXME: Handle "Depth" parameter to print stack trace upto specified Depth
   LocalPrintStackTrace(OS, nullptr);
 }
 
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();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D85458.284685.patch
Type: text/x-patch
Size: 3178 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200811/806cf48f/attachment.bin>


More information about the llvm-commits mailing list