use bt instead of bt full which seems to work
[claws.git] / src / crash.c
index e19d86090826a2dd3b4d014e3b2c5ea81d6adccc..b5080298f4327cd3579cb081e9d0c33a94314a9a 100644 (file)
@@ -26,6 +26,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <signal.h>
+#include <time.h>
 
 #include <errno.h>
 #include <fcntl.h>
@@ -41,7 +42,7 @@
 #include "intl.h"
 #include "crash.h"
 #include "utils.h"
-#include "prefs.h"
+#include "filesel.h"
 #include "version.h"
 
 #if 0
  * to be possible.
  */
 
-static void crash_handler                      (int sig);
-static gboolean is_crash_dialog_allowed                (void);
-static void crash_debug                                (unsigned long crash_pid, GString *string);
-static gboolean crash_create_debugger_file     (void);
+static void             crash_handler                  (int sig);
+static gboolean                 is_crash_dialog_allowed        (void);
+static void             crash_debug                    (unsigned long crash_pid, 
+                                                        gchar   *exe_image,
+                                                        GString *debug_output);
+static gboolean                 crash_create_debugger_file     (void);
+static void             crash_save_crash_log           (GtkButton *, const gchar *);
+
+static const gchar     *get_compiled_in_features       (void);
+static const gchar     *get_lib_version                (void);
+static const gchar     *get_operating_system           (void);
 
-static const gchar *get_compiled_in_features   (void);
-static const gchar *get_lib_version            (void);
-static const gchar *get_operating_system       (void);
 
 /***/
 
-static const gchar *DEBUG_SCRIPT = "bt full\nq";
+static const gchar *DEBUG_SCRIPT = "bt\nq";
 
 /***/
 
@@ -91,6 +96,7 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
        GtkWidget *pixwid;
        GdkPixmap *pix;
        GdkBitmap *msk;
+       gchar     *crash_report;
 
        window1 = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        gtk_container_set_border_width(GTK_CONTAINER(window1), 5);
@@ -117,7 +123,7 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
 #endif 
 
        label1 = gtk_label_new
-           (g_strdup_printf(_("%s.\n Please file a bug report and include the information below."), text));
+           (g_strdup_printf(_("%s.\nPlease file a bug report and include the information below."), text));
        gtk_widget_show(label1);
        gtk_box_pack_start(GTK_BOX(hbox1), label1, TRUE, TRUE, 0);
        gtk_misc_set_alignment(GTK_MISC(label1), 7.45058e-09, 0.5);
@@ -134,19 +140,20 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
                                       GTK_POLICY_NEVER, GTK_POLICY_ALWAYS);
 
        text1 = gtk_text_new(NULL, NULL);
+       gtk_text_set_editable(GTK_TEXT(text1), FALSE);
        gtk_widget_show(text1);
        gtk_container_add(GTK_CONTAINER(scrolledwindow1), text1);
+       
+       crash_report = g_strdup_printf(
+               "Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures:%s\nOperating system: %s\nC Library: %s\n--\n%s",
+               VERSION,
+               gtk_major_version, gtk_minor_version, gtk_micro_version,
+               get_compiled_in_features(),
+               get_operating_system(),
+               get_lib_version(),
+               debug_output);
 
-       gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL,
-                       g_strdup_printf(
-                               _("Sylpheed version %s\nGTK+ version %d.%d.%d\nFeatures: %s\nOperating system: %s\nC Library: %s\n--\n%s"),
-                               VERSION,
-                               gtk_major_version, gtk_minor_version, gtk_micro_version,
-                               get_compiled_in_features(),
-                               get_operating_system(),
-                               get_lib_version(),
-                               debug_output),
-                       -1);
+       gtk_text_insert(GTK_TEXT(text1), NULL, NULL, NULL, crash_report, -1);
 
        hbuttonbox3 = gtk_hbutton_box_new();
        gtk_widget_show(hbuttonbox3);
@@ -175,6 +182,9 @@ static GtkWidget *crash_dialog_new(const gchar *text, const gchar *debug_output)
                           GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
        gtk_signal_connect(GTK_OBJECT(button3),   "clicked",
                           GTK_SIGNAL_FUNC(gtk_main_quit), NULL);
+       gtk_signal_connect(GTK_OBJECT(button4), "clicked",
+                          GTK_SIGNAL_FUNC(crash_save_crash_log),
+                          crash_report);
 
        gtk_widget_show(window1);
 
@@ -232,6 +242,7 @@ void crash_main(const char *arg)
        gchar **tokens;
        unsigned long pid;
        GString *output;
+       extern gchar *startup_dir;
 
        crash_create_debugger_file();
        tokens = g_strsplit(arg, ",", 0);
@@ -241,7 +252,7 @@ void crash_main(const char *arg)
                               pid, atol(tokens[1]));
 
        output = g_string_new("");     
-       crash_debug(pid, output);
+       crash_debug(pid, tokens[2], output);
        crash_dialog_new(text, output->str);
        g_string_free(output, TRUE);
        g_free(text);
