summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/ftrace/boottime/run_boottime_test.sh
blob: 6e8593d5a2c549bcde7900b0e0748d4547bb6181 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
# Copyright (C) 2026, Google LLC.
#
# Generic Boot Tracing Test Harness
# Builds a lightweight initramfs, applies bootconfigs and kernel cmdline parameters,
# boots QEMU, and verifies tracefs configuration using test scripts.

set -e

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
if [ -d "$SCRIPT_DIR/boottime" ]; then
	SCRIPT_DIR="$SCRIPT_DIR/boottime"
fi
KERNEL_SRC="$(cd "$SCRIPT_DIR/../../../../.." && pwd)"

# Source KTAP helpers
if [ -f "$SCRIPT_DIR/../../kselftest/ktap_helpers.sh" ]; then
	KSELFTEST_DIR="$(cd "$SCRIPT_DIR/../../kselftest" && pwd)"
elif [ -f "$SCRIPT_DIR/../kselftest/ktap_helpers.sh" ]; then
	KSELFTEST_DIR="$(cd "$SCRIPT_DIR/../kselftest" && pwd)"
else
	echo "Error: ktap_helpers.sh not found relative to $SCRIPT_DIR" >&2
	exit 1
fi
. "$KSELFTEST_DIR/ktap_helpers.sh"

BOOTCONFIG="${BOOTCONFIG:-"$KERNEL_SRC/tools/bootconfig/bootconfig"}"
KERNEL="${KERNEL:-"$KERNEL_SRC/arch/x86/boot/bzImage"}"
QEMU="${QEMU:-"qemu-system-x86_64"}"
BUSYBOX="${BUSYBOX:-"$(which busybox 2>/dev/null || echo "/usr/bin/busybox")"}"
TIMEOUT="${TIMEOUT:-60s}"
LOGDIR="${LOGDIR:-""}"
TARGET_TEST="all"

BUSYBOX_APPLETS=(
	sh
	cat
	mount
	umount
	echo
	sync
	reboot
	poweroff
	cpio
	grep
	sleep
	sed
)

usage() {
	echo "Usage: $0 [options]"
	echo "Options:"
	echo "  -b, --bootconfig PATH   Path to bootconfig tool (default: $BOOTCONFIG)"
	echo "  -k, --kernel PATH       Path to kernel image (default: $KERNEL)"
	echo "  -q, --qemu PATH         Path to QEMU executable (default: $QEMU)"
	echo "  -B, --busybox PATH      Path to busybox binary (default: $BUSYBOX)"
	echo "  -T, --timeout DURATION  QEMU execution timeout (default: $TIMEOUT)"
	echo "  -l, --logdir DIR        Directory to save QEMU log files"
	echo "  -t, --test NAME         Run specific test (e.g. 01-kprobe) or 'all'"
	echo "  -h, --help              Show this help message"
	exit 1
}

while [ $# -gt 0 ]; do
	case "$1" in
		-b|--bootconfig)
			BOOTCONFIG="$2"
			shift 2
			;;
		-k|--kernel)
			KERNEL="$2"
			shift 2
			;;
		-q|--qemu)
			QEMU="$2"
			shift 2
			;;
		-B|--busybox)
			BUSYBOX="$2"
			shift 2
			;;
		-T|--timeout)
			TIMEOUT="$2"
			shift 2
			;;
		-l|--logdir)
			LOGDIR="$2"
			shift 2
			;;
		-t|--test)
			TARGET_TEST="$2"
			shift 2
			;;
		-h|--help)
			usage
			;;
		*)
			echo "Unknown option: $1"
			usage
			;;
	esac
done

# Check host architecture
HOST_ARCH="$(uname -m)"
case "$HOST_ARCH" in
	x86_64|i686|i386|x86)
		;;
	*)
		if [ "$KERNEL" = "$KERNEL_SRC/arch/x86/boot/bzImage" ]; then
			ktap_print_header
			ktap_skip_all \
				"Default boot tracing test is x86 only ($HOST_ARCH)"
			exit 0
		fi
		;;
esac

# Ensure bootconfig tool is built if Makefile exists
if [ ! -x "$BOOTCONFIG" ]; then
	if [ -f "$KERNEL_SRC/tools/bootconfig/Makefile" ]; then
		make -C "$KERNEL_SRC/tools/bootconfig" > /dev/null 2>&1 || true
	fi
	if [ ! -x "$BOOTCONFIG" ]; then
		BOOTCONFIG="$(which bootconfig 2>/dev/null || true)"
	fi
fi

if [ ! -x "$BOOTCONFIG" ]; then
	ktap_print_header
	ktap_skip_all "bootconfig tool not found at $BOOTCONFIG"
	exit 0
fi

if [ ! -f "$KERNEL" ]; then
	ktap_print_header
	ktap_skip_all "Kernel image not found at $KERNEL"
	exit 0
