# Script for standalone use of ftrace for testing on a workstation.
# NOT FOR USE ON VEHICLE. Vehicles use ebpf from the bpf_system_monitor node

#!/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

log_dir="$HOME/.log/ftrace"
if [ ! -d $log_dir ]; then
  echo "Log dir $log_dir does not exist.  Please create it to continue"
  exit 1
fi

# allow userspace applications to write to the trace buffer
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 \
    /sys/kernel/debug

# reset tracing
echo "0"        | sudo tee /sys/kernel/debug/tracing/tracing_on
echo "1"        | sudo tee /sys/kernel/debug/tracing/free_buffer

# 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 "nop"      | sudo tee /sys/kernel/debug/tracing/current_tracer
echo ""         | sudo tee /sys/kernel/debug/tracing/set_event

# configure providers
echo "1"        | sudo tee /sys/kernel/debug/tracing/events/sched/sched_wakeup/enable
echo "1"        | sudo tee /sys/kernel/debug/tracing/events/sched/sched_switch/enable
echo "mono"     | sudo tee /sys/kernel/debug/tracing/trace_clock
echo "1"        | sudo tee /sys/kernel/debug/tracing/options/overwrite
echo "4096"     | sudo tee /sys/kernel/debug/tracing/buffer_size_kb


log_filename="$log_dir/trace-$(hostname)-$(date "+%Y%m%d-%H%M%S").log"

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

echo "----------------"
echo "Tracing starting"
echo "Press any key to stop tracing"
read -n 1

# done tracing, copy buffer
echo "0"        | sudo tee /sys/kernel/debug/tracing/tracing_on

dd iflag=fullblock,count_bytes if=/sys/kernel/debug/tracing/trace of=$log_filename bs=512K
echo "Saved ftrace to $log_filename"

echo "1"        | sudo tee /sys/kernel/debug/tracing/free_buffer
