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.
24 #include <sys/times.h>
25 #include <sys/types.h>
31 #ifdef HAVE_SYS_SELECT_H
32 #include <sys/select.h>
33 #endif /* HAVE_SYS_SELECT_H */
36 #include <string.h> /* for bzero on BSD systems */
41 # define SELECT_MASK fd_set
47 # define SELECT_MASK void
49 # define SELECT_MASK int
54 static void stack_trace (char **args);
56 extern volatile gboolean glib_on_error_halt;
57 volatile gboolean glib_on_error_halt = TRUE;
60 g_on_error_query (const gchar *prg_name)
62 static const gchar *query1 = "[E]xit, [H]alt";
63 static const gchar *query2 = ", show [S]tack trace";
64 static const gchar *query3 = " or [P]roceed";
68 prg_name = g_get_prgname ();
74 "%s (pid:%u): %s%s%s: ",
82 "(process:%u): %s%s: ",
88 fgets (buf, 8, stdin);
90 if ((buf[0] == 'E' || buf[0] == 'e')
93 else if ((buf[0] == 'P' || buf[0] == 'p')
97 && (buf[0] == 'S' || buf[0] == 's')
100 g_on_error_stack_trace (prg_name);
103 else if ((buf[0] == 'H' || buf[0] == 'h')
106 while (glib_on_error_halt)
108 glib_on_error_halt = TRUE;
116 g_on_error_stack_trace (const gchar *prg_name)
120 gchar *args[4] = { "gdb", NULL, NULL, NULL };
125 sprintf (buf, "%u", (guint) getpid ());
127 args[1] = (gchar*) prg_name;
136 else if (pid == (pid_t) -1)
138 perror ("unable to fork gdb");
142 while (glib_on_error_halt)
144 glib_on_error_halt = TRUE;
147 static gboolean stack_trace_done = FALSE;
150 stack_trace_sigchld (int signum)
152 stack_trace_done = TRUE;
156 stack_trace (char **args)
164 int sel, index, state;
168 stack_trace_done = FALSE;
169 signal (SIGCHLD, stack_trace_sigchld);
171 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
173 perror ("unable to open pipe");
180 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
181 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
182 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
184 execvp (args[0], args); /* exec gdb */
185 perror ("exec failed");
188 else if (pid == (pid_t) -1)
190 perror ("unable to fork");
195 FD_SET (out_fd[0], &fdset);
197 write (in_fd[1], "backtrace\n", 10);
198 write (in_fd[1], "p x = 0\n", 8);
199 write (in_fd[1], "quit\n", 5);
210 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
214 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
216 if (read (out_fd[0], &c, 1))
230 if ((c == '\n') || (c == '\r'))
233 fprintf (stdout, "%s", buffer);
243 else if (stack_trace_done)