GObject: substantially rework g_object_new()
[glib.git] / glib / gbacktrace.c
blob4d19d6fd0c2f40749ab6fc31c490e3caefd41d4c
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
29 * then
32 #include "config.h"
33 #include "glibconfig.h"
35 #include <signal.h>
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <stdlib.h>
40 #ifdef HAVE_SYS_TIME_H
41 #include <sys/time.h>
42 #endif
43 #ifdef HAVE_SYS_TIMES_H
44 #include <sys/times.h>
45 #endif
46 #include <sys/types.h>
47 #ifdef HAVE_SYS_WAIT_H
48 #include <sys/wait.h>
49 #endif
51 #include <time.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
56 #ifdef HAVE_SYS_SELECT_H
57 #include <sys/select.h>
58 #endif /* HAVE_SYS_SELECT_H */
60 #include <string.h> /* for bzero on BSD systems */
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 #include "gbacktrace.h"
71 #include "gtypes.h"
72 #include "gmain.h"
73 #include "gprintfint.h"
74 #include "gutils.h"
77 #ifndef NO_FD_SET
78 # define SELECT_MASK fd_set
79 #else
80 # if defined(_IBMR2)
81 # define SELECT_MASK void
82 # else
83 # define SELECT_MASK int
84 # endif
85 #endif
88 #ifndef G_OS_WIN32
89 static void stack_trace (char **args);
90 #endif
92 /* People want to hit this from their debugger... */
93 GLIB_AVAILABLE_IN_ALL volatile gboolean glib_on_error_halt;
94 volatile gboolean glib_on_error_halt = TRUE;
96 /**
97 * g_on_error_query:
98 * @prg_name: the program name, needed by <command>gdb</command>
99 * for the [S]tack trace option. If @prg_name is %NULL, g_get_prgname()
100 * is called to get the program name (which will work correctly if
101 * gdk_init() or gtk_init() has been called)
103 * Prompts the user with
104 * <computeroutput>[E]xit, [H]alt, show [S]tack trace or [P]roceed</computeroutput>.
105 * This function is intended to be used for debugging use only.
106 * The following example shows how it can be used together with
107 * the g_log() functions.
109 * |[
110 * &num;include &lt;glib.h&gt;
112 * static void
113 * log_handler (const gchar *log_domain,
114 * GLogLevelFlags log_level,
115 * const gchar *message,
116 * gpointer user_data)
118 * g_log_default_handler (log_domain, log_level, message, user_data);
120 * g_on_error_query (MY_PROGRAM_NAME);
123 * int
124 * main (int argc, char *argv[])
126 * g_log_set_handler (MY_LOG_DOMAIN,
127 * G_LOG_LEVEL_WARNING |
128 * G_LOG_LEVEL_ERROR |
129 * G_LOG_LEVEL_CRITICAL,
130 * log_handler,
131 * NULL);
132 * /&ast; ... &ast;/
133 * ]|
135 * If [E]xit is selected, the application terminates with a call
136 * to <literal>_exit(0)</literal>.
138 * If [S]tack trace is selected, g_on_error_stack_trace() is called.
139 * This invokes <command>gdb</command>, which attaches to the current
140 * process and shows a stack trace. The prompt is then shown again.
142 * If [P]roceed is selected, the function returns.
144 * This function may cause different actions on non-UNIX platforms.
146 void
147 g_on_error_query (const gchar *prg_name)
149 #ifndef G_OS_WIN32
150 static const gchar * const query1 = "[E]xit, [H]alt";
151 static const gchar * const query2 = ", show [S]tack trace";
152 static const gchar * const query3 = " or [P]roceed";
153 gchar buf[16];
155 if (!prg_name)
156 prg_name = g_get_prgname ();
158 retry:
160 if (prg_name)
161 _g_fprintf (stdout,
162 "%s (pid:%u): %s%s%s: ",
163 prg_name,
164 (guint) getpid (),
165 query1,
166 query2,
167 query3);
168 else
169 _g_fprintf (stdout,
170 "(process:%u): %s%s: ",
171 (guint) getpid (),
172 query1,
173 query3);
174 fflush (stdout);
176 if (isatty(0) && isatty(1))
177 fgets (buf, 8, stdin);
178 else
179 strcpy (buf, "E\n");
181 if ((buf[0] == 'E' || buf[0] == 'e')
182 && buf[1] == '\n')
183 _exit (0);
184 else if ((buf[0] == 'P' || buf[0] == 'p')
185 && buf[1] == '\n')
186 return;
187 else if (prg_name
188 && (buf[0] == 'S' || buf[0] == 's')
189 && buf[1] == '\n')
191 g_on_error_stack_trace (prg_name);
192 goto retry;
194 else if ((buf[0] == 'H' || buf[0] == 'h')
195 && buf[1] == '\n')
197 while (glib_on_error_halt)
199 glib_on_error_halt = TRUE;
200 return;
202 else
203 goto retry;
204 #else
205 if (!prg_name)
206 prg_name = g_get_prgname ();
208 MessageBox (NULL, "g_on_error_query called, program terminating",
209 (prg_name && *prg_name) ? prg_name : NULL,
210 MB_OK|MB_ICONERROR);
211 _exit(0);
212 #endif
216 * g_on_error_stack_trace:
217 * @prg_name: the program name, needed by <command>gdb</command>
218 * for the [S]tack trace option.
220 * Invokes <command>gdb</command>, which attaches to the current
221 * process and shows a stack trace. Called by g_on_error_query()
222 * when the [S]tack trace option is selected. You can get the current
223 * process's "program name" with g_get_prgname(), assuming that you
224 * have called gtk_init() or gdk_init().
226 * This function may cause different actions on non-UNIX platforms.
228 void
229 g_on_error_stack_trace (const gchar *prg_name)
231 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
232 pid_t pid;
233 gchar buf[16];
234 gchar *args[4] = { "gdb", NULL, NULL, NULL };
235 int status;
237 if (!prg_name)
238 return;
240 _g_sprintf (buf, "%u", (guint) getpid ());
242 args[1] = (gchar*) prg_name;
243 args[2] = buf;
245 pid = fork ();
246 if (pid == 0)
248 stack_trace (args);
249 _exit (0);
251 else if (pid == (pid_t) -1)
253 perror ("unable to fork gdb");
254 return;
257 waitpid (pid, &status, 0);
258 #else
259 if (IsDebuggerPresent ())
260 G_BREAKPOINT ();
261 else
262 abort ();
263 #endif
266 #ifndef G_OS_WIN32
268 static gboolean stack_trace_done = FALSE;
270 static void
271 stack_trace_sigchld (int signum)
273 stack_trace_done = TRUE;
276 static void
277 stack_trace (char **args)
279 pid_t pid;
280 int in_fd[2];
281 int out_fd[2];
282 SELECT_MASK fdset;
283 SELECT_MASK readset;
284 struct timeval tv;
285 int sel, idx, state;
286 char buffer[256];
287 char c;
289 stack_trace_done = FALSE;
290 signal (SIGCHLD, stack_trace_sigchld);
292 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
294 perror ("unable to open pipe");
295 _exit (0);
298 pid = fork ();
299 if (pid == 0)
301 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
302 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
303 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
305 execvp (args[0], args); /* exec gdb */
306 perror ("exec failed");
307 _exit (0);
309 else if (pid == (pid_t) -1)
311 perror ("unable to fork");
312 _exit (0);
315 FD_ZERO (&fdset);
316 FD_SET (out_fd[0], &fdset);
318 write (in_fd[1], "backtrace\n", 10);
319 write (in_fd[1], "p x = 0\n", 8);
320 write (in_fd[1], "quit\n", 5);
322 idx = 0;
323 state = 0;
325 while (1)
327 readset = fdset;
328 tv.tv_sec = 1;
329 tv.tv_usec = 0;
331 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
332 if (sel == -1)
333 break;
335 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
337 if (read (out_fd[0], &c, 1))
339 switch (state)
341 case 0:
342 if (c == '#')
344 state = 1;
345 idx = 0;
346 buffer[idx++] = c;
348 break;
349 case 1:
350 buffer[idx++] = c;
351 if ((c == '\n') || (c == '\r'))
353 buffer[idx] = 0;
354 _g_fprintf (stdout, "%s", buffer);
355 state = 0;
356 idx = 0;
358 break;
359 default:
360 break;
364 else if (stack_trace_done)
365 break;
368 close (in_fd[0]);
369 close (in_fd[1]);
370 close (out_fd[0]);
371 close (out_fd[1]);
372 _exit (0);
375 #endif /* !G_OS_WIN32 */