Don't define WIN32 and NATIVE_WIN32.
[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 G_OS_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 G_OS_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   if (isatty(0) && isatty(1))
115     fgets (buf, 8, stdin); 
116   else
117     strcpy (buf, "E\n");
118
119   if ((buf[0] == 'E' || buf[0] == 'e')
120       && buf[1] == '\n')
121     _exit (0);
122   else if ((buf[0] == 'P' || buf[0] == 'p')
123            && buf[1] == '\n')
124     return;
125   else if (prg_name
126            && (buf[0] == 'S' || buf[0] == 's')
127            && buf[1] == '\n')
128     {
129       g_on_error_stack_trace (prg_name);
130       goto retry;
131     }
132   else if ((buf[0] == 'H' || buf[0] == 'h')
133            && buf[1] == '\n')
134     {
135       while (glib_on_error_halt)
136         ;
137       glib_on_error_halt = TRUE;
138       return;
139     }
140   else
141     goto retry;
142 #else
143   if (!prg_name)
144     prg_name = g_get_prgname ();
145   
146   MessageBox (NULL, "g_on_error_query called, program terminating",
147               (prg_name && *prg_name) ? prg_name : NULL,
148               MB_OK|MB_ICONERROR);
149   _exit(0);
150 #endif
151 }
152
153 void
154 g_on_error_stack_trace (const gchar *prg_name)
155 {
156 #ifdef G_OS_UNIX
157   pid_t pid;
158   gchar buf[16];
159   gchar *args[4] = { "gdb", NULL, NULL, NULL };
160
161   if (!prg_name)
162     return;
163
164   sprintf (buf, "%u", (guint) getpid ());
165
166   args[1] = (gchar*) prg_name;
167   args[2] = buf;
168
169   pid = fork ();
170   if (pid == 0)
171     {
172       stack_trace (args);
173       _exit (0);
174     }
175   else if (pid == (pid_t) -1)
176     {
177       perror ("unable to fork gdb");
178       return;
179     }
180   
181   while (glib_on_error_halt)
182     ;
183   glib_on_error_halt = TRUE;
184 #else
185   abort ();
186 #endif
187 }
188
189 static gboolean stack_trace_done = FALSE;
190
191 static void
192 stack_trace_sigchld (int signum)
193 {
194   stack_trace_done = TRUE;
195 }
196
197 static void
198 stack_trace (char **args)
199 {
200 #ifdef G_OS_UNIX
201   pid_t pid;
202   int in_fd[2];
203   int out_fd[2];
204   SELECT_MASK fdset;
205   SELECT_MASK readset;
206   struct timeval tv;
207   int sel, index, state;
208   char buffer[256];
209   char c;
210
211   stack_trace_done = FALSE;
212   signal (SIGCHLD, stack_trace_sigchld);
213
214   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
215     {
216       perror ("unable to open pipe");
217       _exit (0);
218     }
219
220   pid = fork ();
221   if (pid == 0)
222     {
223       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
224       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
225       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
226
227       execvp (args[0], args);      /* exec gdb */
228       perror ("exec failed");
229       _exit (0);
230     }
231   else if (pid == (pid_t) -1)
232     {
233       perror ("unable to fork");
234       _exit (0);
235     }
236
237   FD_ZERO (&fdset);
238   FD_SET (out_fd[0], &fdset);
239
240   write (in_fd[1], "backtrace\n", 10);
241   write (in_fd[1], "p x = 0\n", 8);
242   write (in_fd[1], "quit\n", 5);
243
244   index = 0;
245   state = 0;
246
247   while (1)
248     {
249       readset = fdset;
250       tv.tv_sec = 1;
251       tv.tv_usec = 0;
252
253       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
254       if (sel == -1)
255         break;
256
257       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
258         {
259           if (read (out_fd[0], &c, 1))
260             {
261               switch (state)
262                 {
263                 case 0:
264                   if (c == '#')
265                     {
266                       state = 1;
267                       index = 0;
268                       buffer[index++] = c;
269                     }
270                   break;
271                 case 1:
272                   buffer[index++] = c;
273                   if ((c == '\n') || (c == '\r'))
274                     {
275                       buffer[index] = 0;
276                       fprintf (stdout, "%s", buffer);
277                       state = 0;
278                       index = 0;
279                     }
280                   break;
281                 default:
282                   break;
283                 }
284             }
285         }
286       else if (stack_trace_done)
287         break;
288     }
289
290   close (in_fd[0]);
291   close (in_fd[1]);
292   close (out_fd[0]);
293   close (out_fd[1]);
294   _exit (0);
295 #else
296   abort ();
297 #endif
298 }