[sheepdog] [PATCH 3/3] show backtrace when program exits unexpectedly

MORITA Kazutaka morita.kazutaka at gmail.com
Fri Feb 22 01:47:09 CET 2013


From: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>

Signed-off-by: MORITA Kazutaka <morita.kazutaka at lab.ntt.co.jp>
---
 collie/collie.c |    9 ++++-----
 include/util.h  |    1 +
 lib/logger.c    |   12 +++++-------
 lib/util.c      |    9 +++++++++
 sheep/sheep.c   |    9 +++++++++
 5 files changed, 28 insertions(+), 12 deletions(-)

diff --git a/collie/collie.c b/collie/collie.c
index 38d9e8e..4f5e974 100644
--- a/collie/collie.c
+++ b/collie/collie.c
@@ -316,10 +316,9 @@ static const struct sd_option *build_sd_options(const char *opts)
 
 static void crash_handler(int signo)
 {
-	if (signo == SIGABRT)
-		fprintf(stderr, "collie abort.\n");
-	else
-		fprintf(stderr, "collie got unexpected signal %d.\n", signo);
+	fprintf(stderr, "collie exits unexpectedly (%s).\n", strsignal(signo));
+
+	sd_backtrace();
 
 	exit(EXIT_SYSFAIL);
 }
@@ -334,7 +333,7 @@ int main(int argc, char **argv)
 	char *p;
 	const struct sd_option *sd_opts;
 
-	install_sighandler(SIGABRT, crash_handler, true);
+	install_crash_handler(crash_handler);
 
 	init_commands(&commands);
 
diff --git a/include/util.h b/include/util.h
index 5f37dc7..8588ddd 100644
--- a/include/util.h
+++ b/include/util.h
@@ -86,6 +86,7 @@ void pstrcpy(char *buf, int buf_size, const char *str);
 int rmdir_r(char *dir_path);
 bool is_numeric(const char *p);
 int install_sighandler(int signum, void (*handler)(int), bool once);
+int install_crash_handler(void (*handler)(int));
 
 void trim_zero_sectors(void *buf, uint64_t *offset, uint32_t *len);
 void untrim_zero_sectors(void *buf, uint64_t offset, uint32_t len,
diff --git a/lib/logger.c b/lib/logger.c
index ca9c391..dc72303 100644
--- a/lib/logger.c
+++ b/lib/logger.c
@@ -447,15 +447,13 @@ static notrace void log_flush(void)
 
 static notrace void crash_handler(int signo)
 {
-	if (signo == SIGSEGV) {
-		sd_printf(SDOG_ERR, "logger pid %d segfaulted.\n",
-			getpid());
-	} else if (signo == SIGHUP) {
+	if (signo == SIGHUP) {
 		sd_printf(SDOG_ERR, "sheep pid %d exited unexpectedly.\n",
 			sheep_pid);
 	} else {
-		sd_printf(SDOG_ERR, "logger pid %d got unexpected signal %d.\n",
-			getpid(), signo);
+		sd_printf(SDOG_ERR, "logger pid %d exits unexpectedly (%s).\n",
+			  getpid(), strsignal(signo));
+		sd_backtrace();
 	}
 
 	log_flush();
@@ -493,7 +491,7 @@ static notrace void logger(char *log_dir, char *outfile)
 	}
 
 	/* flush when either the logger or its parent dies */
-	install_sighandler(SIGSEGV, crash_handler, true);
+	install_crash_handler(crash_handler);
 	install_sighandler(SIGHUP, crash_handler, false);
 
 	prctl(PR_SET_PDEATHSIG, SIGHUP);
diff --git a/lib/util.c b/lib/util.c
index 0999f0e..b0d5616 100644
--- a/lib/util.c
+++ b/lib/util.c
@@ -366,3 +366,12 @@ int install_sighandler(int signum, void (*handler)(int), bool once)
 
 	return sigaction(signum, &sa, NULL);
 }
+
+int install_crash_handler(void (*handler)(int))
+{
+	return install_sighandler(SIGSEGV, handler, true) ||
+		install_sighandler(SIGABRT, handler, true) ||
+		install_sighandler(SIGBUS, handler, true) ||
+		install_sighandler(SIGILL, handler, true) ||
+		install_sighandler(SIGFPE, handler, true);
+}
diff --git a/sheep/sheep.c b/sheep/sheep.c
index 0d05d0b..7a76f6f 100644
--- a/sheep/sheep.c
+++ b/sheep/sheep.c
@@ -175,6 +175,14 @@ static int init_signal(void)
 	return 0;
 }
 
+static void crash_handler(int signo)
+{
+	sd_printf(SDOG_EMERG, "sheep exits unexpectedly (%s).\n",
+		  strsignal(signo));
+
+	sd_backtrace();
+}
+
 static struct cluster_info __sys;
 struct cluster_info *sys = &__sys;
 
@@ -435,6 +443,7 @@ int main(int argc, char **argv)
 	const char *log_format = "default";
 	static struct logger_user_info sheep_info;
 
+	install_crash_handler(crash_handler);
 	signal(SIGPIPE, SIG_IGN);
 
 	long_options = build_long_options(sheep_options);
-- 
1.7.9.5




More information about the sheepdog mailing list