Skip to content

Commit

Permalink
[memprof] dump memprof profile when receive deadly signals
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Enna1 committed Oct 8, 2022
1 parent f0c93fd commit 9e80add
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions compiler-rt/lib/memprof/memprof_rtl.cpp
Expand Up @@ -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();
Expand Down Expand Up @@ -183,6 +191,7 @@ static void MemprofInitInternal() {
InitializeShadowMemory();

TSDInit(PlatformTSDDtor);
InstallDeadlySignalHandlers(MemprofOnDeadlySignal);

InitializeAllocator();

Expand Down
@@ -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

0 comments on commit 9e80add

Please sign in to comment.