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