1 /* GLIB - Library of useful routines for C programming
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
28 * MT safe ; except for g_on_error_stack_trace, but who wants thread safety
33 #include "glibconfig.h"
40 #ifdef HAVE_SYS_TIME_H
43 #include <sys/types.h>
50 #ifdef HAVE_SYS_SELECT_H
51 #include <sys/select.h>
52 #endif /* HAVE_SYS_SELECT_H */
58 # define STRICT /* Strict typing, please */
59 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
66 #include "gbacktrace.h"
70 #include "gprintfint.h"
75 # define SELECT_MASK fd_set
78 # define SELECT_MASK void
80 # define SELECT_MASK int
86 static void stack_trace (char **args
);
89 /* People want to hit this from their debugger... */
90 GLIB_AVAILABLE_IN_ALL
volatile gboolean glib_on_error_halt
;
91 volatile gboolean glib_on_error_halt
= TRUE
;
95 * @prg_name: the program name, needed by <command>gdb</command>
96 * for the [S]tack trace option. If @prg_name is %NULL, g_get_prgname()
97 * is called to get the program name (which will work correctly if
98 * gdk_init() or gtk_init() has been called)
100 * Prompts the user with
101 * <computeroutput>[E]xit, [H]alt, show [S]tack trace or [P]roceed</computeroutput>.
102 * This function is intended to be used for debugging use only.
103 * The following example shows how it can be used together with
104 * the g_log() functions.
107 * #include <glib.h>
110 * log_handler (const gchar *log_domain,
111 * GLogLevelFlags log_level,
112 * const gchar *message,
113 * gpointer user_data)
115 * g_log_default_handler (log_domain, log_level, message, user_data);
117 * g_on_error_query (MY_PROGRAM_NAME);
121 * main (int argc, char *argv[])
123 * g_log_set_handler (MY_LOG_DOMAIN,
124 * G_LOG_LEVEL_WARNING |
125 * G_LOG_LEVEL_ERROR |
126 * G_LOG_LEVEL_CRITICAL,
132 * If [E]xit is selected, the application terminates with a call
133 * to <literal>_exit(0)</literal>.
135 * If [S]tack trace is selected, g_on_error_stack_trace() is called.
136 * This invokes <command>gdb</command>, which attaches to the current
137 * process and shows a stack trace. The prompt is then shown again.
139 * If [P]roceed is selected, the function returns.
141 * This function may cause different actions on non-UNIX platforms.
144 g_on_error_query (const gchar
*prg_name
)
147 static const gchar
* const query1
= "[E]xit, [H]alt";
148 static const gchar
* const query2
= ", show [S]tack trace";
149 static const gchar
* const query3
= " or [P]roceed";
153 prg_name
= g_get_prgname ();
159 "%s (pid:%u): %s%s%s: ",
167 "(process:%u): %s%s: ",
173 if (isatty(0) && isatty(1))
174 fgets (buf
, 8, stdin
);
178 if ((buf
[0] == 'E' || buf
[0] == 'e')
181 else if ((buf
[0] == 'P' || buf
[0] == 'p')
185 && (buf
[0] == 'S' || buf
[0] == 's')
188 g_on_error_stack_trace (prg_name
);
191 else if ((buf
[0] == 'H' || buf
[0] == 'h')
194 while (glib_on_error_halt
)
196 glib_on_error_halt
= TRUE
;
203 prg_name
= g_get_prgname ();
205 MessageBox (NULL
, "g_on_error_query called, program terminating",
206 (prg_name
&& *prg_name
) ? prg_name
: NULL
,
213 * g_on_error_stack_trace:
214 * @prg_name: the program name, needed by <command>gdb</command>
215 * for the [S]tack trace option.
217 * Invokes <command>gdb</command>, which attaches to the current
218 * process and shows a stack trace. Called by g_on_error_query()
219 * when the [S]tack trace option is selected. You can get the current
220 * process's "program name" with g_get_prgname(), assuming that you
221 * have called gtk_init() or gdk_init().
223 * This function may cause different actions on non-UNIX platforms.
226 g_on_error_stack_trace (const gchar
*prg_name
)
228 #if defined(G_OS_UNIX)
231 gchar
*args
[4] = { "gdb", NULL
, NULL
, NULL
};
237 _g_sprintf (buf
, "%u", (guint
) getpid ());
239 args
[1] = (gchar
*) prg_name
;
248 else if (pid
== (pid_t
) -1)
250 perror ("unable to fork gdb");
254 waitpid (pid
, &status
, 0);
256 if (IsDebuggerPresent ())
265 static gboolean stack_trace_done
= FALSE
;
268 stack_trace_sigchld (int signum
)
270 stack_trace_done
= TRUE
;
274 stack_trace (char **args
)
286 stack_trace_done
= FALSE
;
287 signal (SIGCHLD
, stack_trace_sigchld
);
289 if ((pipe (in_fd
) == -1) || (pipe (out_fd
) == -1))
291 perror ("unable to open pipe");
298 /* Save stderr for printing failure below */
299 int old_err
= dup (2);
300 fcntl (old_err
, F_SETFD
, fcntl (old_err
, F_GETFD
) | FD_CLOEXEC
);
302 close (0); dup (in_fd
[0]); /* set the stdin to the in pipe */
303 close (1); dup (out_fd
[1]); /* set the stdout to the out pipe */
304 close (2); dup (out_fd
[1]); /* set the stderr to the out pipe */
306 execvp (args
[0], args
); /* exec gdb */
308 /* Print failure to original stderr */
309 close (2); dup (old_err
);
310 perror ("exec gdb failed");
313 else if (pid
== (pid_t
) -1)
315 perror ("unable to fork");
320 FD_SET (out_fd
[0], &fdset
);
322 write (in_fd
[1], "backtrace\n", 10);
323 write (in_fd
[1], "p x = 0\n", 8);
324 write (in_fd
[1], "quit\n", 5);
335 sel
= select (FD_SETSIZE
, &readset
, NULL
, NULL
, &tv
);
339 if ((sel
> 0) && (FD_ISSET (out_fd
[0], &readset
)))
341 if (read (out_fd
[0], &c
, 1))
355 if ((c
== '\n') || (c
== '\r'))
358 _g_fprintf (stdout
, "%s", buffer
);
368 else if (stack_trace_done
)
379 #endif /* !G_OS_WIN32 */