Make PLT-reduction work with gcc4, and don't include everything in
[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 #include "config.h"
33
34 #include <signal.h>
35 #include <stdarg.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include "glib.h"
39 #include "gprintfint.h"
40
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 #  undef STRICT
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 #include "galias.h"
79
80 #ifndef G_OS_WIN32
81 static void stack_trace (char **args);
82 #endif
83
84 extern volatile gboolean glib_on_error_halt;
85 volatile gboolean glib_on_error_halt = TRUE;
86
87 void
88 g_on_error_query (const gchar *prg_name)
89 {
90 #ifndef G_OS_WIN32
91   static const gchar *query1 = "[E]xit, [H]alt";
92   static const gchar *query2 = ", show [S]tack trace";
93   static const gchar *query3 = " or [P]roceed";
94   gchar buf[16];
95
96   if (!prg_name)
97     prg_name = g_get_prgname ();
98   
99  retry:
100   
101   if (prg_name)
102     _g_fprintf (stdout,
103                 "%s (pid:%u): %s%s%s: ",
104                 prg_name,
105                 (guint) getpid (),
106                 query1,
107                 query2,
108                 query3);
109   else
110     _g_fprintf (stdout,
111                 "(process:%u): %s%s: ",
112                 (guint) getpid (),
113                 query1,
114                 query3);
115   fflush (stdout);
116   
117   if (isatty(0) && isatty(1))
118     fgets (buf, 8, stdin); 
119   else
120     strcpy (buf, "E\n");
121
122   if ((buf[0] == 'E' || buf[0] == 'e')
123       && buf[1] == '\n')
124     _exit (0);
125   else if ((buf[0] == 'P' || buf[0] == 'p')
126            && buf[1] == '\n')
127     return;
128   else if (prg_name
129            && (buf[0] == 'S' || buf[0] == 's')
130            && buf[1] == '\n')
131     {
132       g_on_error_stack_trace (prg_name);
133       goto retry;
134     }
135   else if ((buf[0] == 'H' || buf[0] == 'h')
136            && buf[1] == '\n')
137     {
138       while (glib_on_error_halt)
139         ;
140       glib_on_error_halt = TRUE;
141       return;
142     }
143   else
144     goto retry;
145 #else
146   if (!prg_name)
147     prg_name = g_get_prgname ();
148   
149   MessageBox (NULL, "g_on_error_query called, program terminating",
150               (prg_name && *prg_name) ? prg_name : NULL,
151               MB_OK|MB_ICONERROR);
152   _exit(0);
153 #endif
154 }
155
156 void
157 g_on_error_stack_trace (const gchar *prg_name)
158 {
159 #ifdef G_OS_UNIX
160   pid_t pid;
161   gchar buf[16];
162   gchar *args[4] = { "gdb", NULL, NULL, NULL };
163
164   if (!prg_name)
165     return;
166
167   _g_sprintf (buf, "%u", (guint) getpid ());
168
169   args[1] = (gchar*) prg_name;
170   args[2] = buf;
171
172   pid = fork ();
173   if (pid == 0)
174     {
175       stack_trace (args);
176       _exit (0);
177     }
178   else if (pid == (pid_t) -1)
179     {
180       perror ("unable to fork gdb");
181       return;
182     }
183   
184   while (glib_on_error_halt)
185     ;
186   glib_on_error_halt = TRUE;
187 #else
188   if (IsDebuggerPresent ())
189     G_BREAKPOINT ();
190   else
191     abort ();
192 #endif
193 }
194
195 #ifndef G_OS_WIN32
196
197 static gboolean stack_trace_done = FALSE;
198
199 static void
200 stack_trace_sigchld (int signum)
201 {
202   stack_trace_done = TRUE;
203 }
204
205 static void
206 stack_trace (char **args)
207 {
208   pid_t pid;
209   int in_fd[2];
210   int out_fd[2];
211   SELECT_MASK fdset;
212   SELECT_MASK readset;
213   struct timeval tv;
214   int sel, index, state;
215   char buffer[256];
216   char c;
217
218   stack_trace_done = FALSE;
219   signal (SIGCHLD, stack_trace_sigchld);
220
221   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
222     {
223       perror ("unable to open pipe");
224       _exit (0);
225     }
226
227   pid = fork ();
228   if (pid == 0)
229     {
230       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
231       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
232       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
233
234       execvp (args[0], args);      /* exec gdb */
235       perror ("exec failed");
236       _exit (0);
237     }
238   else if (pid == (pid_t) -1)
239     {
240       perror ("unable to fork");
241       _exit (0);
242     }
243
244   FD_ZERO (&fdset);
245   FD_SET (out_fd[0], &fdset);
246
247   write (in_fd[1], "backtrace\n", 10);
248   write (in_fd[1], "p x = 0\n", 8);
249   write (in_fd[1], "quit\n", 5);
250
251   index = 0;
252   state = 0;
253
254   while (1)
255     {
256       readset = fdset;
257       tv.tv_sec = 1;
258       tv.tv_usec = 0;
259
260       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
261       if (sel == -1)
262         break;
263
264       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
265         {
266           if (read (out_fd[0], &c, 1))
267             {
268               switch (state)
269                 {
270                 case 0:
271                   if (c == '#')
272                     {
273                       state = 1;
274                       index = 0;
275                       buffer[index++] = c;
276                     }
277                   break;
278                 case 1:
279                   buffer[index++] = c;
280                   if ((c == '\n') || (c == '\r'))
281                     {
282                       buffer[index] = 0;
283                       _g_fprintf (stdout, "%s", buffer);
284                       state = 0;
285                       index = 0;
286                     }
287                   break;
288                 default:
289                   break;
290                 }
291             }
292         }
293       else if (stack_trace_done)
294         break;
295     }
296
297   close (in_fd[0]);
298   close (in_fd[1]);
299   close (out_fd[0]);
300   close (out_fd[1]);
301   _exit (0);
302 }
303
304 #endif /* !G_OS_WIN32 */
305
306 #define __G_BACKTRACE_C__
307 #include "galiasdef.c"