summaryrefslogtreecommitdiffstats
path: root/tools/bootconfig/scripts/ftrace2bconf.sh
blob: 1603801cf1264c42f143180915871b29023ed30f (plain)
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
#!/bin/sh
# SPDX-License-Identifier: GPL-2.0-only

usage() {
	echo "Dump boot-time tracing bootconfig from ftrace"
	echo "Usage: $0 [--debug] [ > BOOTCONFIG-FILE]"
	exit 1
}

DEBUG=
while [ x"$1" != x ]; do
	case "$1" in
	"--debug")
		DEBUG=$1;;
	-*)
		usage
		;;
	esac
	shift 1
done

if [ x"$DEBUG" != x ]; then
	set -x
fi

TRACEFS=`grep -m 1 -w tracefs /proc/mounts | cut -f 2 -d " "`
if [ -z "$TRACEFS" ]; then
	if ! grep -wq debugfs /proc/mounts; then
		echo "Error: No tracefs/debugfs was mounted."
		exit 1
	fi
	TRACEFS=`grep -m 1 -w debugfs /proc/mounts | cut -f 2 -d " "`/tracing
	if [ ! -d $TRACEFS ]; then
		echo "Error: ftrace is not enabled on this kernel." 1>&2
		exit 1
	fi
fi

######## main #########

set -e

emit_kv() { # key =|+= value
	echo "$@"
}

global_options() {
	val=`cat $TRACEFS/max_graph_depth`
	[ $val != 0 ] && emit_kv kernel.fgraph_max_depth = $val
	if grep -qv "^#" $TRACEFS/set_graph_function $TRACEFS/set_graph_notrace ; then
		cat 1>&2 << EOF
# WARN: kernel.fgraph_filters and kernel.fgraph_notrace are not supported, since the wild card expression was expanded and lost from memory.
EOF
	fi
}

kprobe_event_options() {
	cat $TRACEFS/kprobe_events | while read p args; do
		case $p in
		r*)
		cat 1>&2 << EOF
