Skip to content

Commit 8478cca

Browse files
Donglin Pengmhiramat
authored andcommitted
tracing/probe: add a char type to show the character value of traced arguments
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. I add a new type named 'char' to do it and a new test case file named 'kprobe_args_char.tc' to do selftest for char type. 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[5]' > kprobe_events we will get the following trace log: ... myprobe: (demo_func+0x0/0x29) arg1='A' arg2={'b','p','f','1',''} Link: https://lore.kernel.org/all/20221219110613.367098-1-dolinux.peng@gmail.com/ 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> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent 96cd93a commit 8478cca

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

Documentation/trace/kprobetrace.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ Synopsis of kprobe_events
5858
NAME=FETCHARG : Set NAME as the argument name of FETCHARG.
5959
FETCHARG:TYPE : Set TYPE as the type of FETCHARG. Currently, basic types
6060
(u8/u16/u32/u64/s8/s16/s32/s64), hexadecimal types
61-
(x8/x16/x32/x64), "string", "ustring", "symbol", "symstr"
61+
(x8/x16/x32/x64), "char", "string", "ustring", "symbol", "symstr"
6262
and bitfield are supported.
6363

6464
(\*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.
8080
Note that the array can be applied to memory type fetchargs, you can not
8181
apply it to registers/stack-entries etc. (for example, '$stack1:x8[8]' is
8282
wrong, but '+8($stack):x8[8]' is OK.)
83+
Char type can be used to show the character value of traced arguments.
8384
String type is a special type, which fetches a "null-terminated" string from
8485
kernel space. This means it will fail and store NULL if the string container
8586
has been paged out. "ustring" type is an alternative of string for user-space.

kernel/trace/trace.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5615,7 +5615,7 @@ static const char readme_msg[] =
56155615
"\t $stack<index>, $stack, $retval, $comm,\n"
56165616
#endif
56175617
"\t +|-[u]<offset>(<fetcharg>), \\imm-value, \\\"imm-string\"\n"
5618-
"\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, string, symbol,\n"
5618+
"\t type: s8/16/32/64, u8/16/32/64, x8/16/32/64, char, string, symbol,\n"
56195619
"\t b<bit-width>@<bit-offset>/<container-size>, ustring,\n"
56205620
"\t symstr, <type>\\[<array-size>\\]\n"
56215621
#ifdef CONFIG_HIST_TRIGGERS

kernel/trace/trace_probe.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ DEFINE_BASIC_PRINT_TYPE_FUNC(x8, u8, "0x%x")
5050
DEFINE_BASIC_PRINT_TYPE_FUNC(x16, u16, "0x%x")
5151
DEFINE_BASIC_PRINT_TYPE_FUNC(x32, u32, "0x%x")
5252
DEFINE_BASIC_PRINT_TYPE_FUNC(x64, u64, "0x%Lx")
53+
DEFINE_BASIC_PRINT_TYPE_FUNC(char, u8, "'%c'")
5354

5455
int PRINT_TYPE_FUNC_NAME(symbol)(struct trace_seq *s, void *data, void *ent)
5556
{
@@ -95,6 +96,7 @@ static const struct fetch_type probe_fetch_types[] = {
9596
ASSIGN_FETCH_TYPE_ALIAS(x16, u16, u16, 0),
9697
ASSIGN_FETCH_TYPE_ALIAS(x32, u32, u32, 0),
9798
ASSIGN_FETCH_TYPE_ALIAS(x64, u64, u64, 0),
99+
ASSIGN_FETCH_TYPE_ALIAS(char, u8, u8, 0),
98100
ASSIGN_FETCH_TYPE_ALIAS(symbol, ADDR_FETCH_TYPE, ADDR_FETCH_TYPE, 0),
99101

100102
ASSIGN_FETCH_TYPE_END

kernel/trace/trace_probe.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ DECLARE_BASIC_PRINT_TYPE_FUNC(x16);
166166
DECLARE_BASIC_PRINT_TYPE_FUNC(x32);
167167
DECLARE_BASIC_PRINT_TYPE_FUNC(x64);
168168

169+
DECLARE_BASIC_PRINT_TYPE_FUNC(char);
169170
DECLARE_BASIC_PRINT_TYPE_FUNC(string);
170171
DECLARE_BASIC_PRINT_TYPE_FUNC(symbol);
171172

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#!/bin/sh
2+
# SPDX-License-Identifier: GPL-2.0
3+
# description: Kprobe event char type argument
4+
# requires: kprobe_events
5+
6+
case `uname -m` in
7+
x86_64)
8+
ARG1=%di
9+
;;
10+
i[3456]86)
11+
ARG1=%ax
12+
;;
13+
aarch64)
14+
ARG1=%x0
15+
;;
16+
arm*)
17+
ARG1=%r0
18+
;;
19+
ppc64*)
20+
ARG1=%r3
21+
;;
22+
ppc*)
23+
ARG1=%r3
24+
;;
25+
s390*)
26+
ARG1=%r2
27+
;;
28+
mips*)
29+
ARG1=%r4
30+
;;
31+
*)
32+
echo "Please implement other architecture here"
33+
exit_untested
34+
esac
35+
36+
: "Test get argument (1)"
37+
echo "p:testprobe tracefs_create_dir arg1=+0(${ARG1}):char" > kprobe_events
38+
echo 1 > events/kprobes/testprobe/enable
39+
echo "p:test $FUNCTION_FORK" >> kprobe_events
40+
grep -qe "testprobe.* arg1='t'" trace
41+
42+
echo 0 > events/kprobes/testprobe/enable
43+
: "Test get argument (2)"
44+
echo "p:testprobe tracefs_create_dir arg1=+0(${ARG1}):char arg2=+0(${ARG1}):char[4]" > kprobe_events
45+
echo 1 > events/kprobes/testprobe/enable
46+
echo "p:test $FUNCTION_FORK" >> kprobe_events
47+
grep -qe "testprobe.* arg1='t' arg2={'t','e','s','t'}" trace

0 commit comments

Comments
 (0)