summaryrefslogtreecommitdiff
path: root/common/tracing
blob: 8613d044e406bb87a93884b90c393b3730569a29 (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
##/bin/bash
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright (c) 2022 Oracle.  All Rights Reserved.
#
# Routines for dealing with ftrace (or any other tracing).

_require_ftrace() {
	if [ -d /sys/kernel/tracing/instances/ ];then
		FTRACE_ROOT="/sys/kernel/tracing"
		FTRACE_INSTANCES_DIR="/sys/kernel/tracing/instances"
	elif [ -d /sys/kernel/debug/tracing/instances/ ];then
		FTRACE_ROOT="/sys/kernel/debug/tracing"
		FTRACE_INSTANCES_DIR="/sys/kernel/debug/tracing/instances"
	else
		_notrun "The ftrace is not supported, or tracefs is not mounted"
	fi
}

_ftrace_cleanup() {
	if [ -d "$FTRACE_DIR" ]; then
		_ftrace_ignore_events
		# Removing an ftrace buffer requires rmdir, even though the
		# virtual directory contains children.
		rmdir "$FTRACE_DIR"
	fi
}

_ftrace_setup() {
	test -n "$FTRACE_DIR" && _fail "_ftrace_setup already run?"

	# Give this fstest its own ftrace buffer so that we don't mess up
	# any other tracers that might be running.
	FTRACE_DIR="$FTRACE_INSTANCES_DIR/fstests.$seq"

	test -d "$FTRACE_DIR" && rmdir "$FTRACE_DIR"
	mkdir "$FTRACE_DIR"
}

# Intercept the given events.  Arguments may be regular expressions.
_ftrace_record_events() {
	test -d "$FTRACE_DIR" || _fail "_ftrace_setup not run?"

	for arg in "$@"; do
		# Replace slashes with semicolons per ftrace convention
		find "$FTRACE_DIR/events/" -type d -name "$arg" -printf '%P\n' | \
			tr '/' ':' >> "$FTRACE_DIR/set_event"
	done
}

# Stop intercepting the given events.  If no arguments, stops all events.
_ftrace_ignore_events() {
	test -d "$FTRACE_DIR" || _fail "_ftrace_setup not run?"

	if [ "$#" -eq 0 ]; then
		echo > "$FTRACE_DIR/set_event"
	else
		for arg in "$@"; do
			# Replace slashes with semicolons per ftrace convention
			find "$FTRACE_DIR/events/" -type d -name "$arg" -printf '!%P\n' | \
				tr '/' ':' >> "$FTRACE_DIR/set_event"
		done
	fi
}

# Dump whatever was written to the ftrace buffer since the last time this
# helper was called.
_ftrace_dump() {
	test -d "$FTRACE_DIR" || _fail "_ftrace_setup not run?"

	cat "$FTRACE_DIR/trace"
}