18:36. incorporated proposed cleanups from gtk-devel-list.
[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 NATIVE_WIN32
63 #  define STRICT                /* Strict typing, please */
64 #  include <windows.h>
65 #  include <process.h>          /* For _getpid() */
66 #endif
67
68 #ifndef NO_FD_SET
69 #  define SELECT_MASK fd_set
70 #else
71 #  if defined(_IBMR2)
72 #    define SELECT_MASK void
73 #  else
74 #    define SELECT_MASK int
75 #  endif
76 #endif
77
78
79 static void stack_trace (char **args);
80
81 extern volatile gboolean glib_on_error_halt;
82 volatile gboolean glib_on_error_halt = TRUE;
83
84 void
85 g_on_error_query (const gchar *prg_name)
86 {
87 #ifndef NATIVE_WIN32
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 #ifndef NATIVE_WIN32
115   if (isatty(0) && isatty(1))
116     fgets (buf, 8, stdin); 
117   else
118     strcpy (buf, "E\n");
119 #else
120   fgets (buf, 8, stdin); 
121 #endif
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, "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(NATIVE_WIN32) && ! defined(GLIB_NATIVE_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   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   abort ();
190 #endif
191 }
192
193 static gboolean stack_trace_done = FALSE;
194
195 static void
196 stack_trace_sigchld (int signum)
197 {
198   stack_trace_done = TRUE;
199 }
200
201 static void
202 stack_trace (char **args)
203 {
204 #if !defined(NATIVE_WIN32) && !defined(GLIB_NATIVE_BEOS)
205   pid_t pid;
206   int in_fd[2];
207   int out_fd[2];
208   SELECT_MASK fdset;
209   SELECT_MASK readset;
210   struct timeval tv;
211   int sel, index, state;
212   char buffer[256];
213   char c;
214
215   stack_trace_done = FALSE;
216   signal (SIGCHLD, stack_trace_sigchld);
217
218   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
219     {
220       perror ("unable to open pipe");
221       _exit (0);
222     }
223
224   pid = fork ();
225   if (pid == 0)
226     {
227       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
228       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
229       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
230
231       execvp (args[0], args);      /* exec gdb */
232       perror ("exec failed");
233       _exit (0);
234     }
235   else if (pid == (pid_t) -1)
236     {
237       perror ("unable to fork");
238       _exit (0);
239     }
240
241   FD_ZERO (&fdset);
242   FD_SET (out_fd[0], &fdset);
243
244   write (in_fd[1], "backtrace\n", 10);
245   write (in_fd[1], "p x = 0\n", 8);
246   write (in_fd[1], "quit\n", 5);
247
248   index = 0;
249   state = 0;
250
251   while (1)
252     {
253       readset = fdset;
254       tv.tv_sec = 1;
255       tv.tv_usec = 0;
256
257       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
258       if (sel == -1)
259         break;
260
261       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
262         {
263           if (read (out_fd[0], &c, 1))
264             {
265               switch (state)
266                 {
267                 case 0:
268                   if (c == '#')
269                     {
270                       state = 1;
271                       index = 0;
272                       buffer[index++] = c;
273                     }
274                   break;
275                 case 1:
276                   buffer[index++] = c;
277                   if ((c == '\n') || (c == '\r'))
278                     {
279                       buffer[index] = 0;
280                       fprintf (stdout, "%s", buffer);
281                       state = 0;
282                       index = 0;
283                     }
284                   break;
285                 default:
286                   break;
287                 }
288             }
289         }
290       else if (stack_trace_done)
291         break;
292     }
293
294   close (in_fd[0]);
295   close (in_fd[1]);
296   close (out_fd[0]);
297   close (out_fd[1]);
298   _exit (0);
299 #else
300   abort ();
301 #endif
302 }