1f08c3bdfSopenharmony_ci#!/bin/sh
2f08c3bdfSopenharmony_ci# SPDX-License-Identifier: GPL-2.0-or-later
3f08c3bdfSopenharmony_ci# Copyright (C) 2017 Red Hat, Inc.
4f08c3bdfSopenharmony_ci#
5f08c3bdfSopenharmony_ci# Test functionality of dynamic debug feature by enabling
6f08c3bdfSopenharmony_ci# and disabling traces with available flags. Check that these
7f08c3bdfSopenharmony_ci# settings don't cause issues by searching dmesg.
8f08c3bdfSopenharmony_ci#
9f08c3bdfSopenharmony_ci# This test handles changes of dynamic debug interface from
10f08c3bdfSopenharmony_ci# commits 5ca7d2a6 (dynamic_debug: describe_flags with
11f08c3bdfSopenharmony_ci# '=[pmflt_]*') and 8ba6ebf5 (Dynamic debug: Add more flags)
12f08c3bdfSopenharmony_ci
13f08c3bdfSopenharmony_ciTST_TESTFUNC=ddebug_test
14f08c3bdfSopenharmony_ciTST_NEEDS_CMDS="awk /bin/echo"
15f08c3bdfSopenharmony_ciTST_NEEDS_TMPDIR=1
16f08c3bdfSopenharmony_ciTST_NEEDS_ROOT=1
17f08c3bdfSopenharmony_ciTST_SETUP=setup
18f08c3bdfSopenharmony_ciTST_CLEANUP=cleanup
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ciDEBUGFS_WAS_MOUNTED=0
21f08c3bdfSopenharmony_ciDEBUGFS_PATH=""
22f08c3bdfSopenharmony_ciDEBUGFS_CONTROL=""
23f08c3bdfSopenharmony_ciDYNDEBUG_STATEMENTS="./debug_statements"
24f08c3bdfSopenharmony_ciEMPTY_FLAG="=_"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cimount_debugfs()
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	if grep -q debugfs /proc/mounts ; then
29f08c3bdfSopenharmony_ci		DEBUGFS_WAS_MOUNTED=1
30f08c3bdfSopenharmony_ci		DEBUGFS_PATH=$(awk '/debugfs/{print $2}' /proc/mounts)
31f08c3bdfSopenharmony_ci		tst_res TINFO "debugfs already mounted at $DEBUGFS_PATH"
32f08c3bdfSopenharmony_ci	else
33f08c3bdfSopenharmony_ci		if ! grep -q debugfs /proc/filesystems ; then
34f08c3bdfSopenharmony_ci			tst_res TCONF "debugfs not supported"
35f08c3bdfSopenharmony_ci		fi
36f08c3bdfSopenharmony_ci		DEBUGFS_PATH="$PWD/tst_debug"
37f08c3bdfSopenharmony_ci		mkdir "$DEBUGFS_PATH"
38f08c3bdfSopenharmony_ci		if mount -t debugfs xxx "$DEBUGFS_PATH" ; then
39f08c3bdfSopenharmony_ci			tst_res TINFO "debugfs mounted at $DEBUGFS_PATH"
40f08c3bdfSopenharmony_ci		else
41f08c3bdfSopenharmony_ci			tst_res TFAIL "Unable to mount debugfs"
42f08c3bdfSopenharmony_ci		fi
43f08c3bdfSopenharmony_ci	fi
44f08c3bdfSopenharmony_ci}
45f08c3bdfSopenharmony_ci
46f08c3bdfSopenharmony_cisetup()
47f08c3bdfSopenharmony_ci{
48f08c3bdfSopenharmony_ci	mount_debugfs
49f08c3bdfSopenharmony_ci	if [ ! -d "$DEBUGFS_PATH/dynamic_debug" ] ; then
50f08c3bdfSopenharmony_ci		tst_brk TBROK "Unable to find $DEBUGFS_PATH/dynamic_debug"
51f08c3bdfSopenharmony_ci	fi
52f08c3bdfSopenharmony_ci	DEBUGFS_CONTROL="$DEBUGFS_PATH/dynamic_debug/control"
53f08c3bdfSopenharmony_ci	if [ ! -e "$DEBUGFS_CONTROL" ] ; then
54f08c3bdfSopenharmony_ci		tst_brk TBROK "Unable to find $DEBUGFS_CONTROL"
55f08c3bdfSopenharmony_ci	fi
56f08c3bdfSopenharmony_ci
57f08c3bdfSopenharmony_ci	grep -v "^#" "$DEBUGFS_CONTROL" > "$DYNDEBUG_STATEMENTS"
58f08c3bdfSopenharmony_ci}
59f08c3bdfSopenharmony_ci
60f08c3bdfSopenharmony_cido_flag()
61f08c3bdfSopenharmony_ci{
62f08c3bdfSopenharmony_ci	local FLAG="$1"
63f08c3bdfSopenharmony_ci	local OPTION_TO_SET="$2"
64f08c3bdfSopenharmony_ci	local OPTION_VALUE="$3"
65f08c3bdfSopenharmony_ci
66f08c3bdfSopenharmony_ci	if ! echo "$OPTION_TO_SET $OPTION_VALUE $FLAG" > \
67f08c3bdfSopenharmony_ci		"$DEBUGFS_CONTROL" ; then
68f08c3bdfSopenharmony_ci		tst_res TFAIL "Setting '$OPTION_TO_SET $OPTION_VALUE " \
69f08c3bdfSopenharmony_ci			"$FLAG' failed with $?!"
70f08c3bdfSopenharmony_ci	fi
71f08c3bdfSopenharmony_ci}
72f08c3bdfSopenharmony_ci
73f08c3bdfSopenharmony_cido_all_flags()
74f08c3bdfSopenharmony_ci{
75f08c3bdfSopenharmony_ci	OPTION="$1"
76f08c3bdfSopenharmony_ci	ALL_INPUTS="$2"
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	for INPUT_LINE in $ALL_INPUTS; do
79f08c3bdfSopenharmony_ci		do_flag "+p" "$OPTION" "$INPUT_LINE"
80f08c3bdfSopenharmony_ci		do_flag "+flmt" "$OPTION" "$INPUT_LINE"
81f08c3bdfSopenharmony_ci		do_flag "-flmt" "$OPTION" "$INPUT_LINE"
82f08c3bdfSopenharmony_ci		do_flag "-p" "$OPTION" "$INPUT_LINE"
83f08c3bdfSopenharmony_ci	done
84f08c3bdfSopenharmony_ci
85f08c3bdfSopenharmony_ci	if awk -v emp="$EMPTY_FLAG" '$3 != emp' "$DEBUGFS_CONTROL" \
86f08c3bdfSopenharmony_ci		| grep -v -q "^#" ; then
87f08c3bdfSopenharmony_ci		tst_res TFAIL "Failed to remove all set flags"
88f08c3bdfSopenharmony_ci	fi
89f08c3bdfSopenharmony_ci}
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ciddebug_test()
92f08c3bdfSopenharmony_ci{
93f08c3bdfSopenharmony_ci	dmesg > ./dmesg.old
94f08c3bdfSopenharmony_ci
95f08c3bdfSopenharmony_ci	DD_FUNCS=$(awk -F " |]" '{print $3}' "$DYNDEBUG_STATEMENTS" \
96f08c3bdfSopenharmony_ci		| sort | uniq)
97f08c3bdfSopenharmony_ci	DD_FILES=$(awk -F " |:" '{print $1}' "$DYNDEBUG_STATEMENTS" \
98f08c3bdfSopenharmony_ci		| sort | uniq)
99f08c3bdfSopenharmony_ci	DD_LINES=$(awk -F " |:" '{print $2}' "$DYNDEBUG_STATEMENTS" \
100f08c3bdfSopenharmony_ci		| sort | uniq)
101f08c3bdfSopenharmony_ci	DD_MODULES=$(awk -F [][] '{print $2}' "$DYNDEBUG_STATEMENTS" \
102f08c3bdfSopenharmony_ci		| sort | uniq)
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	do_all_flags "func" "$DD_FUNCS"
105f08c3bdfSopenharmony_ci	do_all_flags "file" "$DD_FILES"
106f08c3bdfSopenharmony_ci	do_all_flags "line" "$DD_LINES"
107f08c3bdfSopenharmony_ci	do_all_flags "module" "$DD_MODULES"
108f08c3bdfSopenharmony_ci
109f08c3bdfSopenharmony_ci	dmesg > ./dmesg.new
110f08c3bdfSopenharmony_ci	sed -i -e 1,`wc -l < ./dmesg.old`d ./dmesg.new
111f08c3bdfSopenharmony_ci	if grep -q -e "Kernel panic" -e "Oops" -e "general protection fault" \
112f08c3bdfSopenharmony_ci		-e "general protection handler: wrong gs" -e "\(XEN\) Panic" \
113f08c3bdfSopenharmony_ci		-e "fault" -e "warn" -e "\<BUG\>" ./dmesg.new ; then
114f08c3bdfSopenharmony_ci		tst_res TFAIL "Issues found in dmesg!"
115f08c3bdfSopenharmony_ci	else
116f08c3bdfSopenharmony_ci		tst_res TPASS "Dynamic debug OK"
117f08c3bdfSopenharmony_ci	fi
118f08c3bdfSopenharmony_ci}
119f08c3bdfSopenharmony_ci
120f08c3bdfSopenharmony_cicleanup()
121f08c3bdfSopenharmony_ci{
122f08c3bdfSopenharmony_ci	if [ -e "$DYNDEBUG_STATEMENTS" ]; then
123f08c3bdfSopenharmony_ci		FLAGS_SET=$(awk -v emp="$EMPTY_FLAG" '$3 != emp' $DYNDEBUG_STATEMENTS)
124f08c3bdfSopenharmony_ci	fi
125f08c3bdfSopenharmony_ci	if [ "$FLAGS_SET" ] ; then
126f08c3bdfSopenharmony_ci		/bin/echo "$FLAGS_SET" | while read -r FLAG_LINE ; do
127f08c3bdfSopenharmony_ci			/bin/echo -n "$FLAG_LINE" \
128f08c3bdfSopenharmony_ci				| awk -v prf= -F " |:" \
129f08c3bdfSopenharmony_ci				'{print "file "$1" line "$2" "prf $4}' \
130f08c3bdfSopenharmony_ci				> "$DEBUGFS_CONTROL"
131f08c3bdfSopenharmony_ci		done
132f08c3bdfSopenharmony_ci	fi
133f08c3bdfSopenharmony_ci	if [ $DEBUGFS_WAS_MOUNTED -eq 0 -a -n "$DEBUGFS_PATH" ] ; then
134f08c3bdfSopenharmony_ci		tst_umount "$DEBUGFS_PATH"
135f08c3bdfSopenharmony_ci	fi
136f08c3bdfSopenharmony_ci}
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci. tst_test.sh
139f08c3bdfSopenharmony_citst_run
140