@@ -256,20 +267,37 @@ void crash_main(const char *arg)
  */
 static gboolean crash_create_debugger_file(void)
 {
-       PrefFile *pf;
        gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
-
-       pf = prefs_write_open(filespec);
+       
+       str_write_to_file(DEBUG_SCRIPT, filespec);
        g_free(filespec);
-       if (pf) 
-               fprintf(pf->fp, DEBUG_SCRIPT);
-       prefs_write_close(pf);  
+}
+
+/*
+ *\brief       saves crash log to a file
+ */
+static void crash_save_crash_log(GtkButton *button, const gchar *text)
+{
+       time_t timer;
+       struct tm *lt;
+       char buf[100];
+       gchar *filename;
+
+       timer = time(NULL);
+       lt = localtime(&timer);
+       strftime(buf, sizeof buf, "sylpheed-crash-log-%y-%m-%d-%H-%M-%S.txt", lt);
+       if (NULL != (filename = filesel_select_file(_("Save crash information"), buf))
+       &&  *filename)
+               str_write_to_file(text, filename);
+       g_free(filename);       
 }
 
 /*
  *\brief       launches debugger and attaches it to crashed sylpheed
  */
-static void crash_debug(unsigned long crash_pid, GString *string)
+static void crash_debug(unsigned long crash_pid, 
+                       gchar *exe_image,
+                       GString *debug_output)
 {
        int choutput[2];
        pid_t pid;
@@ -277,7 +305,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
        pipe(choutput);
 
        if (0 == (pid = fork())) {
-               char *argp[9];
+               char *argp[10];
                char **argptr = argp;
                gchar *filespec = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, DEBUGGERRC, NULL);
 
@@ -287,13 +315,14 @@ static void crash_debug(unsigned long crash_pid, GString *string)
                /*
                 * setup debugger to attach to crashed sylpheed
                 */
+               *argptr++ = "gdb"; 
                *argptr++ = "--nw";
                *argptr++ = "--nx";
                *argptr++ = "--quiet";
                *argptr++ = "--batch";
-               *argptr++ = "--command";
-               *argptr++ = g_strdup_printf("%s", filespec);
-               *argptr++ = "sylpheed"; /* program file name */
+               *argptr++ = "-x";
+               *argptr++ = filespec;
+               *argptr++ = exe_image;
                *argptr++ = g_strdup_printf("%d", crash_pid);
                *argptr   = NULL;
 
@@ -324,7 +353,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
                        r = read(choutput[0], buf, sizeof buf - 1);
                        if (r > 0) {
                                buf[r] = 0;
-                               g_string_append(string, buf);
+                               g_string_append(debug_output, buf);
                        }
                } while (r > 0);
                
@@ -345,8 +374,7 @@ static void crash_debug(unsigned long crash_pid, GString *string)
  */
 static const gchar *get_compiled_in_features(void)
 {
-       return g_strdup_printf(
-                  _("Compiled-in features:%s"),
+       return g_strdup_printf("%s",
 #if HAVE_GDK_IMLIB
                   " gdk_imlib"
 #endif
@@ -385,6 +413,9 @@ static const gchar *get_compiled_in_features(void)
 
 /***/
 
+/*
+ *\brief       library version
+ */
 static const gchar *get_lib_version(void)
 {
 #if defined(__GNU_LIBRARY__)
@@ -396,6 +427,9 @@ static const gchar *get_lib_version(void)
 
 /***/
 
+/*
+ *\brief       operating system
+ */
 static const gchar *get_operating_system(void)
 {
 #if HAVE_SYS_UTSNAME_H
@@ -414,15 +448,12 @@ static const gchar *get_operating_system(void)
 /***/
 
 /*
- *\brief       checks KDE, GNOME and Sylpheed specific variables
- *             to see if the crash dialog is allowed (because some
+ *\brief       see if the crash dialog is allowed (because some
  *             developers may prefer to run sylpheed under gdb...)
  */
 static gboolean is_crash_dialog_allowed(void)
 {
-       return getenv("KDE_DEBUG") || 
-              !getenv("GNOME_DISABLE_CRASH_DIALOG") ||
-              !getenv("SYLPHEED_NO_CRASH");
+       return !getenv("SYLPHEED_NO_CRASH");
 }
 
 /*
@@ -434,12 +465,19 @@ static void crash_handler(int sig)
        pid_t pid;
        static volatile unsigned long crashed_ = 0;
 
+       /*
+        * let's hope startup_dir and argv0 aren't trashed.
+        * both are defined in main.c.
+        */
+       extern gchar *startup_dir;
+       extern gchar *argv0;
+
+
        /*
         * besides guarding entrancy it's probably also better 
         * to mask off signals
         */
-       if (crashed_) 
-               return;
+       if (crashed_) return;
 
        crashed_++;
 
@@ -449,27 +487,28 @@ static void crash_handler(int sig)
        gdk_pointer_ungrab(GDK_CURRENT_TIME);
        gdk_keyboard_ungrab(GDK_CURRENT_TIME);
        gdk_flush();
-        
+
+
        if (0 == (pid = fork())) {
                char buf[50];
-               char *args[4];
+               char *args[5];
        
                /*
                 * probably also some other parameters (like GTK+ ones).
+                * also we pass the full startup dir and the real command
+                * line typed in (argv0)
                 */
-               args[0] = "--debug";
-               args[1] = "--crash";
-               sprintf(buf, "%ld,%d", getppid(), sig);
-               args[2] = buf;
-               args[3] = NULL;
-
+               args[0] = argv0; 
+               args[1] = "--debug";
+               args[2] = "--crash";
+               sprintf(buf, "%ld,%d,%s", getppid(), sig, argv0);
+               args[3] = buf;
+               args[4] = NULL;
+
+               chdir(startup_dir);
                setgid(getgid());
                setuid(getuid());
-#if 0
-               execvp("/alfons/Projects/sylpheed-claws/src/sylpheed", args);
-#else
-               execvp("sylpheed", args);
-#endif
+               execvp(argv0, args);
        } else {
                waitpid(pid, NULL, 0);
                _exit(253);