fi

if ! command -v "$QEMU" >/dev/null 2>&1; then
	ktap_print_header
	ktap_skip_all "QEMU binary not found: $QEMU"
	exit 0
fi

if [ ! -x "$BUSYBOX" ]; then
	ktap_print_header
	ktap_skip_all "busybox binary not found at $BUSYBOX"
	exit 0
fi

# Verify file command existence and architecture compatibility
if ! command -v file >/dev/null 2>&1; then
	ktap_print_header
	ktap_skip_all "file tool not found"
	exit 0
fi

KERNEL_INFO="$(file -bL "$KERNEL" 2>/dev/null || true)"
case "$KERNEL_INFO" in
	*x86*)
		;;
	*)
		ktap_print_header
		ktap_skip_all "Kernel architecture is not x86 ($KERNEL_INFO)"
		exit 0
		;;
esac

BUSYBOX_INFO="$(file -bL "$BUSYBOX" 2>/dev/null || true)"
case "$BUSYBOX_INFO" in
	*x86-64*|*x86_64*|*80386*|*i386*)
		;;
	*)
		ktap_print_header
		ktap_skip_all \
			"busybox binary architecture is incompatible with x86 kernel ($BUSYBOX_INFO)"
		exit 0
		;;
esac

if ! command -v cpio >/dev/null 2>&1; then
	ktap_print_header
	ktap_skip_all "cpio tool not found"
	exit 0
fi

if ! command -v timeout >/dev/null 2>&1; then
	ktap_print_header
	ktap_skip_all "timeout tool not found"
	exit 0
fi

double_timeout() {
	local val="$1"
	if [[ "$val" =~ ^([0-9]+)([a-z]*)$ ]]; then
		local num="${BASH_REMATCH[1]}"
		local unit="${BASH_REMATCH[2]}"
		echo "$((num * 2))$unit"
	else
		echo "120s"
	fi
}

