[v4] tracing/probe: add a char type to show the character value of traced arguments

Message ID 20221215091346.33710-1-dolinux.peng@gmail.com
State New
Headers
Series [v4] tracing/probe: add a char type to show the character value of traced arguments |

Commit Message

Donglin Peng Dec. 15, 2022, 9:13 a.m. UTC
  There are scenes that we want to show the character value of traced
arguments other than a decimal or hexadecimal or string value for debug
convinience. Add a new type named 'char' to do it.

For example:

The to be traced function is 'void demo_func(char type, char *name);', we
can add a kprobe event as follows to show argument values as we want:

echo 'p:myprobe demo_func $arg1:char +0($arg2):char[2]' > kprobe_events

we will get the following trace log:

... 95.451350: myprobe: (demo_func+0x0/0x29) arg1=A arg2={b,p}

Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
Reported-by: kernel test robot <lkp@intel.com>
---
Changes in v4:
 - update the example in the commit log

Changes in v3:
 - update readme_msg

Changes in v2:
 - fix build warnings reported by kernel test robot
 - modify commit log
---
 Documentation/trace/kprobetrace.rst | 3 ++-
 kernel/trace/trace.c                | 2 +-
 kernel/trace/trace_probe.c          | 2 ++
 kernel/trace/trace_probe.h          | 1 +
 4 files changed, 6 insertions(+), 2 deletions(-)
  

Comments

Masami Hiramatsu (Google) Dec. 18, 2022, 1:39 a.m. UTC | #1
Hi Donglin,

Thanks for updating you patch. And can you also send a test code update
(tools/testing/selftests/ftrace/test.d/kprobe/kprobe_args_type.tc)?

Thank you,

On Thu, 15 Dec 2022 01:13:46 -0800
Donglin Peng <dolinux.peng@gmail.com> wrote:

