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