run_single_test() {
	local test_script="$1"
	local name
	local dir
	name="$(basename "$test_script" .sh)"
	dir="$(dirname "$test_script")"

	local bconf_file=""
	if [ -f "$SCRIPT_DIR/bootconfigs/$name.bconf" ]; then
		bconf_file="$SCRIPT_DIR/bootconfigs/$name.bconf"
	elif [ -f "$dir/$name.bconf" ]; then
		bconf_file="$dir/$name.bconf"
	fi

	local test_cmdline=""
	if [ -f "$SCRIPT_DIR/cmdlines/$name.cmdline" ]; then
		test_cmdline="$(cat "$SCRIPT_DIR/cmdlines/$name.cmdline")"
	elif [ -f "$dir/$name.cmdline" ]; then
		test_cmdline="$(cat "$dir/$name.cmdline")"
	fi

	local inline_cmdline
	inline_cmdline="$(grep -E '^# *CMDLINE:' "$test_script" | sed 's/^# *CMDLINE://' | xargs || true)"
	if [ -n "$inline_cmdline" ]; then
		test_cmdline="$test_cmdline $inline_cmdline"
	fi

	local test_qemuopts=""
	if [ -f "$SCRIPT_DIR/qemuopts/$name.qemuopts" ]; then
		test_qemuopts="$(cat "$SCRIPT_DIR/qemuopts/$name.qemuopts")"
	elif [ -f "$dir/$name.qemuopts" ]; then
		test_qemuopts="$(cat "$dir/$name.qemuopts")"
	fi

	local inline_qemuopts
	inline_qemuopts="$(grep -E '^# *QEMUOPTS:' "$test_script" | sed 's/^# *QEMUOPTS://' | xargs || true)"
	if [ -n "$inline_qemuopts" ]; then
		test_qemuopts="$test_qemuopts $inline_qemuopts"
	fi

	local test_timeout="$TIMEOUT"
	local inline_timeout
	inline_timeout="$(grep -E '^# *TIMEOUT:' "$test_script" | sed 's/^# *TIMEOUT://' | xargs || true)"
	if [ -n "$inline_timeout" ]; then
		test_timeout="$inline_timeout"
	fi

	local is_reboot=0
	if grep -q -E '^# *REBOOT: *1' "$test_script" || [ -f "$dir/$name.reboot" ]; then
		is_reboot=1
	fi

	local effective_timeout="$test_timeout"
	if [ "$is_reboot" -eq 1 ]; then
		effective_timeout="$(double_timeout "$test_timeout")"
	fi

	local workdir
	workdir="$(mktemp -d)"
	if [ -z "$workdir" ] || [ ! -d "$workdir" ]; then
		ktap_test_fail "$name (failed to create temporary directory)"
		return 0
	fi
	trap 'rm -rf "$workdir"' EXIT

	local test_applets=("${BUSYBOX_APPLETS[@]}")
	local inline_applets
	inline_applets="$(grep -E '^# *APPLETS:' "$test_script" | sed 's/^# *APPLETS://' | xargs || true)"
	if [ -n "$inline_applets" ]; then
		read -r -a extra_applets <<< "$inline_applets" || true
		test_applets+=("${extra_applets[@]}")
	fi

	local rootfs="$workdir/rootfs"
	mkdir -p "$rootfs"/{bin,sbin,etc,proc,sys,dev,tmp}

	cp -L "$BUSYBOX" "$rootfs/bin/busybox"
	chmod +x "$rootfs/bin/busybox"
	(cd "$rootfs/bin" && for applet in "${test_applets[@]}"; do ln -sf busybox "$applet"; done)

	if command -v ldd >/dev/null 2>&1; then
		for lib in $(ldd "$BUSYBOX" 2>/dev/null | grep -o '/[^ ]*' || true); do
			if [ -f "$lib" ]; then
				mkdir -p "$rootfs$(dirname "$lib")"
				cp -L "$lib" "$rootfs$lib" 2>/dev/null || true
			fi
		done
	fi

	cat << 'EOF' > "$rootfs/init"
#!/bin/sh
mount -t proc proc /proc 2>/dev/null
mount -t sysfs sys /sys 2>/dev/null
mount -t tracefs nodev /sys/kernel/tracing 2>/dev/null

/bin/check_test.sh
RET=$?

if [ $RET -eq 0 ]; then
	echo "TEST RESULT: PASS"
else
	echo "TEST RESULT: FAIL"
fi
sync
echo o > /proc/sysrq-trigger 2>/dev/null || true
poweroff -f 2>/dev/null || reboot -f 2>/dev/null || true
while true; do sleep 100 2>/dev/null || break; done
EOF
	chmod +x "$rootfs/init"

	cp "$test_script" "$rootfs/bin/check_test.sh"
	chmod +x "$rootfs/bin/check_test.sh"

	local initramfs="$workdir/initramfs.cpio"
	(cd "$rootfs" && find . | cpio -o -H newc --quiet) > "$initramfs"

	if [ -n "$bconf_file" ] && [ -f "$bconf_file" ]; then
		"$BOOTCONFIG" -a "$bconf_file" "$initramfs" > /dev/null
	fi


	local logfile
	local base_cmdline="bootconfig console=tty0 console=ttyS0 panic=-1"
	if [ -n "$test_cmdline" ]; then
		base_cmdline="$base_cmdline $test_cmdline"
	fi

	if [ -n "$LOGDIR" ]; then
		mkdir -p "$LOGDIR"
		logfile="$LOGDIR/$name.log"
		base_cmdline="$base_cmdline dump_bconf"
	else
		logfile="$workdir/qemu.log"
		base_cmdline="quiet $base_cmdline"
	fi

	local qemu_args=()
	if [ -n "$test_qemuopts" ]; then
		read -r -d '' -a qemu_args <<< "$test_qemuopts" || true
	fi

	if [ "$is_reboot" -ne 1 ]; then
		qemu_args+=("-no-reboot")
	fi

	timeout "$effective_timeout" "$QEMU" -kernel "$KERNEL" \
		-initrd "$initramfs" \
		-append "$base_cmdline" \
		-display none \
		-serial stdio \
		"${qemu_args[@]}" < /dev/null > "$logfile" 2>&1 || true

	if grep -q "TEST RESULT: PASS" "$logfile"; then
		ktap_test_pass "$name"
	else
		ktap_test_fail "$name"
		ktap_print_msg "QEMU Console Output for $name:"
		while IFS= read -r line; do
			ktap_print_msg "$line"
		done < "$logfile"
	fi

	rm -rf "$workdir"
	trap - EXIT
	return 0
}

TEST_SCRIPTS=()
if [ -d "$SCRIPT_DIR/tests" ]; then
	while IFS= read -r f; do
		[ -n "$f" ] && TEST_SCRIPTS+=("$f")
	done < <(find "$SCRIPT_DIR/tests" -type f -name "*.sh" | sort)
fi

TOTAL_TESTS=0
for test_script in "${TEST_SCRIPTS[@]}"; do
	name="$(basename "$test_script" .sh)"
	if [ "$TARGET_TEST" != "all" ] && [ "$TARGET_TEST" != "$name" ]; then
		continue
	fi
	TOTAL_TESTS=$((TOTAL_TESTS + 1))
done

ktap_print_header
ktap_set_plan "$TOTAL_TESTS"

for test_script in "${TEST_SCRIPTS[@]}"; do
	name="$(basename "$test_script" .sh)"

	if [ "$TARGET_TEST" != "all" ] && [ "$TARGET_TEST" != "$name" ]; then
		continue
	fi

	run_single_test "$test_script"
done

ktap_finished