summaryrefslogtreecommitdiff
path: root/tools/perf/tests/shell/test_failed_syscalls_by_pid_python.sh
blob: 070a7ef6b8ad5e26f6b5a825f35fe12d312e0ed3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# failed-syscalls-by-pid python test

set -e

shelldir=$(dirname "$0")
# shellcheck source=lib/setup_python.sh
. "${shelldir}"/lib/setup_python.sh

if ! "$PYTHON" -c 'import perf' > /dev/null 2>&1; then
	echo "Skipping test, perf python module not found"
	exit 2
fi

script_dir="$(dirname "$0")/../../python"
script_path="${script_dir}/failed-syscalls-by-pid.py"

if ! perf check feature -q libtraceevent > /dev/null 2>&1; then
	echo "Skipping test, libtraceevent is disabled"
	exit 2
fi

if [ ! -f "$script_path" ]; then
	echo "Skipping test, failed-syscalls-by-pid.py not found at $script_path"
	exit 2
fi

err=0
temp_dir=$(mktemp -d /tmp/perf-failed-syscalls-by-pid-XXXXXX)
temp_data="${temp_dir}/perf.data"
temp_out="${temp_dir}/perf.out"

cleanup() {
	rm -rf "${temp_dir}"
}

trap 'cleanup' EXIT TERM INT

test_file_mode() {
	echo "Testing failed-syscalls-by-pid.py..."

	# Check if syscalls:sys_exit is supported/readable
	if ! perf record -B -N --no-bpf-event -e syscalls:sys_exit \
		-o /dev/null -- true >/dev/null 2>&1; then
		if ! perf record -B -N --no-bpf-event -e raw_syscalls:sys_exit \
			-o /dev/null -- true >/dev/null 2>&1; then
			echo "Skipping test, no syscalls:sys_exit or raw_syscalls:sys_exit event"
			exit 2
		else
			EVENT="raw_syscalls:sys_exit"
		fi
	else
		EVENT="syscalls:sys_exit"
	fi

	# Generate some events by running a command that fails a syscall
	# (e.g. failing stat on non-existent file), sleeping briefly in the
	# subshell so ls's PERF_RECORD_COMM and sys_exit events are flushed.
	passed=0
	for _ in 1 2 3 4 5; do
		rm -f "${temp_data}" "${temp_out}" "${temp_out}.comm" "${temp_out}.pid"
		perf record -B -N --no-bpf-event -e "${EVENT}" -o "${temp_data}" \
			-- sh -c "ls /does_not_exist 2>/dev/null; sleep 0.05 || true" \
			>/dev/null 2>&1 || true
		if [ ! -s "${temp_data}" ]; then
			continue
		fi

		if perf script failed-syscalls-by-pid -i "${temp_data}" > "${temp_out}" && \
		   grep -n -q "err = ENOENT" "${temp_out}" && \
		   perf script failed-syscalls-by-pid -i "${temp_data}" \
			"ls" > "${temp_out}.comm" && \
		   grep -q "err = ENOENT" "${temp_out}.comm"; then
			ls_pid=$(sed -n 's/^ls \[\([0-9][0-9]*\)\].*/\1/p' \
				"${temp_out}.comm" | head -n 1)
			if [ -n "${ls_pid}" ] && \
			   perf script failed-syscalls-by-pid -i "${temp_data}" \
				"${ls_pid}" > "${temp_out}.pid" && \
			   grep -q "err = ENOENT" "${temp_out}.pid"; then
				passed=1
				break
			fi
		fi
	done

	if [ ! -s "${temp_data}" ]; then
		echo "Skipping test, perf record failed to create data"
		exit 2
	fi

	if [ "$passed" -eq 0 ]; then
		echo "failed-syscalls-by-pid test failed."
		cat "${temp_out}" 2>/dev/null || true
		cat "${temp_out}.comm" 2>/dev/null || true
		cat "${temp_out}.pid" 2>/dev/null || true
		err=1
	else
		echo "failed-syscalls-by-pid test passed."
	fi
	rm -f "${temp_out}" "${temp_out}.comm" "${temp_out}.pid"
}

test_file_mode

exit $err