xref: /linux/tools/testing/selftests/ftrace/test.d/00basic/trace_marker_raw.tc (revision b061fcffe336f1f1d1afe6a904f36962378c4026)
1#!/bin/sh
2# SPDX-License-Identifier: GPL-2.0
3# description: Basic tests on writing to trace_marker_raw
4# requires: trace_marker_raw
5# flags: instance
6
7is_little_endian() {
8	if lscpu | grep -q 'Little Endian'; then
9		echo 1;
10	else
11		echo 0;
12	fi
13}
14
15little=`is_little_endian`
16
17make_str() {
18	id=$1
19	cnt=$2
20
21	if [ $little -eq 1 ]; then
22		val=`printf "\\%03o\\%03o\\%03o\\%03o" \
23			$(($id & 0xff)) \
24			$((($id >> 8) & 0xff)) \
25			$((($id >> 16) & 0xff)) \
26			$((($id >> 24) & 0xff))`
27	else
28		val=`printf "\\%03o\\%03o\\%03o\\%03o" \
29			$((($id >> 24) & 0xff)) \
30			$((($id >> 16) & 0xff)) \
31			$((($id >> 8) & 0xff)) \
32			$(($id & 0xff))`
33	fi
34
35	data=`printf -- 'X%.0s' $(seq $cnt)`
36
37	printf "${val}${data}"
38}
39
40write_buffer() {
41	id=$1
42	size=$2
43
44	# write the string into the raw marker
45	make_str $id $size > trace_marker_raw
46}
47
48
49test_multiple_writes() {
50
51	# Write a bunch of data where the id is the count of
52	# data to write
53	for i in `seq 1 10` `seq 101 110` `seq 1001 1010`; do
54		write_buffer $i $i
55	done
56
57	# add a little buffer
58	echo stop > trace_marker
59
60	# Check to make sure the number of entries is the id (rounded up by 4)
61	awk '/.*: # [0-9a-f]* / {
62			print;
63			cnt = -1;
64			for (i = 0; i < NF; i++) {
65				# The counter is after the "#" marker
66				if ( $i == "#" ) {
67					i++;
68					cnt = strtonum("0x" $i);
69					num = NF - (i + 1);
70					# The number of items is always rounded up by 4
71					cnt2 = int((cnt + 3) / 4) * 4;
72					if (cnt2 != num) {
73						exit 1;
74					}
75					break;
76				}
77			}
78		}
79	// { if (NR > 30) { exit 0; } } ' trace_pipe;
80}
81
82
83get_buffer_data_size() {
84	sed -ne 's/^.*data.*size:\([0-9][0-9]*\).*/\1/p' events/header_page
85}
86
87test_buffer() {
88
89	# The id must be four bytes, test that 3 bytes fails a write
90	if echo -n abc > ./trace_marker_raw ; then
91		echo "Too small of write expected to fail but did not"
92		echo ${ORIG} > buffer_size_kb
93		exit_fail
94	fi
95
96	size=`get_buffer_data_size`
97	echo size = $size
98
99	# Now add a little more than what it can handle
100
101	if write_buffer 0xdeadbeef $size ; then
102		echo "Too big of write expected to fail but did not"
103		echo ${ORIG} > buffer_size_kb
104		exit_fail
105	fi
106}
107
108ORIG=`cat buffer_size_kb`
109
110# test_multiple_writes test needs at least 12KB buffer
111NEW_SIZE=12
112
113if [ ${ORIG} -lt ${NEW_SIZE} ]; then
114	echo ${NEW_SIZE} > buffer_size_kb
115fi
116
117test_buffer
118if ! test_multiple_writes; then
119	echo ${ORIG} > buffer_size_kb
120	exit_fail
121fi
122
123echo ${ORIG} > buffer_size_kb
124