Apply a patch which may make GLib work on BeOS again. (#309157, Kian
[platform/upstream/glib.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
49 #include <time.h>
50 #ifdef HAVE_UNISTD_H
51 #include <unistd.h>
52 #endif
53
54 #ifdef HAVE_SYS_SELECT_H
55 #include <sys/select.h>
56 #endif /* HAVE_SYS_SELECT_H */
57
58 #ifdef STDC_HEADERS
59 #include <string.h> /* for bzero on BSD systems */
60 #endif
61
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
68
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
78
79 #include "galias.h"
80
81 #ifndef G_OS_WIN32
82 static void stack_trace (char **args);
83 #endif
84
85 extern volatile gboolean glib_on_error_halt;
86 volatile gboolean glib_on_error_halt = TRUE;
87
88 void
89 g_on_error_query (const gchar *prg_name)
90 {
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];
96
97   if (!prg_name)
98     prg_name = g_get_prgname ();
99   
100  retry:
101   
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);
117   
118   if (isatty(0) && isatty(1))
119     fgets (buf, 8, stdin); 
120   else
121     strcpy (buf, "E\n");
122
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')
132     {
133       g_on_error_stack_trace (prg_name);
134       goto retry;
135     }
136   else if ((buf[0] == 'H' || buf[0] == 'h')
137            && buf[1] == '\n')
138     {
139       while (glib_on_error_halt)
140         ;
141       glib_on_error_halt = TRUE;
142       return;
143     }
144   else
145     goto retry;
146 #else
147   if (!prg_name)
148     prg_name = g_get_prgname ();
149   
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
155 }
156
157 void
158 g_on_error_stack_trace (const gchar *prg_name)
159 {
160 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
161   pid_t pid;
162   gchar buf[16];
163   gchar *args[4] = { "gdb", NULL, NULL, NULL };
164
165   if (!prg_name)
166     return;
167
168   _g_sprintf (buf, "%u", (guint) getpid ());
169
170   args[1] = (gchar*) prg_name;
171   args[2] = buf;
172
173   pid = fork ();
174   if (pid == 0)
175     {
176       stack_trace (args);
177       _exit (0);
178     }
179   else if (pid == (pid_t) -1)
180     {
181       perror ("unable to fork gdb");
182       return;
183     }
184   
185   while (glib_on_error_halt)
186     ;
187   glib_on_error_halt = TRUE;
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, index, 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   index = 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                       index = 0;
276                       buffer[index++] = c;
277                     }
278                   break;
279                 case 1:
280                   buffer[index++] = c;
281                   if ((c == '\n') || (c == '\r'))
282                     {
283                       buffer[index] = 0;
284                       _g_fprintf (stdout, "%s", buffer);
285                       state = 0;
286                       index = 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"