Drop the GMenu markup functions
[glib.git] / glib / gbacktrace.c
blob957c482b90b6e0763108b0cccae2c1f0e1125258
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 extern volatile gboolean glib_on_error_halt;
93 volatile gboolean glib_on_error_halt = TRUE;
95 /**
96 * g_on_error_query:
97 * @prg_name: the program name, needed by <command>gdb</command>
98 * for the [S]tack trace option. If @prg_name is %NULL, g_get_prgname()
99 * is called to get the program name (which will work correctly if
100 * gdk_init() or gtk_init() has been called)
102 * Prompts the user with
103 * <computeroutput>[E]xit, [H]alt, show [S]tack trace or [P]roceed</computeroutput>.
104 * This function is intended to be used for debugging use only.
105 * The following example shows how it can be used together with
106 * the g_log() functions.
108 * |[
109 * &num;include &lt;glib.h&gt;
111 * static void
112 * log_handler (const gchar *log_domain,
113 * GLogLevelFlags log_level,
114 * const gchar *message,
115 * gpointer user_data)
117 * g_log_default_handler (log_domain, log_level, message, user_data);
119 * g_on_error_query (MY_PROGRAM_NAME);
122 * int
123 * main (int argc, char *argv[])
125 * g_log_set_handler (MY_LOG_DOMAIN,
126 * G_LOG_LEVEL_WARNING |
127 * G_LOG_LEVEL_ERROR |
128 * G_LOG_LEVEL_CRITICAL,
129 * log_handler,
130 * NULL);
131 * /&ast; ... &ast;/
132 * ]|
134 * If [E]xit is selected, the application terminates with a call
135 * to <literal>_exit(0)</literal>.
137 * If [S]tack trace is selected, g_on_error_stack_trace() is called.
138 * This invokes <command>gdb</command>, which attaches to the current
139 * process and shows a stack trace. The prompt is then shown again.
141 * If [P]roceed is selected, the function returns.
143 * This function may cause different actions on non-UNIX platforms.
145 void
146 g_on_error_query (const gchar *prg_name)
148 #ifndef G_OS_WIN32
149 static const gchar * const query1 = "[E]xit, [H]alt";
150 static const gchar * const query2 = ", show [S]tack trace";
151 static const gchar * const query3 = " or [P]roceed";
152 gchar buf[16];
154 if (!prg_name)
155 prg_name = g_get_prgname ();
157 retry:
159 if (prg_name)
160 _g_fprintf (stdout,
161 "%s (pid:%u): %s%s%s: ",
162 prg_name,
163 (guint) getpid (),
164 query1,
165 query2,
166 query3);
167 else
168 _g_fprintf (stdout,
169 "(process:%u): %s%s: ",
170 (guint) getpid (),
171 query1,
172 query3);
173 fflush (stdout);
175 if (isatty(0) && isatty(1))
176 fgets (buf, 8, stdin);
177 else
178 strcpy (buf, "E\n");
180 if ((buf[0] == 'E' || buf[0] == 'e')
181 && buf[1] == '\n')
182 _exit (0);
183 else if ((buf[0] == 'P' || buf[0] == 'p')
184 && buf[1] == '\n')
185 return;
186 else if (prg_name
187 && (buf[0] == 'S' || buf[0] == 's')
188 && buf[1] == '\n')
190 g_on_error_stack_trace (prg_name);
191 goto retry;
193 else if ((buf[0] == 'H' || buf[0] == 'h')
194 && buf[1] == '\n')
196 while (glib_on_error_halt)
198 glib_on_error_halt = TRUE;
199 return;
201 else
202 goto retry;
203 #else
204 if (!prg_name)
205 prg_name = g_get_prgname ();
207 MessageBox (NULL, "g_on_error_query called, program terminating",
208 (prg_name && *prg_name) ? prg_name : NULL,
209 MB_OK|MB_ICONERROR);
210 _exit(0);
211 #endif
215 * g_on_error_stack_trace:
216 * @prg_name: the program name, needed by <command>gdb</command>
217 * for the [S]tack trace option.
219 * Invokes <command>gdb</command>, which attaches to the current
220 * process and shows a stack trace. Called by g_on_error_query()
221 * when the [S]tack trace option is selected. You can get the current
222 * process's "program name" with g_get_prgname(), assuming that you
223 * have called gtk_init() or gdk_init().
225 * This function may cause different actions on non-UNIX platforms.
227 void
228 g_on_error_stack_trace (const gchar *prg_name)
230 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
231 pid_t pid;
232 gchar buf[16];
233 gchar *args[4] = { "gdb", NULL, NULL, NULL };
234 int status;
236 if (!prg_name)
237 return;
239 _g_sprintf (buf, "%u", (guint) getpid ());
241 args[1] = (gchar*) prg_name;
242 args[2] = buf;
244 pid = fork ();
245 if (pid == 0)
247 stack_trace (args);
248 _exit (0);
250 else if (pid == (pid_t) -1)
252 perror ("unable to fork gdb");
253 return;
256 waitpid (pid, &status, 0);
257 #else
258 if (IsDebuggerPresent ())
259 G_BREAKPOINT ();
260 else
261 abort ();
262 #endif
265 #ifndef G_OS_WIN32
267 static gboolean stack_trace_done = FALSE;
269 static void
270 stack_trace_sigchld (int signum)
272 stack_trace_done = TRUE;
275 static void
276 stack_trace (char **args)
278 pid_t pid;
279 int in_fd[2];
280 int out_fd[2];
281 SELECT_MASK fdset;
282 SELECT_MASK readset;
283 struct timeval tv;
284 int sel, idx, state;
285 char buffer[256];
286 char c;
288 stack_trace_done = FALSE;
289 signal (SIGCHLD, stack_trace_sigchld);
291 if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
293 perror ("unable to open pipe");
294 _exit (0);
297 pid = fork ();
298 if (pid == 0)
300 close (0); dup (in_fd[0]); /* set the stdin to the in pipe */
301 close (1); dup (out_fd[1]); /* set the stdout to the out pipe */
302 close (2); dup (out_fd[1]); /* set the stderr to the out pipe */
304 execvp (args[0], args); /* exec gdb */
305 perror ("exec failed");
306 _exit (0);
308 else if (pid == (pid_t) -1)
310 perror ("unable to fork");
311 _exit (0);
314 FD_ZERO (&fdset);
315 FD_SET (out_fd[0], &fdset);
317 write (in_fd[1], "backtrace\n", 10);
318 write (in_fd[1], "p x = 0\n", 8);
319 write (in_fd[1], "quit\n", 5);
321 idx = 0;
322 state = 0;
324 while (1)
326 readset = fdset;
327 tv.tv_sec = 1;
328 tv.tv_usec = 0;
330 sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
331 if (sel == -1)
332 break;
334 if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
336 if (read (out_fd[0], &c, 1))
338 switch (state)
340 case 0:
341 if (c == '#')
343 state = 1;
344 idx = 0;
345 buffer[idx++] = c;
347 break;
348 case 1:
349 buffer[idx++] = c;
350 if ((c == '\n') || (c == '\r'))
352 buffer[idx] = 0;
353 _g_fprintf (stdout, "%s", buffer);
354 state = 0;
355 idx = 0;
357 break;
358 default:
359 break;
363 else if (stack_trace_done)
364 break;
367 close (in_fd[0]);
368 close (in_fd[1]);
369 close (out_fd[0]);
370 close (out_fd[1]);
371 _exit (0);
374 #endif /* !G_OS_WIN32 */