/* Katoob * Copyright (c) 2002,2003 Arabeyes, Mohammed Sameer. * * 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., 675 Mass Ave, Cambridge, MA 02139, USA. */ #ifdef HAVE_CONFIG_H #include #endif #ifdef EXPERIMENTAL #include "katoob.h" #include "crash.h" #include #include #include #include #include #include #include #include "misc.h" #include "file.h" #define DEBUG_COMMANDS "bt\nq" static void add_debug_text (gchar * buff); void create_crash_dialog (gchar * name, unsigned long pid, gint sig); void show_crash_dialog (); void katoob_debug_save (); void destroy_cb (); void close_cb (); GtkWidget *window; GtkTextBuffer *buffer; static void add_debug_text (gchar * buff) { katoob_debug (__FUNCTION__); katoob_debug (buff); gtk_text_buffer_insert_at_cursor (buffer, buff, -1); } void create_crash_dialog (gchar * name, unsigned long pid, gint sig) { GtkWidget *view, *label, *vbox, *hbox, *save_button, *close_button, *scrolledwin; gchar *tmp = NULL; katoob_debug (__FUNCTION__); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); gtk_window_set_position (GTK_WINDOW (window), GTK_WIN_POS_CENTER); gtk_window_set_title (GTK_WINDOW (window), _("Katoob debugging output.")); gtk_widget_set_size_request (window, 400, 300); vbox = gtk_vbox_new (FALSE, 0); gtk_container_add (GTK_CONTAINER (window), vbox); tmp = g_strdup_printf (_ ("Katoob has crashed, Please email the following debugging output to developer@arabeyes.org and tell them exactly what you were doing.")); /* (_ ("Katoob process No. (%ld) received signal %d (%s)\nPlease email the debugging output to developer@arabeyes.org and tell them what you were doing."), pid, sig, g_strsignal (sig)); */ label = gtk_label_new (tmp); g_free (tmp); gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_CENTER); gtk_label_set_line_wrap (GTK_LABEL (label), TRUE); gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0); scrolledwin = gtk_scrolled_window_new (NULL, NULL); gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC); gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwin), GTK_SHADOW_IN); view = gtk_text_view_new (); gtk_text_view_set_editable (GTK_TEXT_VIEW (view), FALSE); gtk_container_add (GTK_CONTAINER (scrolledwin), view); gtk_box_pack_start (GTK_BOX (vbox), scrolledwin, TRUE, TRUE, 0); buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (view)); hbox = gtk_hbox_new (TRUE, 10); gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0); save_button = gtk_button_new_from_stock (GTK_STOCK_SAVE); gtk_box_pack_start (GTK_BOX (hbox), save_button, TRUE, TRUE, 0); close_button = gtk_button_new_from_stock (GTK_STOCK_CLOSE); gtk_box_pack_start (GTK_BOX (hbox), close_button, TRUE, TRUE, 0); g_signal_connect (G_OBJECT (window), "destroy", G_CALLBACK (destroy_cb), NULL); g_signal_connect (G_OBJECT (close_button), "clicked", G_CALLBACK (close_cb), NULL); g_signal_connect (G_OBJECT (save_button), "clicked", G_CALLBACK (katoob_debug_save), NULL); tmp = g_strdup_printf ("Katoob version: %s\n Compiled in features:", VERSION); add_debug_text (tmp); g_free (tmp); #ifdef EXPERIMENTAL add_debug_text (" EXPERIMENTAL"); #endif /* EXPERIMENTAL */ #ifdef DEBUG add_debug_text (" DEBUG"); #endif /* DEBUG */ #ifdef ENABLE_PRINT add_debug_text (" PRINT"); #endif /* ENABLE_PRINT */ #ifdef ENABLE_HIGHLIGHT add_debug_text (" HIGHLIGHT"); #endif /* ENABLE_HIGHLIGHT */ #ifdef HAVE_NEW_SPELL add_debug_text (" NEW_SPELL"); #endif /* HAVE_NEW_SPELL */ #ifdef HAVE_OLD_SPELL add_debug_text (" OLD_SPELL"); #endif /* HAVE_OLD_SPELL */ add_debug_text ("\n"); } void destroy_cb () { exit (255); } void close_cb () { exit (255); } void show_crash_dialog () { katoob_debug (__FUNCTION__); gtk_widget_show_all (window); return; } void katoob_debug_save () { GtkWidget *file_selector; file_selector = gtk_file_selection_new (_("Save")); if (gtk_dialog_run (GTK_DIALOG (file_selector)) == GTK_RESPONSE_OK) { GtkTextIter start, end; gchar *buff; gchar *file; FILE *f; file = g_strdup (gtk_file_selection_get_filename (GTK_FILE_SELECTION (file_selector))); gtk_widget_destroy (file_selector); if (!file) { return; } /* If the file exists, Prompt the user to overwrite. */ if (g_file_test (file, G_FILE_TEST_EXISTS)) { gint result; gchar *_tmp = g_strdup_printf (_ ("Are you sure you want to overwrite the file %s ?"), file); result = katoob_create_question (_tmp); g_free (_tmp); switch (result) { case GTK_RESPONSE_YES: break; case GTK_RESPONSE_NO: g_free (file); return; break; } } f = fopen (file, "w+"); if (!f) { katoob_error (_ ("The requested file couldn't be opened for saving")); g_free (file); return; } gtk_text_buffer_get_bounds (buffer, &start, &end); buff = gtk_text_buffer_get_text (buffer, &start, &end, TRUE); fwrite (buff, strlen (buff), 1, f); fclose (f); g_free (buffer); g_free (file); } exit (255); } void katoob_save_open () { } /* Called when we catch a signal. */ void katoob_init_debug (gint sig) { extern gchar *_name; extern unsigned long _pid; gchar *command; gint fd; gchar *file; gchar buff[100]; FILE *fl; katoob_debug (__FUNCTION__); file = g_strdup_printf ("%s/%sXXXXXX", g_get_tmp_dir (), PACKAGE); fd = g_mkstemp (file); if (fd == -1) { g_warning ("Couldn't create temp file"); g_free (file); exit (1); } fl = fopen (file, "wb"); if (!fl) { g_warning ("Couldn't write gdb script."); g_free (file); exit (1); } if (!fwrite (DEBUG_COMMANDS, strlen (DEBUG_COMMANDS), 1, fl)) { fclose (fl); g_free (file); g_warning ("Couldn't write to temp file"); exit (1); } fclose (fl); /* * gdb: -x -batch -n */ command = g_strdup_printf ("gdb -n -batch -x %s %s %ld", file, _name, _pid); fl = popen (command, "r"); if (!fl) { g_warning ("Couldn't execute gdb"); g_free (command); unlink (file); g_free (file); exit (1); } g_free (command); create_crash_dialog (_name, _pid, sig); while (fgets (buff, 100, fl)) { add_debug_text (buff); } pclose (fl); unlink (file); g_free (file); show_crash_dialog (); } #endif