From: Adam Dickmeiss Date: Mon, 17 Nov 2014 14:17:42 +0000 (+0100) Subject: Dump backtrace in case of SIGABRT, SIGSEGV YAZ-787 X-Git-Tag: v5.7.0~2 X-Git-Url: http://git.indexdata.com/?p=yaz-moved-to-github.git;a=commitdiff_plain;h=51e1ca7946289042c3c09e92afc1c1c465f5d870;ds=sidebyside Dump backtrace in case of SIGABRT, SIGSEGV YAZ-787 New function yaz_enable_panic_backtrace which enables backtrace handler for current process. The handler will call backtrace always, then gdb to provide as much detail as possible. --- diff --git a/configure.ac b/configure.ac index 7ceddfe..e1623bb 100644 --- a/configure.ac +++ b/configure.ac @@ -26,7 +26,7 @@ dnl YAZ_DOC dnl dnl -AC_CHECK_HEADERS([dirent.h fnmatch.h wchar.h locale.h langinfo.h pwd.h unistd.h sys/select.h sys/socket.h sys/stat.h sys/time.h sys/times.h sys/types.h sys/un.h sys/wait.h sys/prctl.h netdb.h arpa/inet.h netinet/tcp.h netinet/in_systm.h],[],[],[]) +AC_CHECK_HEADERS([dirent.h fnmatch.h wchar.h locale.h langinfo.h pwd.h unistd.h sys/select.h sys/socket.h sys/stat.h sys/time.h sys/times.h sys/types.h sys/un.h sys/wait.h sys/prctl.h netdb.h arpa/inet.h netinet/tcp.h netinet/in_systm.h execinfo.h],[],[],[]) AC_CHECK_HEADERS([net/if.h netinet/in.h netinet/if_ether.h],[],[],[ #if HAVE_SYS_TYPES_H #include diff --git a/include/yaz/Makefile.am b/include/yaz/Makefile.am index 0c5ab8a..21d55dc 100644 --- a/include/yaz/Makefile.am +++ b/include/yaz/Makefile.am @@ -3,7 +3,7 @@ noinst_HEADERS = icu_I18N.h -pkginclude_HEADERS= backend.h base64.h \ +pkginclude_HEADERS= backend.h backtrace.h base64.h \ ccl.h ccl_xml.h cookie.h cql.h rpn2cql.h rpn2solr.h \ solr.h comstack.h \ diagbib1.h diagsrw.h diagsru_update.h sortspec.h log.h logrpn.h marcdisp.h \ diff --git a/include/yaz/backtrace.h b/include/yaz/backtrace.h new file mode 100644 index 0000000..8469c3f --- /dev/null +++ b/include/yaz/backtrace.h @@ -0,0 +1,57 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data. + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Index Data nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/** + * \file + * \brief backtrace handling + */ +#ifndef YAZ_BACKTRACE_H +#define YAZ_BACKTRACE_H + +#include +#include + +YAZ_BEGIN_CDECL + +/** \brief enables backtrace when SIGSEGV/SIGABRT/.. signal is received + \param progname name of executable that we run +*/ + +YAZ_EXPORT void yaz_enable_panic_backtrace(const char *progname); + +YAZ_END_CDECL + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/Makefile.am b/src/Makefile.am index 3f431f0..de50bca 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -111,7 +111,7 @@ libyaz_la_SOURCES= $(GEN_FILES) \ iconv_encode_marc8.c iconv_encode_iso_8859_1.c iconv_encode_wchar.c \ iconv_decode_marc8.c iconv_decode_iso5426.c iconv_decode_danmarc.c sc.c \ json.c xml_include.c file_glob.c dirent.c mutex-p.h mutex.c condvar.c \ - thread_id.c gettimeofday.c thread_create.c spipe.c url.c + thread_id.c gettimeofday.c thread_create.c spipe.c url.c backtrace.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) diff --git a/src/backtrace.c b/src/backtrace.c new file mode 100644 index 0000000..567e55f --- /dev/null +++ b/src/backtrace.c @@ -0,0 +1,180 @@ +/* This file is part of the YAZ toolkit. + * Copyright (C) Index Data + * See the file LICENSE for details. + */ + +/** + * \file + * \brief get information for abnormal terminated, crashes, etc + */ + + +#if HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#if HAVE_UNISTD_H +#include +#endif +#include +#if HAVE_SYS_WAIT_H +#include +#endif +#include +#include +#include +#include + +#if HAVE_SYS_TYPES_H +#include +#endif + +#if HAVE_EXECINFO_H +#include +#endif + +#define BACKTRACE_SZ 100 + +static char static_progname[256]; + +static void yaz_invoke_backtrace(char *buf, int buf_sz) +{ + FILE *file = yaz_log_file(); + int fd = fileno(file); +#if HAVE_EXECINFO_H + pid_t pid; + int fds[2]; + void *backtrace_info[BACKTRACE_SZ]; + int sz = BACKTRACE_SZ; + + write(fd, buf, strlen(buf)); + sz = backtrace(backtrace_info, sz); + backtrace_symbols_fd(backtrace_info, sz, fd); + + pipe(fds); + pid = fork(); + if (pid == (pid_t) (-1)) + { /* error */ + const char *cp = "backtrace: fork failure"; + write(fd, cp, strlen(cp)); + } + else if (pid == 0) + { /* child */ + char *arg[20]; + int arg_no = 0; + char pidstr[40]; + const char *cp = "backtrace: could not exec gdb"; + + close(fds[1]); + close(0); + dup(fds[0]); + if (fd != 1) + { + close(1); + dup(fd); + } + if (fd != 2) + { + close(2); + dup(fd); + } + arg[arg_no++] = "/usr/bin/gdb"; + arg[arg_no++] = "-n"; + arg[arg_no++] = "-batch"; + arg[arg_no++] = "-ex"; + arg[arg_no++] = "info threads"; + arg[arg_no++] = "-ex"; + arg[arg_no++] = "thread apply all bt"; + arg[arg_no++] = static_progname; + sprintf(pidstr, NMEM_INT_PRINTF, (nmem_int_t) getppid()); + arg[arg_no++] = pidstr; + arg[arg_no] = 0; + execv(arg[0], arg); + write(2, cp, strlen(cp)); /* exec failure if we make it this far */ + _exit(1); + } + else + { /* parent */ + int sec = 0; + + close(fds[0]); + write(fds[1], "quit\n", 5); + while (1) + { + int status; + pid_t s = waitpid(pid, &status, WNOHANG); + if (s != 0) + break; + if (sec == 9) + kill(pid, SIGTERM); + if (sec == 10) + kill(pid, SIGKILL); + if (sec == 11) + break; + if (sec > 3) + write(fds[1], "quit\n", 5); + sleep(1); + sec++; + } + close(fds[1]); + } +#else + strcat(buf, "no backtrace support (execinfo.h not found)\n"); + write(fd, buf, strlen(buf)); +#endif +} + +static void yaz_panic_sig_handler(int sig) +{ + char buf[512]; + + signal(SIGABRT, SIG_DFL); + strcpy(buf, "\nYAZ panic received "); + switch (sig) + { + case SIGSEGV: + strcat(buf, "SIGSEGV"); + break; + case SIGABRT: + strcat(buf, "SIGABRT"); + break; + case SIGFPE: + strcat(buf, "SIGFPE"); + break; + case SIGBUS: + strcat(buf, "SIGBUS"); + break; + default: + yaz_snprintf(buf + strlen(buf), sizeof buf, "signo=%d", sig); + break; + } + yaz_snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf) - 1, + " PID=" NMEM_INT_PRINTF "\n", (nmem_int_t) getpid()); + yaz_invoke_backtrace(buf, sizeof buf); + abort(); +} + +void yaz_enable_panic_backtrace(const char *progname) +{ + strncpy(static_progname, progname, sizeof(static_progname) - 1); + static_progname[sizeof(static_progname) - 1] = '\0'; +#if HAVE_EXECINFO_H + signal(SIGABRT, yaz_panic_sig_handler); + signal(SIGSEGV, yaz_panic_sig_handler); + signal(SIGFPE, yaz_panic_sig_handler); + signal(SIGBUS, yaz_panic_sig_handler); +#endif +} + +/* + * Local variables: + * c-basic-offset: 4 + * c-file-style: "Stroustrup" + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/win/makefile b/win/makefile index 9d9395d..643a06a 100644 --- a/win/makefile +++ b/win/makefile @@ -555,7 +555,8 @@ MISC_OBJS= \ $(OBJDIR)\file_glob.obj \ $(OBJDIR)\thread_id.obj \ $(OBJDIR)\dirent.obj \ - $(OBJDIR)\url.obj + $(OBJDIR)\url.obj \ + $(OBJDIR)\backtrace.obj Z3950_OBJS= \ $(OBJDIR)\z-date.obj\ diff --git a/ztest/ztest.c b/ztest/ztest.c index c938c71..cc7736d 100644 --- a/ztest/ztest.c +++ b/ztest/ztest.c @@ -33,6 +33,7 @@ #include #include #include +#include #include "ztest.h" @@ -1158,6 +1159,8 @@ void bend_close(void *handle) int main(int argc, char **argv) { + yaz_enable_panic_backtrace(argv[0]); + return statserv_main(argc, argv, bend_init, bend_close); } /*