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
39 #include "gprintfint.h"
41 #ifdef HAVE_SYS_TIME_H
44 #ifdef HAVE_SYS_TIMES_H
45 #include <sys/times.h>
47 #include <sys/types.h>
48 #ifdef HAVE_SYS_WAIT_H
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif /* HAVE_SYS_SELECT_H */
61 #include <string.h> /* for bzero on BSD systems */
64 # define STRICT /* Strict typing, please */
65 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
71 # define SELECT_MASK fd_set
74 # define SELECT_MASK void
76 # define SELECT_MASK int
83 static void stack_trace (char **args
);
86 extern volatile gboolean glib_on_error_halt
;
87 volatile gboolean glib_on_error_halt
= TRUE
;
90 g_on_error_query (const gchar
*prg_name
)
93 static const gchar
* const query1
= "[E]xit, [H]alt";
94 static const gchar
* const query2
= ", show [S]tack trace";
95 static const gchar
* const query3
= " or [P]roceed";
99 prg_name
= g_get_prgname ();
105 "%s (pid:%u): %s%s%s: ",
113 "(process:%u): %s%s: ",
119 if (isatty(0) && isatty(1))
120 fgets (buf
, 8, stdin
);
124 if ((buf
[0] == 'E' || buf
[0] == 'e')
127 else if ((buf
[0] == 'P' || buf
[0] == 'p')
131 && (buf
[0] == 'S' || buf
[0] == 's')
134 g_on_error_stack_trace (prg_name
);
137 else if ((buf
[0] == 'H' || buf
[0] == 'h')
140 while (glib_on_error_halt
)
142 glib_on_error_halt
= TRUE
;
149 prg_name
= g_get_prgname ();
151 MessageBox (NULL
, "g_on_error_query called, program terminating",
152 (prg_name
&& *prg_name
) ? prg_name
: NULL
,
159 g_on_error_stack_trace (const gchar
*prg_name
)
161 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
164 gchar
*args
[4] = { "gdb", NULL
, NULL
, NULL
};
170 _g_sprintf (buf
, "%u", (guint
) getpid ());
172 args
[1] = (gchar
*) prg_name
;
181 else if (pid
== (pid_t
) -1)
183 perror ("unable to fork gdb");
187 waitpid (pid
, &status
, 0);
189 if (IsDebuggerPresent ())
198 static gboolean stack_trace_done
= FALSE
;
201 stack_trace_sigchld (int signum
)
203 stack_trace_done
= TRUE
;
207 stack_trace (char **args
)
215 int sel
, index
, state
;
219 stack_trace_done
= FALSE
;
220 signal (SIGCHLD
, stack_trace_sigchld
);
222 if ((pipe (in_fd
) == -1) || (pipe (out_fd
) == -1))
224 perror ("unable to open pipe");
231 close (0); dup (in_fd
[0]); /* set the stdin to the in pipe */
232 close (1); dup (out_fd
[1]); /* set the stdout to the out pipe */
233 close (2); dup (out_fd
[1]); /* set the stderr to the out pipe */
235 execvp (args
[0], args
); /* exec gdb */
236 perror ("exec failed");
239 else if (pid
== (pid_t
) -1)
241 perror ("unable to fork");
246 FD_SET (out_fd
[0], &fdset
);
248 write (in_fd
[1], "backtrace\n", 10);
249 write (in_fd
[1], "p x = 0\n", 8);
250 write (in_fd
[1], "quit\n", 5);
261 sel
= select (FD_SETSIZE
, &readset
, NULL
, NULL
, &tv
);
265 if ((sel
> 0) && (FD_ISSET (out_fd
[0], &readset
)))
267 if (read (out_fd
[0], &c
, 1))
281 if ((c
== '\n') || (c
== '\r'))
284 _g_fprintf (stdout
, "%s", buffer
);
294 else if (stack_trace_done
)
305 #endif /* !G_OS_WIN32 */
307 #define __G_BACKTRACE_C__
308 #include "galiasdef.c"