diff options
author | Sebastien Helleu <flashcode@flashtux.org> | 2006-05-19 22:49:24 +0000 |
---|---|---|
committer | Sebastien Helleu <flashcode@flashtux.org> | 2006-05-19 22:49:24 +0000 |
commit | 15f75d157c6ec6118f7800a433c13e2347632b92 (patch) | |
tree | 1740a4ff8851ed72397b0aaeff446e1b2f484305 /src/common | |
parent | 7e802070ca0cc4f06ee1cbec590ba87261d0f3e0 (diff) | |
download | weechat-15f75d157c6ec6118f7800a433c13e2347632b92.zip |
Added backtrace when WeeChat crashes
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/Makefile.am | 2 | ||||
-rw-r--r-- | src/common/backtrace.c | 136 | ||||
-rw-r--r-- | src/common/backtrace.h | 28 | ||||
-rw-r--r-- | src/common/weechat.c | 25 |
4 files changed, 186 insertions, 5 deletions
diff --git a/src/common/Makefile.am b/src/common/Makefile.am index 1e84078ec..78d82d531 100644 --- a/src/common/Makefile.am +++ b/src/common/Makefile.am @@ -21,6 +21,8 @@ noinst_LIBRARIES = lib_weechat_main.a lib_weechat_main_a_SOURCES = weechat.c \ weechat.h \ + backtrace.c \ + backtrace.h \ command.c \ command.h \ completion.c \ diff --git a/src/common/backtrace.c b/src/common/backtrace.c new file mode 100644 index 000000000..9c67c2f40 --- /dev/null +++ b/src/common/backtrace.c @@ -0,0 +1,136 @@ +/* + * Copyright (c) 2003-2006 by FlashCode <flashcode@flashtux.org> + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* backtrace.c: display backtrace after a segfault */ + + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#define __USE_GNU +#include <dlfcn.h> + +#ifdef HAVE_BACKTRACE +#include <execinfo.h> +#endif + +#include "weechat.h" +#include "backtrace.h" + + +/* + * weechat_backtrace_addr2line: display function name and line with a backtrace address + */ + +void +weechat_backtrace_addr2line (int number, void *address, char *symbol) +{ + int rc; + Dl_info info; + const void *addr; + FILE *output; + char cmd_line[1024]; + char line[1024], *ptr_line, *pos; + char function_name[1024]; + int file_line; + + rc = dladdr (address, &info); + if ((rc == 0) || !info.dli_fname || !info.dli_fname[0]) + { + fprintf (stderr, "%03d %s\n", number, symbol); + return; + } + + addr = address; + if (info.dli_fbase >= (const void *) 0x40000000) + addr = (const char *) addr - (unsigned int) info.dli_fbase; + + snprintf (cmd_line, sizeof (cmd_line), + "addr2line --functions --demangle -e %s %p", + info.dli_fname, addr); + output = popen (cmd_line, "r"); + if (!output) + { + fprintf (stderr, "%03d %s\n", number, symbol); + return; + } + function_name[0] = '\0'; + file_line = 0; + while (!feof (output)) + { + ptr_line = fgets (line, sizeof (line) - 1, output); + if (ptr_line && ptr_line[0]) + { + pos = strchr (ptr_line, '\n'); + if (pos) + pos[0] = '\0'; + if (strchr (ptr_line, ':')) + { + file_line = 1; + fprintf (stderr, "%03d %s%s%s%s\n", + number, + ptr_line, + (function_name[0]) ? " [function " : "", + function_name, + (function_name[0]) ? "]" : ""); + function_name[0] = '\0'; + } + else + { + if (function_name[0]) + fprintf (stderr, "%03d %s", number, function_name); + snprintf (function_name, sizeof (function_name), + "%s", ptr_line); + } + } + } + if (function_name[0]) + fprintf (stderr, "%03d %s\n", number, function_name); + pclose (output); +} + +/* + * weechat_backtrace: display backtrace (called when a SIGSEGV is received) + */ + +void +weechat_backtrace () +{ +#ifdef HAVE_BACKTRACE + void *trace[BACKTRACE_MAX]; + int trace_size, i; + char **symbols; + + trace_size = backtrace (trace, BACKTRACE_MAX); + symbols = backtrace_symbols (trace, trace_size); + + for (i = 0; i < trace_size; i++) + { + weechat_backtrace_addr2line (i + 1, trace[i], symbols[i]); + } +#else + fprintf (stderr, + " No backtrace info (no debug info available or no backtrace possible " + "on your system).\n"); +#endif +} diff --git a/src/common/backtrace.h b/src/common/backtrace.h new file mode 100644 index 000000000..67e3751dc --- /dev/null +++ b/src/common/backtrace.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2003-2006 by FlashCode <flashcode@flashtux.org> + * See README for License detail, AUTHORS for developers list. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + + +#ifndef __WEECHAT_BACKTACE_H +#define __WEECHAT_BACKTACE_H 1 + +#define BACKTRACE_MAX 128 + +extern void weechat_backtrace (); + +#endif /* backtrace.h */ diff --git a/src/common/weechat.c b/src/common/weechat.c index 8a69facdb..f82a622e7 100644 --- a/src/common/weechat.c +++ b/src/common/weechat.c @@ -62,6 +62,7 @@ #endif #include "weechat.h" +#include "backtrace.h" #include "weeconfig.h" #include "command.h" #include "fifo.h" @@ -1075,12 +1076,26 @@ weechat_sigsegv () dcc_end (); server_free_all (); gui_main_end (); + fprintf (stderr, "\n"); - fprintf (stderr, "*** Very bad! WeeChat has crashed (SIGSEGV received)\n"); - fprintf (stderr, "*** Full crash dump was saved to %s/weechat.log file\n", weechat_home); - fprintf (stderr, "*** Please send this file to WeeChat developers.\n"); - fprintf (stderr, "*** (be careful, private info may be in this file since\n"); - fprintf (stderr, "*** part of chats are displayed, so remove lines if needed)\n\n"); + fprintf (stderr, "*** Very bad! WeeChat is crashing (SIGSEGV received)\n"); + fprintf (stderr, "*** (%s, compiled on %s %s)\n", + PACKAGE_STRING, __DATE__, __TIME__); + fprintf (stderr, "*** Full crash dump was saved to %s/weechat.log file.\n", + weechat_home); + fprintf (stderr, "***\n"); + fprintf (stderr, "*** Please help WeeChat developers to fix this bug:\n"); + fprintf (stderr, "*** 1. if you have a core file, please run: gdb weechat-curses core\n"); + fprintf (stderr, "*** then issue \"bt\" command and send result to developers\n"); + fprintf (stderr, "*** 2. otherwise send backtrace displayed below and weechat.log\n"); + fprintf (stderr, "*** (be careful, private info may be in this file since\n"); + fprintf (stderr, "*** part of chats are displayed, so remove lines if needed)\n\n"); + + fprintf (stderr, "======= WeeChat backtrace =======\n"); + weechat_backtrace (); + fprintf (stderr, "======= End of backtrace =======\n"); + + /* shutdown with error code */ weechat_shutdown (EXIT_FAILURE, 1); } |