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;;
47*)
48  echo "Please implement other architecture here"
49  exit_untested
50esac
51
52test_goodarg() # Good-args
53{
54  while [ "$1" ]; do
55    echo "p ${PROBEFUNC} $1" > kprobe_events
56    shift 1
57  done;
58}
59
60test_badarg() # Bad-args
61{
62  while [ "$1" ]; do
63    ! echo "p ${PROBEFUNC} $1" > kprobe_events
64    shift 1
65  done;
66}
67
68echo > kprobe_events
69
70: "Register access"
71test_goodarg ${GOODREG}
72test_badarg ${BADREG}
73
74: "Symbol access"
75test_goodarg "@${GOODSYM}" "@${SYMADDR}" "@${GOODSYM}+10" "@${GOODSYM}-10"
76test_badarg "@" "@${BADSYM}" "@${GOODSYM}*10" "@${GOODSYM}/10" \
77	    "@${GOODSYM}%10" "@${GOODSYM}&10" "@${GOODSYM}|10"
78
79: "Stack access"
80test_goodarg "\$stack" "\$stack0" "\$stack1"
81test_badarg "\$stackp" "\$stack0+10" "\$stack1-10"
82
83: "Retval access"
84echo "r ${PROBEFUNC} \$retval" > kprobe_events
85! echo "p ${PROBEFUNC} \$retval" > kprobe_events
86
87# $comm was introduced in 4.8, older kernels reject it.
88if grep -A1 "fetcharg:" README | grep -q '\$comm' ; then
89: "Comm access"
90test_goodarg "\$comm"
91fi
92
93: "Indirect memory access"
94test_goodarg "+0(${GOODREG})" "-0(${GOODREG})" "+10(\$stack)" \
95	"+0(\$stack1)" "+10(@${GOODSYM}-10)" "+0(+10(+20(\$stack)))"
96test_badarg "+(${GOODREG})" "(${GOODREG}+10)" "-(${GOODREG})" "(${GOODREG})" \
97	"+10(\$comm)" "+0(${GOODREG})+10"
98
99: "Name assignment"
100test_goodarg "varname=${GOODREG}"
101test_badarg "varname=varname2=${GOODREG}"
102
103: "Type syntax"
104test_goodarg "${GOODREG}:${GOODTYPE}"
105test_badarg "${GOODREG}::${GOODTYPE}" "${GOODREG}:${BADTYPE}" \
106	"${GOODTYPE}:${GOODREG}"
107
108: "Combination check"
109
110test_goodarg "\$comm:string" "+0(\$stack):string"
111test_badarg "\$comm:x64" "\$stack:string" "${GOODREG}:string"
112