Skip to content

Commit

Permalink
sighandler: Move signal code into sighandler files
Browse files Browse the repository at this point in the history
It's better to move signal handler registration logic from
libheaptrace.cc to sighandler.cc.

Signed-off-by: Honggyu Kim <[email protected]>
  • Loading branch information
honggyukim authored and Bojun-Seo committed Dec 3, 2022
1 parent 18bf46f commit 7174765
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 27 deletions.
23 changes: 2 additions & 21 deletions libheaptrace.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
Expand Down
32 changes: 29 additions & 3 deletions sighandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
4 changes: 1 addition & 3 deletions sighandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

0 comments on commit 7174765

Please sign in to comment.