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