Improvement to g_on_error_stack_trace [PATCH]



I'd like to use it in production services, to automatically log any core
dump, but currently g_on_error_stack_trace lacks the flexibility needed,
so I've done some modifications:

I renamed g_on_error_stack_trace() to g_on_error_stack_trace_x() and
added two parameters, a file descriptor where the backtrace will be
written to, and a flag to tell whether you want the process to be killed
with SIGABRT after the trace is written.

I've created a g_on_error_stack_trace() call as a wrapper to the new
one, to preserve backwards compatibility.

The name g_on_error_stack_trace_x() should be changed to something
better (with the older name kept for backward compatibility). I would
use something more simple as g_print_stack_trace() (why "on error"? "on
error", as used in other languages, seem to imply that the function is
setting a callback for the future, as signal() ).

Here is the patch (to the most current version of the files in the 1.3
branch):

Index: gerror.c
===================================================================
RCS file: /cvs/gnome/glib/gerror.c,v
retrieving revision 1.13
diff -u -u -r1.13 gerror.c
--- gerror.c	1999/10/04 23:04:33	1.13
+++ gerror.c	2000/05/16 22:01:21
@@ -81,6 +81,9 @@
 extern volatile gboolean glib_on_error_halt;
 volatile gboolean glib_on_error_halt = TRUE;
 
+static int stack_trace_fd = 2;
+static gboolean kill_process = TRUE;
+
 void
 g_on_error_query (const gchar *prg_name)
 {
@@ -150,8 +153,9 @@
 #endif
 }
 
+
 void
-g_on_error_stack_trace (const gchar *prg_name)
+g_on_error_stack_trace_x (const gchar *prg_name, int fd, gboolean abort_after)
 {
 #ifdef G_OS_UNIX
   pid_t pid;
@@ -166,6 +170,9 @@
   args[1] = (gchar*) prg_name;
   args[2] = buf;
 
+  stack_trace_fd = fd;
+  kill_process = abort_after;
+
   pid = fork ();
   if (pid == 0)
     {
@@ -186,6 +193,12 @@
 #endif
 }
 
+void
+g_on_error_stack_trace (const gchar *prg_name)
+{
+	g_on_error_stack_trace_x (prg_name, 2, FALSE);
+}
+
 static gboolean stack_trace_done = FALSE;
 
 static void
@@ -273,7 +286,7 @@
                   if ((c == '\n') || (c == '\r'))
                     {
                       buffer[index] = 0;
-                      fprintf (stdout, "%s", buffer);
+                      write(stack_trace_fd,buffer,strlen(buffer));
                       state = 0;
                       index = 0;
                     }
@@ -291,6 +304,12 @@
   close (in_fd[1]);
   close (out_fd[0]);
   close (out_fd[1]);
+
+  if(kill_process)
+    {
+      kill(getppid(),SIGABRT);
+    }
+
   _exit (0);
 #else
   abort ();
Index: glib.h
===================================================================
RCS file: /cvs/gnome/glib/glib.h,v
retrieving revision 1.168
diff -u -u -r1.168 glib.h
--- glib.h	2000/05/13 19:30:56	1.168
+++ glib.h	2000/05/16 22:01:28
@@ -1403,6 +1403,7 @@
  */
 void g_on_error_query (const gchar *prg_name);
 void g_on_error_stack_trace (const gchar *prg_name);
+void g_on_error_stack_trace_x (const gchar *prg_name, int fd, gboolean abort_after)
 
 
 /* Logging mechanism


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]