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