#!/bin/sh
# SPDX-License-Identifier: GPL-2.0
# description: BTF event with typecast and percpu access
# requires: dynamic_events "this_cpu_read(<fetcharg>)":README "[(structname[,field])]<argname>[->field[->field|.field...]]":README

# Check if the sample module is loaded
if ! lsmod | grep -q trace_events_sample; then
  modprobe trace-events-sample || exit_unresolved
fi

echo 0 > events/enable
echo > dynamic_events

# The sample_timer_cb(struct timer_list *t) is called.
# We want to check (STRUCT,FIELD)VAR typecast and this_cpu_read() access.
# (foo_timer_data,timer)t converts t to struct foo_timer_data * using container_of.
# data->counter is a per-cpu pointer to int.
# this_cpu_read(data->counter) should give the value of the counter.

echo 'f:mysample/myevent sample_timer_cb name=(foo_timer_data,timer)t->name:string count=this_cpu_read((foo_timer_data,timer)t->counter)' >> dynamic_events

echo 1 > events/mysample/myevent/enable
echo 1 > events/sample-trace/foo_timer_fn/enable

sleep 2

echo 0 > events/mysample/myevent/enable
echo 0 > events/sample-trace/foo_timer_fn/enable

# Compare the values.
MATCH=0
while read line; do
  if echo $line | grep -q "foo_timer_fn:"; then
    NAME=`echo $line | sed 's/.*name=\([^ ]*\) .*/\1/'`
    COUNT=`echo $line | sed 's/.*count=\([^ ]*\).*/\1/'`
    if grep -q "myevent:.*name=\"${NAME}\" count=$COUNT" trace; then
       MATCH=$((MATCH+1))
    fi
  fi
done < trace

if [ $MATCH -eq 0 ]; then
  echo "No matching events found"
  exit_fail
fi

# Clean up
echo 0 > events/mysample/myevent/enable
echo 0 > events/sample-trace/foo_timer_fn/enable
echo > dynamic_events
clear_trace