浏览代码

crash handler that launches gdb. activated when started with -D option.
simplifies debugging crashes during the reg-tests.

Oswald Buddenhagen 19 年之前
父节点
当前提交
ab11737b33
共有 1 个文件被更改,包括 37 次插入0 次删除
  1. 37 0
      src/main.c

+ 37 - 0
src/main.c

@@ -26,6 +26,9 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
+#include <fcntl.h>
+#include <signal.h>
+#include <sys/wait.h>
 
 int Pid;		/* for maildir and imap */
 char Hostname[256];	/* for maildir */
@@ -79,6 +82,34 @@ PACKAGE " " VERSION " - mailbox synchronizer\n"
 	exit( code );
 }
 
+static void
+crashHandler( int n )
+{
+	int dpid;
+	char pbuf[10], pabuf[20];
+
+	close( 0 );
+	open( "/dev/tty", O_RDWR );
+	dup2( 0, 1 );
+	dup2( 0, 2 );
+	fprintf( stderr, "*** " EXE " caught signal %d. Starting debugger ...\n", n );
+	switch ((dpid = fork ())) {
+	case -1:
+		perror( "fork()" );
+		break;
+	case 0:
+		sprintf( pbuf, "%d", Pid );
+		sprintf( pabuf, "/proc/%d/exe", Pid );
+		execlp( "gdb", "gdb", pabuf, pbuf, (char *)0 );
+		perror( "execlp()" );
+		_exit( 1 );
+	default:
+		waitpid( dpid, 0, 0 );
+		break;
+	}
+	exit( 3 );
+}
+
 static int
 matches( const char *t, const char *p )
 {
@@ -385,6 +416,12 @@ main( int argc, char **argv )
 		}
 	}
 
+	if (DFlags & DEBUG) {
+		signal( SIGSEGV, crashHandler );
+		signal( SIGBUS, crashHandler );
+		signal( SIGILL, crashHandler );
+	}
+
 	if (merge_ops( cops, ops ))
 		return 1;