[llvm] a7da7e4 - [Support] Allow printing the stack trace only for a given depth
Alexandre Ganea via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 06:27:52 PDT 2020
Author: Dibya Ranjan Mishra
Date: 2020-08-26T09:27:42-04:00
New Revision: a7da7e421c54e0053628f18f3750d4a8588cd627
URL: https://github.com/llvm/llvm-project/commit/a7da7e421c54e0053628f18f3750d4a8588cd627
DIFF: https://github.com/llvm/llvm-project/commit/a7da7e421c54e0053628f18f3750d4a8588cd627.diff
LOG: [Support] Allow printing the stack trace only for a given depth
Differential Revision: https://reviews.llvm.org/D85458
Added:
Modified:
llvm/include/llvm/Support/Signals.h
llvm/lib/Support/Unix/Signals.inc
llvm/lib/Support/Windows/Signals.inc
llvm/unittests/Support/CrashRecoveryTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Support/Signals.h b/llvm/include/llvm/Support/Signals.h
index e0a18e72f2a7..c5b94f5ac776 100644
--- a/llvm/include/llvm/Support/Signals.h
+++ b/llvm/include/llvm/Support/Signals.h
@@ -50,7 +50,9 @@ namespace sys {
void DisableSystemDialogsOnCrash();
/// Print the stack trace using the given \c raw_ostream object.
- void PrintStackTrace(raw_ostream &OS);
+ /// \param Depth refers to the number of stackframes to print. If not
+ /// specified, the entire frame is printed.
+ void PrintStackTrace(raw_ostream &OS, int Depth = 0);
// Run all registered signal handlers.
void RunSignalHandlers();
diff --git a/llvm/lib/Support/Unix/Signals.inc b/llvm/lib/Support/Unix/Signals.inc
index ce1fccf0b427..3587394a6ec5 100644
--- a/llvm/lib/Support/Unix/Signals.inc
+++ b/llvm/lib/Support/Unix/Signals.inc
@@ -553,7 +553,7 @@ static int unwindBacktrace(void **StackTrace, int MaxEntries) {
//
// 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,11 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS) {
#endif
if (!depth)
return;
-
- if (printSymbolizedStackTrace(Argv0, StackTrace, depth, OS))
+ // If "Depth" is not provided by the caller, use the return value of
+ // backtrace() for printing a 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 +617,7 @@ void llvm::sys::PrintStackTrace(raw_ostream &OS) {
OS << '\n';
}
#elif defined(HAVE_BACKTRACE)
- backtrace_symbols_fd(StackTrace, depth, STDERR_FILENO);
+ backtrace_symbols_fd(StackTrace, Depth, STDERR_FILENO);
#endif
#endif
}
diff --git a/llvm/lib/Support/Windows/Signals.inc b/llvm/lib/Support/Windows/Signals.inc
index 0c3681fa9654..71dc6324e99f 100644
--- a/llvm/lib/Support/Windows/Signals.inc
+++ b/llvm/lib/Support/Windows/Signals.inc
@@ -552,7 +552,8 @@ static void LocalPrintStackTrace(raw_ostream &OS, PCONTEXT C) {
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);
}
diff --git a/llvm/unittests/Support/CrashRecoveryTest.cpp b/llvm/unittests/Support/CrashRecoveryTest.cpp
index 6a62c7cab9d8..8af66461f005 100644
--- a/llvm/unittests/Support/CrashRecoveryTest.cpp
+++ b/llvm/unittests/Support/CrashRecoveryTest.cpp
@@ -6,10 +6,13 @@
//
//===----------------------------------------------------------------------===//
+#include "llvm/ADT/Triple.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/CrashRecoveryContext.h"
#include "llvm/Support/FileSystem.h"
+#include "llvm/Support/Host.h"
#include "llvm/Support/Signals.h"
+#include "llvm/Support/raw_ostream.h"
#include "gtest/gtest.h"
#ifdef _WIN32
@@ -90,6 +93,17 @@ TEST(CrashRecoveryTest, DumpStackCleanup) {
llvm::CrashRecoveryContext::Disable();
}
+TEST(CrashRecoveryTest, LimitedStackTrace) {
+ std::string Res;
+ llvm::raw_string_ostream RawStream(Res);
+ PrintStackTrace(RawStream, 1);
+ std::string Str = RawStream.str();
+ // FIXME: Handle "Depth" parameter in PrintStackTrace() function
+ // to print stack trace upto a specified Depth.
+ if (!Triple(sys::getProcessTriple()).isOSWindows())
+ EXPECT_EQ(std::string::npos, Str.find("#1"));
+}
+
#ifdef _WIN32
static void raiseIt() {
RaiseException(123, EXCEPTION_NONCONTINUABLE, 0, NULL);
More information about the llvm-commits
mailing list