[PATCH] improved ptrace syscall tracing (backward compatible)

From: Mike Coleman (mkc@kc.net)
Date: Mon Mar 13 2000 - 18:34:06 EST


At Pavel Machek's suggestion, I rewrote this patch to make it backward
compatible.

This patch allows the tracer to unambiguously distinguish between a ptrace
stop due to a system call in the traced process and a ptrace stop due to
delivery of SIGTRAP to the traced process. (SUBTERFUGUE requires this, and
strace would benefit, too, as it currently has to guess, AFAIK.)

The new functionality is enabled on a per-child basis by the ptracer, and is
disabled by default, thus preserving backward compatibility.

--Mike

--- linux-2.3.51/kernel/fork.c.orig Sat Mar 11 18:56:41 2000
+++ linux-2.3.51/kernel/fork.c Sat Mar 11 18:58:03 2000
@@ -577,7 +577,7 @@
         new_flags &= ~(PF_SUPERPRIV | PF_USEDFPU | PF_VFORK);
         new_flags |= PF_FORKNOEXEC;
         if (!(clone_flags & CLONE_PTRACE))
- new_flags &= ~(PF_PTRACED|PF_TRACESYS);
+ new_flags &= ~(PF_PTRACED|PF_TRACESYS|PF_TRACESYSGOOD);
         if (clone_flags & CLONE_VFORK)
                 new_flags |= PF_VFORK;
         p->flags = new_flags;
--- linux-2.3.51/kernel/exit.c.orig Sat Mar 11 18:56:41 2000
+++ linux-2.3.51/kernel/exit.c Sat Mar 11 18:58:03 2000
@@ -365,7 +365,7 @@
                 p = current->p_cptr;
                 current->p_cptr = p->p_osptr;
                 p->p_ysptr = NULL;
- p->flags &= ~(PF_PTRACED|PF_TRACESYS);
+ p->flags &= ~(PF_PTRACED|PF_TRACESYS|PF_TRACESYSGOOD);
 
                 p->p_pptr = p->p_opptr;
                 p->p_osptr = p->p_pptr->p_cptr;
--- linux-2.3.51/include/linux/sched.h.orig Sat Mar 11 18:54:21 2000
+++ linux-2.3.51/include/linux/sched.h Sat Mar 11 18:58:03 2000
@@ -376,6 +376,10 @@
 
 #define PF_USEDFPU 0x00100000 /* task used FPU this quantum (SMP) */
 #define PF_DTRACE 0x00200000 /* delayed trace (used on m68k, i386) */
+#define PF_TRACESYSGOOD 0x00400000 /* PF_TRACESYS: differentiate syscall
+ from signal stops. (This flag
+ provides backward compatibility
+ with old 'strace'.) */
 
 /*
  * Limit the stack by to some sane default: root can always
--- linux-2.3.51/include/asm-i386/ptrace.h.orig Tue Dec 9 19:57:09 1997
+++ linux-2.3.51/include/asm-i386/ptrace.h Sat Mar 11 19:22:14 2000
@@ -47,6 +47,12 @@
 #define PTRACE_GETFPREGS 14
 #define PTRACE_SETFPREGS 15
 
+#define PTRACE_SETOPTIONS 21
+
+/* options set using PTRACE_SETOPTIONS */
+#define PTRACE_O_TRACESYSGOOD 0x00000001
+
+
 #ifdef __KERNEL__
 #define user_mode(regs) ((VM_MASK & (regs)->eflags) || (3 & (regs)->xcs))
 #define instruction_pointer(regs) ((regs)->eip)
--- linux-2.3.51/arch/i386/kernel/ptrace.c.orig Sat Mar 11 18:50:43 2000
+++ linux-2.3.51/arch/i386/kernel/ptrace.c Sat Mar 11 19:27:50 2000
@@ -345,7 +345,7 @@
                 ret = -EIO;
                 if ((unsigned long) data > _NSIG)
                         break;
- child->flags &= ~(PF_PTRACED|PF_TRACESYS);
+ child->flags &= ~(PF_PTRACED|PF_TRACESYS|PF_TRACESYSGOOD);
                 child->exit_code = data;
                 write_lock_irqsave(&tasklist_lock, flags);
                 REMOVE_LINKS(child);
@@ -431,6 +431,15 @@
                 break;
         }
 
+ case PTRACE_SETOPTIONS: {
+ if (data & PTRACE_O_TRACESYSGOOD)
+ child->flags |= PF_TRACESYSGOOD;
+ else
+ child->flags &= ~PF_TRACESYSGOOD;
+ ret = 0;
+ break;
+ }
+
         default:
                 ret = -EIO;
                 break;
@@ -445,7 +454,10 @@
         if ((current->flags & (PF_PTRACED|PF_TRACESYS))
                         != (PF_PTRACED|PF_TRACESYS))
                 return;
- current->exit_code = SIGTRAP;
+ /* the 0x80 provides a way for the tracing parent to distinguish
+ between a syscall stop and SIGTRAP delivery */
+ current->exit_code = SIGTRAP | ((current->flags & PF_TRACESYSGOOD)
+ ? 0x80 : 0);
         current->state = TASK_STOPPED;
         notify_parent(current, SIGCHLD);
         schedule();

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/



This archive was generated by hypermail 2b29 : Wed Mar 15 2000 - 21:00:27 EST