Mention 64bit integer types.
[glib.git] / glib / gbacktrace.c
blobac30b00e942886406c1dd02c67cbac0629887e27
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/.
27 /*
28 * MT safe ; except for g_on_error_stack_trace, but who wants thread safety
29 * then
32 #include "config.h"
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include "glib.h"
39 #include "gprintfint.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 G_OS_WIN32
63 # define STRICT /* Strict typing, please */
64 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
65 # include <windows.h>
66 # undef STRICT
67 #endif
69 #ifndef NO_FD_SET
70 # define SELECT_MASK fd_set
71 #else
72 # if defined(_IBMR2)
73 # define SELECT_MASK void
74 # else
75 # define SELECT_MASK int
76 # endif
77 #endif
79 #include "galias.h"
81 #ifndef G_OS_WIN32
82 static void stack_trace (char **args);
83 #endif
85 extern volatile gboolean glib_on_error_halt;
86 volatile gboolean glib_on_error_halt = TRUE;
88 void
89 g_on_error_query (const gchar *prg_name)
91 #ifndef G_OS_WIN32
92 static const gchar *query1 = "[E]xit, [H]alt";
93 static const gchar *query2 = ", show [S]tack trace";
94 static const gchar *query3 = " or [P]roceed";
95 gchar buf[16];
97 if (!prg_name)
98 prg_name = g_get_prgname ();
100 retry:
102 if (prg_name)
103 _g_fprintf (stdout,
104 "%s (pid:%u): %s%s%s: ",
105 prg_name,
106 (guint) getpid (),
107 query1,
108 query2,
109 query3);
110 else
111 _g_fprintf (stdout,
112 "(process:%u): %s%s: ",
113 (guint) getpid (),
114 query1,
115 query3);
116 fflush (stdout);
118 if (isatty(0) && isatty(1))
119 fgets (buf, 8, stdin);
120 else
121 strcpy (buf, "E\n");
123 if ((buf[0] == 'E' || buf[0] == 'e')
124 && buf[1] == '\n')
125 _exit (0);
126 else if ((buf[0] == 'P' || buf[0] == 'p')
127 && buf[1] == '\n')
128 return;
129 else if (prg_name
130 && (buf[0] == 'S' || buf[0] == 's')
131 && buf[1] == '\n')
133 g_on_error_stack_trace (prg_name);
134 goto retry;
136 else if ((buf[0] == 'H' || buf[0] == 'h')
137 && buf[1] == '\n')
139 while (glib_on_error_halt)
141 glib_on_error_halt = TRUE;
142 return;
144 else
145 goto retry;
146 #else
147 if (!prg_name)
148 prg_name = g_get_prgname ();
150 MessageBox (NULL, "g_on_error_query called, program terminating",
151 (prg_name && *prg_name) ? prg_name : NULL,
152 MB_OK|MB_ICONERROR);
153 _exit(0);
154 #endif
157 void
158 g_on_error_stack_trace (const gchar *prg_name)
160 #ifdef G_OS_UNIX
161 pid_t pid;
162 gchar buf[16];
163 gchar *args[4] = { "gdb", NULL, NULL, NULL };
165 if (!prg_name)
166 return;
168 _g_sprintf (buf, "%u", (guint) getpid ());
170 args[1] = (gchar*) prg_name;
171 args[2] = buf;
173 pid = fork ();
174 if (pid == 0)
176 stack_trace (args);
177 _exit (0);
179 else if (pid == (pid_t) -1)
181 perror ("unable to fork gdb");
182 return;
185 while (glib_on_error_halt)
187 glib_on_error_halt = TRUE;
188 #else
189 if (IsDebuggerPresent ())
190 G_BREAKPOINT ();
191 else
192 abort ();
193 #endif
196 #ifndef G_OS_WIN32
198 static gboolean stack_trace_done = FALSE;
200 static void
201 stack_trace_sigchld (int signum)
203 stack_trace_done = TRUE;
206 static void
207 stack_trace (char **args)
209 pid_t pid;
210 int in_fd[2];
211 int out_fd[2];
212 SELECT_MASK fdset;
213 SELECT_MASK readset;
214 struct timeval tv;
215 int sel, index, state;
216 char buffer[256];
217 char c;
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");
225 _exit (0);
228 pid = fork ();
229 if (pid == 0)
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");
237 _exit (0);
239 else if (pid == (pid_t) -1)
241 perror ("unable to fork");
242 _exit (0);
245 FD_ZERO (&fdset);
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);
252 index = 0;
253 state = 0;
255 while (1)
257 readset = fdset;
258 tv.tv_sec = 1;
259 tv.tv_usec = 0;
261 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
262 if (sel == -1)
263 break;
265 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
267 if (read (out_fd[0], &c, 1))
269 switch (state)
271 case 0:
272 if (c == '#')
274 state = 1;
275 index = 0;
276 buffer[index++] = c;
278 break;
279 case 1:
280 buffer[index++] = c;
281 if ((c == '\n') || (c == '\r'))
283 buffer[index] = 0;
284 _g_fprintf (stdout, "%s", buffer);
285 state = 0;
286 index = 0;
288 break;
289 default:
290 break;
294 else if (stack_trace_done)
295 break;
298 close (in_fd[0]);
299 close (in_fd[1]);
300 close (out_fd[0]);
301 close (out_fd[1]);
302 _exit (0);
305 #endif /* !G_OS_WIN32 */
307 #define __G_BACKTRACE_C__
308 #include "galiasdef.c"