[PATCH] D143607: [Support][WIP] Add unit test for symbolized stack traces

Luís Marques via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 8 14:24:01 PST 2023


luismarques created this revision.
luismarques added a reviewer: dblaikie.
Herald added a subscriber: krytarowski.
Herald added a project: All.
luismarques requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Stack trace symbolization is missing unit tests. This patch adds such a test.

The preprocessor condition gating the new test is the same as in LLVM's `Signal.inc` implementation. Some of the platforms that match that condition actually have broken stack traces and/or broken symbolization. The WIP is to investigate on what platforms symbolized traces actually work and to update the gating condition.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D143607

Files:
  llvm/unittests/Support/CrashRecoveryTest.cpp


Index: llvm/unittests/Support/CrashRecoveryTest.cpp
===================================================================
--- llvm/unittests/Support/CrashRecoveryTest.cpp
+++ llvm/unittests/Support/CrashRecoveryTest.cpp
@@ -17,6 +17,7 @@
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TargetParser/Triple.h"
 #include "gtest/gtest.h"
+#include <charconv>
 
 #ifdef _WIN32
 #define WIN32_LEAN_AND_MEAN
@@ -114,6 +115,31 @@
   EXPECT_EQ(std::string::npos, Str.find("#1"));
 }
 
+#if defined(HAVE_BACKTRACE) && ENABLE_BACKTRACES && HAVE_LINK_H &&             \
+    (defined(__linux__) || defined(__FreeBSD__) ||                             \
+     defined(__FreeBSD_kernel__) || defined(__NetBSD__))
+TEST(CrashRecoveryTest, SymbolizedStackTrace) {
+  std::string Res;
+  llvm::raw_string_ostream RawStream(Res);
+  PrintStackTrace(RawStream);
+  int ExpectedLine = __LINE__ - 1; // The line that calls PrintStackTrace.
+  std::string Str = RawStream.str();
+  // Symbolized trace lines have "file:line:column", others "(file+offset)".
+  std::string ExpectedFile = __FILE__ ":"; // Match this file and the colon.
+  size_t ActualLineStartPos = Str.find(ExpectedFile) + ExpectedFile.length();
+  ASSERT_NE(std::string::npos, ActualLineStartPos);
+  int ActualLine;
+  auto LineResult = std::from_chars(Str.data() + ActualLineStartPos,
+                                    Str.data() + Str.size(), ActualLine);
+  ASSERT_EQ(LineResult.ec, std::errc());
+  // Check that the stack trace contains the correct line for this source file.
+  // At the time this test was added x86_64 Linux was printing the line after
+  // the PrintStackTrace call while AArch64 Linux was printing the expected line
+  // so for now we do a fuzzy match.
+  EXPECT_NEAR(ExpectedLine, ActualLine, 1);
+}
+#endif
+
 #ifdef _WIN32
 static void raiseIt() {
   RaiseException(123, EXCEPTION_NONCONTINUABLE, 0, NULL);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D143607.495947.patch
Type: text/x-patch
Size: 1893 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230208/1f8d5bc6/attachment.bin>


More information about the llvm-commits mailing list