Land glib-rrh-19981025-0.patch.
[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 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include "glib.h"
29 #ifdef HAVE_SYS_TIME_H
30 #include <sys/time.h>
31 #endif
32 #ifdef HAVE_SYS_TIMES_H
33 #include <sys/times.h>
34 #endif
35 #include <sys/types.h>
36
37 #include <time.h>
38 #ifdef HAVE_UNISTD_H
39 #include <unistd.h>
40 #endif
41
42 #ifdef HAVE_SYS_SELECT_H
43 #include <sys/select.h>
44 #endif /* HAVE_SYS_SELECT_H */
45
46 #ifdef STDC_HEADERS
47 #include <string.h> /* for bzero on BSD systems */
48 #endif
49
50 #ifdef _MSC_VER
51 #include <process.h>            /* For _getpid() */
52 #endif
53
54 #ifndef NO_FD_SET
55 #  define SELECT_MASK fd_set
56 #else
57 #  ifndef _AIX
58      typedef long fd_mask;
59 #  endif
60 #  if defined(_IBMR2)
61 #    define SELECT_MASK void
62 #  else
63 #    define SELECT_MASK int
64 #  endif
65 #endif
66
67
68 static void stack_trace (char **args);
69
70 extern volatile gboolean glib_on_error_halt;
71 volatile gboolean glib_on_error_halt = TRUE;
72
73 void
74 g_on_error_query (const gchar *prg_name)
75 {
76   static const gchar *query1 = "[E]xit, [H]alt";
77   static const gchar *query2 = ", show [S]tack trace";
78   static const gchar *query3 = " or [P]roceed";
79   gchar buf[16];
80
81   if (!prg_name)
82     prg_name = g_get_prgname ();
83   
84  retry:
85   
86   if (prg_name)
87     fprintf (stdout,
88              "%s (pid:%u): %s%s%s: ",
89              prg_name,
90              (guint) getpid (),
91              query1,
92              query2,
93              query3);
94   else
95     fprintf (stdout,
96              "(process:%u): %s%s: ",
97              (guint) getpid (),
98              query1,
99              query3);
100   fflush (stdout);
101   
102   fgets (buf, 8, stdin);
103
104   if ((buf[0] == 'E' || buf[0] == 'e')
105       && buf[1] == '\n')
106     _exit (0);
107   else if ((buf[0] == 'P' || buf[0] == 'p')
108            && buf[1] == '\n')
109     return;
110   else if (prg_name
111            && (buf[0] == 'S' || buf[0] == 's')
112            && buf[1] == '\n')
113     {
114       g_on_error_stack_trace (prg_name);
115       goto retry;
116     }
117   else if ((buf[0] == 'H' || buf[0] == 'h')
118            && buf[1] == '\n')
119     {
120       while (glib_on_error_halt)
121         ;
122       glib_on_error_halt = TRUE;
123       return;
124     }
125   else
126     goto retry;
127 }
128
129 void
130 g_on_error_stack_trace (const gchar *prg_name)
131 {
132 #ifndef NATIVE_WIN32
133   pid_t pid;
134   gchar buf[16];
135   gchar *args[4] = { "gdb", NULL, NULL, NULL };
136
137   if (!prg_name)
138     return;
139
140   sprintf (buf, "%u", (guint) getpid ());
141
142   args[1] = (gchar*) prg_name;
143   args[2] = buf;
144
145   pid = fork ();
146   if (pid == 0)
147     {
148       stack_trace (args);
149       _exit (0);
150     }
151   else if (pid == (pid_t) -1)
152     {
153       perror ("unable to fork gdb");
154       return;
155     }
156   
157   while (glib_on_error_halt)
158     ;
159   glib_on_error_halt = TRUE;
160 #else
161   abort ();
162 #endif
163 }
164
165 static gboolean stack_trace_done = FALSE;
166
167 static void
168 stack_trace_sigchld (int signum)
169 {
170   stack_trace_done = TRUE;
171 }
172
173 static void
174 stack_trace (char **args)
175 {
176 #ifndef NATIVE_WIN32
177   pid_t pid;
178   int in_fd[2];
179   int out_fd[2];
180   SELECT_MASK fdset;
181   SELECT_MASK readset;
182   struct timeval tv;
183   int sel, index, state;
184   char buffer[256];
185   char c;
186
187   stack_trace_done = FALSE;
188   signal (SIGCHLD, stack_trace_sigchld);
189
190   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
191     {
192       perror ("unable to open pipe");
193       _exit (0);
194     }
195
196   pid = fork ();
197   if (pid == 0)
198     {
199       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
200       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
201       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
202
203       execvp (args[0], args);      /* exec gdb */
204       perror ("exec failed");
205       _exit (0);
206     }
207   else if (pid == (pid_t) -1)
208     {
209       perror ("unable to fork");
210       _exit (0);
211     }
212
213   FD_ZERO (&fdset);
214   FD_SET (out_fd[0], &fdset);
215
216   write (in_fd[1], "backtrace\n", 10);
217   write (in_fd[1], "p x = 0\n", 8);
218   write (in_fd[1], "quit\n", 5);
219
220   index = 0;
221   state = 0;
222
223   while (1)
224     {
225       readset = fdset;
226       tv.tv_sec = 1;
227       tv.tv_usec = 0;
228
229       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
230       if (sel == -1)
231         break;
232
233       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
234         {
235           if (read (out_fd[0], &c, 1))
236             {
237               switch (state)
238                 {
239                 case 0:
240                   if (c == '#')
241                     {
242                       state = 1;
243                       index = 0;
244                       buffer[index++] = c;
245                     }
246                   break;
247                 case 1:
248                   buffer[index++] = c;
249                   if ((c == '\n') || (c == '\r'))
250                     {
251                       buffer[index] = 0;
252                       fprintf (stdout, "%s", buffer);
253                       state = 0;
254                       index = 0;
255                     }
256                   break;
257                 default:
258                   break;
259                 }
260             }
261         }
262       else if (stack_trace_done)
263         break;
264     }
265
266   close (in_fd[0]);
267   close (in_fd[1]);
268   close (out_fd[0]);
269   close (out_fd[1]);
270   _exit (0);
271 #else
272   abort ();
273 #endif
274 }