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