Clean up g_thread_yield implementation
[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 /**
95  * g_on_error_query:
96  * @prg_name: the program name, needed by <command>gdb</command>
97  *     for the [S]tack trace option. If @prg_name is %NULL, g_get_prgname()
98  *     is called to get the program name (which will work correctly if
99  *     gdk_init() or gtk_init() has been called)
100  *
101  * Prompts the user with
102  * <computeroutput>[E]xit, [H]alt, show [S]tack trace or [P]roceed</computeroutput>.
103  * This function is intended to be used for debugging use only.
104  * The following example shows how it can be used together with
105  * the g_log() functions.
106  *
107  * |[
108  * &num;include &lt;glib.h&gt;
109  *
110  * static void
111  * log_handler (const gchar   *log_domain,
112  *              GLogLevelFlags log_level,
113  *              const gchar   *message,
114  *              gpointer       user_data)
115  * {
116  *   g_log_default_handler (log_domain, log_level, message, user_data);
117  *
118  *   g_on_error_query (MY_PROGRAM_NAME);
119  * }
120  *
121  * int
122  * main (int argc, char *argv[])
123  * {
124  *   g_log_set_handler (MY_LOG_DOMAIN,
125  *                      G_LOG_LEVEL_WARNING |
126  *                      G_LOG_LEVEL_ERROR |
127  *                      G_LOG_LEVEL_CRITICAL,
128  *                      log_handler,
129  *                      NULL);
130  *   /&ast; ... &ast;/
131  * ]|
132  *
133  * If [E]xit is selected, the application terminates with a call
134  * to <literal>_exit(0)</literal>.
135  *
136  * If [S]tack trace is selected, g_on_error_stack_trace() is called.
137  * This invokes <command>gdb</command>, which attaches to the current
138  * process and shows a stack trace. The prompt is then shown again.
139  *
140  * If [P]roceed is selected, the function returns.
141  *
142  * This function may cause different actions on non-UNIX platforms.
143  */
144 void
145 g_on_error_query (const gchar *prg_name)
146 {
147 #ifndef G_OS_WIN32
148   static const gchar * const query1 = "[E]xit, [H]alt";
149   static const gchar * const query2 = ", show [S]tack trace";
150   static const gchar * const query3 = " or [P]roceed";
151   gchar buf[16];
152
153   if (!prg_name)
154     prg_name = g_get_prgname ();
155
156  retry:
157
158   if (prg_name)
159     _g_fprintf (stdout,
160                 "%s (pid:%u): %s%s%s: ",
161                 prg_name,
162                 (guint) getpid (),
163                 query1,
164                 query2,
165                 query3);
166   else
167     _g_fprintf (stdout,
168                 "(process:%u): %s%s: ",
169                 (guint) getpid (),
170                 query1,
171                 query3);
172   fflush (stdout);
173
174   if (isatty(0) && isatty(1))
175     fgets (buf, 8, stdin);
176   else
177     strcpy (buf, "E\n");
178
179   if ((buf[0] == 'E' || buf[0] == 'e')
180       && buf[1] == '\n')
181     _exit (0);
182   else if ((buf[0] == 'P' || buf[0] == 'p')
183            && buf[1] == '\n')
184     return;
185   else if (prg_name
186            && (buf[0] == 'S' || buf[0] == 's')
187            && buf[1] == '\n')
188     {
189       g_on_error_stack_trace (prg_name);
190       goto retry;
191     }
192   else if ((buf[0] == 'H' || buf[0] == 'h')
193            && buf[1] == '\n')
194     {
195       while (glib_on_error_halt)
196         ;
197       glib_on_error_halt = TRUE;
198       return;
199     }
200   else
201     goto retry;
202 #else
203   if (!prg_name)
204     prg_name = g_get_prgname ();
205
206   MessageBox (NULL, "g_on_error_query called, program terminating",
207               (prg_name && *prg_name) ? prg_name : NULL,
208               MB_OK|MB_ICONERROR);
209   _exit(0);
210 #endif
211 }
212
213 /**
214  * g_on_error_stack_trace:
215  * @prg_name: the program name, needed by <command>gdb</command>
216  *     for the [S]tack trace option. If @prg_name is %NULL, g_get_prgname()
217  *     is called to get the program name (which will work correctly if
218  *     gdk_init() or gtk_init() has been called)
219  *
220  * Invokes <command>gdb</command>, which attaches to the current
221  * process and shows a stack trace. Called by g_on_error_query()
222  * when the [S]tack trace option is selected.
223  *
224  * This function may cause different actions on non-UNIX platforms.
225  */
226 void
227 g_on_error_stack_trace (const gchar *prg_name)
228 {
229 #if defined(G_OS_UNIX) || defined(G_OS_BEOS)
230   pid_t pid;
231   gchar buf[16];
232   gchar *args[4] = { "gdb", NULL, NULL, NULL };
233   int status;
234
235   if (!prg_name)
236     return;
237
238   _g_sprintf (buf, "%u", (guint) getpid ());
239
240   args[1] = (gchar*) prg_name;
241   args[2] = buf;
242
243   pid = fork ();
244   if (pid == 0)
245     {
246       stack_trace (args);
247       _exit (0);
248     }
249   else if (pid == (pid_t) -1)
250     {
251       perror ("unable to fork gdb");
252       return;
253     }
254
255   waitpid (pid, &status, 0);
256 #else
257   if (IsDebuggerPresent ())
258     G_BREAKPOINT ();
259   else
260     abort ();
261 #endif
262 }
263
264 #ifndef G_OS_WIN32
265
266 static gboolean stack_trace_done = FALSE;
267
268 static void
269 stack_trace_sigchld (int signum)
270 {
271   stack_trace_done = TRUE;
272 }
273
274 static void
275 stack_trace (char **args)
276 {
277   pid_t pid;
278   int in_fd[2];
279   int out_fd[2];
280   SELECT_MASK fdset;
281   SELECT_MASK readset;
282   struct timeval tv;
283   int sel, idx, state;
284   char buffer[256];
285   char c;
286
287   stack_trace_done = FALSE;
288   signal (SIGCHLD, stack_trace_sigchld);
289
290   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
291     {
292       perror ("unable to open pipe");
293       _exit (0);
294     }
295
296   pid = fork ();
297   if (pid == 0)
298     {
299       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
300       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
301       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
302
303       execvp (args[0], args);      /* exec gdb */
304       perror ("exec failed");
305       _exit (0);
306     }
307   else if (pid == (pid_t) -1)
308     {
309       perror ("unable to fork");
310       _exit (0);
311     }
312
313   FD_ZERO (&fdset);
314   FD_SET (out_fd[0], &fdset);
315
316   write (in_fd[1], "backtrace\n", 10);
317   write (in_fd[1], "p x = 0\n", 8);
318   write (in_fd[1], "quit\n", 5);
319
320   idx = 0;
321   state = 0;
322
323   while (1)
324     {
325       readset = fdset;
326       tv.tv_sec = 1;
327       tv.tv_usec = 0;
328
329       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
330       if (sel == -1)
331         break;
332
333       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
334         {
335           if (read (out_fd[0], &c, 1))
336             {
337               switch (state)
338                 {
339                 case 0:
340                   if (c == '#')
341                     {
342                       state = 1;
343                       idx = 0;
344                       buffer[idx++] = c;
345                     }
346                   break;
347                 case 1:
348                   buffer[idx++] = c;
349                   if ((c == '\n') || (c == '\r'))
350                     {
351                       buffer[idx] = 0;
352                       _g_fprintf (stdout, "%s", buffer);
353                       state = 0;
354                       idx = 0;
355                     }
356                   break;
357                 default:
358                   break;
359                 }
360             }
361         }
362       else if (stack_trace_done)
363         break;
364     }
365
366   close (in_fd[0]);
367   close (in_fd[1]);
368   close (out_fd[0]);
369   close (out_fd[1]);
370   _exit (0);
371 }
372
373 #endif /* !G_OS_WIN32 */