> There are scenes that we want to show the character value of traced
> arguments other than a decimal or hexadecimal or string value for debug
> convinience. Add a new type named 'char' to do it.
> 
> For example:
> 
> The to be traced function is 'void demo_func(char type, char *name);', we
> can add a kprobe event as follows to show argument values as we want:
> 
> echo 'p:myprobe demo_func $arg1:char +0($arg2):char[2]' > kprobe_events
> 
> we will get the following trace log:
> 
> ... 95.451350: myprobe: (demo_func+0x0/0x29) arg1=A arg2={b,p}
> 
> Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> Reported-by: kernel test robot <lkp@intel.com>
> ---
> Changes in v4:
>  - update the example in the commit log
> 
> Changes in v3:
>  - update readme_msg
> 
> Changes in v2:
>  - fix build warnings reported by kernel test robot
>  - modify commit log
> ---
>  Documentation/trace/kprobetrace.rst | 3 ++-
>  kernel/trace/trace.c                | 2 +-
>  kernel/trace/trace_probe.c          | 2 ++
>  kernel/trace/trace_probe.h          | 1 +
>  4 files changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/Documentation/trace/kprobetrace.rst b/Documentation/trace/kprobetrace.rst
> index 4274cc6a2f94..007972a3c5c4 100644
> --- a/Documentation/trace/kprobetrace.rst
> +++ b/Documentation/trace/kprobetrace.rst
> @@ -58,7 +58,7 @@ Synopsis of kprobe_events
>    NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
>    FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
>  		  (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
> -		  (x8/x16/x32/x64), "string", "ustring" and bitfield
> +		  (x8/x16/x32/x64), "char", "string", "ustring" and bitfield
>  		  are supported.
>  
>    (\*1) only for the probe on function entry (offs == 0).
> @@ -80,6 +80,7 @@ E.g. 'x16[4]' means an array of x16 (2bytes hex) with 4 elements.
>  Note that the array can be applied to memory type fetchargs, you can not
>  apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
>  wrong, but '+8($stack):x8[8]' is OK.)
> +Char type can be used to show the character value of traced arguments.
>  String type is a special type, which fetches a "null-terminated" string from
>  kernel space. This means it will fail and store NULL if the string container
>  has been paged out. "ustring" type is an alternative of string for user-space.
> diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> index 5cfc95a52bc3..a64e206f94e6 100644
> --- a/kernel/trace/trace.c
> +++ b/kernel/trace/trace.c
> @@ -5615,7 +5615,7 @@ static const char readme_msg[] =
>  	"\t           $stack<index>, $stack, $retval, $comm,\n"
>  #endif
>  	"\t           +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
> -	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
> +	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
>  	"\t           b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
>  	"\t           <type>\\[<array-size>\\]\n"
>  #ifdef CONFIG_HIST_TRIGGERS
> diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
> index 36dff277de46..a4abf7f6c295 100644
> --- a/kernel/trace/trace_probe.c
> +++ b/kernel/trace/trace_probe.c
> @@ -50,6 +50,7 @@ DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
>  DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
>  DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
>  DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
> +DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "%c")
>  
>  int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
>  {
> @@ -93,6 +94,7 @@ static const struct fetch_type probe_fetch_types[] = {
>  	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
>  	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
>  	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
> +	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
>  	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
>  
>  	ASSIGN_FETCH_TYPE_END
> diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
> index de38f1c03776..8c86aaa8b0c9 100644
> --- a/kernel/trace/trace_probe.h
> +++ b/kernel/trace/trace_probe.h
> @@ -164,6 +164,7 @@ DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
>  DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
>  DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
>  
> +DECLARE_BASIC_PRINT_TYPE_FUNC(char);
>  DECLARE_BASIC_PRINT_TYPE_FUNC(string);
>  DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
>  
> -- 
> 2.25.1
>
  
Donglin Peng Dec. 19, 2022, 7:08 a.m. UTC | #2
On Sun, Dec 18, 2022 at 9:39 AM Masami Hiramatsu <mhiramat@kernel.org> wrote:
>
> Hi Donglin,
>
> Thanks for updating you patch. And can you also send a test code update
> (tools/testing/selftests/ftrace/test.d/kprobe/kprobe_args_type.tc)?
>
Yes, no problem.
I will refer to kprobe_args_string.tc to add a new file named
kprobe_args_char.tc.
> Thank you,
>
> On Thu, 15 Dec 2022 01:13:46 -0800
> Donglin Peng <dolinux.peng@gmail.com> wrote:
>
> > There are scenes that we want to show the character value of traced
> > arguments other than a decimal or hexadecimal or string value for debug
> > convinience. Add a new type named 'char' to do it.
> >
> > For example:
> >
> > The to be traced function is 'void demo_func(char type, char *name);', we
> > can add a kprobe event as follows to show argument values as we want:
> >
> > echo 'p:myprobe demo_func $arg1:char +0($arg2):char[2]' > kprobe_events
> >
> > we will get the following trace log:
> >
> > ... 95.451350: myprobe: (demo_func+0x0/0x29) arg1=A arg2={b,p}
> >
> > Signed-off-by: Donglin Peng <dolinux.peng@gmail.com>
> > Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
> > Reported-by: kernel test robot <lkp@intel.com>
> > ---
> > Changes in v4:
> >  - update the example in the commit log
> >
> > Changes in v3:
> >  - update readme_msg
> >
> > Changes in v2:
> >  - fix build warnings reported by kernel test robot
> >  - modify commit log
> > ---
> >  Documentation/trace/kprobetrace.rst | 3 ++-
> >  kernel/trace/trace.c                | 2 +-
> >  kernel/trace/trace_probe.c          | 2 ++
> >  kernel/trace/trace_probe.h          | 1 +
> >  4 files changed, 6 insertions(+), 2 deletions(-)
> >
> > diff --git a/Documentation/trace/kprobetrace.rst b/Documentation/trace/kprobetrace.rst
> > index 4274cc6a2f94..007972a3c5c4 100644
> > --- a/Documentation/trace/kprobetrace.rst
> > +++ b/Documentation/trace/kprobetrace.rst
> > @@ -58,7 +58,7 @@ Synopsis of kprobe_events
> >    NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
> >    FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
> >                 (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
> > -               (x8/x16/x32/x64), "string", "ustring" and bitfield
> > +               (x8/x16/x32/x64), "char", "string", "ustring" and bitfield
> >                 are supported.
> >
> >    (\*1) only for the probe on function entry (offs == 0).
> > @@ -80,6 +80,7 @@ E.g. 'x16[4]' means an array of x16 (2bytes hex) with 4 elements.
> >  Note that the array can be applied to memory type fetchargs, you can not
> >  apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
> >  wrong, but '+8($stack):x8[8]' is OK.)
> > +Char type can be used to show the character value of traced arguments.
> >  String type is a special type, which fetches a "null-terminated" string from
> >  kernel space. This means it will fail and store NULL if the string container
> >  has been paged out. "ustring" type is an alternative of string for user-space.
> > diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
> > index 5cfc95a52bc3..a64e206f94e6 100644
> > --- a/kernel/trace/trace.c
> > +++ b/kernel/trace/trace.c
> > @@ -5615,7 +5615,7 @@ static const char readme_msg[] =
> >       "\t           $stack<index>, $stack, $retval, $comm,\n"
> >  #endif
> >       "\t           +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
> > -     "\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
> > +     "\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
> >       "\t           b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
> >       "\t           <type>\\[<array-size>\\]\n"
> >  #ifdef CONFIG_HIST_TRIGGERS
> > diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
> > index 36dff277de46..a4abf7f6c295 100644
> > --- a/kernel/trace/trace_probe.c
> > +++ b/kernel/trace/trace_probe.c
> > @@ -50,6 +50,7 @@ DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
> >  DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
> >  DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
> >  DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
> > +DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "%c")
> >
> >  int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
> >  {
> > @@ -93,6 +94,7 @@ static const struct fetch_type probe_fetch_types[] = {
> >       ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
> >       ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
> >       ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
> > +     ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
> >       ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
> >
> >       ASSIGN_FETCH_TYPE_END
> > diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
> > index de38f1c03776..8c86aaa8b0c9 100644
> > --- a/kernel/trace/trace_probe.h
> > +++ b/kernel/trace/trace_probe.h
> > @@ -164,6 +164,7 @@ DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
> >  DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
> >  DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
> >
> > +DECLARE_BASIC_PRINT_TYPE_FUNC(char);
> >  DECLARE_BASIC_PRINT_TYPE_FUNC(string);
> >  DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
> >
> > --
> > 2.25.1
> >
>
>
> --
> Masami Hiramatsu (Google) <mhiramat@kernel.org>
  

Patch

diff --git a/Documentation/trace/kprobetrace.rst b/Documentation/trace/kprobetrace.rst
index 4274cc6a2f94..007972a3c5c4 100644
--- a/Documentation/trace/kprobetrace.rst
+++ b/Documentation/trace/kprobetrace.rst
@@ -58,7 +58,7 @@  Synopsis of kprobe_events
   NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
   FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
 		  (u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
-		  (x8/x16/x32/x64), "string", "ustring" and bitfield
+		  (x8/x16/x32/x64), "char", "string", "ustring" and bitfield
 		  are supported.
 
   (\*1) only for the probe on function entry (offs == 0).
@@ -80,6 +80,7 @@  E.g. 'x16[4]' means an array of x16 (2bytes hex) with 4 elements.
 Note that the array can be applied to memory type fetchargs, you can not
 apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
 wrong, but '+8($stack):x8[8]' is OK.)
+Char type can be used to show the character value of traced arguments.
 String type is a special type, which fetches a "null-terminated" string from
 kernel space. This means it will fail and store NULL if the string container
 has been paged out. "ustring" type is an alternative of string for user-space.
diff --git a/kernel/trace/trace.c b/kernel/trace/trace.c
index 5cfc95a52bc3..a64e206f94e6 100644
--- a/kernel/trace/trace.c
+++ b/kernel/trace/trace.c
@@ -5615,7 +5615,7 @@  static const char readme_msg[] =
 	"\t           $stack<index>, $stack, $retval, $comm,\n"
 #endif
 	"\t           +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
-	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
+	"\t     type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
 	"\t           b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
 	"\t           <type>\\[<array-size>\\]\n"
 #ifdef CONFIG_HIST_TRIGGERS
diff --git a/kernel/trace/trace_probe.c b/kernel/trace/trace_probe.c
index 36dff277de46..a4abf7f6c295 100644
--- a/kernel/trace/trace_probe.c
+++ b/kernel/trace/trace_probe.c
@@ -50,6 +50,7 @@  DEFINE_BASIC_PRINT_TYPE_FUNC(x8,  u8,  "0x%x")
 DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
 DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
 DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
+DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "%c")
 
 int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
 {
@@ -93,6 +94,7 @@  static const struct fetch_type probe_fetch_types[] = {
 	ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
 	ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
 	ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
+	ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8,  0),
 	ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
 
 	ASSIGN_FETCH_TYPE_END
diff --git a/kernel/trace/trace_probe.h b/kernel/trace/trace_probe.h
index de38f1c03776..8c86aaa8b0c9 100644
--- a/kernel/trace/trace_probe.h
+++ b/kernel/trace/trace_probe.h
@@ -164,6 +164,7 @@  DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
 DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
 DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
 
+DECLARE_BASIC_PRINT_TYPE_FUNC(char);
 DECLARE_BASIC_PRINT_TYPE_FUNC(string);
 DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);