File: gimp-debug-tool.c

package info (click to toggle)
gimp 3.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 222,880 kB
  • sloc: ansic: 870,914; python: 10,965; lisp: 10,857; cpp: 7,355; perl: 4,536; sh: 1,753; xml: 972; yacc: 609; lex: 348; javascript: 150; makefile: 42
file content (134 lines) | stat: -rw-r--r-- 3,701 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* 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 <https://www.gnu.org/licenses/>.
 */

/*
 * GimpDebug simply displays a dialog with debug data (backtraces,
 * version, etc.), 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 to allocate memory
 * anymore. Therefore creating a new dialog is an impossible action.
 * So we call instead a separate program, then exit.
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <sys/stat.h>

#include <gio/gio.h>
#include <glib.h>
#include <glib/gi18n.h>
#ifdef G_OS_WIN32
#include <windows.h>
#endif

#include <gtk/gtk.h>

#include "app/widgets/gimpcriticaldialog.h"



int
main (int    argc,
      char **argv)
{
  const gchar *program;
  const gchar *pid;
  const gchar *reason;
  const gchar *message;
  const gchar *bt_file      = NULL;
  const gchar *last_version = NULL;
  const gchar *release_date = NULL;
  gchar       *trace        = NULL;
  gchar       *error;
  GtkWidget   *dialog;

#ifdef G_OS_WIN32
  if (AttachConsole (ATTACH_PARENT_PROCESS) != 0 && ! g_getenv ("TERM") && ! g_getenv ("SHELL"))
    {
      /* 'r' is needed to prevent interleaving and '+' to support colors */
      freopen ("CONOUT$", "r+", stdout);
      freopen ("CONOUT$", "r+", stderr);
      _flushall ();

      {
        /* CTRL+C handling */
        HANDLE hIn = GetStdHandle (STD_INPUT_HANDLE);
        SetConsoleMode (hIn, ENABLE_PROCESSED_INPUT);
      }
    }
#endif

  if (argc != 6 && argc != 8)
    {
      g_print ("Usage: gimp-debug-tool-%s [PROGRAM] [PID] [REASON] [MESSAGE] [BT_FILE] "
               "([LAST_VERSION] [RELEASE_TIMESTAMP])\n", GIMP_APP_VERSION);
      exit (EXIT_FAILURE);
    }

  program = argv[1];
  pid     = argv[2];
  reason  = argv[3];
  message = argv[4];

  error   = g_strdup_printf ("%s: %s", reason, message);

  bt_file = argv[5];
  g_file_get_contents (bt_file, &trace, NULL, NULL);

  if (trace == NULL || strlen (trace) == 0)
    exit (EXIT_FAILURE);

  if (argc == 8)
    {
      last_version = argv[6];
      release_date = argv[7];
    }

  gtk_init (&argc, &argv);

  dialog = gimp_critical_dialog_new (_("GIMP Crash Debug"), last_version,
                                     release_date ? g_ascii_strtoll (release_date, NULL, 10) : -1);
  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);
}

#ifdef G_OS_WIN32
int WINAPI
WinMain (HINSTANCE hInstance,
         HINSTANCE hPrevInstance,
         LPSTR     lpCmdLine,
         int       nCmdShow)
{
  return main (__argc, __argv);
}
#endif