2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2002 by the Sylpheed Claws Team and Hiroyuki Yamamoto
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
38 static void crash_handler (int sig);
39 static gboolean is_crash_dialog_allowed (void);
40 static void crash_debug (unsigned long crash_pid, GString *string);
41 static gboolean crash_create_debugger_file (void);
43 static const gchar *DEBUG_SCRIPT = "bt full\nq";
47 GtkWidget *crash_dialog_new (const gchar *text, const gchar *debug_output)
52 GtkWidget *scrolledwindow1;
54 GtkWidget *hbuttonbox1;
58 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
59 gtk_object_set_data(GTK_OBJECT(window1), "window1", window1);
60 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed Claws - It Bites!"));
62 vbox1 = gtk_vbox_new(FALSE, 0);
63 gtk_widget_show(vbox1);
64 gtk_container_add(GTK_CONTAINER(window1), vbox1);
65 gtk_container_set_border_width(GTK_CONTAINER (vbox1), 1);
67 label1 = gtk_label_new(text);
68 gtk_widget_show(label1);
69 gtk_box_pack_start(GTK_BOX(vbox1), label1, FALSE, FALSE, 0);
71 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
72 gtk_widget_show(scrolledwindow1);
73 gtk_box_pack_start(GTK_BOX(vbox1), scrolledwindow1, TRUE, TRUE, 0);
74 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
75 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
77 text1 = gtk_text_new(NULL, NULL);
78 gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL,
79 debug_output, strlen(debug_output));
80 gtk_widget_show(text1);
81 gtk_container_add(GTK_CONTAINER (scrolledwindow1), text1);
83 hbuttonbox1 = gtk_hbutton_box_new();
84 gtk_widget_show(hbuttonbox1);
85 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox1, FALSE, TRUE, 0);
86 gtk_button_box_set_child_ipadding(GTK_BUTTON_BOX (hbuttonbox1), 5, -1);
88 close = gtk_button_new_with_label(_("Close"));
89 gtk_widget_show(close);
90 gtk_container_add(GTK_CONTAINER(hbuttonbox1), close);
91 GTK_WIDGET_SET_FLAGS(close, GTK_CAN_DEFAULT);
93 button3 = gtk_button_new_with_label(_("Save..."));
94 gtk_widget_show(button3);
95 gtk_container_add(GTK_CONTAINER(hbuttonbox1), button3);
96 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
98 gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
99 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
100 gtk_signal_connect(GTK_OBJECT(close), "clicked",
101 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
103 gtk_widget_show(window1);
110 void crash_install_handlers(void)
115 if (!is_crash_dialog_allowed()) return;
120 signal(SIGSEGV, crash_handler);
121 sigaddset(&mask, SIGSEGV);
125 signal(SIGFPE, crash_handler);
126 sigaddset(&mask, SIGFPE);
130 signal(SIGILL, crash_handler);
131 sigaddset(&mask, SIGILL);
135 signal(SIGABRT, crash_handler);
136 sigaddset(&mask, SIGABRT);
139 sigprocmask(SIG_UNBLOCK, &mask, 0);
140 #endif /* HAVE_GDB */
146 *\brief crash dialog entry point
148 void crash_main(const char *arg)
156 crash_create_debugger_file();
157 tokens = g_strsplit(arg, ",", 0);
159 pid = atol(tokens[0]);
160 text = g_strdup_printf("Sylpheed process (%lx) received signal %ld",
161 pid, atol(tokens[1]));
163 output = g_string_new("DEBUG LOG\n");
164 crash_debug(pid, output);
165 crash_dialog_new(text, output->str);
166 g_string_free(output, TRUE);
169 #endif /* HAVE_GDB */
173 *\brief create debugger script file in sylpheed directory.
174 * all the other options (creating temp files) looked too
177 static gboolean crash_create_debugger_file(void)
180 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
182 pf = prefs_write_open(filespec);
185 fprintf(pf->fp, DEBUG_SCRIPT);
186 prefs_write_close(pf);
190 *\brief launches debugger and attaches it to crashed sylpheed
192 static void crash_debug(unsigned long crash_pid, GString *string)
199 if (0 == (pid = fork())) {
201 char **argptr = argp;
202 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
208 * setup debugger to attach to crashed sylpheed
212 *argptr++ = "--quiet";
213 *argptr++ = "--batch";
214 *argptr++ = "--command";
215 *argptr++ = g_strdup_printf("%s", filespec);
216 *argptr++ = "sylpheed"; /* program file name */
217 *argptr++ = g_strdup_printf("%d", crash_pid);
221 * redirect output to write end of pipe
226 if (-1 == execvp("gdb", argp))
227 puts("error execvp\n");
232 waitpid(pid, NULL, 0);
235 * make it non blocking
237 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
238 puts("set to non blocking failed\n");
244 r = read(choutput[0], buf, sizeof buf - 1);
247 g_string_append(string, buf);
255 * kill the process we attached to
257 kill(crash_pid, SIGCONT);
262 *\brief checks KDE, GNOME and Sylpheed specific variables
263 * to see if the crash dialog is allowed (because some
264 * developers may prefer to run sylpheed under gdb...)
266 static gboolean is_crash_dialog_allowed(void)
268 return getenv("KDE_DEBUG") ||
269 !getenv("GNOME_DISABLE_CRASH_DIALOG") ||
270 !getenv("SYLPHEED_NO_CRASH");
274 *\brief this handler will probably evolve into
277 static void crash_handler(int sig)
280 static volatile unsigned long crashed_ = 0;
283 * besides guarding entrancy it's probably also better
284 * to mask off signals
292 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
294 gdk_pointer_ungrab(GDK_CURRENT_TIME);
295 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
298 if (0 == (pid = fork())) {
303 * probably also some other parameters (like GTK+ ones).
307 sprintf(buf, "%ld,%d", getppid(), sig);
314 execvp("/alfons/Projects/sylpheed-claws/src/sylpheed", args);
316 execvp("sylpheed", args);
319 waitpid(pid, NULL, 0);