public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* PTRACE_GET_THREAD_AREA
@ 2002-12-20  8:32 Roland McGrath
  2002-12-20 10:24 ` PTRACE_GET_THREAD_AREA Christoph Hellwig
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Roland McGrath @ 2002-12-20  8:32 UTC (permalink / raw)
  To: linux-kernel; +Cc: Ingo Molnar

This patch vs 2.5.51 (should apply fine to 2.5.52) adds two new ptrace
requests for i386, PTRACE_GET_THREAD_AREA and PTRACE_SET_THREAD_AREA.
These let another process using ptrace do the equivalent of performing
get_thread_area and set_thread_area system calls for another thread.

We are working on gdb support for the new threading code in the kernel
using the new NPTL library, and use PTRACE_GET_THREAD_AREA for that.
This patch has been working fine for that.

I added PTRACE_SET_THREAD_AREA just for completeness, so that you can
change all the state via ptrace that you can read via ptrace as has
previously been the case.  It doesn't have an equivalent of set_thread_area
with .entry_number = -1, but is otherwise the same.

Both requests use the ptrace `addr' argument for the entry number rather
than the entry_number field in the struct.  The `data' parameter gives the
address of a struct user_desc as used by the set/get_thread_area syscalls.

The code is quite simple, and doesn't need any special synchronization
because in the ptrace context the thread must be stopped already.

I chose the new request numbers arbitrarily from ones not used on i386.
I have no opinion on what values should be used.

People I talked to preferred adding this interface over putting an array of
struct user_desc in struct user as accessed by PTRACE_PEEKUSR/POKEUSR
(which would be a bit unnatural since those calls access one word at a time).


Thanks,
Roland



--- linux-2.5.51/include/asm-i386/ptrace.h.orig	Mon Dec  9 18:45:43 2002
+++ linux-2.5.51/include/asm-i386/ptrace.h	Thu Dec 12 04:42:06 2002
@@ -51,6 +51,10 @@ struct pt_regs {
 
 #define PTRACE_OLDSETOPTIONS         21
 
+#define PTRACE_GET_THREAD_AREA    25
+#define PTRACE_SET_THREAD_AREA    26
+
+
 #ifdef __KERNEL__
 #define user_mode(regs) ((VM_MASK & (regs)->eflags) || (3 & (regs)->xcs))
 #define instruction_pointer(regs) ((regs)->eip)
--- linux-2.5.51/arch/i386/kernel/ptrace.c.orig	Mon Dec  9 18:45:52 2002
+++ linux-2.5.51/arch/i386/kernel/ptrace.c	Thu Dec 12 04:42:12 2002
@@ -21,6 +21,8 @@
 #include <asm/processor.h>
 #include <asm/i387.h>
 #include <asm/debugreg.h>
+#include <asm/ldt.h>
+#include <asm/desc.h>
 
 /*
  * does not yet catch signals sent when the child dies.
@@ -416,6 +418,80 @@ asmlinkage int sys_ptrace(long request, 
 		break;
 	}
 
+	case PTRACE_GET_THREAD_AREA: {
+		int idx = addr;
+		struct user_desc info;
+		struct desc_struct *desc;
+
+/*
+ * Get the current Thread-Local Storage area:
+ */
+
+#define GET_BASE(desc) ( \
+	(((desc)->a >> 16) & 0x0000ffff) | \
+	(((desc)->b << 16) & 0x00ff0000) | \
+	( (desc)->b        & 0xff000000)   )
+
+#define GET_LIMIT(desc) ( \
+	((desc)->a & 0x0ffff) | \
+	 ((desc)->b & 0xf0000) )
+
+#define GET_32BIT(desc)		(((desc)->b >> 23) & 1)
+#define GET_CONTENTS(desc)	(((desc)->b >> 10) & 3)
+#define GET_WRITABLE(desc)	(((desc)->b >>  9) & 1)
+#define GET_LIMIT_PAGES(desc)	(((desc)->b >> 23) & 1)
+#define GET_PRESENT(desc)	(((desc)->b >> 15) & 1)
+#define GET_USEABLE(desc)	(((desc)->b >> 20) & 1)
+
+		if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) {
+			ret = -EINVAL;
+			break;
+		}
+
+		desc = child->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
+
+		info.entry_number = idx;
+		info.base_addr = GET_BASE(desc);
+		info.limit = GET_LIMIT(desc);
+		info.seg_32bit = GET_32BIT(desc);
+		info.contents = GET_CONTENTS(desc);
+		info.read_exec_only = !GET_WRITABLE(desc);
+		info.limit_in_pages = GET_LIMIT_PAGES(desc);
+		info.seg_not_present = !GET_PRESENT(desc);
+		info.useable = GET_USEABLE(desc);
+
+		if (copy_to_user((struct user_desc *) data,
+				 &info, sizeof(info)))
+			ret = -EFAULT;
+		break;
+	}
+
+	case PTRACE_SET_THREAD_AREA: {
+		int idx = addr;
+		struct user_desc info;
+		struct desc_struct *desc;
+
+		if (copy_from_user(&info,
+				   (struct user_desc *) data, sizeof(info))) {
+			ret = -EFAULT;
+			break;
+		}
+		if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX) {
+			ret = -EINVAL;
+			break;
+		}
+		desc = current->thread.tls_array + idx - GDT_ENTRY_TLS_MIN;
+		if (LDT_empty(&info)) {
+			desc->a = 0;
+			desc->b = 0;
+		} else {
+			desc->a = LDT_entry_a(&info);
+			desc->b = LDT_entry_b(&info);
+		}
+		ret = 0;
+		break;
+	}
+
 	default:
 		ret = ptrace_request(child, request, addr, data);
 		break;

^ permalink raw reply	[flat|nested] 13+ messages in thread
[parent not found: <3E22B2F0.31397.E719685@localhost>]

end of thread, other threads:[~2003-01-14  3:07 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-12-20  8:32 PTRACE_GET_THREAD_AREA Roland McGrath
2002-12-20 10:24 ` PTRACE_GET_THREAD_AREA Christoph Hellwig
2002-12-20 15:44   ` PTRACE_GET_THREAD_AREA Daniel Jacobowitz
2002-12-20 17:36   ` PTRACE_GET_THREAD_AREA Linus Torvalds
2002-12-20 15:48 ` PTRACE_GET_THREAD_AREA Daniel Jacobowitz
2002-12-20 15:55   ` PTRACE_GET_THREAD_AREA Jakub Jelinek
2002-12-20 16:08     ` PTRACE_GET_THREAD_AREA Daniel Jacobowitz
2002-12-20 21:27   ` PTRACE_GET_THREAD_AREA Roland McGrath
2002-12-20 17:42 ` PTRACE_GET_THREAD_AREA Linus Torvalds
2003-01-13  3:51   ` PTRACE_GET_THREAD_AREA Roland McGrath
2003-01-13  4:03     ` PTRACE_GET_THREAD_AREA Linus Torvalds
2003-01-13  5:29       ` PTRACE_GET_THREAD_AREA Roland McGrath
     [not found] <3E22B2F0.31397.E719685@localhost>
2003-01-14  3:15 ` PTRACE_GET_THREAD_AREA Roland McGrath

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox