Skip to contentSkip to conversation

selective signal ptracing

RFC PATCH In review John Blackwood 5 participants 8 messages Jun 15, 2007 – Jun 19, 2007

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 selective signal ptracing

Selective Ptraced Signal Support - A proposed enhancement

This is a proposal for a ptrace enhancement that adds two new ptrace(2) commands that let a debugger view and modify the set of signals that are being ptraced.

By default all signals are ptraced as before. However, a debugger may now modify the set of per-task ptraced signals, where only the signals in this ptrace signal mask will be ptraced.

Signals that are not being ptraced will be handled normally by the ptraced process, and no notification will be passed on to the debugger process. Only ptraced signals that are received by the ptraced task will cause that task to stop and notify the debugger.

This new ptrace selective signal support provides a method for improving debug session performance and for reducing the amount of ptrace-related interference of a ptraced process, particularly when an an application is making use of one or more signals that are not currently of interest to the debug session, but are occuring frequently.

The command interfaces are:

long int ptrace(enum __ptrace_request request,
pid_t target_pid, void *addr, void *data)

request:
When PTRACE_SETSIGTRACE is specified, then the set of signals in the sigset_t structure (pointed to by the data parameter) will become the set of signals that are ptraced for the target process.

When PTRACE_GETSIGTRACE is specified, then the current set of ptraced signals will be returned at the sigset_t structure location (pointed to by the data parameter).

pid: pid of the target/ptraced task

addr: not used

data: pointer to a sigset_t structure

Tasks that have the PTRACE_O_TRACEFORK, PTRACE_O_TRACECLONE or PTRACE_O_TRACECLONE option set at the time of a fork, vfork or clone call that are currently being ptraced will yield child tasks that inherited their parent task's ptrace signal mask.

When single-stepping and a user's signal handler is executed, the ptraced task only single-steps/stops in the signal handler if that signal is in the ptraced signal mask. Otherwise, the ptraced task will execute the entire signal handler and then stop at the next instruction in the normal (non-signal related) execution flow.

The proposed patch below implements ptrace selective signal ptracing for the i386 and x86_64 architecutres. Other architectures will ptrace all signals, in the same way as before.

arch/i386/kernel/ptrace.c +38 -0
diff -rup linux-2.6.22-rc4-git5.orig/arch/i386/kernel/ptrace.c linux-2.6.22-rc4-git5.new/arch/i386/kernel/ptrace.c
@@ -620,6 +620,44 @@ long arch_ptrace(struct task_struct *chi
 					(struct user_desc __user *) data);
 		break;
 
+	case PTRACE_GETSIGTRACE: { /* Get the set of ptraced signals. */
+		int i;
+
+		if (!access_ok(VERIFY_WRITE, datap, sizeof(sigset_t))) {
+			ret = -EIO;
+			break;
+		}
+		for (i = 0; i < _NSIG_WORDS; i++) {
+			ret = __put_user(child->ptrace_sigmask.sig[i], datap);
+			if (ret)
+				break;
+			datap++;
+		}
+		break;
+	}
+
+	case PTRACE_SETSIGTRACE: { /* Set ptraced signal mask. */
+		int i;
+		sigset_t new_smask;
+
+		if (!access_ok( VERIFY_READ, datap, sizeof(sigset_t))) {
+			ret = -EIO;
+			break;
+		}
+		for (i = 0; i < _NSIG_WORDS; i++) {
+			ret = __get_user(new_smask.sig[i], datap);
+			if (ret)
+				break;
+			datap++;
+		}
+		if (ret)
+			break;
+
+		/* Update child task's ptrace signal mask. */
+		ptrace_update_traced_signals(child, &new_smask);
+		break;
+	}
+
 	default:
 		ret = ptrace_request(child, request, addr, data);
 		break;
