[gimp/wip/bug792787-debug-stacktrace-GUI: 3/3] app, tools: add backtrace GUI for crashes as well.



commit fc449e052c99292341020c67a61e142f5f12cf12
Author: Jehan <jehan girinstud io>
Date:   Thu Jan 25 14:32:39 2018 +0100

    app, tools: add backtrace GUI for crashes as well.
    
    This was a bit harder since even though we handle fatal signals,
    allowing us to do any last action before GIMP crashes, it seems more
    memory allocation is not allowed at this time. So creating a dialog or
    simply getting the return output of gdb into the main process is not
    allowed. What I do instead is running a separate program (gimpdebug)
    which will take care of creating the new dialog and running a debugger.
    I still use GimpCriticalDialog code from this separate binary, while I
    continue to use this widget also within GIMP for non-fatal errors. The
    reason why we still want to use it within GIMP is that we can bundle
    several non-fatal errors and backtrace this way (fatal errors don't
    return anyway) and it's easier to do so when created from the main
    process.

 app/errors.c                     |   41 +++++++------
 app/gui/gui-message.c            |    3 +-
 app/widgets/gimpcriticaldialog.c |   87 +++++++++++++++++++++-----
 app/widgets/gimpcriticaldialog.h |   15 +++--
 tools/Makefile.am                |   20 ++++++-
 tools/gimpdebug.c                |  126 ++++++++++++++++++++++++++++++++++++++
 6 files changed, 249 insertions(+), 43 deletions(-)
---
diff --git a/app/errors.c b/app/errors.c
index 48fa176..90c998a 100644
--- a/app/errors.c
+++ b/app/errors.c
@@ -262,27 +262,30 @@ gimp_eek (const gchar *reason,
           break;
 
         case GIMP_STACK_TRACE_QUERY:
-          {
-            sigset_t sigset;
-
-            sigemptyset (&sigset);
-            sigprocmask (SIG_SETMASK, &sigset, NULL);
-
-            if (the_errors_gimp)
-              gimp_gui_ungrab (the_errors_gimp);
-
-            g_on_error_query (full_prog_name);
-          }
-          break;
-
+          /* Basically we don't have the "QUERY" case anymore, at least
+           * for now until I figure out something.
+           */
         case GIMP_STACK_TRACE_ALWAYS:
           {
-            sigset_t sigset;
-
-            sigemptyset (&sigset);
-            sigprocmask (SIG_SETMASK, &sigset, NULL);
-
-            g_on_error_stack_trace (full_prog_name);
+#if defined(G_OS_UNIX)
+            gchar   *args[6] = { "gimpdebug-2.0", full_prog_name, NULL,
+                                 (gchar *) reason, (gchar *) message, NULL };
+            gchar    pid[16];
+            gint     exit_status;
+
+            g_snprintf (pid, 16, "%u", (guint) getpid ());
+            args[2] = pid;
+
+            /* We don't care about any return value. If it fails, too
+             * bad, we just won't have any stack trace.
+             * We still need to use the sync() variant because we have
+             * to keep GIMP up long enough for the debugger to get its
+             * trace.
+             */
+            g_spawn_sync (NULL, args, NULL,
+                          G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL | G_SPAWN_STDOUT_TO_DEV_NULL,
+                          NULL, NULL, NULL, NULL, &exit_status, NULL);
+#endif
           }
           break;
 
diff --git a/app/gui/gui-message.c b/app/gui/gui-message.c
index c30cd4c..1f97423 100644
--- a/app/gui/gui-message.c
+++ b/app/gui/gui-message.c
@@ -318,8 +318,7 @@ gui_message_error_dialog (Gimp                *gimp,
 
       dialog = global_critical_dialog ();
       text = gui_message_format (severity, domain, message);
-      gimp_critical_dialog_add (GIMP_CRITICAL_DIALOG (dialog),
-                                text, trace);
+      gimp_critical_dialog_add (dialog, text, trace, FALSE, NULL, 0);
       g_free (text);
       gtk_widget_show (dialog);
 
diff --git a/app/widgets/gimpcriticaldialog.c b/app/widgets/gimpcriticaldialog.c
index 80cd56f..7453e68 100644
--- a/app/widgets/gimpcriticaldialog.c
+++ b/app/widgets/gimpcriticaldialog.c
@@ -18,32 +18,37 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/*
+ * This widget is particular that I want to be able to use it internally
+ * but also from an alternate tool (gimpdebug). It means that the
+ * implementation must stay as generic glib/GTK+ as possible.
+ */
+
 #include "config.h"
 
 #include <string.h>
 
-#include <gegl.h>
+#include <glib.h>
 #include <gtk/gtk.h>
 
-#include "libgimpbase/gimpbase.h"
-#include "libgimpwidgets/gimpwidgets.h"
-
-#include "widgets-types.h"
-
 #include "gimpcriticaldialog.h"
 
 #include "version.h"
 
 #include "gimp-intl.h"
 
+typedef struct _GimpCriticalDialog       GimpCriticalDialog;
+
 #define GIMP_CRITICAL_RESPONSE_CLIPBOARD 1
 #define GIMP_CRITICAL_RESPONSE_URL       2
+#define GIMP_CRITICAL_RESPONSE_RESTART   3
 
 
-static void    gimp_critical_dialog_response (GtkDialog           *dialog,
-                                              gint                 response_id);
+static void    gimp_critical_dialog_finalize (GObject    *object);
+static void    gimp_critical_dialog_response (GtkDialog  *dialog,
+                                              gint        response_id);
 
-G_DEFINE_TYPE (GimpCriticalDialog, gimp_critical_dialog, GIMP_TYPE_DIALOG)
+G_DEFINE_TYPE (GimpCriticalDialog, gimp_critical_dialog, GTK_TYPE_DIALOG)
 
 #define parent_class gimp_critical_dialog_parent_class
 
@@ -51,8 +56,11 @@ G_DEFINE_TYPE (GimpCriticalDialog, gimp_critical_dialog, GIMP_TYPE_DIALOG)
 static void
 gimp_critical_dialog_class_init (GimpCriticalDialogClass *klass)
 {
+  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
   GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
 
+  object_class->finalize = gimp_critical_dialog_finalize;
+
   dialog_class->response = gimp_critical_dialog_response;
 }
 
@@ -78,7 +86,7 @@ gimp_critical_dialog_init (GimpCriticalDialog *dialog)
   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
   gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE);
 
-  dialog->vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 0);
+  dialog->vbox = gtk_vbox_new (FALSE, 0);
   gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
                       dialog->vbox, TRUE, TRUE, 0);
   gtk_widget_show (dialog->vbox);
