Update the pthreads snapshot version we want. Advice how to hand-expand
[glib.git] / gerror.c
blob6e6e74f66fd2a6589ac3e3e081bc568cb2db73db
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/.
27 /*
28 * MT safe ; except for g_on_error_stack_trace, but who wants thread safety
29 * then
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "glib.h"
41 #ifdef HAVE_SYS_TIME_H
42 #include <sys/time.h>
43 #endif
44 #ifdef HAVE_SYS_TIMES_H
45 #include <sys/times.h>
46 #endif
47 #include <sys/types.h>
49 #include <time.h>
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif /* HAVE_SYS_SELECT_H */
58 #ifdef STDC_HEADERS
59 #include <string.h> /* for bzero on BSD systems */
60 #endif
62 #ifdef NATIVE_WIN32
63 # define STRICT /* Strict typing, please */
64 # include <windows.h>
65 # include <process.h> /* For _getpid() */
66 #endif
68 #ifndef NO_FD_SET
69 # define SELECT_MASK fd_set
70 #else
71 # if defined(_IBMR2)
72 # define SELECT_MASK void
73 # else
74 # define SELECT_MASK int
75 # endif
76 #endif
79 static void stack_trace (char **args);
81 extern volatile gboolean glib_on_error_halt;
82 volatile gboolean glib_on_error_halt = TRUE;
84 void
85 g_on_error_query (const gchar *prg_name)
87 #ifndef NATIVE_WIN32
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";
91 gchar buf[16];
93 if (!prg_name)
94 prg_name = g_get_prgname ();
96 retry:
98 if (prg_name)
99 fprintf (stdout,
100 "%s (pid:%u): %s%s%s: ",
101 prg_name,
102 (guint) getpid (),
103 query1,
104 query2,
105 query3);
106 else
107 fprintf (stdout,
108 "(process:%u): %s%s: ",
109 (guint) getpid (),
110 query1,
111 query3);
112 fflush (stdout);
114 fgets (buf, 8, stdin);
116 if ((buf[0] == 'E' || buf[0] == 'e')
117 && buf[1] == '\n')
118 _exit (0);
119 else if ((buf[0] == 'P' || buf[0] == 'p')
120 && buf[1] == '\n')
121 return;
122 else if (prg_name
123 && (buf[0] == 'S' || buf[0] == 's')
124 && buf[1] == '\n')
126 g_on_error_stack_trace (prg_name);
127 goto retry;
129 else if ((buf[0] == 'H' || buf[0] == 'h')
130 && buf[1] == '\n')
132 while (glib_on_error_halt)
134 glib_on_error_halt = TRUE;
135 return;
137 else
138 goto retry;
139 #else
140 if (!prg_name)
141 prg_name = g_get_prgname ();
143 MessageBox (NULL, "Terminating",
144 (prg_name && *prg_name) ? prg_name : NULL,
145 MB_OK|MB_ICONERROR);
146 _exit(0);
147 #endif
150 void
151 g_on_error_stack_trace (const gchar *prg_name)
153 #if !defined(NATIVE_WIN32) && ! defined(GLIB_NATIVE_BEOS)
154 pid_t pid;
155 gchar buf[16];
156 gchar *args[4] = { "gdb", NULL, NULL, NULL };
158 if (!prg_name)
159 return;
161 sprintf (buf, "%u", (guint) getpid ());
163 args[1] = (gchar*) prg_name;
164 args[2] = buf;
166 pid = fork ();
167 if (pid == 0)
169 stack_trace (args);
170 _exit (0);
172 else if (pid == (pid_t) -1)
174 perror ("unable to fork gdb");
175 return;
178 while (glib_on_error_halt)
180 glib_on_error_halt = TRUE;
181 #else
182 abort ();
183 #endif
186 static gboolean stack_trace_done = FALSE;
188 static void
189 stack_trace_sigchld (int signum)
191 stack_trace_done = TRUE;
194 static void
195 stack_trace (char **args)
197 #if !defined(NATIVE_WIN32) && !defined(GLIB_NATIVE_BEOS)
198 pid_t pid;
199 int in_fd[2];
200 int out_fd[2];
201 SELECT_MASK fdset;
202 SELECT_MASK readset;
203 struct timeval tv;
204 int sel, index, state;
205 char buffer[256];
206 char c;
208 stack_trace_done = FALSE;
209 signal (SIGCHLD, stack_trace_sigchld);
211 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
213 perror ("unable to open pipe");
214 _exit (0);
217 pid = fork ();
218 if (pid == 0)
220 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
221 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
222 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
224 execvp (args[0], args); /* exec gdb */
225 perror ("exec failed");
226 _exit (0);
228 else if (pid == (pid_t) -1)
230 perror ("unable to fork");
231 _exit (0);
234 FD_ZERO (&fdset);
235 FD_SET (out_fd[0], &fdset);
237 write (in_fd[1], "backtrace\n", 10);
238 write (in_fd[1], "p x = 0\n", 8);
239 write (in_fd[1], "quit\n", 5);
241 index = 0;
242 state = 0;
244 while (1)
246 readset = fdset;
247 tv.tv_sec = 1;
248 tv.tv_usec = 0;
250 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
251 if (sel == -1)
252 break;
254 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
256 if (read (out_fd[0], &c, 1))
258 switch (state)
260 case 0:
261 if (c == '#')
263 state = 1;
264 index = 0;
265 buffer[index++] = c;
267 break;
268 case 1:
269 buffer[index++] = c;
270 if ((c == '\n') || (c == '\r'))
272 buffer[index] = 0;
273 fprintf (stdout, "%s", buffer);
274 state = 0;
275 index = 0;
277 break;
278 default:
279 break;
283 else if (stack_trace_done)
284 break;
287 close (in_fd[0]);
288 close (in_fd[1]);
289 close (out_fd[0]);
290 close (out_fd[1]);
291 _exit (0);
292 #else
293 abort ();
294 #endif