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.
34 #if HAVE_SYS_UTSNAME_H
35 # include <sys/utsname.h>
38 #if defined(__GNU_LIBRARY__)
39 # include <gnu/libc-version.h>
50 #include "pixmaps/notice_error.xpm"
54 * NOTE 1: the crash dialog is called when sylpheed is not
55 * initialized, so do not assume settings are available.
56 * for example, loading / creating pixmaps seems not
60 static void crash_handler (int sig);
61 static gboolean is_crash_dialog_allowed (void);
62 static void crash_debug (unsigned long crash_pid,
64 GString *debug_output);
65 static gboolean crash_create_debugger_file (void);
66 static void crash_save_crash_log (GtkButton *, const gchar *);
68 static const gchar *get_compiled_in_features (void);
69 static const gchar *get_lib_version (void);
70 static const gchar *get_operating_system (void);
75 static const gchar *DEBUG_SCRIPT = "bt full\nq";
80 *\brief (can't get pixmap working, so discarding it)
82 static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
89 GtkWidget *scrolledwindow1;
91 GtkWidget *hbuttonbox3;
92 GtkWidget *hbuttonbox4;
101 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
102 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
103 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
104 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
105 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
106 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
109 vbox1 = gtk_vbox_new(FALSE, 2);
110 gtk_widget_show(vbox1);
111 gtk_container_add(GTK_CONTAINER(window1), vbox1);
113 hbox1 = gtk_hbox_new(FALSE, 4);
114 gtk_widget_show(hbox1);
115 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
116 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
119 PIXMAP_CREATE(window1, pix, msk, notice_error_xpm);
120 pixwid = gtk_pixmap_new(pix, msk);
121 gtk_widget_show(pixwid);
122 gtk_box_pack_start(GTK_BOX(hbox1), pixwid, TRUE, TRUE, 0);
125 label1 = gtk_label_new
126 (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
127 gtk_widget_show(label1);
128 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
129 gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
131 frame1 = gtk_frame_new(_("Debug log"));
132 gtk_widget_show(frame1);
133 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
135 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
136 gtk_widget_show(scrolledwindow1);
137 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
138 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
139 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
140 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
142 text1 = gtk_text_new(NULL, NULL);
143 gtk_text_set_editable(GTK_TEXT(text1), FALSE);
144 gtk_widget_show(text1);
145 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
147 crash_report = g_strdup_printf(
148 "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
150 gtk_major_version, gtk_minor_version, gtk_micro_version,
151 get_compiled_in_features(),
152 get_operating_system(),
156 gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
158 hbuttonbox3 = gtk_hbutton_box_new();
159 gtk_widget_show(hbuttonbox3);
160 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
162 hbuttonbox4 = gtk_hbutton_box_new();
163 gtk_widget_show(hbuttonbox4);
164 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
166 button3 = gtk_button_new_with_label(_("Close"));
167 gtk_widget_show(button3);
168 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
169 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
171 button4 = gtk_button_new_with_label(_("Save..."));
172 gtk_widget_show(button4);
173 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
174 GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
176 button5 = gtk_button_new_with_label(_("Create bug report"));
177 gtk_widget_show(button5);
178 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
179 GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
181 gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
182 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
183 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
184 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
185 gtk_signal_connect(GTK_OBJECT(button4), "clicked",
186 GTK_SIGNAL_FUNC(crash_save_crash_log),
189 gtk_widget_show(window1);
198 *\brief install crash handlers
200 void crash_install_handlers(void)
205 if (!is_crash_dialog_allowed()) return;
210 signal(SIGSEGV, crash_handler);
211 sigaddset(&mask, SIGSEGV);
215 signal(SIGFPE, crash_handler);
216 sigaddset(&mask, SIGFPE);
220 signal(SIGILL, crash_handler);
221 sigaddset(&mask, SIGILL);
225 signal(SIGABRT, crash_handler);
226 sigaddset(&mask, SIGABRT);
229 sigprocmask(SIG_UNBLOCK, &mask, 0);
230 #endif /* HAVE_GDB */
236 *\brief crash dialog entry point
238 void crash_main(const char *arg)
245 extern gchar *startup_dir;
247 crash_create_debugger_file();
248 tokens = g_strsplit(arg, ",", 0);
250 pid = atol(tokens[0]);
251 text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
252 pid, atol(tokens[1]));
254 output = g_string_new("");
255 crash_debug(pid, tokens[2], output);
256 crash_dialog_new(text, output->str);
257 g_string_free(output, TRUE);
260 #endif /* HAVE_GDB */
264 *\brief create debugger script file in sylpheed directory.
265 * all the other options (creating temp files) looked too
268 static gboolean crash_create_debugger_file(void)
270 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
272 str_write_to_file(DEBUG_SCRIPT, filespec);
277 *\brief saves crash log to a file
279 static void crash_save_crash_log(GtkButton *button, const gchar *text)
287 lt = localtime(&timer);
288 strftime(buf, sizeof buf, "sylpheed-crash-log-%y-%m-%d-%H-%M-%S.txt", lt);
289 if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
291 str_write_to_file(text, filename);
296 *\brief launches debugger and attaches it to crashed sylpheed
298 static void crash_debug(unsigned long crash_pid,
300 GString *debug_output)
307 if (0 == (pid = fork())) {
309 char **argptr = argp;
310 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
316 * setup debugger to attach to crashed sylpheed
321 *argptr++ = "--quiet";
322 *argptr++ = "--batch";
324 *argptr++ = filespec;
325 *argptr++ = exe_image;
326 *argptr++ = g_strdup_printf("%d", crash_pid);
330 * redirect output to write end of pipe
335 if (-1 == execvp("gdb", argp))
336 puts("error execvp\n");
341 waitpid(pid, NULL, 0);
344 * make it non blocking
346 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
347 puts("set to non blocking failed\n");
353 r = read(choutput[0], buf, sizeof buf - 1);
356 g_string_append(debug_output, buf);
364 * kill the process we attached to
366 kill(crash_pid, SIGCONT);
375 static const gchar *get_compiled_in_features(void)
377 return g_strdup_printf("%s",
417 *\brief library version
419 static const gchar *get_lib_version(void)
421 #if defined(__GNU_LIBRARY__)
422 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
424 return g_strdup(_("Unknown"));
431 *\brief operating system
433 static const gchar *get_operating_system(void)
435 #if HAVE_SYS_UTSNAME_H
436 struct utsname utsbuf;
438 return g_strdup_printf("%s %s (%s)",
443 return g_strdup(_("Unknown"));
451 *\brief see if the crash dialog is allowed (because some
452 * developers may prefer to run sylpheed under gdb...)
454 static gboolean is_crash_dialog_allowed(void)
456 return !getenv("SYLPHEED_NO_CRASH");
460 *\brief this handler will probably evolve into
463 static void crash_handler(int sig)
466 static volatile unsigned long crashed_ = 0;
469 * let's hope startup_dir and argv0 aren't trashed.
470 * both are defined in main.c.
472 extern gchar *startup_dir;
477 * besides guarding entrancy it's probably also better
478 * to mask off signals
480 if (crashed_) return;
485 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
487 gdk_pointer_ungrab(GDK_CURRENT_TIME);
488 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
492 if (0 == (pid = fork())) {
497 * probably also some other parameters (like GTK+ ones).
498 * also we pass the full startup dir and the real command
499 * line typed in (argv0)
504 sprintf(buf, "%ld,%d,%s", getppid(), sig, argv0);
513 waitpid(pid, NULL, 0);