kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -tip 2/6 V4] x86: add arch-dep register and stack access API  to ptrace
@ 2009-04-02 17:24 Masami Hiramatsu
  2009-04-02 23:48 ` Frederic Weisbecker
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-02 17:24 UTC (permalink / raw)
  To: Ingo Molnar, Frederic Weisbecker, Ananth N Mavinakayanahalli,
	Steven Rostedt
  Cc: Andrew Morton, Andi Kleen, Arnaldo Carvalho de Melo,
	Jim Keniston, systemtap-ml, LKML, kvm

Add following APIs for accessing registers and stack entries from pt_regs.
- query_register_offset(const char *name)
   Query the offset of "name" register.

- query_register_name(unsigned offset)
   Query the name of register by its offset.

- get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- valid_stack_address(struct pt_regs *regs, unsigned long addr)
   Check the address is in the stack.

- get_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the stack. (N >= 0)

- get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
---

 arch/x86/include/asm/ptrace.h |   66 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |   59 +++++++++++++++++++++++++++++++++++++
 2 files changed, 125 insertions(+), 0 deletions(-)


diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index aed0894..44773b8 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@

 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif

 #ifndef __ASSEMBLY__
@@ -215,6 +216,71 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }

+/* Query offset/name of register from its name/offset */
+extern int query_register_offset(const char *name);
+extern const char *query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, sp))
+
+/* Get register value from its offset */
+static inline unsigned long get_register(struct pt_regs *regs, unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/* Check the address in the stack */
+static inline int valid_stack_address(struct pt_regs *regs, unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_trap_sp(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/* Get Nth entry of the stack */
+static inline unsigned long get_stack_nth(struct pt_regs *regs, unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_trap_sp(regs);
+	addr += n;
+	if (valid_stack_address(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/* Get Nth argument at function call */
+static inline unsigned long get_argument_nth(struct pt_regs *regs, unsigned n)
+{
+#ifdef CONFIG_X86_32
+#define NR_REGPARMS 3
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->ax;
+		case 1: return regs->dx;
+		case 2: return regs->cx;
+		}
+		return 0;
+#else /* CONFIG_X86_64 */
+#define NR_REGPARMS 6
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->di;
+		case 1: return regs->si;
+		case 2: return regs->dx;
+		case 3: return regs->cx;
+		case 4: return regs->r8;
+		case 5: return regs->r9;
+		}
+		return 0;
+#endif
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return get_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 5c6e463..8d65dcb 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -46,6 +46,65 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };

+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET(r) offsetof(struct pt_regs, r)
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = REG_OFFSET(r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_END,
+};
+
+int query_register_offset(const char *name)
+{
+	struct pt_regs_offset *roff = regoffset_table;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return (unsigned)roff->offset;
+	return -EINVAL;
+}
+
+const char *query_register_name(unsigned offset)
+{
+	struct pt_regs_offset *roff = regoffset_table;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.
-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4] x86: add arch-dep register and stack  access API to ptrace
  2009-04-02 17:24 [PATCH -tip 2/6 V4] x86: add arch-dep register and stack access API to ptrace Masami Hiramatsu
@ 2009-04-02 23:48 ` Frederic Weisbecker
  2009-04-03  0:45   ` Masami Hiramatsu
  2009-04-03 16:02   ` [PATCH -tip 2/6 V4.1] " Masami Hiramatsu
  0 siblings, 2 replies; 9+ messages in thread
From: Frederic Weisbecker @ 2009-04-02 23:48 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Ananth N Mavinakayanahalli, Steven Rostedt,
	Andrew Morton, Andi Kleen, Arnaldo Carvalho de Melo,
	Jim Keniston, systemtap-ml, LKML, kvm

