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 Library 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 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library 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-1999. 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
41 #ifdef HAVE_SYS_TIME_H
44 #ifdef HAVE_SYS_TIMES_H
45 #include <sys/times.h>
47 #include <sys/types.h>
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif /* HAVE_SYS_SELECT_H */
59 #include <string.h> /* for bzero on BSD systems */
63 # define STRICT /* Strict typing, please */
65 # include <process.h> /* For _getpid() */
69 # define SELECT_MASK fd_set
72 # define SELECT_MASK void
74 # define SELECT_MASK int
79 static void stack_trace (char **args);
81 extern volatile gboolean glib_on_error_halt;
82 volatile gboolean glib_on_error_halt = TRUE;
85 g_on_error_query (const gchar *prg_name)
88 static const gchar *query1 = "[E]xit, [H]alt";
89 static const gchar *query2 = ", show [S]tack trace";
90 static const gchar *query3 = " or [P]roceed";
94 prg_name = g_get_prgname ();
100 "%s (pid:%u): %s%s%s: ",
108 "(process:%u): %s%s: ",
115 if (isatty(0) && isatty(1))
116 fgets (buf, 8, stdin);
120 fgets (buf, 8, stdin);
123 if ((buf[0] == 'E' || buf[0] == 'e')
126 else if ((buf[0] == 'P' || buf[0] == 'p')
130 && (buf[0] == 'S' || buf[0] == 's')
133 g_on_error_stack_trace (prg_name);
136 else if ((buf[0] == 'H' || buf[0] == 'h')
139 while (glib_on_error_halt)
141 glib_on_error_halt = TRUE;
148 prg_name = g_get_prgname ();
150 MessageBox (NULL, "Terminating",
151 (prg_name && *prg_name) ? prg_name : NULL,
158 g_on_error_stack_trace (const gchar *prg_name)
160 #if !defined(NATIVE_WIN32) && ! defined(GLIB_NATIVE_BEOS)
163 gchar *args[4] = { "gdb", NULL, NULL, NULL };
168 sprintf (buf, "%u", (guint) getpid ());
170 args[1] = (gchar*) prg_name;
179 else if (pid == (pid_t) -1)
181 perror ("unable to fork gdb");
185 while (glib_on_error_halt)
187 glib_on_error_halt = TRUE;
193 static gboolean stack_trace_done = FALSE;
196 stack_trace_sigchld (int signum)
198 stack_trace_done = TRUE;
202 stack_trace (char **args)
204 #if !defined(NATIVE_WIN32) && !defined(GLIB_NATIVE_BEOS)
211 int sel, index, state;
215 stack_trace_done = FALSE;
216 signal (SIGCHLD, stack_trace_sigchld);
218 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
220 perror ("unable to open pipe");
227 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
228 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
229 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
231 execvp (args[0], args); /* exec gdb */
232 perror ("exec failed");
235 else if (pid == (pid_t) -1)
237 perror ("unable to fork");
242 FD_SET (out_fd[0], &fdset);
244 write (in_fd[1], "backtrace\n", 10);
245 write (in_fd[1], "p x = 0\n", 8);
246 write (in_fd[1], "quit\n", 5);
257 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
261 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
263 if (read (out_fd[0], &c, 1))
277 if ((c == '\n') || (c == '\r'))
280 fprintf (stdout, "%s", buffer);
290 else if (stack_trace_done)