[compiler-rt] 9e80add - [memprof] dump memprof profile when receive deadly signals

via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 7 19:18:20 PDT 2022


Author: Enna1
Date: 2022-10-08T10:08:23+08:00
New Revision: 9e80add2cfa9bb03bbe77cca9adec18137204538

URL: https://github.com/llvm/llvm-project/commit/9e80add2cfa9bb03bbe77cca9adec18137204538
DIFF: https://github.com/llvm/llvm-project/commit/9e80add2cfa9bb03bbe77cca9adec18137204538.diff

LOG: [memprof] dump memprof profile when receive deadly signals

Currently memprof profile is dumped when program exits (call `FinishAndWrite()` in ~Allocator) or `__memprof_profile_dump` is manually called.
For programs that never exit (e.g. server-side application), it will be useful to dump memprof profile when specific signal is received.
This patch installs a signal handler for deadly signals(SIGSEGV, SIGBUS, SIGABRT, SIGILL, SIGTRAP, SIGFPE) like we do in other sanitizers. In the signal handler `__memprof_profile_dump` is called to dump memprof profile.

Reviewed By: tejohnson

Differential Revision: https://reviews.llvm.org/D134795

Added: 
    compiler-rt/test/memprof/TestCases/memprof_profile_dump_on_abort.cpp

Modified: 
    compiler-rt/lib/memprof/memprof_rtl.cpp

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/memprof/memprof_rtl.cpp b/compiler-rt/lib/memprof/memprof_rtl.cpp
index d568a075c3e18..d30b80304f6bc 100644
--- a/compiler-rt/lib/memprof/memprof_rtl.cpp
+++ b/compiler-rt/lib/memprof/memprof_rtl.cpp
@@ -50,6 +50,14 @@ static void MemprofDie() {
   }
 }
 
+static void MemprofOnDeadlySignal(int signo, void *siginfo, void *context) {
+  // We call StartReportDeadlySignal not HandleDeadlySignal so we get the
+  // deadly signal message to stderr but no writing to the profile output file
+  StartReportDeadlySignal();
+  __memprof_profile_dump();
+  Die();
+}
+
 static void CheckUnwind() {
   GET_STACK_TRACE(kStackTraceMax, common_flags()->fast_unwind_on_check);
   stack.Print();
@@ -183,6 +191,7 @@ static void MemprofInitInternal() {
   InitializeShadowMemory();
 
   TSDInit(PlatformTSDDtor);
+  InstallDeadlySignalHandlers(MemprofOnDeadlySignal);
 
   InitializeAllocator();
 

diff  --git a/compiler-rt/test/memprof/TestCases/memprof_profile_dump_on_abort.cpp b/compiler-rt/test/memprof/TestCases/memprof_profile_dump_on_abort.cpp
new file mode 100644
index 0000000000000..5df611b7e606a
--- /dev/null
+++ b/compiler-rt/test/memprof/TestCases/memprof_profile_dump_on_abort.cpp
@@ -0,0 +1,18 @@
+// RUN: %clangxx_memprof %s -o %t
+
+// RUN: %env_memprof_opts=print_text=true:log_path=stdout:handle_abort=1 not %run %t 2>&1 | FileCheck --check-prefix=CHECK-TEXT %s
+
+#include <stdlib.h>
+#include <string.h>
+int main(int argc, char **argv) {
+  char *x = (char *)malloc(10);
+  memset(x, 0, 10);
+  free(x);
+  abort();
+  return 0;
+}
+
+// CHECK-TEXT: MemProfiler:DEADLYSIGNAL
+// CHECK-TEXT: Recorded MIBs (incl. live on exit):
+// CHECK-TEXT: Memory allocation stack id
+// CHECK-TEXT: Stack for id


        


More information about the llvm-commits mailing list