# WARN: A return probe found but it is not supported by bootconfig. Skip it.
EOF
		continue;;
		esac
		p=${p#*:}
		event=${p#*/}
		group=${p%/*}
		if [ $group != "kprobes" ]; then
			cat 1>&2 << EOF
# WARN: kprobes group name $group is changed to "kprobes" for bootconfig.
EOF
		fi
		emit_kv $PREFIX.event.kprobes.$event.probes += $args
	done
}

synth_event_options() {
	cat $TRACEFS/synthetic_events | while read event fields; do
		emit_kv $PREFIX.event.synthetic.$event.fields = `echo $fields | sed "s/;/,/g"`
	done
}

# Variables resolver
DEFINED_VARS=
UNRESOLVED_EVENTS=

defined_vars() { # event-dir
	grep "^hist" $1/trigger | grep -o ':[a-zA-Z0-9]*='
}
referred_vars() {
	grep "^hist" $1/trigger | grep -o '$[a-zA-Z0-9]*'
}

event_is_enabled() { # enable-file
	test -f $1 && grep -q "1" $1
}

per_event_options() { # event-dir
	evdir=$1
	# Check the special event which has no filter and no trigger
	[ ! -f $evdir/filter ] && return

	if grep -q "^hist:" $evdir/trigger; then
		# hist action can refer the undefined variables
		__vars=`defined_vars $evdir`
		for v in `referred_vars $evdir`; do
			if echo $DEFINED_VARS $__vars | grep -vqw ${v#$}; then
				# $v is not defined yet, defer it
				UNRESOLVED_EVENTS="$UNRESOLVED_EVENTS $evdir"
				return;
			fi
		done
		DEFINED_VARS="$DEFINED_VARS "`defined_vars $evdir`
	fi
	grep -v "^#" $evdir/trigger | while read action active; do
		emit_kv $PREFIX.event.$group.$event.actions += \'$action\'
	done

	if [ $GROUP_ENABLED -eq 0 ] && event_is_enabled $evdir/enable; then
		emit_kv $PREFIX.event.$group.$event.enable
	fi
	val=`cat $evdir/filter`
	if [ "$val" != "none" ]; then
		emit_kv $PREFIX.event.$group.$event.filter = "$val"
	fi
}

retry_unresolved() {
	unresolved=$UNRESOLVED_EVENTS
	UNRESOLVED_EVENTS=
	for evdir in $unresolved; do
		event=${evdir##*/}
		group=${evdir%/*}; group=${group##*/}
		per_event_options $evdir
	done
}

event_options() {
	# PREFIX and INSTANCE must be set
	if [ $PREFIX = "ftrace" ]; then
		# define the dynamic events
		kprobe_event_options
		synth_event_options
	fi
	ALL_ENABLED=0
	if event_is_enabled $INSTANCE/events/enable; then
		emit_kv $PREFIX.event.enable
		ALL_ENABLED=1
	fi
	for group in `ls $INSTANCE/events/` ; do
		[ ! -d $INSTANCE/events/$group ] && continue
		GROUP_ENABLED=$ALL_ENABLED
		if [ $ALL_ENABLED -eq 0 ] && \
		   event_is_enabled $INSTANCE/events/$group/enable ;then
			emit_kv $PREFIX.event.$group.enable
			GROUP_ENABLED=1
		fi
		for event in `ls $INSTANCE/events/$group/` ;do
			[ ! -d $INSTANCE/events/$group/$event ] && continue
			per_event_options $INSTANCE/events/$group/$event
		done
	done
	retry=0
	while [ $retry -lt 3 ]; do
		retry_unresolved
		retry=$((retry + 1))
	done
	if [ "$UNRESOLVED_EVENTS" ]; then
		cat 1>&2 << EOF
! ERROR: hist triggers in $UNRESOLVED_EVENTS use some undefined variables.
EOF
	fi
}

is_default_trace_option() { # option
grep -qw $1 << EOF
print-parent
nosym-offset
nosym-addr
noverbose
noraw
nohex
nobin
noblock
trace_printk
annotate
nouserstacktrace
nosym-userobj
noprintk-msg-only
context-info
nolatency-format
record-cmd
norecord-tgid
overwrite
nodisable_on_free
irq-info
markers
noevent-fork
nopause-on-trace
function-trace
nofunction-fork
nodisplay-graph
nostacktrace
notest_nop_accept
notest_nop_refuse
EOF
}

instance_options() { # [instance-name]
	if [ $# -eq 0 ]; then
		PREFIX="ftrace"
		INSTANCE=$TRACEFS
	else
		PREFIX="ftrace.instance.$1"
		INSTANCE=$TRACEFS/instances/$1
	fi
	val=
	for i in `cat $INSTANCE/trace_options`; do
		is_default_trace_option $i && continue
		val="$val, $i"
	done
	[ "$val" ] && emit_kv $PREFIX.options = "${val#,}"
	val="local"
	for i in `cat $INSTANCE/trace_clock` ; do
		[ "${i#*]}" ] && continue
		i=${i%]}; val=${i#[}
	done
	[ $val != "local" ] && emit_kv $PREFIX.trace_clock = $val
	val=`cat $INSTANCE/buffer_size_kb`
	if echo $val | grep -vq "expanded" ; then
		emit_kv $PREFIX.buffer_size = $val"KB"
	fi
	if grep -q "is allocated" $INSTANCE/snapshot ; then
		emit_kv $PREFIX.alloc_snapshot
	fi
	val=`cat $INSTANCE/tracing_cpumask`
	if [ `echo $val | sed -e s/f//g`x != x ]; then
		emit_kv $PREFIX.cpumask = $val
	fi
	val=`cat $INSTANCE/tracing_on`
	if [ "$val" = "0" ]; then
		emit_kv $PREFIX.tracing_on = 0
	fi

	val=`cat $INSTANCE/current_tracer`
	[ $val != nop ] && emit_kv $PREFIX.tracer = $val
	if grep -qv "^#" $INSTANCE/set_ftrace_filter $INSTANCE/set_ftrace_notrace; then
		cat 1>&2 << EOF
# WARN: kernel.ftrace.filters and kernel.ftrace.notrace are not supported, since the wild card expression was expanded and lost from memory.
EOF
	fi
	event_options
}

global_options
instance_options
for i in `ls $TRACEFS/instances` ; do
	instance_options $i
done