@@ -142,6 +150,20 @@ gimp_critical_dialog_init (GimpCriticalDialog *dialog)
   gtk_text_view_set_editable (GTK_TEXT_VIEW (dialog->details), FALSE);
   gtk_widget_show (dialog->details);
   gtk_container_add (GTK_CONTAINER (widget), dialog->details);
+
+  dialog->pid     = 0;
+  dialog->program = NULL;
+}
+
+static void
+gimp_critical_dialog_finalize (GObject *object)
+{
+  GimpCriticalDialog *dialog = GIMP_CRITICAL_DIALOG (object);
+
+  if (dialog->program)
+    g_free (dialog->program);
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
 }
 
 static void
@@ -191,6 +213,17 @@ gimp_critical_dialog_response (GtkDialog *dialog,
                         NULL);
         }
       break;
+    case GIMP_CRITICAL_RESPONSE_RESTART:
+        {
+          gchar *args[2] = { critical->program , NULL };
+
+          if (critical->pid > 0)
+            kill (critical->pid, SIGINT);
+          if (critical->program)
+            g_spawn_async (NULL, args, NULL, G_SPAWN_DEFAULT,
+                           NULL, NULL, NULL, NULL);
+        }
+      /* Fall through. */
     case GTK_RESPONSE_DELETE_EVENT:
     case GTK_RESPONSE_CLOSE:
     default:
@@ -212,10 +245,14 @@ gimp_critical_dialog_new (const gchar *title)
 }
 
 void
