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