[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
This commit is contained in:
Enna1 2022-10-08 09:55:40 +08:00 committed by xumingjie.enna1
parent f0c93fd4ca
commit 9e80add2cf
2 changed files with 27 additions and 0 deletions

View File

@ -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();

View File

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