-gimp_critical_dialog_add (GimpCriticalDialog  *dialog,
+gimp_critical_dialog_add (GtkWidget  *dialog,
                           const gchar         *message,
-                          const gchar         *trace)
+                          const gchar         *trace,
+                          gboolean             is_fatal,
+                          const gchar         *program,
+                          pid_t                pid)
 {
+  GimpCriticalDialog *critical;
   GtkTextBuffer *buffer;
   GtkTextIter    end;
   gchar         *text;
@@ -230,10 +267,16 @@ gimp_critical_dialog_add (GimpCriticalDialog  *dialog,
        */
       return;
     }
+  critical = GIMP_CRITICAL_DIALOG (dialog);
 
   /* The user text, which should be localized. */
-  if (! gtk_label_get_text (GTK_LABEL (dialog->label)) ||
-      strlen (gtk_label_get_text (GTK_LABEL (dialog->label))) == 0)
+  if (is_fatal)
+    {
+      text = g_strdup_printf (_("GIMP crashed with a fatal error: %s"),
+                              message);
+    }
+  else if (! gtk_label_get_text (GTK_LABEL (critical->label)) ||
+           strlen (gtk_label_get_text (GTK_LABEL (critical->label))) == 0)
     {
       /* First critical error. Let's just display it. */
       text = g_strdup_printf (_("GIMP encountered a critical error: %s"),
@@ -246,7 +289,7 @@ gimp_critical_dialog_add (GimpCriticalDialog  *dialog,
        */
       text = g_strdup_printf (_("GIMP encountered several critical errors!"));
     }
-  gtk_label_set_text (GTK_LABEL (dialog->label),
+  gtk_label_set_text (GTK_LABEL (critical->label),
                       text);
   g_free (text);
 
@@ -254,9 +297,21 @@ gimp_critical_dialog_add (GimpCriticalDialog  *dialog,
    * meant to go to clipboard for the bug report. It has to be in
    * English.
    */
-  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (dialog->details));
+  buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (critical->details));
   gtk_text_buffer_get_iter_at_offset (buffer, &end, -1);
   text = g_strdup_printf ("\n\n> %s\n\nStack trace:\n%s", message, trace);
   gtk_text_buffer_insert (buffer, &end, text, -1);
   g_free (text);
+
+  /* Finally when encountering a fatal message, propose one more button
+   * to restart GIMP.
+   */
+  if (is_fatal)
+    {
+      gtk_dialog_add_buttons (GTK_DIALOG (dialog),
+                              _("_Restart GIMP"), GIMP_CRITICAL_RESPONSE_RESTART,
+                              NULL);
+      critical->program = g_strdup (program);
+      critical->pid     = pid;
+    }
 }
diff --git a/app/widgets/gimpcriticaldialog.h b/app/widgets/gimpcriticaldialog.h
index 070fc32..76a9581 100644
--- a/app/widgets/gimpcriticaldialog.h
+++ b/app/widgets/gimpcriticaldialog.h
@@ -36,27 +36,32 @@ typedef struct _GimpCriticalDialogClass  GimpCriticalDialogClass;
 
 struct _GimpCriticalDialog
 {
-  GimpDialog       parent_instance;
+  GtkDialog        parent_instance;
 
   GtkWidget       *vbox;
 
   GtkWidget       *label;
   GtkWidget       *details;
+
+  gchar           *program;
+  pid_t            pid;
 };
 
 struct _GimpCriticalDialogClass
 {
-  GimpDialogClass  parent_class;
+  GtkDialogClass   parent_class;
 };
 
 
 GType       gimp_critical_dialog_get_type (void) G_GNUC_CONST;
 
 GtkWidget * gimp_critical_dialog_new      (const gchar        *title);
-void        gimp_critical_dialog_add      (GimpCriticalDialog *dialog,
+void        gimp_critical_dialog_add      (GtkWidget          *dialog,
                                            const gchar        *message,
-                                           const gchar        *trace);
-
+                                           const gchar        *trace,
+                                           gboolean            is_fatal,
+                                           const gchar        *program,
+                                           pid_t               pid);
 
 
 G_END_DECLS
diff --git a/tools/Makefile.am b/tools/Makefile.am
index 37e7b33..c111e83 100644
--- a/tools/Makefile.am
+++ b/tools/Makefile.am
@@ -1,5 +1,7 @@
 ## Process this file with automake to produce Makefile.in
 
+AUTOMAKE_OPTIONS = subdir-objects
+
 libgimpbase = $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la
 
 if OS_WIN32
@@ -12,7 +14,7 @@ gimp_debug_resume_SOURCES = gimp-debug-resume.c
 
 else
 
-bin_PROGRAMS = gimptool-2.0
+bin_PROGRAMS = gimptool-2.0 gimpdebug-2.0
 
 endif
 
@@ -21,6 +23,22 @@ noinst_PROGRAMS = test-clipboard
 EXTRA_PROGRAMS = \
        kernelgen
 
+gimpdebug_2_0_SOURCES = gimpdebug.c                         \
+                       ../app/widgets/gimpcriticaldialog.h \
+                       ../app/widgets/gimpcriticaldialog.c \
+                       ../app/version.h                    \
+                       ../app/version.c
+
+gimpdebug_2_0_CPPFLAGS = \
+       -DCC_VERSION=\""$(CC_VERSION)"\" \
+       -I$(top_srcdir)/app              \
+       $(AM_CPPFLAGS)                   \
+       $(GTK_CFLAGS)
+
+gimpdebug_2_0_LDADD =   \
+       $(GEGL_LIBS)    \
+       $(GTK_LIBS)
+
 
 gimptool_2_0_SOURCES = gimptool.c
 
diff --git a/tools/gimpdebug.c b/tools/gimpdebug.c
new file mode 100644
index 0000000..5ae723f
--- /dev/null
+++ b/tools/gimpdebug.c
@@ -0,0 +1,126 @@
+/* gimpdebug
+ * Copyright (C) 2018 Jehan <jehan gimp org>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+/*
+ * GimpDebug simply runs a debugger on a given executable and PID and
+ * displays a dialog proposing to create a bug report. The reason why it
+ * is a separate executable is simply that when the program crashed,
+ * even though some actions are possible before exit() by catching fatal
+ * errors and signals, it may not be possible anymore to allocate memory
+ * anymore. Therefore creating a new dialog, or even just allocating a
+ * string with the contents of the debugger are impossible actions.
+ * So we call instead a separate program, then exit.
+ *
+ * Note: this initial version does not handle Windows yet.
+ */
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+
+#include <sys/stat.h>
+
+#include <gio/gio.h>
+#include <glib.h>
+#include <glib/gi18n.h>
+
+#include <gtk/gtk.h>
+
+#include "app/widgets/gimpcriticaldialog.h"
+
+
+static gchar * gimp_debug_get_stack_trace (const gchar *full_prog_name,
+                                           const gchar *pid);
+
+
+
+int
+main (int    argc,
+      char **argv)
+{
+  const gchar *program;
+  const gchar *pid;
+  const gchar *reason;
+  const gchar *message;
+  gchar       *trace;
+  gchar       *error;
+  GtkWidget   *dialog;
+
+  if (argc != 5)
+    {
+      g_print ("Usage: gimpdebug-2.0 [PROGRAM] [PID] [REASON] [MESSAGE]\n");
+      exit (EXIT_FAILURE);
+    }
+
+  program = argv[1];
+  pid     = argv[2];
+  reason  = argv[3];
+  message = argv[4];
+  error   = g_strdup_printf ("%s: %s", reason, message);
+
+  trace   = gimp_debug_get_stack_trace (program, pid);
+
+  if (trace == NULL || strlen (trace) == 0)
+    exit (EXIT_FAILURE);
+
+  gtk_init (&argc, &argv);
+
+  dialog = gimp_critical_dialog_new (_("GIMP critical error"));
+  gimp_critical_dialog_add (dialog, error, trace, TRUE, program,
+                            g_ascii_strtoull (pid, NULL, 10));
+  g_free (error);
+  g_free (trace);
+  g_signal_connect (dialog, "delete-event",
+                    G_CALLBACK (gtk_main_quit), NULL);
+  g_signal_connect (dialog, "destroy",
+                    G_CALLBACK (gtk_main_quit), NULL);
+
+  gtk_widget_show (dialog);
+  gtk_main ();
+
+  exit (EXIT_SUCCESS);
+}
+
+
+static gchar *
+gimp_debug_get_stack_trace (const gchar *full_prog_name,
+                            const gchar *pid)
+{
+  gchar   *trace  = NULL;
+
+  /* This works only on UNIX systems. On Windows, we'll have to find
+   * another method, probably with DrMingW.
+   */
+#if defined(G_OS_UNIX)
+  const gchar *args[7] = { "gdb", "-batch", "-ex", "backtrace full",
+                       full_prog_name, pid, NULL };
+  gchar       *gdb_stdout;
+
+  if (g_spawn_sync (NULL, (gchar **) args, NULL,
+                    G_SPAWN_SEARCH_PATH | G_SPAWN_STDERR_TO_DEV_NULL,
+                    NULL, NULL, &gdb_stdout, NULL, NULL, NULL))
+    {
+      trace = g_strdup (gdb_stdout);
+    }
+  else if (gdb_stdout)
+    {
+      g_free (gdb_stdout);
+    }
+#endif
+
+  return trace;
+}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]