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.
This commit is contained in:
Jehan 2018-01-25 14:32:39 +01:00
parent bb88a2d52f
commit beede1718a
6 changed files with 249 additions and 41 deletions

View File

@ -262,27 +262,30 @@ gimp_eek (const gchar *reason,
break; break;
case GIMP_STACK_TRACE_QUERY: case GIMP_STACK_TRACE_QUERY:
{ /* Basically we don't have the "QUERY" case anymore, at least
sigset_t sigset; * for now until I figure out something.
*/
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;
case GIMP_STACK_TRACE_ALWAYS: case GIMP_STACK_TRACE_ALWAYS:
{ {
sigset_t sigset; #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;
sigemptyset (&sigset); g_snprintf (pid, 16, "%u", (guint) getpid ());
sigprocmask (SIG_SETMASK, &sigset, NULL); args[2] = pid;
g_on_error_stack_trace (full_prog_name); /* 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; break;

View File

@ -318,8 +318,7 @@ gui_message_error_dialog (Gimp *gimp,
dialog = global_critical_dialog (); dialog = global_critical_dialog ();
text = gui_message_format (severity, domain, message); text = gui_message_format (severity, domain, message);
gimp_critical_dialog_add (GIMP_CRITICAL_DIALOG (dialog), gimp_critical_dialog_add (dialog, text, trace, FALSE, NULL, 0);
text, trace);
g_free (text); g_free (text);
gtk_widget_show (dialog); gtk_widget_show (dialog);

View File

@ -18,32 +18,37 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * 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 "config.h"
#include <string.h> #include <string.h>
#include <gegl.h> #include <glib.h>
#include <gtk/gtk.h> #include <gtk/gtk.h>
#include "libgimpbase/gimpbase.h"
#include "libgimpwidgets/gimpwidgets.h"
#include "widgets-types.h"
#include "gimpcriticaldialog.h" #include "gimpcriticaldialog.h"
#include "version.h" #include "version.h"
#include "gimp-intl.h" #include "gimp-intl.h"
typedef struct _GimpCriticalDialog GimpCriticalDialog;
#define GIMP_CRITICAL_RESPONSE_CLIPBOARD 1 #define GIMP_CRITICAL_RESPONSE_CLIPBOARD 1
#define GIMP_CRITICAL_RESPONSE_URL 2 #define GIMP_CRITICAL_RESPONSE_URL 2
#define GIMP_CRITICAL_RESPONSE_RESTART 3
static void gimp_critical_dialog_response (GtkDialog *dialog, static void gimp_critical_dialog_finalize (GObject *object);
gint response_id); 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 #define parent_class gimp_critical_dialog_parent_class
@ -51,8 +56,11 @@ G_DEFINE_TYPE (GimpCriticalDialog, gimp_critical_dialog, GIMP_TYPE_DIALOG)
static void static void
gimp_critical_dialog_class_init (GimpCriticalDialogClass *klass) gimp_critical_dialog_class_init (GimpCriticalDialogClass *klass)
{ {
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass); GtkDialogClass *dialog_class = GTK_DIALOG_CLASS (klass);
object_class->finalize = gimp_critical_dialog_finalize;
dialog_class->response = gimp_critical_dialog_response; 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_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_CLOSE);
gtk_window_set_resizable (GTK_WINDOW (dialog), TRUE); 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))), gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
dialog->vbox, TRUE, TRUE, 0); dialog->vbox, TRUE, TRUE, 0);
gtk_widget_show (dialog->vbox); 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_text_view_set_editable (GTK_TEXT_VIEW (dialog->details), FALSE);
gtk_widget_show (dialog->details); gtk_widget_show (dialog->details);
gtk_container_add (GTK_CONTAINER (widget), 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 static void
@ -191,6 +213,17 @@ gimp_critical_dialog_response (GtkDialog *dialog,
NULL); NULL);
} }
break; 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_DELETE_EVENT:
case GTK_RESPONSE_CLOSE: case GTK_RESPONSE_CLOSE:
default: default:
@ -212,10 +245,14 @@ gimp_critical_dialog_new (const gchar *title)
} }
void void
gimp_critical_dialog_add (GimpCriticalDialog *dialog, gimp_critical_dialog_add (GtkWidget *dialog,
const gchar *message, const gchar *message,
const gchar *trace) const gchar *trace,
gboolean is_fatal,
const gchar *program,
pid_t pid)
{ {
GimpCriticalDialog *critical;
GtkTextBuffer *buffer; GtkTextBuffer *buffer;
GtkTextIter end; GtkTextIter end;
gchar *text; gchar *text;
@ -230,10 +267,16 @@ gimp_critical_dialog_add (GimpCriticalDialog *dialog,
*/ */
return; return;
} }
critical = GIMP_CRITICAL_DIALOG (dialog);
/* The user text, which should be localized. */ /* The user text, which should be localized. */
if (! gtk_label_get_text (GTK_LABEL (dialog->label)) || if (is_fatal)
strlen (gtk_label_get_text (GTK_LABEL (dialog->label))) == 0) {
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. */ /* First critical error. Let's just display it. */
text = g_strdup_printf (_("GIMP encountered a critical error: %s"), 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!")); 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); text);
g_free (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 * meant to go to clipboard for the bug report. It has to be in
* English. * 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); gtk_text_buffer_get_iter_at_offset (buffer, &end, -1);
text = g_strdup_printf ("\n\n> %s\n\nStack trace:\n%s", message, trace); text = g_strdup_printf ("\n\n> %s\n\nStack trace:\n%s", message, trace);
gtk_text_buffer_insert (buffer, &end, text, -1); gtk_text_buffer_insert (buffer, &end, text, -1);
g_free (text); 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;
}
} }

View File

@ -36,27 +36,32 @@ typedef struct _GimpCriticalDialogClass GimpCriticalDialogClass;
struct _GimpCriticalDialog struct _GimpCriticalDialog
{ {
GimpDialog parent_instance; GtkDialog parent_instance;
GtkWidget *vbox; GtkWidget *vbox;
GtkWidget *label; GtkWidget *label;
GtkWidget *details; GtkWidget *details;
gchar *program;
pid_t pid;
}; };
struct _GimpCriticalDialogClass struct _GimpCriticalDialogClass
{ {
GimpDialogClass parent_class; GtkDialogClass parent_class;
}; };
GType gimp_critical_dialog_get_type (void) G_GNUC_CONST; GType gimp_critical_dialog_get_type (void) G_GNUC_CONST;
GtkWidget * gimp_critical_dialog_new (const gchar *title); 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 *message,
const gchar *trace); const gchar *trace,
gboolean is_fatal,
const gchar *program,
pid_t pid);
G_END_DECLS G_END_DECLS

View File

@ -1,5 +1,7 @@
## Process this file with automake to produce Makefile.in ## Process this file with automake to produce Makefile.in
AUTOMAKE_OPTIONS = subdir-objects
libgimpbase = $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la libgimpbase = $(top_builddir)/libgimpbase/libgimpbase-$(GIMP_API_VERSION).la
if OS_WIN32 if OS_WIN32
@ -12,7 +14,7 @@ gimp_debug_resume_SOURCES = gimp-debug-resume.c
else else
bin_PROGRAMS = gimptool-2.0 bin_PROGRAMS = gimptool-2.0 gimpdebug-2.0
endif endif
@ -21,6 +23,24 @@ noinst_PROGRAMS = test-clipboard
EXTRA_PROGRAMS = \ EXTRA_PROGRAMS = \
kernelgen 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 \
-I$(top_builddir)/app \
$(AM_CPPFLAGS) \
$(GEGL_CFLAGS) \
$(GTK_CFLAGS)
gimpdebug_2_0_LDADD = \
$(GEGL_LIBS) \
$(GTK_LIBS)
gimptool_2_0_SOURCES = gimptool.c gimptool_2_0_SOURCES = gimptool.c

126
tools/gimpdebug.c Normal file
View File

@ -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;
}