55457e2c1cd5112dca9b9343aa1e19261ec1b0c4
[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
39 #ifdef HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif
42 #ifdef HAVE_SYS_TIMES_H
43 #include <sys/times.h>
44 #endif
45 #include <sys/types.h>
46 #ifdef HAVE_SYS_WAIT_H
47 #include <sys/wait.h>
48 #endif
49
50 #include <time.h>
51 #ifdef HAVE_UNISTD_H
52 #include <unistd.h>
53 #endif
54
55 #ifdef HAVE_SYS_SELECT_H
56 #include <sys/select.h>
57 #endif /* HAVE_SYS_SELECT_H */
58
59 #include <string.h> /* for bzero on BSD systems */
60
61 #ifdef G_OS_WIN32
62 #  define STRICT                /* Strict typing, please */
63 #  define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
64 #  include <windows.h>
65 #  undef STRICT
66 #endif
67
68 #include "gbacktrace.h"
69
70 #include "gtypes.h"
71 #include "gmain.h"
72 #include "gprintfint.h"
73
74
75 #ifndef NO_FD_SET
76 #  define SELECT_MASK fd_set
77 #else
78 #  if defined(_IBMR2)
79 #    define SELECT_MASK void
80 #  else
81 #    define SELECT_MASK int
82 #  endif
83 #endif
84
85
86 #ifndef G_OS_WIN32
87 static void stack_trace (char **args);
88 #endif
89
90 extern volatile gboolean glib_on_error_halt;
91 volatile gboolean glib_on_error_halt = TRUE;
92
93 void
94 g_on_error_query (const gchar *prg_name)
95 {
96 #ifndef G_OS_WIN32
97   static const gchar * const query1 = "[E]xit, [H]alt";
98   static const gchar * const query2 = ", show [S]tack trace";
99   static const gchar * const query3 = " or [P]roceed";
100   gchar buf[16];
101
102   if (!prg_name)
103     prg_name = g_get_prgname ();
104
105  retry:
106
107   if (prg_name)
108     _g_fprintf (stdout,
109                 "%s (pid:%u): %s%s%s: ",
110                 prg_name,
111                 (guint) getpid (),
112                 query1,
113                 query2,
114                 query3);
115   else
116     _g_fprintf (stdout,
117                 "(process:%u): %s%s: ",
118                 (guint) getpid (),
119                 query1,
120                 query3);
121   fflush (stdout);
122
123   if (isatty(0) && isatty(1))
124     fgets (buf, 8, stdin);
125   else
126     strcpy (buf, "E\n");
127
128   if ((buf[0] == 'E' || buf[0] == 'e')
129       && buf[1] == '\n')
130     _exit (0);
131   else if ((buf[0] == 'P' || buf[0] == 'p')
132            && buf[1] == '\n')
133     return;
134   else if (prg_name
135            && (buf[0] == 'S' || buf[0] == 's')
136            && buf[1] == '\n')
137     {
138       g_on_error_stack_trace (prg_name);
139       goto retry;
140     }
141   else if ((buf[0] == 'H' || buf[0] == 'h')
142            && buf[1] == '\n')
143     {
144       while (glib_on_error_halt)
145         ;
146       glib_on_error_halt = TRUE;
147       return;
148     }
149   else
150     goto retry;
151 #else
152   if (!prg_name)
153     prg_name = g_get_prgname ();
154
155   MessageBox (NULL, "g_on_error_query called, program terminating",
156               (prg_name && *prg_name) ? prg_name : NULL,
157               MB_OK|MB_ICONERROR);
158   _exit(0);
159 #endif
160 }
161
162 void
163 g_on_error_stack_trace (const gchar *prg_name)
164 {
165 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
166   pid_t pid;
167   gchar buf[16];
168   gchar *args[4] = { "gdb", NULL, NULL, NULL };
169   int status;
170
171   if (!prg_name)
172     return;
173
174   _g_sprintf (buf, "%u", (guint) getpid ());
175
176   args[1] = (gchar*) prg_name;
177   args[2] = buf;
178
179   pid = fork ();
180   if (pid == 0)
181     {
182       stack_trace (args);
183       _exit (0);
184     }
185   else if (pid == (pid_t) -1)
186     {
187       perror ("unable to fork gdb");
188       return;
189     }
190
191   waitpid (pid, &status, 0);
192 #else
193   if (IsDebuggerPresent ())
194     G_BREAKPOINT ();
195   else
196     abort ();
197 #endif
198 }
199
200 #ifndef G_OS_WIN32
201
202 static gboolean stack_trace_done = FALSE;
203
204 static void
205 stack_trace_sigchld (int signum)
206 {
207   stack_trace_done = TRUE;
208 }
209
210 static void
211 stack_trace (char **args)
212 {
213   pid_t pid;
214   int in_fd[2];
215   int out_fd[2];
216   SELECT_MASK fdset;
217   SELECT_MASK readset;
218   struct timeval tv;
219   int sel, idx, state;
220   char buffer[256];
221   char c;
222
223   stack_trace_done = FALSE;
224   signal (SIGCHLD, stack_trace_sigchld);
225
226   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
227     {
228       perror ("unable to open pipe");
229       _exit (0);
230     }
231
232   pid = fork ();
233   if (pid == 0)
234     {
235       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
236       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
237       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
238
239       execvp (args[0], args);      /* exec gdb */
240       perror ("exec failed");
241       _exit (0);
242     }
243   else if (pid == (pid_t) -1)
244     {
245       perror ("unable to fork");
246       _exit (0);
247     }
248
249   FD_ZERO (&fdset);
250   FD_SET (out_fd[0], &fdset);
251
252   write (in_fd[1], "backtrace\n", 10);
253   write (in_fd[1], "p x = 0\n", 8);
254   write (in_fd[1], "quit\n", 5);
255
256   idx = 0;
257   state = 0;
258
259   while (1)
260     {
261       readset = fdset;
262       tv.tv_sec = 1;
263       tv.tv_usec = 0;
264
265       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
266       if (sel == -1)
267         break;
268
269       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
270         {
271           if (read (out_fd[0], &c, 1))
272             {
273               switch (state)
274                 {
275                 case 0:
276                   if (c == '#')
277                     {
278                       state = 1;
279                       idx = 0;
280                       buffer[idx++] = c;
281                     }
282                   break;
283                 case 1:
284                   buffer[idx++] = c;
285                   if ((c == '\n') || (c == '\r'))
286                     {
287                       buffer[idx] = 0;
288                       _g_fprintf (stdout, "%s", buffer);
289                       state = 0;
290                       idx = 0;
291                     }
292                   break;
293                 default:
294                   break;
295                 }
296             }
297         }
298       else if (stack_trace_done)
299         break;
300     }
301
302   close (in_fd[0]);
303   close (in_fd[1]);
304   close (out_fd[0]);
305   close (out_fd[1]);
306   _exit (0);
307 }
308
309 #endif /* !G_OS_WIN32 */