On Thu, Apr 02, 2009 at 01:24:47PM -0400, Masami Hiramatsu wrote:
> Add following APIs for accessing registers and stack entries from pt_regs.
> - query_register_offset(const char *name)
>    Query the offset of "name" register.
> 
> - query_register_name(unsigned offset)
>    Query the name of register by its offset.
> 
> - get_register(struct pt_regs *regs, unsigned offset)
>    Get the value of a register by its offset.
> 
> - valid_stack_address(struct pt_regs *regs, unsigned long addr)
>    Check the address is in the stack.
> 
> - get_stack_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth entry of the stack. (N >= 0)
> 
> - get_argument_nth(struct pt_regs *reg, unsigned nth)
>    Get Nth argument at function call. (N >= 0)
> 
> Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
> Cc: Steven Rostedt <rostedt@goodmis.org>
> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
> Cc: Ingo Molnar <mingo@elte.hu>
> Cc: Frederic Weisbecker <fweisbec@gmail.com>
> ---
> 
>  arch/x86/include/asm/ptrace.h |   66 +++++++++++++++++++++++++++++++++++++++++
>  arch/x86/kernel/ptrace.c      |   59 +++++++++++++++++++++++++++++++++++++
>  2 files changed, 125 insertions(+), 0 deletions(-)
> 
> 
> diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
> index aed0894..44773b8 100644
> --- a/arch/x86/include/asm/ptrace.h
> +++ b/arch/x86/include/asm/ptrace.h
> @@ -7,6 +7,7 @@
> 
>  #ifdef __KERNEL__
>  #include <asm/segment.h>
> +#include <asm/page_types.h>
>  #endif
> 
>  #ifndef __ASSEMBLY__
> @@ -215,6 +216,71 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
>  	return regs->sp;
>  }
> 
> +/* Query offset/name of register from its name/offset */
> +extern int query_register_offset(const char *name);
> +extern const char *query_register_name(unsigned offset);
> +#define MAX_REG_OFFSET (offsetof(struct pt_regs, sp))
> +
> +/* Get register value from its offset */
> +static inline unsigned long get_register(struct pt_regs *regs, unsigned offset)
> +{
> +	if (unlikely(offset > MAX_REG_OFFSET))
> +		return 0;
> +	return *(unsigned long *)((unsigned long)regs + offset);
> +}
> +
> +/* Check the address in the stack */
> +static inline int valid_stack_address(struct pt_regs *regs, unsigned long addr)
> +{
> +	return ((addr & ~(THREAD_SIZE - 1))  ==
> +		(kernel_trap_sp(regs) & ~(THREAD_SIZE - 1)));
> +}
> +
> +/* Get Nth entry of the stack */
> +static inline unsigned long get_stack_nth(struct pt_regs *regs, unsigned n)
> +{
> +	unsigned long *addr = (unsigned long *)kernel_trap_sp(regs);
> +	addr += n;
> +	if (valid_stack_address(regs, (unsigned long)addr))
> +		return *addr;
> +	else
> +		return 0;
> +}
> +
> +/* Get Nth argument at function call */
> +static inline unsigned long get_argument_nth(struct pt_regs *regs, unsigned n)
> +{
> +#ifdef CONFIG_X86_32
> +#define NR_REGPARMS 3
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0: return regs->ax;
> +		case 1: return regs->dx;
> +		case 2: return regs->cx;
> +		}
> +		return 0;
> +#else /* CONFIG_X86_64 */
> +#define NR_REGPARMS 6
> +	if (n < NR_REGPARMS) {
> +		switch (n) {
> +		case 0: return regs->di;
> +		case 1: return regs->si;
> +		case 2: return regs->dx;
> +		case 3: return regs->cx;
> +		case 4: return regs->r8;
> +		case 5: return regs->r9;
> +		}
> +		return 0;
> +#endif
> +	} else {
> +		/*
> +		 * The typical case: arg n is on the stack.
> +		 * (Note: stack[0] = return address, so skip it)
> +		 */
> +		return get_stack_nth(regs, 1 + n - NR_REGPARMS);
> +	}
> +}
> +
>  /*
>   * These are defined as per linux/ptrace.h, which see.
>   */
> diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
> index 5c6e463..8d65dcb 100644
> --- a/arch/x86/kernel/ptrace.c
> +++ b/arch/x86/kernel/ptrace.c
> @@ -46,6 +46,65 @@ enum x86_regset {
>  	REGSET_IOPERM32,
>  };
> 
> +struct pt_regs_offset {
> +	const char *name;
> +	int offset;
> +};
> +
> +#define REG_OFFSET(r) offsetof(struct pt_regs, r)
> +#define REG_OFFSET_NAME(r) {.name = #r, .offset = REG_OFFSET(r)}
> +#define REG_OFFSET_END {.name = NULL, .offset = 0}
> +
> +static struct pt_regs_offset regoffset_table[] = {
> +#ifdef CONFIG_X86_64
> +	REG_OFFSET_NAME(r15),
> +	REG_OFFSET_NAME(r14),
> +	REG_OFFSET_NAME(r13),
> +	REG_OFFSET_NAME(r12),
> +	REG_OFFSET_NAME(r11),
> +	REG_OFFSET_NAME(r10),
> +	REG_OFFSET_NAME(r9),
> +	REG_OFFSET_NAME(r8),
> +#endif
> +	REG_OFFSET_NAME(bx),
> +	REG_OFFSET_NAME(cx),
> +	REG_OFFSET_NAME(dx),
> +	REG_OFFSET_NAME(si),
> +	REG_OFFSET_NAME(di),
> +	REG_OFFSET_NAME(bp),
> +	REG_OFFSET_NAME(ax),
> +#ifdef CONFIG_X86_32
> +	REG_OFFSET_NAME(ds),
> +	REG_OFFSET_NAME(es),
> +	REG_OFFSET_NAME(fs),
> +	REG_OFFSET_NAME(gs),
> +#endif
> +	REG_OFFSET_NAME(orig_ax),
> +	REG_OFFSET_NAME(ip),
> +	REG_OFFSET_NAME(cs),



You seem to have also forgotten ss here.



> +	REG_OFFSET_NAME(flags),
> +	REG_OFFSET_NAME(sp),
> +	REG_OFFSET_END,
> +};
> +
> +int query_register_offset(const char *name)
> +{
> +	struct pt_regs_offset *roff = regoffset_table;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (!strcmp(roff->name, name))
> +			return (unsigned)roff->offset;



Tiny thing here: could you set the full (unsigned int) cast?



> +	return -EINVAL;
> +}
> +
> +const char *query_register_name(unsigned offset)
> +{
> +	struct pt_regs_offset *roff = regoffset_table;
> +	for (roff = regoffset_table; roff->name != NULL; roff++)
> +		if (roff->offset == offset)
> +			return roff->name;
> +	return NULL;
> +}
> +
>  /*
>   * does not yet catch signals sent when the child dies.
>   * in exit.c or in signal.c.


All in one it looks good!

Frederic.

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4] x86: add arch-dep register and stack access  API to ptrace
  2009-04-02 23:48 ` Frederic Weisbecker
@ 2009-04-03  0:45   ` Masami Hiramatsu
  2009-04-03 16:02   ` [PATCH -tip 2/6 V4.1] " Masami Hiramatsu
  1 sibling, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-03  0:45 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: Ingo Molnar, Ananth N Mavinakayanahalli, Steven Rostedt,
	Andrew Morton, Andi Kleen, Arnaldo Carvalho de Melo,
	Jim Keniston, systemtap-ml, LKML, kvm

Frederic Weisbecker wrote:
> On Thu, Apr 02, 2009 at 01:24:47PM -0400, Masami Hiramatsu wrote:
>> Add following APIs for accessing registers and stack entries from pt_regs.
>> - query_register_offset(const char *name)
>>    Query the offset of "name" register.
>>
>> - query_register_name(unsigned offset)
>>    Query the name of register by its offset.
>>
>> - get_register(struct pt_regs *regs, unsigned offset)
>>    Get the value of a register by its offset.
>>
>> - valid_stack_address(struct pt_regs *regs, unsigned long addr)
>>    Check the address is in the stack.
>>
>> - get_stack_nth(struct pt_regs *reg, unsigned nth)
>>    Get Nth entry of the stack. (N >= 0)
>>
>> - get_argument_nth(struct pt_regs *reg, unsigned nth)
>>    Get Nth argument at function call. (N >= 0)
>>
>> Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
>> Cc: Steven Rostedt <rostedt@goodmis.org>
>> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
>> Cc: Ingo Molnar <mingo@elte.hu>
>> Cc: Frederic Weisbecker <fweisbec@gmail.com>
>> ---
>>
>>  arch/x86/include/asm/ptrace.h |   66 +++++++++++++++++++++++++++++++++++++++++
>>  arch/x86/kernel/ptrace.c      |   59 +++++++++++++++++++++++++++++++++++++
>>  2 files changed, 125 insertions(+), 0 deletions(-)
>>
>>
>> diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
>> index aed0894..44773b8 100644
>> --- a/arch/x86/include/asm/ptrace.h
>> +++ b/arch/x86/include/asm/ptrace.h
>> @@ -7,6 +7,7 @@
>>
>>  #ifdef __KERNEL__
>>  #include <asm/segment.h>
>> +#include <asm/page_types.h>
>>  #endif
>>
>>  #ifndef __ASSEMBLY__
>> @@ -215,6 +216,71 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
>>  	return regs->sp;
>>  }
>>
>> +/* Query offset/name of register from its name/offset */
>> +extern int query_register_offset(const char *name);
>> +extern const char *query_register_name(unsigned offset);
>> +#define MAX_REG_OFFSET (offsetof(struct pt_regs, sp))
>> +
>> +/* Get register value from its offset */
>> +static inline unsigned long get_register(struct pt_regs *regs, unsigned offset)
>> +{
>> +	if (unlikely(offset > MAX_REG_OFFSET))
>> +		return 0;
>> +	return *(unsigned long *)((unsigned long)regs + offset);
>> +}
>> +
>> +/* Check the address in the stack */
>> +static inline int valid_stack_address(struct pt_regs *regs, unsigned long addr)
>> +{
>> +	return ((addr & ~(THREAD_SIZE - 1))  ==
>> +		(kernel_trap_sp(regs) & ~(THREAD_SIZE - 1)));
>> +}
>> +
>> +/* Get Nth entry of the stack */
>> +static inline unsigned long get_stack_nth(struct pt_regs *regs, unsigned n)
>> +{
>> +	unsigned long *addr = (unsigned long *)kernel_trap_sp(regs);
>> +	addr += n;
>> +	if (valid_stack_address(regs, (unsigned long)addr))
>> +		return *addr;
>> +	else
>> +		return 0;
>> +}
>> +
>> +/* Get Nth argument at function call */
>> +static inline unsigned long get_argument_nth(struct pt_regs *regs, unsigned n)
>> +{
>> +#ifdef CONFIG_X86_32
>> +#define NR_REGPARMS 3
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0: return regs->ax;
>> +		case 1: return regs->dx;
>> +		case 2: return regs->cx;
>> +		}
>> +		return 0;
>> +#else /* CONFIG_X86_64 */
>> +#define NR_REGPARMS 6
>> +	if (n < NR_REGPARMS) {
>> +		switch (n) {
>> +		case 0: return regs->di;
>> +		case 1: return regs->si;
>> +		case 2: return regs->dx;
>> +		case 3: return regs->cx;
>> +		case 4: return regs->r8;
>> +		case 5: return regs->r9;
>> +		}
>> +		return 0;
>> +#endif
>> +	} else {
>> +		/*
>> +		 * The typical case: arg n is on the stack.
>> +		 * (Note: stack[0] = return address, so skip it)
>> +		 */
>> +		return get_stack_nth(regs, 1 + n - NR_REGPARMS);
>> +	}
>> +}
>> +
>>  /*
>>   * These are defined as per linux/ptrace.h, which see.
>>   */
>> diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
>> index 5c6e463..8d65dcb 100644
>> --- a/arch/x86/kernel/ptrace.c
>> +++ b/arch/x86/kernel/ptrace.c
>> @@ -46,6 +46,65 @@ enum x86_regset {
>>  	REGSET_IOPERM32,
>>  };
>>
>> +struct pt_regs_offset {
>> +	const char *name;
>> +	int offset;
>> +};
>> +
>> +#define REG_OFFSET(r) offsetof(struct pt_regs, r)
>> +#define REG_OFFSET_NAME(r) {.name = #r, .offset = REG_OFFSET(r)}
>> +#define REG_OFFSET_END {.name = NULL, .offset = 0}
>> +
>> +static struct pt_regs_offset regoffset_table[] = {
>> +#ifdef CONFIG_X86_64
>> +	REG_OFFSET_NAME(r15),
>> +	REG_OFFSET_NAME(r14),
>> +	REG_OFFSET_NAME(r13),
>> +	REG_OFFSET_NAME(r12),
>> +	REG_OFFSET_NAME(r11),
>> +	REG_OFFSET_NAME(r10),
>> +	REG_OFFSET_NAME(r9),
>> +	REG_OFFSET_NAME(r8),
>> +#endif
>> +	REG_OFFSET_NAME(bx),
>> +	REG_OFFSET_NAME(cx),
>> +	REG_OFFSET_NAME(dx),
>> +	REG_OFFSET_NAME(si),
>> +	REG_OFFSET_NAME(di),
>> +	REG_OFFSET_NAME(bp),
>> +	REG_OFFSET_NAME(ax),
>> +#ifdef CONFIG_X86_32
>> +	REG_OFFSET_NAME(ds),
>> +	REG_OFFSET_NAME(es),
>> +	REG_OFFSET_NAME(fs),
>> +	REG_OFFSET_NAME(gs),
>> +#endif
>> +	REG_OFFSET_NAME(orig_ax),
>> +	REG_OFFSET_NAME(ip),
>> +	REG_OFFSET_NAME(cs),
> 
> 
> 
> You seem to have also forgotten ss here.

Oh, I forgot it because ss usually ignored in the kernel...
Anyway, it should be supported.


>> +	REG_OFFSET_NAME(flags),
>> +	REG_OFFSET_NAME(sp),
>> +	REG_OFFSET_END,
>> +};
>> +
>> +int query_register_offset(const char *name)
>> +{
>> +	struct pt_regs_offset *roff = regoffset_table;
>> +	for (roff = regoffset_table; roff->name != NULL; roff++)
>> +		if (!strcmp(roff->name, name))
>> +			return (unsigned)roff->offset;
> 
> 
> 
> Tiny thing here: could you set the full (unsigned int) cast?

or, I don't need to cast it...

>> +	return -EINVAL;
>> +}
>> +
>> +const char *query_register_name(unsigned offset)
>> +{
>> +	struct pt_regs_offset *roff = regoffset_table;
>> +	for (roff = regoffset_table; roff->name != NULL; roff++)
>> +		if (roff->offset == offset)
>> +			return roff->name;
>> +	return NULL;
>> +}
>> +
>>  /*
>>   * does not yet catch signals sent when the child dies.
>>   * in exit.c or in signal.c.
> 
> 
> All in one it looks good!

Thanks!

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH -tip 2/6 V4.1] x86: add arch-dep register and stack access  API to ptrace
  2009-04-02 23:48 ` Frederic Weisbecker
  2009-04-03  0:45   ` Masami Hiramatsu
@ 2009-04-03 16:02   ` Masami Hiramatsu
  2009-04-03 20:20     ` Roland McGrath
  1 sibling, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-03 16:02 UTC (permalink / raw)
  To: Frederic Weisbecker
  Cc: Ingo Molnar, Ananth N Mavinakayanahalli, Steven Rostedt,
	Andrew Morton, Andi Kleen, Arnaldo Carvalho de Melo,
	Jim Keniston, systemtap-ml, LKML, kvm

Add following APIs for accessing registers and stack entries from pt_regs.
- query_register_offset(const char *name)
   Query the offset of "name" register.

- query_register_name(unsigned offset)
   Query the name of register by its offset.

- get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- valid_stack_address(struct pt_regs *regs, unsigned long addr)
   Check the address is in the stack.

- get_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the stack. (N >= 0)

- get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

changes from v4:
 - support querying "ss" register.
 - remove unneeded cast.

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
---

 arch/x86/include/asm/ptrace.h |   66 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |   60 +++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+), 0 deletions(-)


diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index aed0894..51e5844 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@

 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif

 #ifndef __ASSEMBLY__
@@ -215,6 +216,71 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }

+/* Query offset/name of register from its name/offset */
+extern int query_register_offset(const char *name);
+extern const char *query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/* Get register value from its offset */
+static inline unsigned long get_register(struct pt_regs *regs, unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/* Check the address in the stack */
+static inline int valid_stack_address(struct pt_regs *regs, unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_trap_sp(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/* Get Nth entry of the stack */
+static inline unsigned long get_stack_nth(struct pt_regs *regs, unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_trap_sp(regs);
+	addr += n;
+	if (valid_stack_address(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/* Get Nth argument at function call */
+static inline unsigned long get_argument_nth(struct pt_regs *regs, unsigned n)
+{
+#ifdef CONFIG_X86_32
+#define NR_REGPARMS 3
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->ax;
+		case 1: return regs->dx;
+		case 2: return regs->cx;
+		}
+		return 0;
+#else /* CONFIG_X86_64 */
+#define NR_REGPARMS 6
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->di;
+		case 1: return regs->si;
+		case 2: return regs->dx;
+		case 3: return regs->cx;
+		case 4: return regs->r8;
+		case 5: return regs->r9;
+		}
+		return 0;
+#endif
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return get_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index 5c6e463..3f504fd 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -46,6 +46,66 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };

+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET(r) offsetof(struct pt_regs, r)
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = REG_OFFSET(r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+int query_register_offset(const char *name)
+{
+	struct pt_regs_offset *roff = regoffset_table;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+const char *query_register_name(unsigned offset)
+{
+	struct pt_regs_offset *roff = regoffset_table;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4.1] x86: add arch-dep register and stack access  API to ptrace
  2009-04-03 16:02   ` [PATCH -tip 2/6 V4.1] " Masami Hiramatsu
@ 2009-04-03 20:20     ` Roland McGrath
  2009-04-03 21:34       ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Roland McGrath @ 2009-04-03 20:20 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Frederic Weisbecker, Ingo Molnar, Ananth N Mavinakayanahalli,
	Steven Rostedt, Andrew Morton, Andi Kleen,
	Arnaldo Carvalho de Melo, Jim Keniston, systemtap-ml, LKML, kvm

> +static struct pt_regs_offset regoffset_table[] = {

         ^ const

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4.1] x86: add arch-dep register and stack access   API to ptrace
  2009-04-03 20:20     ` Roland McGrath
@ 2009-04-03 21:34       ` Masami Hiramatsu
  2009-04-03 23:29         ` [PATCH -tip 2/6 V4.2] " Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-03 21:34 UTC (permalink / raw)
  To: Roland McGrath
  Cc: Frederic Weisbecker, Ingo Molnar, Ananth N Mavinakayanahalli,
	Steven Rostedt, Andrew Morton, Andi Kleen,
	Arnaldo Carvalho de Melo, Jim Keniston, systemtap-ml, LKML, kvm

Roland McGrath wrote:
>> +static struct pt_regs_offset regoffset_table[] = {
> 
>          ^ const

Oops, exactly.
Perhaps, I need to update insn.c to make bitmap tables
"static const" too.

Thank you,

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

* [PATCH -tip 2/6 V4.2] x86: add arch-dep register and stack access  API to ptrace
  2009-04-03 21:34       ` Masami Hiramatsu
@ 2009-04-03 23:29         ` Masami Hiramatsu
  2009-04-06 19:22           ` Roland McGrath
  0 siblings, 1 reply; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-03 23:29 UTC (permalink / raw)
  To: Roland McGrath, Ingo Molnar
  Cc: Frederic Weisbecker, Ananth N Mavinakayanahalli, Steven Rostedt,
	Andrew Morton, Andi Kleen, Arnaldo Carvalho de Melo,
	Jim Keniston, systemtap-ml, LKML, kvm

Add following APIs for accessing registers and stack entries from pt_regs.
- query_register_offset(const char *name)
   Query the offset of "name" register.

- query_register_name(unsigned offset)
   Query the name of register by its offset.

- get_register(struct pt_regs *regs, unsigned offset)
   Get the value of a register by its offset.

- valid_stack_address(struct pt_regs *regs, unsigned long addr)
   Check the address is in the stack.

- get_stack_nth(struct pt_regs *reg, unsigned nth)
   Get Nth entry of the stack. (N >= 0)

- get_argument_nth(struct pt_regs *reg, unsigned nth)
   Get Nth argument at function call. (N >= 0)

changes from v4.1:
 - make regoffset_table constant.
 - remove needless local variable initialization in query_register_*.

Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com>
Cc: Ingo Molnar <mingo@elte.hu>
Cc: Frederic Weisbecker <fweisbec@gmail.com>
Cc: Roland McGrath <roland@redhat.com>
---

 arch/x86/include/asm/ptrace.h |   66 +++++++++++++++++++++++++++++++++++++++++
 arch/x86/kernel/ptrace.c      |   60 +++++++++++++++++++++++++++++++++++++
 2 files changed, 126 insertions(+), 0 deletions(-)


diff --git a/arch/x86/include/asm/ptrace.h b/arch/x86/include/asm/ptrace.h
index aed0894..51e5844 100644
--- a/arch/x86/include/asm/ptrace.h
+++ b/arch/x86/include/asm/ptrace.h
@@ -7,6 +7,7 @@

 #ifdef __KERNEL__
 #include <asm/segment.h>
+#include <asm/page_types.h>
 #endif

 #ifndef __ASSEMBLY__
@@ -215,6 +216,71 @@ static inline unsigned long user_stack_pointer(struct pt_regs *regs)
 	return regs->sp;
 }

+/* Query offset/name of register from its name/offset */
+extern int query_register_offset(const char *name);
+extern const char *query_register_name(unsigned offset);
+#define MAX_REG_OFFSET (offsetof(struct pt_regs, ss))
+
+/* Get register value from its offset */
+static inline unsigned long get_register(struct pt_regs *regs, unsigned offset)
+{
+	if (unlikely(offset > MAX_REG_OFFSET))
+		return 0;
+	return *(unsigned long *)((unsigned long)regs + offset);
+}
+
+/* Check the address in the stack */
+static inline int valid_stack_address(struct pt_regs *regs, unsigned long addr)
+{
+	return ((addr & ~(THREAD_SIZE - 1))  ==
+		(kernel_trap_sp(regs) & ~(THREAD_SIZE - 1)));
+}
+
+/* Get Nth entry of the stack */
+static inline unsigned long get_stack_nth(struct pt_regs *regs, unsigned n)
+{
+	unsigned long *addr = (unsigned long *)kernel_trap_sp(regs);
+	addr += n;
+	if (valid_stack_address(regs, (unsigned long)addr))
+		return *addr;
+	else
+		return 0;
+}
+
+/* Get Nth argument at function call */
+static inline unsigned long get_argument_nth(struct pt_regs *regs, unsigned n)
+{
+#ifdef CONFIG_X86_32
+#define NR_REGPARMS 3
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->ax;
+		case 1: return regs->dx;
+		case 2: return regs->cx;
+		}
+		return 0;
+#else /* CONFIG_X86_64 */
+#define NR_REGPARMS 6
+	if (n < NR_REGPARMS) {
+		switch (n) {
+		case 0: return regs->di;
+		case 1: return regs->si;
+		case 2: return regs->dx;
+		case 3: return regs->cx;
+		case 4: return regs->r8;
+		case 5: return regs->r9;
+		}
+		return 0;
+#endif
+	} else {
+		/*
+		 * The typical case: arg n is on the stack.
+		 * (Note: stack[0] = return address, so skip it)
+		 */
+		return get_stack_nth(regs, 1 + n - NR_REGPARMS);
+	}
+}
+
 /*
  * These are defined as per linux/ptrace.h, which see.
  */
diff --git a/arch/x86/kernel/ptrace.c b/arch/x86/kernel/ptrace.c
index fe9345c..8a8af27 100644
--- a/arch/x86/kernel/ptrace.c
+++ b/arch/x86/kernel/ptrace.c
@@ -46,6 +46,66 @@ enum x86_regset {
 	REGSET_IOPERM32,
 };

+struct pt_regs_offset {
+	const char *name;
+	int offset;
+};
+
+#define REG_OFFSET(r) offsetof(struct pt_regs, r)
+#define REG_OFFSET_NAME(r) {.name = #r, .offset = REG_OFFSET(r)}
+#define REG_OFFSET_END {.name = NULL, .offset = 0}
+
+static const struct pt_regs_offset regoffset_table[] = {
+#ifdef CONFIG_X86_64
+	REG_OFFSET_NAME(r15),
+	REG_OFFSET_NAME(r14),
+	REG_OFFSET_NAME(r13),
+	REG_OFFSET_NAME(r12),
+	REG_OFFSET_NAME(r11),
+	REG_OFFSET_NAME(r10),
+	REG_OFFSET_NAME(r9),
+	REG_OFFSET_NAME(r8),
+#endif
+	REG_OFFSET_NAME(bx),
+	REG_OFFSET_NAME(cx),
+	REG_OFFSET_NAME(dx),
+	REG_OFFSET_NAME(si),
+	REG_OFFSET_NAME(di),
+	REG_OFFSET_NAME(bp),
+	REG_OFFSET_NAME(ax),
+#ifdef CONFIG_X86_32
+	REG_OFFSET_NAME(ds),
+	REG_OFFSET_NAME(es),
+	REG_OFFSET_NAME(fs),
+	REG_OFFSET_NAME(gs),
+#endif
+	REG_OFFSET_NAME(orig_ax),
+	REG_OFFSET_NAME(ip),
+	REG_OFFSET_NAME(cs),
+	REG_OFFSET_NAME(flags),
+	REG_OFFSET_NAME(sp),
+	REG_OFFSET_NAME(ss),
+	REG_OFFSET_END,
+};
+
+int query_register_offset(const char *name)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (!strcmp(roff->name, name))
+			return roff->offset;
+	return -EINVAL;
+}
+
+const char *query_register_name(unsigned offset)
+{
+	const struct pt_regs_offset *roff;
+	for (roff = regoffset_table; roff->name != NULL; roff++)
+		if (roff->offset == offset)
+			return roff->name;
+	return NULL;
+}
+
 /*
  * does not yet catch signals sent when the child dies.
  * in exit.c or in signal.c.

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply related	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4.2] x86: add arch-dep register and stack access  API to ptrace
  2009-04-03 23:29         ` [PATCH -tip 2/6 V4.2] " Masami Hiramatsu
@ 2009-04-06 19:22           ` Roland McGrath
  2009-04-06 19:57             ` Masami Hiramatsu
  0 siblings, 1 reply; 9+ messages in thread
From: Roland McGrath @ 2009-04-06 19:22 UTC (permalink / raw)
  To: Masami Hiramatsu
  Cc: Ingo Molnar, Frederic Weisbecker, Ananth N Mavinakayanahalli,
	Steven Rostedt, Andrew Morton, Andi Kleen,
	Arnaldo Carvalho de Melo, Jim Keniston, systemtap-ml, LKML, kvm

I have no comments about the patch, but the subject line is misleading
because this has nothing do with ptrace.  It's in asm/ptrace.h but it being
struct pt_regs does not really have anything to do with ptrace.


Thanks,
Roland

^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: [PATCH -tip 2/6 V4.2] x86: add arch-dep register and stack access  API to ptrace
  2009-04-06 19:22           ` Roland McGrath
@ 2009-04-06 19:57             ` Masami Hiramatsu
  0 siblings, 0 replies; 9+ messages in thread
From: Masami Hiramatsu @ 2009-04-06 19:57 UTC (permalink / raw)
  To: Roland McGrath
  Cc: Ingo Molnar, Frederic Weisbecker, Ananth N Mavinakayanahalli,
	Steven Rostedt, Andrew Morton, Andi Kleen,
	Arnaldo Carvalho de Melo, Jim Keniston, systemtap-ml, LKML, kvm

Roland McGrath wrote:
> I have no comments about the patch, but the subject line is misleading
> because this has nothing do with ptrace.  It's in asm/ptrace.h but it being
> struct pt_regs does not really have anything to do with ptrace.
> 

Hmm, so, it should be "x86: add arch-dep register and stack access API".

-- 
Masami Hiramatsu

Software Engineer
Hitachi Computer Products (America) Inc.
Software Solutions Division

e-mail: mhiramat@redhat.com

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2009-04-06 19:57 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-04-02 17:24 [PATCH -tip 2/6 V4] x86: add arch-dep register and stack access API to ptrace Masami Hiramatsu
2009-04-02 23:48 ` Frederic Weisbecker
2009-04-03  0:45   ` Masami Hiramatsu
2009-04-03 16:02   ` [PATCH -tip 2/6 V4.1] " Masami Hiramatsu
2009-04-03 20:20     ` Roland McGrath
2009-04-03 21:34       ` Masami Hiramatsu
2009-04-03 23:29         ` [PATCH -tip 2/6 V4.2] " Masami Hiramatsu
2009-04-06 19:22           ` Roland McGrath
2009-04-06 19:57             ` Masami Hiramatsu

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).