1#!/bin/bash 2# Add 'perf probe's, list and remove them 3# SPDX-License-Identifier: GPL-2.0 4 5# 6# test_adding_kernel of perf_probe test 7# Author: Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com> 8# Author: Michael Petlan <mpetlan@redhat.com> 9# 10# Description: 11# 12# This test tests adding of probes, their correct listing 13# and removing. 14# 15 16# include working environment 17. ../common/init.sh 18. ./settings.sh 19 20# shellcheck disable=SC2034 # the variable is later used after the working environment is included 21THIS_TEST_NAME=`basename $0 .sh` 22TEST_RESULT=0 23 24TEST_PROBE=${TEST_PROBE:-"inode_permission"} 25 26check_kprobes_available 27if [ $? -ne 0 ]; then 28 print_overall_skipped 29 exit 0 30fi 31 32 33### basic probe adding 34 35for opt in "" "-a" "--add"; do 36 clear_all_probes 37 $CMD_PERF probe $opt $TEST_PROBE 2> $LOGS_DIR/adding_kernel_add$opt.err 38 PERF_EXIT_CODE=$? 39 40 ../common/check_all_patterns_found.pl "Added new events?:" "probe:$TEST_PROBE" "on $TEST_PROBE" < $LOGS_DIR/adding_kernel_add$opt.err 41 CHECK_EXIT_CODE=$? 42 43 print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "adding probe $TEST_PROBE :: $opt" 44 (( TEST_RESULT += $? )) 45done 46 47 48### listing added probe :: perf list 49 50# any added probes should appear in perf-list output 51$CMD_PERF list probe:\* > $LOGS_DIR/adding_kernel_list.log 52PERF_EXIT_CODE=$? 53 54../common/check_all_lines_matched.pl "$RE_LINE_EMPTY" "List of pre-defined events" "probe:${TEST_PROBE}(?:_\d+)?\s+\[Tracepoint event\]" "Metric Groups:" < $LOGS_DIR/adding_kernel_list.log 55CHECK_EXIT_CODE=$? 56 57print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "listing added probe :: perf list" 58(( TEST_RESULT += $? )) 59 60 61### listing added probe :: perf probe -l 62 63# '-l' should list all the added probes as well 64$CMD_PERF probe -l > $LOGS_DIR/adding_kernel_list-l.log 65PERF_EXIT_CODE=$? 66 67../common/check_all_patterns_found.pl "\s*probe:${TEST_PROBE}(?:_\d+)?\s+\(on ${TEST_PROBE}(?:[:\+]$RE_NUMBER_HEX)?@.+\)" < $LOGS_DIR/adding_kernel_list-l.log 68CHECK_EXIT_CODE=$? 69 70print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "listing added probe :: perf probe -l" 71(( TEST_RESULT += $? )) 72 73 74### using added probe 75 76$CMD_PERF stat -e probe:$TEST_PROBE\* -o $LOGS_DIR/adding_kernel_using_probe.log -- cat /proc/uptime > /dev/null 77PERF_EXIT_CODE=$? 78 79REGEX_STAT_HEADER="\s*Performance counter stats for \'cat /proc/uptime\':" 80REGEX_STAT_VALUES="\s*\d+\s+probe:$TEST_PROBE" 81# the value should be greater than 1 82REGEX_STAT_VALUE_NONZERO="\s*[1-9][0-9]*\s+probe:$TEST_PROBE" 83REGEX_STAT_TIME="\s*$RE_NUMBER\s+seconds (?:time elapsed|user|sys)" 84../common/check_all_lines_matched.pl "$REGEX_STAT_HEADER" "$REGEX_STAT_VALUES" "$REGEX_STAT_TIME" "$RE_LINE_COMMENT" "$RE_LINE_EMPTY" < $LOGS_DIR/adding_kernel_using_probe.log 85CHECK_EXIT_CODE=$? 86../common/check_all_patterns_found.pl "$REGEX_STAT_HEADER" "$REGEX_STAT_VALUE_NONZERO" "$REGEX_STAT_TIME" < $LOGS_DIR/adding_kernel_using_probe.log 87(( CHECK_EXIT_CODE += $? )) 88 89print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "using added probe" 90(( TEST_RESULT += $? )) 91 92 93### removing added probe 94 95# '-d' should remove the probe 96$CMD_PERF probe -d $TEST_PROBE\* 2> $LOGS_DIR/adding_kernel_removing.err 97PERF_EXIT_CODE=$? 98 99../common/check_all_lines_matched.pl "Removed event: probe:$TEST_PROBE" < $LOGS_DIR/adding_kernel_removing.err 100CHECK_EXIT_CODE=$? 101 102print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "deleting added probe" 103(( TEST_RESULT += $? )) 104 105 106### listing removed probe 107 108# removed probes should NOT appear in perf-list output 109$CMD_PERF list probe:\* > $LOGS_DIR/adding_kernel_list_removed.log 110PERF_EXIT_CODE=$? 111 112../common/check_all_lines_matched.pl "$RE_LINE_EMPTY" "List of pre-defined events" "Metric Groups:" < $LOGS_DIR/adding_kernel_list_removed.log 113CHECK_EXIT_CODE=$? 114 115print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "listing removed probe (should NOT be listed)" 116(( TEST_RESULT += $? )) 117 118 119### dry run 120 121# the '-n' switch should run it in dry mode 122$CMD_PERF probe -n --add $TEST_PROBE 2> $LOGS_DIR/adding_kernel_dryrun.err 123PERF_EXIT_CODE=$? 124 125# check for the output (should be the same as usual) 126../common/check_all_patterns_found.pl "Added new events?:" "probe:$TEST_PROBE" "on $TEST_PROBE" < $LOGS_DIR/adding_kernel_dryrun.err 127CHECK_EXIT_CODE=$? 128 129# check that no probe was added in real 130! ( $CMD_PERF probe -l | grep "probe:$TEST_PROBE" ) 131(( CHECK_EXIT_CODE += $? )) 132 133print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "dry run :: adding probe" 134(( TEST_RESULT += $? )) 135 136 137### force-adding probes 138 139# when using '--force' a probe should be added even if it is already there 140$CMD_PERF probe --add $TEST_PROBE 2> $LOGS_DIR/adding_kernel_forceadd_01.err 141PERF_EXIT_CODE=$? 142 143../common/check_all_patterns_found.pl "Added new events?:" "probe:$TEST_PROBE" "on $TEST_PROBE" < $LOGS_DIR/adding_kernel_forceadd_01.err 144CHECK_EXIT_CODE=$? 145 146print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "force-adding probes :: first probe adding" 147(( TEST_RESULT += $? )) 148 149# adding existing probe without '--force' should fail 150! $CMD_PERF probe --add $TEST_PROBE 2> $LOGS_DIR/adding_kernel_forceadd_02.err 151PERF_EXIT_CODE=$? 152 153../common/check_all_patterns_found.pl "Error: event \"$TEST_PROBE\" already exists." "Error: Failed to add events." < $LOGS_DIR/adding_kernel_forceadd_02.err 154CHECK_EXIT_CODE=$? 155 156print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "force-adding probes :: second probe adding (without force)" 157(( TEST_RESULT += $? )) 158 159# adding existing probe with '--force' should pass 160NO_OF_PROBES=`$CMD_PERF probe -l | wc -l` 161$CMD_PERF probe --force --add $TEST_PROBE 2> $LOGS_DIR/adding_kernel_forceadd_03.err 162PERF_EXIT_CODE=$? 163 164../common/check_all_patterns_found.pl "Added new events?:" "probe:${TEST_PROBE}_${NO_OF_PROBES}" "on $TEST_PROBE" < $LOGS_DIR/adding_kernel_forceadd_03.err 165CHECK_EXIT_CODE=$? 166 167print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "force-adding probes :: second probe adding (with force)" 168(( TEST_RESULT += $? )) 169 170 171### using doubled probe 172 173# since they are the same, they should produce the same results 174$CMD_PERF stat -e probe:$TEST_PROBE -e probe:${TEST_PROBE}_${NO_OF_PROBES} -x';' -o $LOGS_DIR/adding_kernel_using_two.log -- bash -c 'cat /proc/cpuinfo > /dev/null' 175PERF_EXIT_CODE=$? 176 177REGEX_LINE="$RE_NUMBER;+probe:${TEST_PROBE}_?(?:$NO_OF_PROBES)?;$RE_NUMBER;$RE_NUMBER" 178../common/check_all_lines_matched.pl "$REGEX_LINE" "$RE_LINE_EMPTY" "$RE_LINE_COMMENT" < $LOGS_DIR/adding_kernel_using_two.log 179CHECK_EXIT_CODE=$? 180 181VALUE_1=`grep "$TEST_PROBE;" $LOGS_DIR/adding_kernel_using_two.log | awk -F';' '{print $1}'` 182VALUE_2=`grep "${TEST_PROBE}_${NO_OF_PROBES};" $LOGS_DIR/adding_kernel_using_two.log | awk -F';' '{print $1}'` 183 184test $VALUE_1 -eq $VALUE_2 185(( CHECK_EXIT_CODE += $? )) 186 187print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "using doubled probe" 188 189 190### removing multiple probes 191 192# using wildcards should remove all matching probes 193$CMD_PERF probe --del \* 2> $LOGS_DIR/adding_kernel_removing_wildcard.err 194PERF_EXIT_CODE=$? 195 196../common/check_all_lines_matched.pl "Removed event: probe:$TEST_PROBE" "Removed event: probe:${TEST_PROBE}_1" < $LOGS_DIR/adding_kernel_removing_wildcard.err 197CHECK_EXIT_CODE=$? 198 199print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "removing multiple probes" 200(( TEST_RESULT += $? )) 201 202 203### wildcard adding support 204 205$CMD_PERF probe -nf --max-probes=512 -a 'vfs_* $params' 2> $LOGS_DIR/adding_kernel_adding_wildcard.err 206PERF_EXIT_CODE=$? 207 208../common/check_all_patterns_found.pl "probe:vfs_mknod" "probe:vfs_create" "probe:vfs_rmdir" "probe:vfs_link" "probe:vfs_write" < $LOGS_DIR/adding_kernel_adding_wildcard.err 209CHECK_EXIT_CODE=$? 210 211print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "wildcard adding support" 212(( TEST_RESULT += $? )) 213 214 215### non-existing variable 216 217# perf probe should survive a non-existing variable probing attempt 218{ $CMD_PERF probe 'vfs_read somenonexistingrandomstuffwhichisalsoprettylongorevenlongertoexceed64' ; } 2> $LOGS_DIR/adding_kernel_nonexisting.err 219PERF_EXIT_CODE=$? 220 221# the exitcode should not be 0 or segfault 222test $PERF_EXIT_CODE -ne 139 -a $PERF_EXIT_CODE -ne 0 223PERF_EXIT_CODE=$? 224 225# check that the error message is reasonable 226../common/check_all_patterns_found.pl "Failed to find" "somenonexistingrandomstuffwhichisalsoprettylongorevenlongertoexceed64" < $LOGS_DIR/adding_kernel_nonexisting.err 227CHECK_EXIT_CODE=$? 228../common/check_all_patterns_found.pl "in this function|at this address" "Error" "Failed to add events" < $LOGS_DIR/adding_kernel_nonexisting.err 229(( CHECK_EXIT_CODE += $? )) 230../common/check_all_lines_matched.pl "Failed to find" "Error" "Probe point .+ not found" "optimized out" "Use.+\-\-range option to show.+location range" < $LOGS_DIR/adding_kernel_nonexisting.err 231(( CHECK_EXIT_CODE += $? )) 232../common/check_no_patterns_found.pl "$RE_SEGFAULT" < $LOGS_DIR/adding_kernel_nonexisting.err 233(( CHECK_EXIT_CODE += $? )) 234 235print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "non-existing variable" 236(( TEST_RESULT += $? )) 237 238 239### function with return value 240 241# adding probe with return value 242$CMD_PERF probe --add "$TEST_PROBE%return \$retval" 2> $LOGS_DIR/adding_kernel_func_retval_add.err 243PERF_EXIT_CODE=$? 244 245../common/check_all_patterns_found.pl "Added new events?:" "probe:$TEST_PROBE" "on $TEST_PROBE%return with \\\$retval" < $LOGS_DIR/adding_kernel_func_retval_add.err 246CHECK_EXIT_CODE=$? 247 248print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "function with retval :: add" 249(( TEST_RESULT += $? )) 250 251# recording some data 252$CMD_PERF record -e probe:$TEST_PROBE\* -o $CURRENT_TEST_DIR/perf.data -- cat /proc/cpuinfo > /dev/null 2> $LOGS_DIR/adding_kernel_func_retval_record.err 253PERF_EXIT_CODE=$? 254 255../common/check_all_patterns_found.pl "$RE_LINE_RECORD1" "$RE_LINE_RECORD2" < $LOGS_DIR/adding_kernel_func_retval_record.err 256CHECK_EXIT_CODE=$? 257 258print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "function with retval :: record" 259(( TEST_RESULT += $? )) 260 261# perf script should report the function calls with the correct arg values 262$CMD_PERF script -i $CURRENT_TEST_DIR/perf.data > $LOGS_DIR/adding_kernel_func_retval_script.log 263PERF_EXIT_CODE=$? 264 265REGEX_SCRIPT_LINE="\s*cat\s+$RE_NUMBER\s+\[$RE_NUMBER\]\s+$RE_NUMBER:\s+probe:$TEST_PROBE\w*:\s+\($RE_NUMBER_HEX\s+<\-\s+$RE_NUMBER_HEX\)\s+arg1=$RE_NUMBER_HEX" 266../common/check_all_lines_matched.pl "$REGEX_SCRIPT_LINE" < $LOGS_DIR/adding_kernel_func_retval_script.log 267CHECK_EXIT_CODE=$? 268../common/check_all_patterns_found.pl "$REGEX_SCRIPT_LINE" < $LOGS_DIR/adding_kernel_func_retval_script.log 269(( CHECK_EXIT_CODE += $? )) 270 271print_results $PERF_EXIT_CODE $CHECK_EXIT_CODE "function argument probing :: script" 272(( TEST_RESULT += $? )) 273 274 275clear_all_probes 276 277# print overall results 278print_overall_results "$TEST_RESULT" 279exit $? 280