glib/libglib/src/gbacktrace.c
changeset 0 e4d67989cc36
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GLIB - Library of useful routines for C programming 
       
     2  * Copyright (C) 1995-1997  Peter Mattis, Spencer Kimball and Josh MacDonald
       
     3  * Portions copyright (c) 2006 Nokia Corporation.  All rights reserved.
       
     4  *
       
     5  * This library is free software; you can redistribute it and/or
       
     6  * modify it under the terms of the GNU Lesser General Public
       
     7  * License as published by the Free Software Foundation; either
       
     8  * version 2 of the License, or (at your option) any later version.
       
     9  *
       
    10  * This library is distributed in the hope that it will be useful,
       
    11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    13  * Lesser General Public License for more details.
       
    14  *
       
    15  * You should have received a copy of the GNU Lesser General Public
       
    16  * License along with this library; if not, write to the
       
    17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    18  * Boston, MA 02111-1307, USA.
       
    19  */
       
    20 
       
    21 /*
       
    22  * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
       
    23  * file for a list of people on the GLib Team.  See the ChangeLog
       
    24  * files for a list of changes.  These files are distributed with
       
    25  * GLib at ftp://ftp.gtk.org/pub/gtk/. 
       
    26  */
       
    27 
       
    28 /* 
       
    29  * MT safe ; except for g_on_error_stack_trace, but who wants thread safety 
       
    30  * then
       
    31  */
       
    32 
       
    33 #include "config.h"
       
    34 
       
    35 #ifndef __SYMBIAN32__
       
    36 #include <signal.h>
       
    37 #endif /* __SYMBIAN32__ */
       
    38 #include <stdarg.h>
       
    39 #include <stdio.h>
       
    40 #include <stdlib.h>
       
    41 #include "glib.h"
       
    42 #include "gprintfint.h"
       
    43 
       
    44 #ifdef HAVE_SYS_TIME_H
       
    45 #include <sys/time.h>
       
    46 #endif
       
    47 #ifdef HAVE_SYS_TIMES_H
       
    48 #include <sys/times.h>
       
    49 #endif
       
    50 #include <sys/types.h>
       
    51 #ifdef HAVE_SYS_WAIT_H
       
    52 #include <sys/wait.h>
       
    53 #endif
       
    54 
       
    55 #include <time.h>
       
    56 #ifdef HAVE_UNISTD_H
       
    57 #include <unistd.h>
       
    58 #endif
       
    59 
       
    60 #ifdef HAVE_SYS_SELECT_H
       
    61 #include <sys/select.h>
       
    62 #endif /* HAVE_SYS_SELECT_H */
       
    63 
       
    64 #ifdef STDC_HEADERS
       
    65 #include <string.h> /* for bzero on BSD systems */
       
    66 #endif
       
    67 
       
    68 #ifdef G_OS_WIN32
       
    69 #  define STRICT		/* Strict typing, please */
       
    70 #  define _WIN32_WINDOWS 0x0401 /* to get IsDebuggerPresent */
       
    71 #  include <windows.h>
       
    72 #  undef STRICT
       
    73 #endif
       
    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 #include "galias.h"
       
    86 
       
    87 #ifdef __SYMBIAN32__
       
    88 #include <glib_wsd.h>
       
    89 #endif /* __SYMBIAN32__ */
       
    90 
       
    91 
       
    92 #if !defined(G_OS_WIN32) && !defined(__SYMBIAN32__)
       
    93 static void stack_trace (char **args);
       
    94 #endif
       
    95 
       
    96 #if EMULATOR
       
    97 
       
    98 PLS(glib_on_error_halt,gbacktrace,volatile gboolean)
       
    99 #define glib_on_error_halt  (*FUNCTION_NAME(glib_on_error_halt ,gbacktrace)())
       
   100 
       
   101 #else
       
   102 
       
   103 extern volatile gboolean glib_on_error_halt;
       
   104 volatile gboolean glib_on_error_halt = TRUE;
       
   105 
       
   106 #endif /* EMULATOR */
       
   107 
       
   108 #ifdef __SYMBIAN32__
       
   109 EXPORT_C volatile gboolean *_glib_on_error_halt()
       
   110 {
       
   111 	return &glib_on_error_halt;
       
   112 }
       
   113 #endif /* __SYMBIAN32__ */
       
   114 
       
   115 EXPORT_C void
       
   116 g_on_error_query (const gchar *prg_name)
       
   117 {
       
   118 #ifndef G_OS_WIN32
       
   119   static const gchar * const query1 = "[E]xit, [H]alt";
       
   120   static const gchar * const query2 = ", show [S]tack trace";
       
   121   static const gchar * const query3 = " or [P]roceed";
       
   122   gchar buf[16];
       
   123 
       
   124   if (!prg_name)
       
   125     prg_name = g_get_prgname ();
       
   126   
       
   127  retry:
       
   128   
       
   129   if (prg_name)
       
   130     _g_fprintf (stdout,
       
   131 		"%s (pid:%u): %s%s%s: ",
       
   132 		prg_name,
       
   133 		(guint) getpid (),
       
   134 		query1,
       
   135 		query2,
       
   136 		query3);
       
   137   else
       
   138     _g_fprintf (stdout,
       
   139 		"(process:%u): %s%s: ",
       
   140 		(guint) getpid (),
       
   141 		query1,
       
   142 		query3);
       
   143   fflush (stdout);
       
   144   
       
   145   if (isatty(0) && isatty(1))
       
   146     fgets (buf, 8, stdin); 
       
   147   else
       
   148     strcpy (buf, "E\n");
       
   149 
       
   150   if ((buf[0] == 'E' || buf[0] == 'e')
       
   151       && buf[1] == '\n')
       
   152     _exit (0);
       
   153   else if ((buf[0] == 'P' || buf[0] == 'p')
       
   154 	   && buf[1] == '\n')
       
   155     return;
       
   156   else if (prg_name
       
   157 	   && (buf[0] == 'S' || buf[0] == 's')
       
   158 	   && buf[1] == '\n')
       
   159     {
       
   160       g_on_error_stack_trace (prg_name);
       
   161       goto retry;
       
   162     }
       
   163   else if ((buf[0] == 'H' || buf[0] == 'h')
       
   164 	   && buf[1] == '\n')
       
   165     {
       
   166       while (glib_on_error_halt)
       
   167 	;
       
   168       glib_on_error_halt = TRUE;
       
   169       return;
       
   170     }
       
   171   else
       
   172     goto retry;
       
   173 #else
       
   174   if (!prg_name)
       
   175     prg_name = g_get_prgname ();
       
   176   
       
   177   MessageBox (NULL, "g_on_error_query called, program terminating",
       
   178 	      (prg_name && *prg_name) ? prg_name : NULL,
       
   179 	      MB_OK|MB_ICONERROR);
       
   180   _exit(0);
       
   181 #endif
       
   182 }
       
   183 
       
   184 EXPORT_C void
       
   185 g_on_error_stack_trace (const gchar *prg_name)
       
   186 {
       
   187 #if (defined(G_OS_UNIX) || defined(G_OS_BEOS)) && !defined(__SYMBIAN32__)
       
   188   pid_t pid;
       
   189   gchar buf[16];
       
   190   gchar *args[4] = { "gdb", NULL, NULL, NULL };
       
   191   int status;
       
   192 
       
   193   if (!prg_name)
       
   194     return;
       
   195 
       
   196   _g_sprintf (buf, "%u", (guint) getpid ());
       
   197 
       
   198   args[1] = (gchar*) prg_name;
       
   199   args[2] = buf;
       
   200 
       
   201   pid = fork ();
       
   202   if (pid == 0)
       
   203     {
       
   204       stack_trace (args);
       
   205       _exit (0);
       
   206     }
       
   207   else if (pid == (pid_t) -1)
       
   208     {
       
   209       perror ("unable to fork gdb");
       
   210       return;
       
   211     }
       
   212 
       
   213   waitpid (pid, &status, 0);
       
   214 #elif defined(__SYMBIAN32__)
       
   215 	abort();
       
   216 #else
       
   217   if (IsDebuggerPresent ())
       
   218     G_BREAKPOINT ();
       
   219   else
       
   220     abort ();
       
   221 #endif
       
   222 }
       
   223 
       
   224 #if !defined G_OS_WIN32 && !defined __SYMBIAN32__
       
   225 
       
   226 static gboolean stack_trace_done = FALSE;
       
   227 
       
   228 static void
       
   229 stack_trace_sigchld (int signum)
       
   230 {
       
   231   stack_trace_done = TRUE;
       
   232 }
       
   233 
       
   234 static void
       
   235 stack_trace (char **args)
       
   236 {
       
   237   pid_t pid;
       
   238   int in_fd[2];
       
   239   int out_fd[2];
       
   240   SELECT_MASK fdset;
       
   241   SELECT_MASK readset;
       
   242   struct timeval tv;
       
   243   int sel, index, state;
       
   244   char buffer[256];
       
   245   char c;
       
   246 
       
   247   stack_trace_done = FALSE;
       
   248   signal (SIGCHLD, stack_trace_sigchld);
       
   249 
       
   250   if ((pipe (in_fd) == -1) || (pipe (out_fd) == -1))
       
   251     {
       
   252       perror ("unable to open pipe");
       
   253       _exit (0);
       
   254     }
       
   255 
       
   256   pid = fork ();
       
   257   if (pid == 0)
       
   258     {
       
   259       close (0); dup (in_fd[0]);   /* set the stdin to the in pipe */
       
   260       close (1); dup (out_fd[1]);  /* set the stdout to the out pipe */
       
   261       close (2); dup (out_fd[1]);  /* set the stderr to the out pipe */
       
   262 
       
   263       execvp (args[0], args);      /* exec gdb */
       
   264       perror ("exec failed");
       
   265       _exit (0);
       
   266     }
       
   267   else if (pid == (pid_t) -1)
       
   268     {
       
   269       perror ("unable to fork");
       
   270       _exit (0);
       
   271     }
       
   272 
       
   273   FD_ZERO (&fdset);
       
   274   FD_SET (out_fd[0], &fdset);
       
   275 
       
   276   write (in_fd[1], "backtrace\n", 10);
       
   277   write (in_fd[1], "p x = 0\n", 8);
       
   278   write (in_fd[1], "quit\n", 5);
       
   279 
       
   280   index = 0;
       
   281   state = 0;
       
   282 
       
   283   while (1)
       
   284     {
       
   285       readset = fdset;
       
   286       tv.tv_sec = 1;
       
   287       tv.tv_usec = 0;
       
   288 
       
   289       sel = select (FD_SETSIZE, &readset, NULL, NULL, &tv);
       
   290       if (sel == -1)
       
   291         break;
       
   292 
       
   293       if ((sel > 0) && (FD_ISSET (out_fd[0], &readset)))
       
   294         {
       
   295           if (read (out_fd[0], &c, 1))
       
   296             {
       
   297               switch (state)
       
   298                 {
       
   299                 case 0:
       
   300                   if (c == '#')
       
   301                     {
       
   302                       state = 1;
       
   303                       index = 0;
       
   304                       buffer[index++] = c;
       
   305                     }
       
   306                   break;
       
   307                 case 1:
       
   308                   buffer[index++] = c;
       
   309                   if ((c == '\n') || (c == '\r'))
       
   310                     {
       
   311                       buffer[index] = 0;
       
   312                       _g_fprintf (stdout, "%s", buffer);
       
   313                       state = 0;
       
   314                       index = 0;
       
   315                     }
       
   316                   break;
       
   317                 default:
       
   318                   break;
       
   319                 }
       
   320             }
       
   321         }
       
   322       else if (stack_trace_done)
       
   323         break;
       
   324     }
       
   325 
       
   326   close (in_fd[0]);
       
   327   close (in_fd[1]);
       
   328   close (out_fd[0]);
       
   329   close (out_fd[1]);
       
   330   _exit (0);
       
   331 }
       
   332 
       
   333 #endif /* !G_OS_WIN32 */
       
   334 
       
   335 #define __G_BACKTRACE_C__
       
   336 #include "galiasdef.c"