#!/usr/bin/env bash
set -euxo pipefail

# References:

# https://www.kernel.org/doc/Documentation/trace/ftrace.txt
# https://www.kernel.org/doc/Documentation/trace/events.txt
# https://git.zooxlabs.com/zooxco/catapult/blob/master/systrace/systrace/tracing_agents/ftrace_agent.py#L43

# allow userspace applications to write to the trace buffer and set up kprobe
# events
sudo chmod a+rwx \
    /sys/kernel/debug/tracing/free_buffer \
    /sys/kernel/debug/tracing/trace_pipe \
    /sys/kernel/debug/tracing/trace_marker \
    /sys/kernel/debug/tracing/kprobe_events \
    /sys/kernel/debug/tracing \
    /sys/kernel/debug

# reset tracing

# some options not supported on older kernels
# this list from 4.4.0-98-generic. don't fail for missing options
set +e
echo "print-parent"         | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nosym-offset"         | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nosym-addr"           | sudo tee /sys/kernel/debug/tracing/trace_options
echo "noverbose"            | sudo tee /sys/kernel/debug/tracing/trace_options
echo "noraw"                | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nohex"                | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nobin"                | sudo tee /sys/kernel/debug/tracing/trace_options
echo "noblock"              | sudo tee /sys/kernel/debug/tracing/trace_options
echo "trace_printk"         | sudo tee /sys/kernel/debug/tracing/trace_options
echo "annotate"             | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nouserstacktrace"     | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nosym-userobj"        | sudo tee /sys/kernel/debug/tracing/trace_options
echo "noprintk-msg-only"    | sudo tee /sys/kernel/debug/tracing/trace_options
echo "context-info"         | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nolatency-format"     | sudo tee /sys/kernel/debug/tracing/trace_options
echo "record-cmd"           | sudo tee /sys/kernel/debug/tracing/trace_options
echo "overwrite"            | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nodisable_on_free"    | sudo tee /sys/kernel/debug/tracing/trace_options
echo "irq-info"             | sudo tee /sys/kernel/debug/tracing/trace_options
echo "markers"              | sudo tee /sys/kernel/debug/tracing/trace_options
echo "function-trace"       | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nodisplay-graph"      | sudo tee /sys/kernel/debug/tracing/trace_options
echo "nostacktrace"         | sudo tee /sys/kernel/debug/tracing/trace_options
echo "notest_nop_accept"    | sudo tee /sys/kernel/debug/tracing/trace_options
echo "notest_nop_refuse"    | sudo tee /sys/kernel/debug/tracing/trace_options
set -e

echo "0"        | sudo tee /sys/kernel/debug/tracing/tracing_on
echo "nop"      | sudo tee /sys/kernel/debug/tracing/current_tracer
echo ""         | sudo tee /sys/kernel/debug/tracing/set_event

# configure tracing
# must use the "mono" clock as that is the only one available in ebpf
echo "mono"     | sudo tee /sys/kernel/debug/tracing/trace_clock
echo "1"        | sudo tee /sys/kernel/debug/tracing/options/overwrite
echo "128"      | sudo tee /sys/kernel/debug/tracing/buffer_size_kb

# enable tracing. This causes the linux scheduler to hiccup,
# so it must be not be done while autonomous
echo "1"        | sudo tee /sys/kernel/debug/tracing/tracing_on
