linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] riscv: ptrace: add argn syntax
@ 2021-06-20 12:01 Jeff Xie
  2021-07-06 19:10 ` Palmer Dabbelt
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Xie @ 2021-06-20 12:01 UTC (permalink / raw)
  To: oleg
  Cc: paul.walmsley, palmer, caou, linux-riscv, linux-kernel,
	chongguiguzi, huan.xie

This enables ftrace kprobe events to access kernel function
arguments via $argN syntax.

Signed-off-by: Jeff Xie <huan.xie@suse.com>
---
 arch/riscv/Kconfig              |  1 +
 arch/riscv/include/asm/ptrace.h | 31 +++++++++++++++++++++++++++++++
 2 files changed, 32 insertions(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 18ec0f9bb8d5..f9246a7d0a31 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -88,6 +88,7 @@ config RISCV
 	select HAVE_PERF_REGS
 	select HAVE_PERF_USER_STACK_DUMP
 	select HAVE_REGS_AND_STACK_ACCESS_API
+	select HAVE_FUNCTION_ARG_ACCESS_API
 	select HAVE_STACKPROTECTOR
 	select HAVE_SYSCALL_TRACEPOINTS
 	select IRQ_DOMAIN
diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
index 09ad4e923510..fdb59ee405ee 100644
--- a/arch/riscv/include/asm/ptrace.h
+++ b/arch/riscv/include/asm/ptrace.h
@@ -141,6 +141,37 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
 
 	return *(unsigned long *)((unsigned long)regs + offset);
 }
+
+/**
+ * regs_get_kernel_argument() - get Nth function argument in kernel
+ * @regs:       pt_regs of that context
+ * @n:          function argument number (start from 0)
+ *
+ * regs_get_argument() returns @n th argument of the function call.
+ *
+ * Note you can get the parameter correctly if the function has no
+ * more than eight arguments.
+ */
+static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
+						unsigned int n)
+{
+#define NR_REG_ARGUMENTS 8
+	static const unsigned int argument_offs[] = {
+		offsetof(struct pt_regs, a0),
+		offsetof(struct pt_regs, a1),
+		offsetof(struct pt_regs, a2),
+		offsetof(struct pt_regs, a3),
+		offsetof(struct pt_regs, a4),
+		offsetof(struct pt_regs, a5),
+		offsetof(struct pt_regs, a6),
+		offsetof(struct pt_regs, a7),
+	};
+
+	if (n < NR_REG_ARGUMENTS)
+		return regs_get_register(regs, argument_offs[n]);
+	return 0;
+}
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ASM_RISCV_PTRACE_H */
-- 
2.26.2


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

* Re: [PATCH] riscv: ptrace: add argn syntax
  2021-06-20 12:01 [PATCH] riscv: ptrace: add argn syntax Jeff Xie
@ 2021-07-06 19:10 ` Palmer Dabbelt
  0 siblings, 0 replies; 2+ messages in thread
From: Palmer Dabbelt @ 2021-07-06 19:10 UTC (permalink / raw)
  To: chongguiguzi
  Cc: oleg, Paul Walmsley, caou, linux-riscv, linux-kernel,
	chongguiguzi, huan.xie

On Sun, 20 Jun 2021 05:01:51 PDT (-0700), chongguiguzi@gmail.com wrote:
> This enables ftrace kprobe events to access kernel function
> arguments via $argN syntax.
>
> Signed-off-by: Jeff Xie <huan.xie@suse.com>
> ---
>  arch/riscv/Kconfig              |  1 +
>  arch/riscv/include/asm/ptrace.h | 31 +++++++++++++++++++++++++++++++
>  2 files changed, 32 insertions(+)
>
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index 18ec0f9bb8d5..f9246a7d0a31 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -88,6 +88,7 @@ config RISCV
>  	select HAVE_PERF_REGS
>  	select HAVE_PERF_USER_STACK_DUMP
>  	select HAVE_REGS_AND_STACK_ACCESS_API
> +	select HAVE_FUNCTION_ARG_ACCESS_API
>  	select HAVE_STACKPROTECTOR
>  	select HAVE_SYSCALL_TRACEPOINTS
>  	select IRQ_DOMAIN
> diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> index 09ad4e923510..fdb59ee405ee 100644
> --- a/arch/riscv/include/asm/ptrace.h
> +++ b/arch/riscv/include/asm/ptrace.h
> @@ -141,6 +141,37 @@ static inline unsigned long regs_get_register(struct pt_regs *regs,
>
>  	return *(unsigned long *)((unsigned long)regs + offset);
>  }
> +
> +/**
> + * regs_get_kernel_argument() - get Nth function argument in kernel
> + * @regs:       pt_regs of that context
> + * @n:          function argument number (start from 0)
> + *
> + * regs_get_argument() returns @n th argument of the function call.
> + *
> + * Note you can get the parameter correctly if the function has no
> + * more than eight arguments.
> + */
> +static inline unsigned long regs_get_kernel_argument(struct pt_regs *regs,
> +						unsigned int n)
> +{
> +#define NR_REG_ARGUMENTS 8
> +	static const unsigned int argument_offs[] = {
> +		offsetof(struct pt_regs, a0),
> +		offsetof(struct pt_regs, a1),
> +		offsetof(struct pt_regs, a2),
> +		offsetof(struct pt_regs, a3),
> +		offsetof(struct pt_regs, a4),
> +		offsetof(struct pt_regs, a5),
> +		offsetof(struct pt_regs, a6),
> +		offsetof(struct pt_regs, a7),
> +	};
> +
> +	if (n < NR_REG_ARGUMENTS)
> +		return regs_get_register(regs, argument_offs[n]);
> +	return 0;
> +}
> +
>  #endif /* __ASSEMBLY__ */
>
>  #endif /* _ASM_RISCV_PTRACE_H */

This is on for-next.  I cleaned it up a bit to avoid leaking that 
#define.

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

end of thread, other threads:[~2021-07-06 19:10 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-20 12:01 [PATCH] riscv: ptrace: add argn syntax Jeff Xie
2021-07-06 19:10 ` Palmer Dabbelt

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).