Add automatic stack dumping on SIGSEGV and SIGQUIT
authorDavid Schleef <ds@schleef.org>
Thu, 14 Nov 2002 02:49:16 +0000 (02:49 +0000)
committerDavid Schleef <ds@schleef.org>
Thu, 14 Nov 2002 02:49:16 +0000 (02:49 +0000)
Original commit message from CVS:
Add automatic stack dumping on SIGSEGV and SIGQUIT

tools/gst-launch.c

index 2359aec..6ecd6a2 100644 (file)
@@ -1,5 +1,7 @@
 #include <string.h>
 #include <stdlib.h>
+#include <signal.h>
+#include <sys/wait.h>
 #include <gst/gst.h>
 
 static guint64 iterations = 0;
@@ -104,6 +106,72 @@ xmllaunch_parse_cmdline (const gchar **argv)
     return l->data;
 }
 
+extern volatile gboolean glib_on_error_halt;
+void fault_restore(void);
+
+void fault_handler(int signum, siginfo_t *si, void *misc)
+{
+       int spinning = TRUE;
+
+       fault_restore();
+
+       if(si->si_signo == SIGSEGV){
+               g_print ("Caught SIGSEGV accessing address %p\n", si->si_addr);
+       }else if(si->si_signo == SIGQUIT){
+               g_print ("Caught SIGQUIT\n");
+       }else{
+               g_print ("signo:  %d\n",si->si_signo);
+               g_print ("errno:  %d\n",si->si_errno);
+               g_print ("code:   %d\n",si->si_code);
+       }
+
+       glib_on_error_halt = FALSE;
+       g_on_error_stack_trace("gst-launch");
+
+       wait(NULL);
+
+#if 1
+       /* FIXME how do we know if we were run by libtool? */
+       g_print("Spinning.  Please run 'gdb gst-launch %d' to continue debugging, "
+               "Ctrl-C to quit, or Ctrl-\\ to dump core.\n",
+               getpid());
+       while(spinning)usleep(1000000);
+#else
+       /* This spawns a gdb and attaches it to gst-launch. */
+       {
+               char str[40];
+               sprintf(str,"gdb -quiet gst-launch %d",getpid());
+               system(str);
+       }
+
+       _exit(0);
+#endif
+
+}
+
+void fault_restore(void)
+{
+       struct sigaction action;
+
+       memset(&action,0,sizeof(action));
+       action.sa_handler = SIG_DFL;
+
+       sigaction(SIGSEGV, &action, NULL);
+       sigaction(SIGQUIT, &action, NULL);
+}
+
+void fault_setup(void)
+{
+       struct sigaction action;
+
+       memset(&action,0,sizeof(action));
+       action.sa_sigaction = fault_handler;
+       action.sa_flags = SA_SIGINFO;
+
+       sigaction(SIGSEGV, &action, NULL);
+       sigaction(SIGQUIT, &action, NULL);
+}
+
 int
 main(int argc, char *argv[])
 {
@@ -127,6 +195,8 @@ main(int argc, char *argv[])
 
   free (malloc (8)); /* -lefence */
 
+       fault_setup();
+
   gst_init_with_popt_table (&argc, &argv, options);
   
   /* make a null-terminated version of argv */
@@ -190,3 +260,4 @@ main(int argc, char *argv[])
 
   return 0;
 }
+