[HQ](Debian) Package version up
[external/glib2.0.git] / glib / gbacktrace.c
1 /* GLIB - Library of useful routines for C programming
2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 /* 
28  * MT safe ; except for g_on_error_stack_trace, but who wants thread safety 
29  * then
30  */
31
32 #include "config.h"
33
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include "glib.h"
39 #include "gprintfint.h"
40
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
51
52 #include <time.h>
53 #ifdef HAVE_UNISTD_H
54 #include <unistd.h>
55 #endif
56
57 #ifdef HAVE_SYS_SELECT_H
58 #include <sys/select.h>
59 #endif /* HAVE_SYS_SELECT_H */
60
61 #include <string.h> /* for bzero on BSD systems */
62
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
69
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
79
80 #include "galias.h"
81
82 #ifndef G_OS_WIN32
83 static void stack_trace (char **args);
84 #endif
85
86 extern volatile gboolean glib_on_error_halt;
87 volatile gboolean glib_on_error_halt = TRUE;
88
89 void
90 g_on_error_query (const gchar *prg_name)
91 {
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];
97
98   if (!prg_name)
99     prg_name = g_get_prgname ();
100   
101  retry:
102   
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);
118   
119   if (isatty(0) && isatty(1))
120     fgets (buf, 8, stdin); 
121   else
122     strcpy (buf, "E\n");
123
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')
133     {
134       g_on_error_stack_trace (prg_name);
135       goto retry;
136     }
137   else if ((buf[0] == 'H' || buf[0] == 'h')
138            && buf[1] == '\n')
139     {
140       while (glib_on_error_halt)
141         ;
142       glib_on_error_halt = TRUE;
143       return;
144     }
145   else
146     goto retry;
147 #else
148   if (!prg_name)
149     prg_name = g_get_prgname ();
150   
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
156 }
157
158 void
159 g_on_error_stack_trace (const gchar *prg_name)
160 {
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;
166
167   if (!prg_name)
168     return;
169
170   _g_sprintf (buf, "%u", (guint) getpid ());
171
172   args[1] = (gchar*) prg_name;
173   args[2] = buf;
174
175   pid = fork ();
176   if (pid == 0)
177     {
178       stack_trace (args);
179       _exit (0);
180     }
181   else if (pid == (pid_t) -1)
182     {
183       perror ("unable to fork gdb");
184       return;
185     }
186
187   waitpid (pid, &status, 0);
188 #else
189   if (IsDebuggerPresent ())
190     G_BREAKPOINT ();
191   else
192     abort ();
193 #endif
194 }
195
196 #ifndef G_OS_WIN32
197
198 static gboolean stack_trace_done = FALSE;
199
200 static void
201 stack_trace_sigchld (int signum)
202 {
203   stack_trace_done = TRUE;
204 }
205
206 static void
207 stack_trace (char **args)
208 {
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, idx, state;
216   char buffer[256];
217   char c;
218
219   stack_trace_done = FALSE;
220   signal (SIGCHLD, stack_trace_sigchld);
221
222   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
223     {
224       perror ("unable to open pipe");
225       _exit (0);
226     }
227
228   pid = fork ();
229   if (pid == 0)
230     {
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 */
234
235       execvp (args[0], args);      /* exec gdb */
236       perror ("exec failed");
237       _exit (0);
238     }
239   else if (pid == (pid_t) -1)
240     {
241       perror ("unable to fork");
242       _exit (0);
243     }
244
245   FD_ZERO (&fdset);
246   FD_SET (out_fd[0], &fdset);
247
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);
251
252   idx = 0;
253   state = 0;
254
255   while (1)
256     {
257       readset = fdset;
258       tv.tv_sec = 1;
259       tv.tv_usec = 0;
260
261       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
262       if (sel == -1)
263         break;
264
265       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
266         {
267           if (read (out_fd[0], &c, 1))
268             {
269               switch (state)
270                 {
271                 case 0:
272                   if (c == '#')
273                     {
274                       state = 1;
275                       idx = 0;
276                       buffer[idx++] = c;
277                     }
278                   break;
279                 case 1:
280                   buffer[idx++] = c;
281                   if ((c == '\n') || (c == '\r'))
282                     {
283                       buffer[idx] = 0;
284                       _g_fprintf (stdout, "%s", buffer);
285                       state = 0;
286                       idx = 0;
287                     }
288                   break;
289                 default:
290                   break;
291                 }
292             }
293         }
294       else if (stack_trace_done)
295         break;
296     }
297
298   close (in_fd[0]);
299   close (in_fd[1]);
300   close (out_fd[0]);
301   close (out_fd[1]);
302   _exit (0);
303 }
304
305 #endif /* !G_OS_WIN32 */
306
307 #define __G_BACKTRACE_C__
308 #include "galiasdef.c"