Move docs inline, and improve wording. (#372598, Behdad Esfahbod)
[glib.git] / glib / gbacktrace.c
blob9ff26eea0221eacba1a7d3e8cc82f216c1664174
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>
48 #ifdef HAVE_SYS_WAIT_H
49 #include <sys/wait.h>
50 #endif
52 #include <time.h>
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
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 */
63 #ifdef G_OS_WIN32
64 # define STRICT /* Strict typing, please */
65 # define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
66 # include <windows.h>
67 # undef STRICT
68 #endif
70 #ifndef NO_FD_SET
71 # define SELECT_MASK fd_set
72 #else
73 # if defined(_IBMR2)
74 # define SELECT_MASK void
75 # else
76 # define SELECT_MASK int
77 # endif
78 #endif
80 #include "galias.h"
82 #ifndef G_OS_WIN32
83 static void stack_trace (char **args);
84 #endif
86 extern volatile gboolean glib_on_error_halt;
87 volatile gboolean glib_on_error_halt = TRUE;
89 void
90 g_on_error_query (const gchar *prg_name)
92 #ifndef G_OS_WIN32
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";
96 gchar buf[16];
98 if (!prg_name)
99 prg_name = g_get_prgname ();
101 retry:
103 if (prg_name)
104 _g_fprintf (stdout,
105 "%s (pid:%u): %s%s%s: ",
106 prg_name,
107 (guint) getpid (),
108 query1,
109 query2,
110 query3);
111 else
112 _g_fprintf (stdout,
113 "(process:%u): %s%s: ",
114 (guint) getpid (),
115 query1,
116 query3);
117 fflush (stdout);
119 if (isatty(0) && isatty(1))
120 fgets (buf, 8, stdin);
121 else
122 strcpy (buf, "E\n");
124 if ((buf[0] == 'E' || buf[0] == 'e')
125 && buf[1] == '\n')
126 _exit (0);
127 else if ((buf[0] == 'P' || buf[0] == 'p')
128 && buf[1] == '\n')
129 return;
130 else if (prg_name
131 && (buf[0] == 'S' || buf[0] == 's')
132 && buf[1] == '\n')
134 g_on_error_stack_trace (prg_name);
135 goto retry;
137 else if ((buf[0] == 'H' || buf[0] == 'h')
138 && buf[1] == '\n')
140 while (glib_on_error_halt)
142 glib_on_error_halt = TRUE;
143 return;
145 else
146 goto retry;
147 #else
148 if (!prg_name)
149 prg_name = g_get_prgname ();
151 MessageBox (NULL, "g_on_error_query called, program terminating",
152 (prg_name && *prg_name) ? prg_name : NULL,
153 MB_OK|MB_ICONERROR);
154 _exit(0);
155 #endif
158 void
159 g_on_error_stack_trace (const gchar *prg_name)
161 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
162 pid_t pid;
163 gchar buf[16];
164 gchar *args[4] = { "gdb", NULL, NULL, NULL };
165 int status;
167 if (!prg_name)
168 return;
170 _g_sprintf (buf, "%u", (guint) getpid ());
172 args[1] = (gchar*) prg_name;
173 args[2] = buf;
175 pid = fork ();
176 if (pid == 0)
178 stack_trace (args);
179 _exit (0);
181 else if (pid == (pid_t) -1)
183 perror ("unable to fork gdb");
184 return;
187 waitpid (pid, &status, 0);
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"