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