applied patch from Andreas Persenius <ndap@swipnet.se> that updates the
[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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include <signal.h>
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include "glib.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
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 #  include <windows.h>
65 #endif
66
67 #ifndef NO_FD_SET
68 #  define SELECT_MASK fd_set
69 #else
70 #  if defined(_IBMR2)
71 #    define SELECT_MASK void
72 #  else
73 #    define SELECT_MASK int
74 #  endif
75 #endif
76
77
78 static void stack_trace (char **args);
79
80 extern volatile gboolean glib_on_error_halt;
81 volatile gboolean glib_on_error_halt = TRUE;
82
83 void
84 g_on_error_query (const gchar *prg_name)
85 {
86 #ifndef G_OS_WIN32
87   static const gchar *query1 = "[E]xit, [H]alt";
88   static const gchar *query2 = ", show [S]tack trace";
89   static const gchar *query3 = " or [P]roceed";
90   gchar buf[16];
91
92   if (!prg_name)
93     prg_name = g_get_prgname ();
94   
95  retry:
96   
97   if (prg_name)
98     fprintf (stdout,
99              "%s (pid:%u): %s%s%s: ",
100              prg_name,
101              (guint) getpid (),
102              query1,
103              query2,
104              query3);
105   else
106     fprintf (stdout,
107              "(process:%u): %s%s: ",
108              (guint) getpid (),
109              query1,
110              query3);
111   fflush (stdout);
112   
113   if (isatty(0) && isatty(1))
114     fgets (buf, 8, stdin); 
115   else
116     strcpy (buf, "E\n");
117
118   if ((buf[0] == 'E' || buf[0] == 'e')
119       && buf[1] == '\n')
120     _exit (0);
121   else if ((buf[0] == 'P' || buf[0] == 'p')
122            && buf[1] == '\n')
123     return;
124   else if (prg_name
125            && (buf[0] == 'S' || buf[0] == 's')
126            && buf[1] == '\n')
127     {
128       g_on_error_stack_trace (prg_name);
129       goto retry;
130     }
131   else if ((buf[0] == 'H' || buf[0] == 'h')
132            && buf[1] == '\n')
133     {
134       while (glib_on_error_halt)
135         ;
136       glib_on_error_halt = TRUE;
137       return;
138     }
139   else
140     goto retry;
141 #else
142   if (!prg_name)
143     prg_name = g_get_prgname ();
144   
145   MessageBox (NULL, "g_on_error_query called, program terminating",
146               (prg_name && *prg_name) ? prg_name : NULL,
147               MB_OK|MB_ICONERROR);
148   _exit(0);
149 #endif
150 }
151
152 void
153 g_on_error_stack_trace (const gchar *prg_name)
154 {
155 #ifdef G_OS_UNIX
156   pid_t pid;
157   gchar buf[16];
158   gchar *args[4] = { "gdb", NULL, NULL, NULL };
159
160   if (!prg_name)
161     return;
162
163   sprintf (buf, "%u", (guint) getpid ());
164
165   args[1] = (gchar*) prg_name;
166   args[2] = buf;
167
168   pid = fork ();
169   if (pid == 0)
170     {
171       stack_trace (args);
172       _exit (0);
173     }
174   else if (pid == (pid_t) -1)
175     {
176       perror ("unable to fork gdb");
177       return;
178     }
179   
180   while (glib_on_error_halt)
181     ;
182   glib_on_error_halt = TRUE;
183 #else
184   abort ();
185 #endif
186 }
187
188 static gboolean stack_trace_done = FALSE;
189
190 static void
191 stack_trace_sigchld (int signum)
192 {
193   stack_trace_done = TRUE;
194 }
195
196 static void
197 stack_trace (char **args)
198 {
199 #ifdef G_OS_UNIX
200   pid_t pid;
201   int in_fd[2];
202   int out_fd[2];
203   SELECT_MASK fdset;
204   SELECT_MASK readset;
205   struct timeval tv;
206   int sel, index, state;
207   char buffer[256];
208   char c;
209
210   stack_trace_done = FALSE;
211   signal (SIGCHLD, stack_trace_sigchld);
212
213   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
214     {
215       perror ("unable to open pipe");
216       _exit (0);
217     }
218
219   pid = fork ();
220   if (pid == 0)
221     {
222       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
223       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
224       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
225
226       execvp (args[0], args);      /* exec gdb */
227       perror ("exec failed");
228       _exit (0);
229     }
230   else if (pid == (pid_t) -1)
231     {
232       perror ("unable to fork");
233       _exit (0);
234     }
235
236   FD_ZERO (&fdset);
237   FD_SET (out_fd[0], &fdset);
238
239   write (in_fd[1], "backtrace\n", 10);
240   write (in_fd[1], "p x = 0\n", 8);
241   write (in_fd[1], "quit\n", 5);
242
243   index = 0;
244   state = 0;
245
246   while (1)
247     {
248       readset = fdset;
249       tv.tv_sec = 1;
250       tv.tv_usec = 0;
251
252       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
253       if (sel == -1)
254         break;
255
256       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
257         {
258           if (read (out_fd[0], &c, 1))
259             {
260               switch (state)
261                 {
262                 case 0:
263                   if (c == '#')
264                     {
265                       state = 1;
266                       index = 0;
267                       buffer[index++] = c;
268                     }
269                   break;
270                 case 1:
271                   buffer[index++] = c;
272                   if ((c == '\n') || (c == '\r'))
273                     {
274                       buffer[index] = 0;
275                       fprintf (stdout, "%s", buffer);
276                       state = 0;
277                       index = 0;
278                     }
279                   break;
280                 default:
281                   break;
282                 }
283             }
284         }
285       else if (stack_trace_done)
286         break;
287     }
288
289   close (in_fd[0]);
290   close (in_fd[1]);
291   close (out_fd[0]);
292   close (out_fd[1]);
293   _exit (0);
294 #else
295   abort ();
296 #endif
297 }