History

ptrace: add ability to retrieve signals without removing from a queue (v4)

This patch adds a new ptrace request PTRACE_PEEKSIGINFO. This request is used to retrieve information about pending signals starting with the specified sequence number. Siginfo_t structures are copied from the child into the buffer starting at "data". The argument "addr" is a pointer to struct ptrace_peeksiginfo_args. struct ptrace_peeksiginfo_args { u64 off; /* from which siginfo to start */ u32 flags; s32 nr; /* how may siginfos to take */ }; "nr" has type "s32", because ptrace() returns "long", which has 32 bits on i386 and a negative values is used for errors. Currently here is only one flag PTRACE_PEEKSIGINFO_SHARED for dumping signals from process-wide queue. If this flag is not set, signals are read from a per-thread queue. The request PTRACE_PEEKSIGINFO returns a number of dumped signals. If a signal with the specified sequence number doesn't exist, ptrace returns zero. The request returns an error, if no signal has been dumped. Errors: EINVAL - one or more specified flags are not supported or nr is negative EFAULT - buf or addr is outside your accessible address space. A result siginfo contains a kernel part of si_code which usually striped, but it's required for queuing the same siginfo back during restore of pending signals. This functionality is required for checkpointing pending signals. Pedro Alves suggested using it in "gdb" to peek at pending signals. gdb already uses PTRACE_GETSIGINFO to get the siginfo for the signal which was already dequeued. This functionality allows gdb to look at the pending signals which were not reported yet. The prototype of this code was developed by Oleg Nesterov.

Andrey Vagin committed 13 years ago

Commit 84c751b1 parent 865f38a2 files · +92 −0Browse files

Unified · Split

2 files
  1. include/uapi/linux/ptrace.h +12 −0
  2. kernel/ptrace.c +80 −0
include/uapi/linux/ptrace.h+12−0
@@ -5,6 +5,7 @@ · 4 unmodified lines
55
66 /* has the defines to get at the registers. */
77
8+#include <linux/types.h>
89
910 #define PTRACE_TRACEME 0
1011 #define PTRACE_PEEKTEXT 1
@@ -52,6 +53,17 @@ · 41 unmodified lines
5253 #define PTRACE_INTERRUPT 0x4207
5354 #define PTRACE_LISTEN 0x4208
5455
56+#define PTRACE_PEEKSIGINFO 0x4209
57+
58+struct ptrace_peeksiginfo_args {
59+ __u64 off; /* from which siginfo to start */
60+ __u32 flags;
61+ __s32 nr; /* how may siginfos to take */
62+};
63+
64+/* Read signals from a shared (process wide) queue */
65+#define PTRACE_PEEKSIGINFO_SHARED (1 << 0)
66+
5567 /* Wait extended result codes for the above trace options. */
5668 #define PTRACE_EVENT_FORK 1
5769 #define PTRACE_EVENT_VFORK 2
kernel/ptrace.c+80−0
@@ -24,6 +24,7 @@ · 23 unmodified lines
2424 #include <linux/regset.h>
2525 #include <linux/hw_breakpoint.h>
2626 #include <linux/cn_proc.h>
27+#include <linux/compat.h>
2728
2829
2930 static int ptrace_trapping_sleep_fn(void *flags)
@@ -618,6 +619,81 @@ static int ptrace_setsiginfo(struct task_struct *child, const siginfo_t *info) · 588 unmodified lines
618619 return error;
619620 }
620621
622+static int ptrace_peek_siginfo(struct task_struct *child,
623+ unsigned long addr,
624+ unsigned long data)
625+{
626+ struct ptrace_peeksiginfo_args arg;
627+ struct sigpending *pending;
628+ struct sigqueue *q;
629+ int ret, i;
630+
631+ ret = copy_from_user(&arg, (void __user *) addr,
632+ sizeof(struct ptrace_peeksiginfo_args));
633+ if (ret)
634+ return -EFAULT;
635+
636+ if (arg.flags & ~PTRACE_PEEKSIGINFO_SHARED)
637+ return -EINVAL; /* unknown flags */
638+
639+ if (arg.nr < 0)
640+ return -EINVAL;
641+
642+ if (arg.flags & PTRACE_PEEKSIGINFO_SHARED)
643+ pending = &child->signal->shared_pending;
644+ else
645+ pending = &child->pending;
646+
647+ for (i = 0; i < arg.nr; ) {
648+ siginfo_t info;
649+ s32 off = arg.off + i;
650+
651+ spin_lock_irq(&child->sighand->siglock);
652+ list_for_each_entry(q, &pending->list, list) {
653+ if (!off--) {
654+ copy_siginfo(&info, &q->info);
655+ break;
656+ }
657+ }
658+ spin_unlock_irq(&child->sighand->siglock);
659+
660+ if (off >= 0) /* beyond the end of the list */
661+ break;
662+
663+#ifdef CONFIG_COMPAT
664+ if (unlikely(is_compat_task())) {
665+ compat_siginfo_t __user *uinfo = compat_ptr(data);
666+
667+ ret = copy_siginfo_to_user32(uinfo, &info);
668+ ret |= __put_user(info.si_code, &uinfo->si_code);
669+ } else
670+#endif
671+ {
672+ siginfo_t __user *uinfo = (siginfo_t __user *) data;
673+
674+ ret = copy_siginfo_to_user(uinfo, &info);
675+ ret |= __put_user(info.si_code, &uinfo->si_code);
676+ }
677+
678+ if (ret) {
679+ ret = -EFAULT;
680+ break;
681+ }
682+
683+ data += sizeof(siginfo_t);
684+ i++;
685+
686+ if (signal_pending(current))
687+ break;
688+
689+ cond_resched();
690+ }
691+
692+ if (i > 0)
693+ return i;
694+
695+ return ret;
696+}
621697
622698 #ifdef PTRACE_SINGLESTEP
623699 #define is_singlestep(request) ((request) == PTRACE_SINGLESTEP)
@@ -748,6 +824,10 @@ int ptrace_request(struct task_struct *child, long request, · 124 unmodified lines
748824 ret = put_user(child->ptrace_message, datalp);
749825 break;
750826
827+ case PTRACE_PEEKSIGINFO:
828+ ret = ptrace_peek_siginfo(child, addr, data);
829+ break;
830+
751831 case PTRACE_GETSIGINFO:
752832 ret = ptrace_getsiginfo(child, &siginfo);
753833 if (!ret)