From: Arnaldo Carvalho de Melo Date: Tue, 2 Jun 2026 23:45:14 +0000 (-0300) Subject: perf test: Add file offset diagnostic test for corrupted perf.data X-Git-Url: http://git.ipfire.org/index.cgi?a=commitdiff_plain;h=078fc0430bf17e2a571e36877c1942f6c0860524;p=thirdparty%2Fkernel%2Flinux.git perf test: Add file offset diagnostic test for corrupted perf.data Add a shell test that verifies the file_offset diagnostic messages work correctly when perf encounters corrupted events. The test corrupts a MMAP2 event's size field in a recorded perf.data file, then checks that perf report produces warning messages that include both the file offset (e.g. "at offset 0x2738:") and the event type name with numeric id (e.g. "MMAP2 (10)"). This exercises the diagnostic improvements from the file_offset series, which retrofitted all skip/stop/error messages to include the position and type of the problematic event. Reviewed-by: Ian Rogers Assisted-by: Claude:claude-opus-4.6 Signed-off-by: Arnaldo Carvalho de Melo --- diff --git a/tools/perf/tests/shell/data_file_offset_diagnostics.sh b/tools/perf/tests/shell/data_file_offset_diagnostics.sh new file mode 100755 index 0000000000000..389b0b84752d4 --- /dev/null +++ b/tools/perf/tests/shell/data_file_offset_diagnostics.sh @@ -0,0 +1,91 @@ +#!/bin/bash +# Test that perf report includes file offsets and event type names in diagnostic messages. +# SPDX-License-Identifier: GPL-2.0 +# +# The file_offset diagnostic series adds "at offset 0x...: TYPE (N)" +# to all skip/stop/error messages. This test corrupts an event's size +# field in a perf.data file, then verifies the resulting warning +# includes the file offset and event type. + +err=0 + +cleanup() { + [ -n "${perfdata}" ] && rm -f "${perfdata}" "${perfdata}.old" + rm -f "${corrupted}" "${stderrfile}" + trap - EXIT TERM INT +} +trap 'cleanup; exit 1' TERM INT +trap cleanup EXIT + +perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 +corrupted=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 +stderrfile=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 + +if ! perf record -o "${perfdata}" -- perf test -w noploop 2>/dev/null; then + echo "Skip: perf record failed" + cleanup + exit 2 +fi + +# Find the file offset of the first MMAP2 event via perf report -D. +# Format: "timestamp 0xOFFSET [0xSIZE]: PERF_RECORD_MMAP2 ..." +mmap2_line=$(perf report -D -i "${perfdata}" 2>/dev/null | grep "PERF_RECORD_MMAP2" | head -1) +if [ -z "${mmap2_line}" ]; then + echo "Skip: no MMAP2 events found in perf.data" + cleanup + exit 2 +fi + +mmap2_offset=$(echo "${mmap2_line}" | awk '{print $2}') +mmap2_offset_dec=$((mmap2_offset)) + +# Copy the file and corrupt the MMAP2 event's size field. +# perf_event_header layout: type(u32) misc(u16) size(u16) +# Set size to 16 — below the MMAP2 minimum, which triggers +# the "event size too small" warning. +# +# perf.data uses native byte order, so write the u16 in the +# host's endianness to work on both LE and BE architectures. +cp "${perfdata}" "${corrupted}" +if printf '\x01\x02' | od -A n -t x2 | grep -q "0201"; then + # Little-endian + printf '\x10\x00' +else + # Big-endian + printf '\x00\x10' +fi | dd of="${corrupted}" bs=1 seek=$((mmap2_offset_dec + 6)) conv=notrunc 2>/dev/null + +perf report -i "${corrupted}" --stdio > /dev/null 2> "${stderrfile}" + +# Check that warnings include "at offset 0x..." +if grep -q "at offset 0x" "${stderrfile}"; then + echo "File offset in diagnostics [Success]" +else + echo "File offset in diagnostics [Failed: no 'at offset 0x...' found]" + echo " stderr was:" + head -5 "${stderrfile}" + err=1 +fi + +# Check that the event type name and numeric id appear: "MMAP2 (10)" +if grep -q "MMAP2 (10)" "${stderrfile}"; then + echo "Event type name in diagnostics [Success]" +else + echo "Event type name in diagnostics [Failed: no 'MMAP2 (10)' found]" + echo " stderr was:" + head -5 "${stderrfile}" + err=1 +fi + +# Check that the reported offset matches the actual corruption point +if grep -q "at offset ${mmap2_offset}:" "${stderrfile}"; then + echo "Correct offset reported [Success]" +else + echo "Correct offset reported [Failed: expected offset ${mmap2_offset}]" + echo " stderr was:" + head -5 "${stderrfile}" + err=1 +fi + +cleanup +exit ${err}