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