Include a printf implementation supporting C99 snprintf and SUS
[platform/upstream/glib.git] / glib / gbacktrace.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 Lesser 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  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser 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-2000.  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 #include "gprintfint.h"
42
43 #ifdef HAVE_SYS_TIME_H
44 #include <sys/time.h>
45 #endif
46 #ifdef HAVE_SYS_TIMES_H
47 #include <sys/times.h>
48 #endif
49 #include <sys/types.h>
50
51 #include <time.h>
52 #ifdef HAVE_UNISTD_H
53 #include <unistd.h>
54 #endif
55
56 #ifdef HAVE_SYS_SELECT_H
57 #include <sys/select.h>
58 #endif /* HAVE_SYS_SELECT_H */
59
60 #ifdef STDC_HEADERS
61 #include <string.h> /* for bzero on BSD systems */
62 #endif
63
64 #ifdef G_OS_WIN32
65 #  define STRICT                /* Strict typing, please */
66 #  include <windows.h>
67 #  undef STRICT
68 #endif
69
70 #ifndef NO_FD_SET
71 #  define SELECT_MASK fd_set
72 #else
73 #  if defined(_IBMR2)
74 #    define SELECT_MASK void
75 #  else
76 #    define SELECT_MASK int
77 #  endif
78 #endif
79
80
81 #ifndef G_OS_WIN32
82 static void stack_trace (char **args);
83 #endif
84
85 extern volatile gboolean glib_on_error_halt;
86 volatile gboolean glib_on_error_halt = TRUE;
87
88 void
89 g_on_error_query (const gchar *prg_name)
90 {
91 #ifndef G_OS_WIN32
92   static const gchar *query1 = "[E]xit, [H]alt";
93   static const gchar *query2 = ", show [S]tack trace";
94   static const gchar *query3 = " or [P]roceed";
95   gchar buf[16];
96
97   if (!prg_name)
98     prg_name = g_get_prgname ();
99   
100  retry:
101   
102   if (prg_name)
103     _g_fprintf (stdout,
104                 "%s (pid:%u): %s%s%s: ",
105                 prg_name,
106                 (guint) getpid (),
107                 query1,
108                 query2,
109                 query3);
110   else
111     _g_fprintf (stdout,
112                 "(process:%u): %s%s: ",
113                 (guint) getpid (),
114                 query1,
115                 query3);
116   fflush (stdout);
117   
118   if (isatty(0) && isatty(1))
119     fgets (buf, 8, stdin); 
120   else
121     strcpy (buf, "E\n");
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, "g_on_error_query called, program 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 #ifdef G_OS_UNIX
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   _g_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 #ifndef G_OS_WIN32
194
195 static gboolean stack_trace_done = FALSE;
196
197 static void
198 stack_trace_sigchld (int signum)
199 {
200   stack_trace_done = TRUE;
201 }
202
203 static void
204 stack_trace (char **args)
205 {
206   pid_t pid;
207   int in_fd[2];
208   int out_fd[2];
209   SELECT_MASK fdset;
210   SELECT_MASK readset;
211   struct timeval tv;
212   int sel, index, state;
213   char buffer[256];
214   char c;
215
216   stack_trace_done = FALSE;
217   signal (SIGCHLD, stack_trace_sigchld);
218
219   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
220     {
221       perror ("unable to open pipe");
222       _exit (0);
223     }
224
225   pid = fork ();
226   if (pid == 0)
227     {
228       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
229       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
230       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
231
232       execvp (args[0], args);      /* exec gdb */
233       perror ("exec failed");
234       _exit (0);
235     }
236   else if (pid == (pid_t) -1)
237     {
238       perror ("unable to fork");
239       _exit (0);
240     }
241
242   FD_ZERO (&fdset);
243   FD_SET (out_fd[0], &fdset);
244
245   write (in_fd[1], "backtrace\n", 10);
246   write (in_fd[1], "p x = 0\n", 8);
247   write (in_fd[1], "quit\n", 5);
248
249   index = 0;
250   state = 0;
251
252   while (1)
253     {
254       readset = fdset;
255       tv.tv_sec = 1;
256       tv.tv_usec = 0;
257
258       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
259       if (sel == -1)
260         break;
261
262       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
263         {
264           if (read (out_fd[0], &c, 1))
265             {
266               switch (state)
267                 {
268                 case 0:
269                   if (c == '#')
270                     {
271                       state = 1;
272                       index = 0;
273                       buffer[index++] = c;
274                     }
275                   break;
276                 case 1:
277                   buffer[index++] = c;
278                   if ((c == '\n') || (c == '\r'))
279                     {
280                       buffer[index] = 0;
281                       _g_fprintf (stdout, "%s", buffer);
282                       state = 0;
283                       index = 0;
284                     }
285                   break;
286                 default:
287                   break;
288                 }
289             }
290         }
291       else if (stack_trace_done)
292         break;
293     }
294
295   close (in_fd[0]);
296   close (in_fd[1]);
297   close (out_fd[0]);
298   close (out_fd[1]);
299   _exit (0);
300 }
301
302 #endif /* !G_OS_WIN32 */