xref: /linux/tools/testing/selftests/ftrace/test.d/kprobe/kprobe_args_syntax.tc (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: Kprobe event argument syntax
4# requires: kprobe_events "x8/16/32/64":README
5
6PROBEFUNC="vfs_read"
7GOODREG=
8BADREG=
9GOODSYM="_sdata"
10if ! grep -qw ${GOODSYM} /proc/kallsyms ; then
11  GOODSYM=$PROBEFUNC
12fi
13BADSYM="deaqswdefr"
14SYMADDR=0x`grep -w ${GOODSYM} /proc/kallsyms | cut -f 1 -d " "`
15GOODTYPE="x16"
16BADTYPE="y16"
17
18case `uname -m` in
19x86_64|i[3456]86)
20  GOODREG=%ax
21  BADREG=%ex
22;;
23aarch64)
24  GOODREG=%x0
25  BADREG=%ax
26;;
27arm*)
28  GOODREG=%r0
29  BADREG=%ax
30;;
31ppc*)
32  GOODREG=%r3
33  BADREG=%msr
34;;
35s390*)
36  GOODREG=%r2
37  BADREG=%s2
38;;
39mips*)
40  GOODREG=%r4
41  BADREG=%r12
42;;
43loongarch*)
44  GOODREG=%r4
45  BADREG=%r12
46;;
47riscv*)
48  GOODREG=%a0
49  BADREG=%a8
50;;
51*)
52  echo "Please implement other architecture here"
53  exit_untested
54esac
55
56test_goodarg() # Good-args
57{
58  while [ "$1" ]; do
59    echo "p ${PROBEFUNC} $1" > kprobe_events
60    shift 1
61  done;
62}
63
64test_badarg() # Bad-args
65{
66  while [ "$1" ]; do
67    ! echo "p ${PROBEFUNC} $1" > kprobe_events
68    shift 1
69  done;
70}
71
72echo > kprobe_events
73
74: "Register access"
75test_goodarg ${GOODREG}
76test_badarg ${BADREG}
77
78: "Symbol access"
79test_goodarg "@${GOODSYM}" "@${SYMADDR}" "@${GOODSYM}+10" "@${GOODSYM}-10"
80test_badarg "@" "@${BADSYM}" "@${GOODSYM}*10" "@${GOODSYM}/10" \
81	    "@${GOODSYM}%10" "@${GOODSYM}&10" "@${GOODSYM}|10"
82
83: "Stack access"
84test_goodarg "\$stack" "\$stack0" "\$stack1"
85test_badarg "\$stackp" "\$stack0+10" "\$stack1-10"
86
87: "Retval access"
88echo "r ${PROBEFUNC} \$retval" > kprobe_events
89! echo "p ${PROBEFUNC} \$retval" > kprobe_events
90
91# $comm was introduced in 4.8, older kernels reject it.
92if grep -A1 "fetcharg:" README | grep -q '\$comm' ; then
93: "Comm access"
94test_goodarg "\$comm"
95fi
96
97: "Indirect memory access"
98test_goodarg "+0(${GOODREG})" "-0(${GOODREG})" "+10(\$stack)" \
99	"+0(\$stack1)" "+10(@${GOODSYM}-10)" "+0(+10(+20(\$stack)))"
100test_badarg "+(${GOODREG})" "(${GOODREG}+10)" "-(${GOODREG})" "(${GOODREG})" \
101	"+10(\$comm)" "+0(${GOODREG})+10"
102
103: "Name assignment"
104test_goodarg "varname=${GOODREG}"
105test_badarg "varname=varname2=${GOODREG}"
106
107: "Type syntax"
108test_goodarg "${GOODREG}:${GOODTYPE}"
109test_badarg "${GOODREG}::${GOODTYPE}" "${GOODREG}:${BADTYPE}" \
110	"${GOODTYPE}:${GOODREG}"
111
112: "Combination check"
113
114test_goodarg "\$comm:string" "+0(\$stack):string"
115test_badarg "\$comm:x64" "\$stack:string" "${GOODREG}:string"
116