arch/i386/kernel/signal.c +14 -5
diff -rup linux-2.6.22-rc4-git5.orig/arch/i386/kernel/signal.c linux-2.6.22-rc4-git5.new/arch/i386/kernel/signal.c
@@ -393,8 +393,12 @@ static int setup_frame(int sig, struct k
 	 * handler too.
 	 */
 	regs->eflags &= ~TF_MASK;
-	if (test_thread_flag(TIF_SINGLESTEP))
-		ptrace_notify(SIGTRAP);
+	if (test_thread_flag(TIF_SINGLESTEP)) {
+		if (sigismember(&current->ptrace_sigmask, sig))
+			ptrace_notify(SIGTRAP);
+		else
+			clear_thread_flag(TIF_SINGLESTEP);
+	}
 
 #if DEBUG_SIG
 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
@@ -487,8 +491,12 @@ static int setup_rt_frame(int sig, struc
 	 * handler too.
 	 */
 	regs->eflags &= ~TF_MASK;
-	if (test_thread_flag(TIF_SINGLESTEP))
-		ptrace_notify(SIGTRAP);
+	if (test_thread_flag(TIF_SINGLESTEP)) {
+		if (sigismember(&current->ptrace_sigmask, sig))
+			ptrace_notify(SIGTRAP);
+		else
+			clear_thread_flag(TIF_SINGLESTEP);
+	}
 
 #if DEBUG_SIG
 	printk("SIG deliver (%s:%d): sp=%p pc=%p ra=%p\n",
@@ -538,7 +546,8 @@ handle_signal(unsigned long sig, siginfo
 	 * that register information in the sigcontext is correct.
 	 */
 	if (unlikely(regs->eflags & TF_MASK)
-	    && likely(current->ptrace & PT_DTRACE)) {
+	    && likely(current->ptrace & PT_DTRACE)
+	    && sigismember(&current->ptrace_sigmask, sig)) {
 		current->ptrace &= ~PT_DTRACE;
 		regs->eflags &= ~TF_MASK;
 	}
arch/x86_64/ia32/ia32_signal.c +12 -4
diff -rup linux-2.6.22-rc4-git5.orig/arch/x86_64/ia32/ia32_signal.c linux-2.6.22-rc4-git5.new/arch/x86_64/ia32/ia32_signal.c
@@ -495,8 +495,12 @@ int ia32_setup_frame(int sig, struct k_s
 
 	set_fs(USER_DS);
 	regs->eflags &= ~TF_MASK;
-	if (test_thread_flag(TIF_SINGLESTEP))
-		ptrace_notify(SIGTRAP);
+	if (test_thread_flag(TIF_SINGLESTEP)) {
+		if (sigismember(&current->ptrace_sigmask, sig))
+			ptrace_notify(SIGTRAP);
+		else
+			clear_thread_flag(TIF_SINGLESTEP);
+	}
 
 #if DEBUG_SIG
 	printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
@@ -601,8 +605,12 @@ int ia32_setup_rt_frame(int sig, struct
 
 	set_fs(USER_DS);
 	regs->eflags &= ~TF_MASK;
-	if (test_thread_flag(TIF_SINGLESTEP))
-		ptrace_notify(SIGTRAP);
+	if (test_thread_flag(TIF_SINGLESTEP)) {
+		if (sigismember(&current->ptrace_sigmask, sig))
+			ptrace_notify(SIGTRAP);
+		else
+			clear_thread_flag(TIF_SINGLESTEP);
+	}
 
 #if DEBUG_SIG
 	printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%u\n",
arch/x86_64/ia32/ptrace32.c +2 -0
diff -rup linux-2.6.22-rc4-git5.orig/arch/x86_64/ia32/ptrace32.c linux-2.6.22-rc4-git5.new/arch/x86_64/ia32/ptrace32.c
@@ -247,6 +247,8 @@ asmlinkage long sys32_ptrace(long reques
 	case PTRACE_SETOPTIONS:
 	case PTRACE_SET_THREAD_AREA:
 	case PTRACE_GET_THREAD_AREA:
+	case PTRACE_SETSIGTRACE:
+	case PTRACE_GETSIGTRACE:
 		return sys_ptrace(request, pid, addr, data);
 
 	default:
arch/x86_64/kernel/ptrace.c +42 -0

48 lines not on this page · raw message

arch/x86_64/kernel/signal.c +9 -3
diff -rup linux-2.6.22-rc4-git5.orig/arch/x86_64/kernel/signal.c linux-2.6.22-rc4-git5.new/arch/x86_64/kernel/signal.c
@@ -297,8 +297,13 @@ static int setup_rt_frame(int sig, struc
 	set_fs(USER_DS);
 
 	regs->eflags &= ~TF_MASK;
-	if (test_thread_flag(TIF_SINGLESTEP))
-		ptrace_notify(SIGTRAP);
+	if (test_thread_flag(TIF_SINGLESTEP)) {
+		if (sigismember(&current->ptrace_sigmask, sig))
+			ptrace_notify(SIGTRAP);
+		else
+			clear_thread_flag(TIF_SINGLESTEP);
+	}
+
 #ifdef DEBUG_SIG
 	printk("SIG deliver (%s:%d): sp=%p pc=%lx ra=%p\n",
 		current->comm, current->pid, frame, regs->rip, frame->pretcode);
@@ -355,7 +360,8 @@ handle_signal(unsigned long sig, siginfo
 	 * correct.
 	 */
 	if (unlikely(regs->eflags & TF_MASK)) {
-		if (likely(current->ptrace & PT_DTRACE)) {
+		if (likely(current->ptrace & PT_DTRACE)
+		    && sigismember(&current->ptrace_sigmask, sig)) {
 			current->ptrace &= ~PT_DTRACE;
 			regs->eflags &= ~TF_MASK;
 		}
include/asm-i386/ptrace-abi.h +3 -0
diff -rup linux-2.6.22-rc4-git5.orig/include/asm-i386/ptrace-abi.h linux-2.6.22-rc4-git5.new/include/asm-i386/ptrace-abi.h
@@ -36,4 +36,7 @@
 #define PTRACE_SYSEMU		  31
 #define PTRACE_SYSEMU_SINGLESTEP  32
 
+#define PTRACE_GETSIGTRACE        33
+#define PTRACE_SETSIGTRACE        34
+
 #endif
include/asm-x86_64/ptrace-abi.h renamed from include/asm-x86_64/ptrace-abi.h 2007-04-25 23:08:32.000000000 -0400 +3 -0
diff -rup linux-2.6.22-rc4-git5.orig/include/asm-x86_64/ptrace-abi.h linux-2.6.22-rc4-git5.new/include/asm-x86_64/ptrace-abi.h
@@ -48,4 +48,7 @@
 
 #define PTRACE_ARCH_PRCTL	  30	/* arch_prctl for child */
 
+#define PTRACE_GETSIGTRACE        33
+#define PTRACE_SETSIGTRACE        34
+
 #endif
include/linux/ptrace.h +0 -0
diff -rup linux-2.6.22-rc4-git5.orig/include/linux/ptrace.h linux-2.6.22-rc4-git5.new/include/linux/ptrace.h
@@ -92,6 +92,7 @@ extern void ptrace_disable(struct task_s
 extern int ptrace_check_attach(struct task_struct *task, int kill);
 extern int ptrace_request(struct task_struct *child, long request, 

long addr, long data);
extern void ptrace_notify(int exit_code);
+extern void ptrace_update_traced_signals(struct task_struct *child, sigset_t *smask);
extern void __ptrace_link(struct task_struct *child,
struct task_struct *new_parent);
extern void __ptrace_unlink(struct task_struct *child);

include/linux/sched.h +2 -0
diff -rup linux-2.6.22-rc4-git5.orig/include/linux/sched.h linux-2.6.22-rc4-git5.new/include/linux/sched.h
@@ -1029,6 +1029,8 @@ struct task_struct {
 
 	unsigned long ptrace_message;
 	siginfo_t *last_siginfo; /* For ptrace use.  */
+
+	sigset_t ptrace_sigmask; /* Only signals in this mask are ptraced. */
 /*
  * current io wait handle: wait queue entry to use for io waits
  * If this thread is processing aio, this points at the waitqueue
kernel/exit.c +12 -0
diff -rup linux-2.6.22-rc4-git5.orig/kernel/exit.c linux-2.6.22-rc4-git5.new/kernel/exit.c
@@ -604,6 +604,18 @@ choose_new_parent(struct task_struct *p,
 	p->real_parent = reaper;
 }
 
+static inline void
+ptrace_sigmask_exit(struct task_struct *p, int ptraceflags)
+{
+	sigset_t new_sigmask;
+
+	if (!(ptraceflags & PT_PTRACED))
+		return;
+	/* Clear ptrace signal mask. */
+	sigemptyset(&new_sigmask);
+	ptrace_update_traced_signals(p, &new_sigmask);
+}
+
 static void
 reparent_thread(struct task_struct *p, struct task_struct *father, int 

traced)
{
@@ -621,7 +633,9 @@ reparent_thread(struct task_struct *p, s
/* If this child is being traced, then we're the one tracing it
* anyway, so let go of it.
*/
+ int ptrace = p->ptrace;
p->ptrace = 0;
+ ptrace_sigmask_exit(p, ptrace);
remove_parent(p);
p->parent = p->real_parent;
add_parent(p);
@@ -716,6 +730,7 @@ forget_original_parent(struct task_struc
} else {
/* reparent ptraced task to its real parent */ __ptrace_unlink (p);
+ ptrace_sigmask_exit(p, ptrace);
if (p->exit_state == EXIT_ZOMBIE && p->exit_signal != -1 &&
thread_group_empty(p))
do_notify_parent(p, p->exit_signal);

kernel/fork.c +11 -0
diff -rup linux-2.6.22-rc4-git5.orig/kernel/fork.c linux-2.6.22-rc4-git5.new/kernel/fork.c
@@ -1168,6 +1168,17 @@ static struct task_struct *copy_process(
 	p->exit_state = 0;
 
 	/*
+	 * If child is being ptraced, then inherit the ptrace signal mask
+	 * from the parent.  When the ptrace signal mask support is not
+	 * used, all signals are ptraced and inherited by the child process.
+	 *
+	 * Clear the ptrace signal mask if this child process is not being
+	 * ptraced.
+	 */
+	if (!(p->ptrace & PT_PTRACED))
+		sigemptyset(&p->ptrace_sigmask);	/* no ptracing */
+
+	/*
 	 * Ok, make it visible to the rest of the system.
 	 * We dont wake it up yet.
 	 */
kernel/ptrace.c +3 -0
diff -rup linux-2.6.22-rc4-git5.orig/kernel/ptrace.c linux-2.6.22-rc4-git5.new/kernel/ptrace.c
@@ -206,6 +206,9 @@ repeat:
 	if (capable(CAP_SYS_PTRACE))
 		task->ptrace |= PT_PTRACE_CAP;
 
+	/* All signals are ptraced by default. */
+	sigfillset(&task->ptrace_sigmask);
+
 	__ptrace_link(task, current);
 
 	force_sig_specific(SIGSTOP, task);
@@ -219,10 +222,17 @@ out:
 
 static inline void __ptrace_detach(struct task_struct *child, unsigned 

int data)
{
+ sigset_t new_smask;
+
child->exit_code = data;
- /* .. re-parent .. */
+ /* re-parent */
__ptrace_unlink(child);
- /* .. and wake it up. */
+
+ /* Clear the ptrace signal mask. */
+ sigemptyset(&new_smask);
+ ptrace_update_traced_signals(child, &new_smask);
+
+ /* Wake it up. */
if (child->exit_state != EXIT_ZOMBIE)
wake_up_process(child);
}
@@ -412,8 +422,11 @@ int ptrace_traceme(void)
/*
* Set the ptrace bit in the process ptrace flags. */
- if (!ret)
+ if (!ret) {
current->ptrace |= PT_PTRACED;
+ /* All signals are ptraced by default */
+ sigfillset(&current->ptrace_sigmask);
+ }
}
task_unlock(current);
return ret;

kernel/signal.c +55 -2

75 lines not on this page · raw message

#1 lore raw

I am not in favor of any enhancements to the ptrace interface. It is a terrible interface and just needs to die.

Thanks,
Roland

#2 lore raw
Quoted 2 lines · On Fri, 15 Jun 2007 12:42:29 -0700 (PDT) Roland McGrath <[email protected]>…

I am not in favor of any enhancements to the ptrace interface. It is a terrible interface and just needs to die.

That might make sense if utrace ever looked like it would solve the questions about platforms like ARM

#3 lore raw
Quoted 2 lines

That might make sense if utrace ever looked like it would solve the questions about platforms like ARM

It certainly will. The only difficult limitations have been in communication and understanding. Please don't perpetuate a generic red herring without adding any content to the subject.

Thanks,
Roland

#4 lore raw
In reply to Alan Cox
Quoted 8 lines · Alan Cox
Quoted 2 lines · On Fri, 15 Jun 2007 12:42:29 -0700 (PDT) Roland McGrath <[email protected]>…

I am not in favor of any enhancements to the ptrace interface. It is a terrible interface and just needs to die.

That might make sense if utrace ever looked like it would solve the questions about platforms like ARM

What are the issues with arch like ARM ? Also, is there a mail thread or paper somewhere describing utrace, its interfaces and what's needed to hook it up on a platform ?

/me mostly missed that train

Thanks !

Cheers,
Ben.

#5 lore raw
Quoted 1 line

What are the issues with arch like ARM ?

The interesting class ARM belongs to is machines that don't (or don't always) have hardware support for single-step. Maintaining the status quo of how PTRACE_SINGLESTEP functions on these machines is different in implementation under utrace than it is for machines that always use hardware support. That is the only special complication for ARM, and it is not really very complicated. Apparently the way I described the issue in the past was easily misunderstood.

Quoted 2 lines

Also, is there a mail thread or paper somewhere describing utrace, its interfaces and what's needed to hook it up on a platform ?

See http://redhat.com/~roland/utrace/ for the patches and a porting howto. The patches add a Documentation/ file and kerneldoc stuff for the interfaces.

Thanks,
Roland

#6 lore raw
Quoted 9 lines · Roland McGrath
Quoted 1 line

What are the issues with arch like ARM ?

The interesting class ARM belongs to is machines that don't (or don't always) have hardware support for single-step. Maintaining the status quo of how PTRACE_SINGLESTEP functions on these machines is different in implementation under utrace than it is for machines that always use hardware support. That is the only special complication for ARM, and it is not really very complicated. Apparently the way I described the issue in the past was easily misunderstood.

That isn't just ARM. There are some embedded PowerPC chips that lack an easily usable hardware single step. Just an FYI.

josh

#7 lore raw
Quoted 11 lines · Josh Boyer
Quoted 9 lines · Roland McGrath
Quoted 1 line

What are the issues with arch like ARM ?

The interesting class ARM belongs to is machines that don't (or don't always) have hardware support for single-step. Maintaining the status quo of how PTRACE_SINGLESTEP functions on these machines is different in implementation under utrace than it is for machines that always use hardware support. That is the only special complication for ARM, and it is not really very complicated. Apparently the way I described the issue in the past was easily misunderstood.

Quoted 2 lines

That isn't just ARM. There are some embedded PowerPC chips that lack an easily usable hardware single step. Just an FYI.

Note that we could use xmon infrastructure for that ... Paul has the whole shebang done in there, including emulating branches etc...

Ben.