[MPlayer-dev-eng] [PATCH] automatic gdb attaching

Reimar Döffinger Reimar.Doeffinger at stud.uni-karlsruhe.de
Tue Oct 19 22:16:16 CEST 2004


Hi,

> > the attached patch will try to attach gdb on receiving an "excpetional" 
> > signal. You can also trigger this by sending a SIGTRAP (might be useful 
> > for debugging hangs).
> > The main purpose of this is helping in debugging rare bugs. So you can
> > just go about playing your files, and if that bug occurs you can right
> > start developing ;-).
> > Of course, the code should not be enabled by default as it currently is,
> > and the gdb path should probably be detected by configure as well...
> > Please commment...
> 
> not a bad idea but you're right it should be optional, and
> configurable from the runtime config file as well as at compiletime.

How about the attached version?

> also imo it should use execlp for running gdb, so it uses the user's
> path. anything else seems dumb to me.

Fixed.
I would recommend all developers to test it as I intend to make it
default for everyone with a .developer file ;-)

Greetings,
Reimar Döffinger
-------------- next part --------------
? gdb_att.diff
Index: cfg-mplayer.h
===================================================================
RCS file: /cvsroot/mplayer/main/cfg-mplayer.h,v
retrieving revision 1.229
diff -u -r1.229 cfg-mplayer.h
--- cfg-mplayer.h	15 Sep 2004 13:37:49 -0000	1.229
+++ cfg-mplayer.h	19 Oct 2004 09:45:55 -0000
@@ -329,6 +329,10 @@
 
 //---------------------- mplayer-only options ------------------------
 
+#ifdef CRASH_DEBUG
+	{"crash-debug", &crash_debug, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL},
+	{"nocrash-debug", &crash_debug, CONF_TYPE_FLAG, CONF_GLOBAL, 1, 0, NULL},
+#endif
 	{"osdlevel", &osd_level, CONF_TYPE_INT, CONF_RANGE, 0, 3, NULL},
 #ifdef HAVE_MENU
 	{"menu", &use_menu, CONF_TYPE_FLAG, CONF_GLOBAL, 0, 1, NULL},
Index: configure
===================================================================
RCS file: /cvsroot/mplayer/main/configure,v
retrieving revision 1.921
diff -u -r1.921 configure
--- configure	18 Oct 2004 20:29:17 -0000	1.921
+++ configure	19 Oct 2004 09:45:55 -0000
@@ -302,6 +302,7 @@
   --enable-debug[=1-3]   compile debugging information into mplayer [disable]
   --enable-profile       compile profiling information into mplayer [disable]
   --disable-sighandler   disable sighandler for crashes [enable]
+  --enable-crash-debug   enable automatic gdb attach on crash [disable]
   --enable-i18n          _experimental_ gnu gettext() support [autodetect]
   --enable-dynamic-plugins  Enable support for dynamic a/v plugins [disable]
 
@@ -1338,6 +1339,7 @@
 _i18n=auto
 _dynamic_plugins=no
 _setlocale=auto
+_crash_debug=no
 _sighandler=yes
 _libdv=auto
 _cdparanoia=auto
@@ -1677,6 +1679,12 @@
   --disable-debug)
     _debug=
     ;;
+  --enable-crash-debug)
+    _crash_debug=yes
+    ;;
+  --disable-crash-debug)
+    _crash_debug=no
+    ;;
   --enable-sighandler)
     _sighandler=yes
     ;;
@@ -6125,6 +6133,7 @@
   # always compile with '-g' if .developer:
   if test -f ".developer" ; then
     CFLAGS="-g $CFLAGS"
+    _crash_debug=yes
     _stripbinaries=no
   fi
 else
@@ -6139,6 +6148,15 @@
 
 EOF
 fi
+
+echocheck "automatic gdb attach"
+if test "$_sighandler" = yes ; then
+  _def_crash_debug='#define CRASH_DEBUG 1'
+else
+  _def_crash_debug='#undef CRASH_DEBUG'
+fi
+echores "$_sighandler"
+
 if darwin ; then
   CFLAGS="$CFLAGS -DSYS_DARWIN"
   if [ "$_cc_major" = 3 ] && [ "$_cc_minor" -lt 1 ]; then
@@ -6532,6 +6550,9 @@
 /* enable/disable SIGHANDLER */
 $_def_sighandler
 
+/* enable/disable automatic gdb attach on crash, requires SIGHANDLER */
+$_def_crash_debug
+
 /* Toggles debugging informations */
 $_def_debug
 
Index: mplayer.c
===================================================================
RCS file: /cvsroot/mplayer/main/mplayer.c,v
retrieving revision 1.796
diff -u -r1.796 mplayer.c
--- mplayer.c	18 Oct 2004 21:16:45 -0000	1.796
+++ mplayer.c	19 Oct 2004 09:45:55 -0000
@@ -504,8 +504,16 @@
 }
 #endif
 
+#ifdef CRASH_DEBUG
+static char *prog_path;
+static int crash_debug = 1;
+#endif
+
 static void exit_sighandler(int x){
   static int sig_count=0;
+#ifdef CRASH_DEBUG
+  if (!crash_debug || x != SIGTRAP)
+#endif
   ++sig_count;
   if(inited_flags==0 && sig_count>1) exit(1);
   if(sig_count==5)
@@ -527,7 +535,7 @@
   mp_msg(MSGT_CPLAYER,MSGL_FATAL,"\n" MSGTR_IntBySignal,x,
       current_module?current_module:mp_gettext("unknown")
   );
-  if(sig_count==1)
+  if(sig_count<=1)
   switch(x){
   case SIGINT:
   case SIGQUIT:
@@ -545,6 +553,26 @@
       mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGSEGV_SIGFPE);
   default:
       mp_msg(MSGT_CPLAYER,MSGL_FATAL,MSGTR_Exit_SIGCRASH);
+#ifdef CRASH_DEBUG
+      if (crash_debug) {
+        int gdb_pid;
+        char spid[20];
+        snprintf(spid, 19, "%i", getpid());
+        spid[19] = 0;
+        mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forking...\n");
+        gdb_pid = fork();
+        mp_msg(MSGT_CPLAYER, MSGL_INFO, "Forked...\n");
+        if (gdb_pid == 0) { // We are the child
+          if (execlp("gdb", "gdb", prog_path, spid, NULL) == -1)
+            mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't start gdb\n");
+        } else if (gdb_pid < 0) 
+          mp_msg(MSGT_CPLAYER, MSGL_ERR, "Couldn't fork\n");
+        else {
+          waitpid(gdb_pid, NULL, 0);
+        }
+        if (x == SIGTRAP) return;
+      }
+#endif  
   }
   exit_player(NULL);
 }
@@ -1176,6 +1204,9 @@
   signal(SIGCHLD,child_sighandler);
 #endif
 
+#ifdef CRASH_DEBUG
+  prog_path = argv[0];
+#endif
   //========= Catch terminate signals: ================
   // terminate requests:
   signal(SIGTERM,exit_sighandler); // kill
@@ -1191,6 +1222,10 @@
   signal(SIGILL,exit_sighandler);  // illegal instruction
   signal(SIGFPE,exit_sighandler);  // floating point exc.
   signal(SIGABRT,exit_sighandler); // abort()
+#ifdef CRASH_DEBUG
+  if (crash_debug)
+    signal(SIGTRAP,exit_sighandler);
+#endif
 #endif
 
 #ifdef HAVE_NEW_GUI


More information about the MPlayer-dev-eng mailing list