1#!/bin/bash 2# Test that perf report includes file offsets and event type names in diagnostic messages. 3# SPDX-License-Identifier: GPL-2.0 4# 5# The file_offset diagnostic series adds "at offset 0x...: TYPE (N)" 6# to all skip/stop/error messages. This test corrupts an event's size 7# field in a perf.data file, then verifies the resulting warning 8# includes the file offset and event type. 9 10err=0 11 12cleanup() { 13 [ -n "${perfdata}" ] && rm -f "${perfdata}" "${perfdata}.old" 14 rm -f "${corrupted}" "${stderrfile}" 15 trap - EXIT TERM INT 16} 17trap 'cleanup; exit 1' TERM INT 18trap cleanup EXIT 19 20perfdata=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 21corrupted=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 22stderrfile=$(mktemp /tmp/__perf_test.perf.data.XXXXX) || exit 2 23 24if ! perf record -o "${perfdata}" -- perf test -w noploop 2>/dev/null; then 25 echo "Skip: perf record failed" 26 cleanup 27 exit 2 28fi 29 30# Find the file offset of the first MMAP2 event via perf report -D. 31# Format: "timestamp 0xOFFSET [0xSIZE]: PERF_RECORD_MMAP2 ..." 32mmap2_line=$(perf report -D -i "${perfdata}" 2>/dev/null | grep "PERF_RECORD_MMAP2" | head -1) 33if [ -z "${mmap2_line}" ]; then 34 echo "Skip: no MMAP2 events found in perf.data" 35 cleanup 36 exit 2 37fi 38 39mmap2_offset=$(echo "${mmap2_line}" | awk '{print $2}') 40mmap2_offset_dec=$((mmap2_offset)) 41 42# Copy the file and corrupt the MMAP2 event's size field. 43# perf_event_header layout: type(u32) misc(u16) size(u16) 44# Set size to 16 — below the MMAP2 minimum, which triggers 45# the "event size too small" warning. 46# 47# perf.data uses native byte order, so write the u16 in the 48# host's endianness to work on both LE and BE architectures. 49cp "${perfdata}" "${corrupted}" 50if printf '\x01\x02' | od -A n -t x2 | grep -q "0201"; then 51 # Little-endian 52 printf '\x10\x00' 53else 54 # Big-endian 55 printf '\x00\x10' 56fi | dd of="${corrupted}" bs=1 seek=$((mmap2_offset_dec + 6)) conv=notrunc 2>/dev/null 57 58perf report -i "${corrupted}" --stdio > /dev/null 2> "${stderrfile}" 59 60# Check that warnings include "at offset 0x..." 61if grep -q "at offset 0x" "${stderrfile}"; then 62 echo "File offset in diagnostics [Success]" 63else 64 echo "File offset in diagnostics [Failed: no 'at offset 0x...' found]" 65 echo " stderr was:" 66 head -5 "${stderrfile}" 67 err=1 68fi 69 70# Check that the event type name and numeric id appear: "MMAP2 (10)" 71if grep -q "MMAP2 (10)" "${stderrfile}"; then 72 echo "Event type name in diagnostics [Success]" 73else 74 echo "Event type name in diagnostics [Failed: no 'MMAP2 (10)' found]" 75 echo " stderr was:" 76 head -5 "${stderrfile}" 77 err=1 78fi 79 80# Check that the reported offset matches the actual corruption point 81if grep -q "at offset ${mmap2_offset}:" "${stderrfile}"; then 82 echo "Correct offset reported [Success]" 83else 84 echo "Correct offset reported [Failed: expected offset ${mmap2_offset}]" 85 echo " stderr was:" 86 head -5 "${stderrfile}" 87 err=1 88fi 89 90cleanup 91exit ${err} 92