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