From 71747656586aa85f65ab003b4d8edb62af8ed7b7 Mon Sep 17 00:00:00 2001 From: Honggyu Kim Date: Tue, 11 Feb 2020 16:47:41 +0900 Subject: [PATCH] sighandler: Move signal code into sighandler files It's better to move signal handler registration logic from libheaptrace.cc to sighandler.cc. Signed-off-by: Honggyu Kim --- libheaptrace.cc | 23 ++--------------------- sighandler.cc | 32 +++++++++++++++++++++++++++++--- sighandler.h | 4 +--- 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/libheaptrace.cc b/libheaptrace.cc index 0aa0d0f..91d680f 100644 --- a/libheaptrace.cc +++ b/libheaptrace.cc @@ -57,7 +57,6 @@ __constructor static void heaptrace_init() { auto* tfs = &thread_flags; - struct sigaction sigusr1, sigusr2, sigquit; int pid = getpid(); std::stringstream ss; std::string comm = utils::get_comm_name(); @@ -69,26 +68,8 @@ static void heaptrace_init() real_memalign = (MemalignFunction)dlsym(RTLD_NEXT, "memalign"); real_posix_memalign = (PosixMemalignFunction)dlsym(RTLD_NEXT, "posix_memalign"); - sigusr1.sa_handler = sigusr1_handler; - sigemptyset(&sigusr1.sa_mask); - sigusr1.sa_flags = 0; - - sigusr2.sa_handler = sigusr2_handler; - sigemptyset(&sigusr2.sa_mask); - sigusr2.sa_flags = 0; - - sigquit.sa_handler = sigquit_handler; - sigemptyset(&sigquit.sa_mask); - sigquit.sa_flags = 0; - - if (sigaction(SIGUSR1, &sigusr1, 0) == -1) - pr_dbg("signal(SIGUSR1) error"); - - if (sigaction(SIGUSR2, &sigusr2, 0) == -1) - pr_dbg("signal(SIGUSR2) error"); - - if (sigaction(SIGQUIT, &sigquit, 0) == -1) - pr_dbg("signal(SIGQUIT) error"); + // initialize signal handlers + sighandler_init(); // setup option values opts.top = strtol(getenv("HEAPTRACE_NUM_TOP_BACKTRACE"), NULL, 0); diff --git a/sighandler.cc b/sighandler.cc index 33e7b98..2ffb30c 100644 --- a/sighandler.cc +++ b/sighandler.cc @@ -5,20 +5,46 @@ #include "heaptrace.h" #include "stacktrace.h" -void sigusr1_handler(int signo) +static void sigusr1_handler(int signo) { pr_dbg("\n=== sigusr1_handler(%d) ===\n", signo); dump_stackmap(ALLOC_SIZE, opts.flamegraph); } -void sigusr2_handler(int signo) +static void sigusr2_handler(int signo) { pr_dbg("\n=== sigusr2_handler(%d) ===\n", signo); dump_stackmap(ALLOC_COUNT, opts.flamegraph); } -void sigquit_handler(int signo) +static void sigquit_handler(int signo) { pr_dbg("\n=== sigquit_handler(%d) ===\n", signo); clear_stackmap(); } + +void sighandler_init(void) +{ + struct sigaction sigusr1, sigusr2, sigquit; + + sigusr1.sa_handler = sigusr1_handler; + sigemptyset(&sigusr1.sa_mask); + sigusr1.sa_flags = 0; + + sigusr2.sa_handler = sigusr2_handler; + sigemptyset(&sigusr2.sa_mask); + sigusr2.sa_flags = 0; + + sigquit.sa_handler = sigquit_handler; + sigemptyset(&sigquit.sa_mask); + sigquit.sa_flags = 0; + + if (sigaction(SIGUSR1, &sigusr1, 0) == -1) + pr_dbg("signal(SIGUSR1) error"); + + if (sigaction(SIGUSR2, &sigusr2, 0) == -1) + pr_dbg("signal(SIGUSR2) error"); + + if (sigaction(SIGQUIT, &sigquit, 0) == -1) + pr_dbg("signal(SIGQUIT) error"); +} diff --git a/sighandler.h b/sighandler.h index c2dc2d1..5875fdf 100644 --- a/sighandler.h +++ b/sighandler.h @@ -3,8 +3,6 @@ #ifndef HEAPTOP_SIGHANDLER_H #define HEAPTOP_SIGHANDLER_H -void sigusr1_handler(int signo); -void sigusr2_handler(int signo); -void sigquit_handler(int signo); +void sighandler_init(void); #endif /* HEAPTOP_SIGHANDLER_H */