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>
51 #include "pixmaps/notice_error.xpm"
55 * NOTE 1: the crash dialog is called when sylpheed is not
56 * initialized, so do not assume settings are available.
57 * for example, loading / creating pixmaps seems not
61 static void crash_handler (int sig);
62 static gboolean is_crash_dialog_allowed (void);
63 static void crash_debug (unsigned long crash_pid, GString *string);
64 static gboolean crash_create_debugger_file (void);
65 static void crash_save_crash_log (GtkButton *, const gchar *);
67 static const gchar *get_compiled_in_features (void);
68 static const gchar *get_lib_version (void);
69 static const gchar *get_operating_system (void);
74 static const gchar *DEBUG_SCRIPT = "bt full\nq";
79 *\brief (can't get pixmap working, so discarding it)
81 static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
88 GtkWidget *scrolledwindow1;
90 GtkWidget *hbuttonbox3;
91 GtkWidget *hbuttonbox4;
100 window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
101 gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
102 gtk_window_set_title(GTK_WINDOW(window1), _("Sylpheed has crashed"));
103 gtk_window_set_position(GTK_WINDOW(window1), GTK_WIN_POS_CENTER);
104 gtk_window_set_modal(GTK_WINDOW(window1), TRUE);
105 gtk_window_set_default_size(GTK_WINDOW(window1), 460, 272);
108 vbox1 = gtk_vbox_new(FALSE, 2);
109 gtk_widget_show(vbox1);
110 gtk_container_add(GTK_CONTAINER(window1), vbox1);
112 hbox1 = gtk_hbox_new(FALSE, 4);
113 gtk_widget_show(hbox1);
114 gtk_box_pack_start(GTK_BOX(vbox1), hbox1, FALSE, TRUE, 0);
115 gtk_container_set_border_width(GTK_CONTAINER(hbox1), 4);
118 PIXMAP_CREATE(window1, pix, msk, notice_error_xpm);
119 pixwid = gtk_pixmap_new(pix, msk);
120 gtk_widget_show(pixwid);
121 gtk_box_pack_start(GTK_BOX(hbox1), pixwid, TRUE, TRUE, 0);
124 label1 = gtk_label_new
125 (g_strdup_printf(_("%s.\n Please file a bug report and include the information below."), text));
126 gtk_widget_show(label1);
127 gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
128 gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
130 frame1 = gtk_frame_new(_("Debug log"));
131 gtk_widget_show(frame1);
132 gtk_box_pack_start(GTK_BOX(vbox1), frame1, TRUE, TRUE, 0);
134 scrolledwindow1 = gtk_scrolled_window_new(NULL, NULL);
135 gtk_widget_show(scrolledwindow1);
136 gtk_container_add(GTK_CONTAINER(frame1), scrolledwindow1);
137 gtk_container_set_border_width(GTK_CONTAINER(scrolledwindow1), 3);
138 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolledwindow1),
139 GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
141 text1 = gtk_text_new(NULL, NULL);
142 gtk_text_set_editable(GTK_TEXT(text1), FALSE);
143 gtk_widget_show(text1);
144 gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
146 crash_report = g_strdup_printf(
147 _("Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s"),
149 gtk_major_version, gtk_minor_version, gtk_micro_version,
150 get_compiled_in_features(),
151 get_operating_system(),
155 gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
157 hbuttonbox3 = gtk_hbutton_box_new();
158 gtk_widget_show(hbuttonbox3);
159 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox3, FALSE, FALSE, 0);
161 hbuttonbox4 = gtk_hbutton_box_new();
162 gtk_widget_show(hbuttonbox4);
163 gtk_box_pack_start(GTK_BOX(vbox1), hbuttonbox4, FALSE, FALSE, 0);
165 button3 = gtk_button_new_with_label(_("Close"));
166 gtk_widget_show(button3);
167 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button3);
168 GTK_WIDGET_SET_FLAGS(button3, GTK_CAN_DEFAULT);
170 button4 = gtk_button_new_with_label(_("Save..."));
171 gtk_widget_show(button4);
172 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button4);
173 GTK_WIDGET_SET_FLAGS(button4, GTK_CAN_DEFAULT);
175 button5 = gtk_button_new_with_label(_("Create bug report"));
176 gtk_widget_show(button5);
177 gtk_container_add(GTK_CONTAINER(hbuttonbox4), button5);
178 GTK_WIDGET_SET_FLAGS(button5, GTK_CAN_DEFAULT);
180 gtk_signal_connect(GTK_OBJECT(window1), "delete_event",
181 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
182 gtk_signal_connect(GTK_OBJECT(button3), "clicked",
183 GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
184 gtk_signal_connect(GTK_OBJECT(button4), "clicked",
185 GTK_SIGNAL_FUNC(crash_save_crash_log),
188 gtk_widget_show(window1);
197 *\brief install crash handlers
199 void crash_install_handlers(void)
204 if (!is_crash_dialog_allowed()) return;
209 signal(SIGSEGV, crash_handler);
210 sigaddset(&mask, SIGSEGV);
214 signal(SIGFPE, crash_handler);
215 sigaddset(&mask, SIGFPE);
219 signal(SIGILL, crash_handler);
220 sigaddset(&mask, SIGILL);
224 signal(SIGABRT, crash_handler);
225 sigaddset(&mask, SIGABRT);
228 sigprocmask(SIG_UNBLOCK, &mask, 0);
229 #endif /* HAVE_GDB */
235 *\brief crash dialog entry point
237 void crash_main(const char *arg)
245 crash_create_debugger_file();
246 tokens = g_strsplit(arg, ",", 0);
248 pid = atol(tokens[0]);
249 text = g_strdup_printf(_("Sylpheed process (%ld) received signal %ld"),
250 pid, atol(tokens[1]));
252 output = g_string_new("");
253 crash_debug(pid, output);
254 crash_dialog_new(text, output->str);
255 g_string_free(output, TRUE);
258 #endif /* HAVE_GDB */
262 *\brief create debugger script file in sylpheed directory.
263 * all the other options (creating temp files) looked too
266 static gboolean crash_create_debugger_file(void)
269 gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
271 pf = prefs_write_open(filespec);
274 fprintf(pf->fp, DEBUG_SCRIPT);
275 prefs_write_close(pf);
279 *\brief saves crash log to a file
281 static void crash_save_crash_log(GtkButton *button, const gchar *text)
289 lt = localtime(&timer);
290 strftime(buf, sizeof buf, "sylpheed-crash-log-%y-%m-%d-%H-%M-%S.txt", lt);
291 if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
293 str_write_to_file(text, filename);
298 *\brief launches debugger and attaches it to crashed sylpheed
300 static void crash_debug(unsigned long crash_pid, GString *string)
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
320 *argptr++ = "--quiet";
321 *argptr++ = "--batch";
322 *argptr++ = "--command";
323 *argptr++ = g_strdup_printf("%s", filespec);
324 *argptr++ = "sylpheed"; /* program file name */
325 *argptr++ = g_strdup_printf("%d", crash_pid);
329 * redirect output to write end of pipe
334 if (-1 == execvp("gdb", argp))
335 puts("error execvp\n");
340 waitpid(pid, NULL, 0);
343 * make it non blocking
345 if (-1 == fcntl(choutput[0], F_SETFL, O_NONBLOCK))
346 puts("set to non blocking failed\n");
352 r = read(choutput[0], buf, sizeof buf - 1);
355 g_string_append(string, buf);
363 * kill the process we attached to
365 kill(crash_pid, SIGCONT);
374 static const gchar *get_compiled_in_features(void)
376 return g_strdup_printf("%s",
416 *\brief library version
418 static const gchar *get_lib_version(void)
420 #if defined(__GNU_LIBRARY__)
421 return g_strdup_printf("GNU libc %s", gnu_get_libc_version());
423 return g_strdup(_("Unknown"));
430 *\brief operating system
432 static const gchar *get_operating_system(void)
434 #if HAVE_SYS_UTSNAME_H
435 struct utsname utsbuf;
437 return g_strdup_printf("%s %s (%s)",
442 return g_strdup(_("Unknown"));
450 *\brief checks KDE, GNOME and Sylpheed specific variables
451 * to 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 * besides guarding entrancy it's probably also better
470 * to mask off signals
478 * gnome ungrabs focus, and flushes gdk. mmmh, good idea.
480 gdk_pointer_ungrab(GDK_CURRENT_TIME);
481 gdk_keyboard_ungrab(GDK_CURRENT_TIME);
484 if (0 == (pid = fork())) {
489 * probably also some other parameters (like GTK+ ones).
493 sprintf(buf, "%ld,%d", getppid(), sig);
500 execvp("/alfons/Projects/sylpheed-claws/src/sylpheed", args);
502 execvp("sylpheed", args);
505 waitpid(pid, NULL, 0);