Skip to contentSkip to conversation

Exception trace for i386, mark II

PATCH New Andi Kleen 1 participant 1 message May 31, 2003 Proc sysctl

lore
Share & tools
lore

Andi Kleen

author
#0 lore raw

PATCH 1/1 Exception trace for i386, mark II

This is a new implementation of exception trace for i386.

It adds a new exception-trace sysctl (default to off), which when enabled triggers printk for unhandled fault signals (SIGSEGV etc.). This is very useful to catch silent segfaults, e.g. from programs who run in unwritable directories and cannot coredump. It's also very useful for running LTP. In general it gives you an early warning when something is wrong with your system.

It won't trigger on intentional signal users who catch the signal or when an debugger is active. This also makes the similar facility which has been in amd64 for a long time switchable using the sysctl. The default behaviour does not change.

-Andi

Index: linux/arch/i386/kernel/traps.c
===================================================================
RCS file: /home/cvs/linux-2.5/arch/i386/kernel/traps.c,v retrieving revision 1.62

arch/i386/kernel/traps.c +19 -3
diff -u -u -r1.62 traps.c
@@ -53,6 +53,8 @@
 
 #include "mach_traps.h"
 
+extern int exception_trace;
+
 asmlinkage int system_call(void);
 asmlinkage void lcall7(void);
 asmlinkage void lcall27(void);
@@ -303,6 +305,13 @@
 
 	trap_signal: {
 		struct task_struct *tsk = current;
+
+		if (exception_trace && !(tsk->flags & PT_PTRACED) && 
+		    (tsk->sighand->action[signr-1].sa.sa_handler == SIG_IGN ||
+		     (tsk->sighand->action[signr-1].sa.sa_handler == SIG_DFL)))   
+			printk(KERN_INFO "%s[%d] trap %s at eip:%lx esp:%lx err:%lx\n",
+		       tsk->comm, tsk->pid, str, regs->eip, regs->esp, error_code);
+			
 		tsk->thread.error_code = error_code;
 		tsk->thread.trap_no = trapnr;
 		if (info)
@@ -372,15 +381,22 @@
 
 asmlinkage void do_general_protection(struct pt_regs * regs, long error_code)
 {
+	struct task_struct *tsk = current;
 	if (regs->eflags & VM_MASK)
 		goto gp_in_vm86;
 
 	if (!(regs->xcs & 3))
 		goto gp_in_kernel;
 
-	current->thread.error_code = error_code;
-	current->thread.trap_no = 13;
-	force_sig(SIGSEGV, current);
+	if (exception_trace && !(tsk->flags & PT_PTRACED) && 
+	    (tsk->sighand->action[SIGSEGV-1].sa.sa_handler == SIG_IGN ||
+	     (tsk->sighand->action[SIGSEGV-1].sa.sa_handler == SIG_DFL)))
+		printk(KERN_INFO "%s[%d] gpf at eip:%lx esp:%lx err:%lx\n",
+		       tsk->comm, tsk->pid, regs->eip, regs->esp, error_code);
+	
+	tsk->thread.error_code = error_code;
+	tsk->thread.trap_no = 13;
+	force_sig(SIGSEGV, tsk);
 	return;
 
 gp_in_vm86:

Index: linux/arch/i386/mm/fault.c
===================================================================
RCS file: /home/cvs/linux-2.5/arch/i386/mm/fault.c,v retrieving revision 1.35

arch/i386/mm/fault.c +11 -0
diff -u -u -r1.35 fault.c
@@ -57,6 +57,8 @@
 
 asmlinkage void do_invalid_op(struct pt_regs *, unsigned long);
 
+int exception_trace;
+
 /*
  * This routine handles page faults.  It determines the address,
  * and the problem, and then passes it off to one of the appropriate
@@ -200,6 +202,15 @@
 
 	/* User mode accesses just cause a SIGSEGV */
 	if (error_code & 4) {
+
+		if (exception_trace && !(tsk->flags & PT_PTRACED) && 
+		    (tsk->sighand->action[SIGSEGV-1].sa.sa_handler == SIG_IGN ||
+		    (tsk->sighand->action[SIGSEGV-1].sa.sa_handler == SIG_DFL)))
+			printk(KERN_INFO 
+		       "%s[%d] segfault at eip:%lx esp:%lx adr:%lx err:%lx\n",
+		       tsk->comm, tsk->pid, regs->eip, regs->esp, address, 
+		       error_code);
+
 		tsk->thread.cr2 = address;
 		tsk->thread.error_code = error_code;
 		tsk->thread.trap_no = 14;

Index: linux/include/linux/sysctl.h
===================================================================
RCS file: /home/cvs/linux-2.5/include/linux/sysctl.h,v retrieving revision 1.52

include/linux/sysctl.h +1 -0
diff -u -u -r1.52 sysctl.h
@@ -130,6 +130,7 @@
 	KERN_PIDMAX=55,		/* int: PID # limit */
   	KERN_CORE_PATTERN=56,	/* string: pattern for core-file names */
 	KERN_PANIC_ON_OOPS=57,  /* int: whether we will panic on an oops */
+	KERN_EXCEPTION_TRACE=58, /* int: log user traps in kernel log */
 };
 
 

Index: linux/kernel/sysctl.c
===================================================================
RCS file: /home/cvs/linux-2.5/kernel/sysctl.c,v
retrieving revision 1.50

kernel/sysctl.c +5 -0
diff -u -u -r1.50 sysctl.c
@@ -57,6 +57,7 @@
 extern int cad_pid;
 extern int pid_max;
 extern int sysctl_lower_zone_protection;
+extern int exception_trace;
 
 /* this is needed for the proc_dointvec_minmax for [fs_]overflow UID and GID */
 static int maxolduid = 65535;
@@ -265,6 +266,10 @@
 	 0600, NULL, &proc_dointvec},
 	{KERN_PANIC_ON_OOPS,"panic_on_oops",
 	 &panic_on_oops,sizeof(int),0644,NULL,&proc_dointvec},
+#ifdef CONFIG_X86
+	{KERN_EXCEPTION_TRACE,"exception_trace",
+	 &exception_trace,sizeof(int),0644,NULL,&proc_dointvec},
+#endif
 	{0}
 };