Back to home page

OSCL-LXR

 
 

    


0001 #!/bin/sh
0002 # SPDX-License-Identifier: GPL-2.0-only
0003 
0004 usage() {
0005         echo "Ftrace boottime trace test tool"
0006         echo "Usage: $0 [--apply|--init] [--debug] BOOTCONFIG-FILE"
0007         echo "    --apply: Test actual apply to tracefs (need sudo)"
0008         echo "    --init:  Initialize ftrace before applying (imply --apply)"
0009         exit 1
0010 }
0011 
0012 [ $# -eq 0 ] && usage
0013 
0014 BCONF=
0015 DEBUG=
0016 APPLY=
0017 INIT=
0018 while [ x"$1" != x ]; do
0019         case "$1" in
0020         "--debug")
0021                 DEBUG=$1;;
0022         "--apply")
0023                 APPLY=$1;;
0024         "--init")
0025                 APPLY=$1
0026                 INIT=$1;;
0027         *)
0028                 [ ! -f $1 ] && usage
0029                 BCONF=$1;;
0030         esac
0031         shift 1
0032 done
0033 
0034 if [ x"$APPLY" != x ]; then
0035         if [ `id -u` -ne 0 ]; then
0036                 echo "This must be run by root user. Try sudo." 1>&2
0037                 exec sudo $0 $DEBUG $APPLY $BCONF
0038         fi
0039 fi
0040 
0041 run_cmd() { # command
0042         echo "$*"
0043         if [ x"$APPLY" != x ]; then # apply command
0044                 eval $*
0045         fi
0046 }
0047 
0048 if [ x"$DEBUG" != x ]; then
0049         set -x
0050 fi
0051 
0052 TRACEFS=`grep -m 1 -w tracefs /proc/mounts | cut -f 2 -d " "`
0053 if [ -z "$TRACEFS" ]; then
0054         if ! grep -wq debugfs /proc/mounts; then
0055                 echo "Error: No tracefs/debugfs was mounted." 1>&2
0056                 exit 1
0057         fi
0058         TRACEFS=`grep -m 1 -w debugfs /proc/mounts | cut -f 2 -d " "`/tracing
0059         if [ ! -d $TRACEFS ]; then
0060                 echo "Error: ftrace is not enabled on this kernel." 1>&2
0061                 exit 1
0062         fi
0063 fi
0064 
0065 if [ x"$INIT" != x ]; then
0066         . `dirname $0`/ftrace.sh
0067         (cd $TRACEFS; initialize_ftrace)
0068 fi
0069 
0070 . `dirname $0`/xbc.sh
0071 
0072 ######## main #########
0073 set -e
0074 
0075 xbc_init $BCONF
0076 
0077 set_value_of() { # key file
0078         if xbc_has_key $1; then
0079                 val=`xbc_get_val $1 1`
0080                 run_cmd "echo '$val' >> $2"
0081         fi
0082 }
0083 
0084 set_array_of() { # key file
0085         if xbc_has_key $1; then
0086                 xbc_get_val $1 | while read line; do
0087                         run_cmd "echo '$line' >> $2"
0088                 done
0089         fi
0090 }
0091 
0092 compose_synth() { # event_name branch
0093         echo -n "$1 "
0094         xbc_get_val $2 | while read field; do echo -n "$field; "; done
0095 }
0096 
0097 print_hist_array() { # prefix key
0098         __sep="="
0099         if xbc_has_key ${1}.${2}; then
0100                 echo -n ":$2"
0101                 xbc_get_val ${1}.${2} | while read field; do
0102                         echo -n "$__sep$field"; __sep=","
0103                 done
0104         fi
0105 }
0106 
0107 print_hist_action_array() { # prefix key
0108         __sep="("
0109         echo -n ".$2"
0110         xbc_get_val ${1}.${2} | while read field; do
0111                 echo -n "$__sep$field"; __sep=","
0112         done
0113         echo -n ")"
0114 }
0115 
0116 print_hist_one_action() { # prefix handler param
0117         echo -n ":${2}("`xbc_get_val ${1}.${3}`")"
0118         if xbc_has_key "${1}.trace"; then
0119                 print_hist_action_array ${1} "trace"
0120         elif xbc_has_key "${1}.save"; then
0121                 print_hist_action_array ${1} "save"
0122         elif xbc_has_key "${1}.snapshot"; then
0123                 echo -n ".snapshot()"
0124         fi
0125 }
0126 
0127 print_hist_actions() { # prefix handler param
0128         for __hdr in `xbc_subkeys ${1}.${2} 1 ".[0-9]"`; do
0129                 print_hist_one_action ${1}.${2}.$__hdr ${2} ${3}
0130         done
0131         if xbc_has_key ${1}.${2}.${3} ; then
0132                 print_hist_one_action ${1}.${2} ${2} ${3}
0133         fi
0134 }
0135 
0136 print_hist_var() { # prefix varname
0137         echo -n ":${2}="`xbc_get_val ${1}.var.${2} | tr -d [:space:]`
0138 }
0139 
0140 print_one_histogram() { # prefix
0141         echo -n "hist"
0142         print_hist_array $1 "keys"
0143         print_hist_array $1 "values"
0144         print_hist_array $1 "sort"
0145         if xbc_has_key "${1}.size"; then
0146                 echo -n ":size="`xbc_get_val ${1}.size`
0147         fi
0148         if xbc_has_key "${1}.name"; then
0149                 echo -n ":name="`xbc_get_val ${1}.name`
0150         fi
0151         for __var in `xbc_subkeys "${1}.var" 1`; do
0152                 print_hist_var ${1} ${__var}
0153         done
0154         if xbc_has_key "${1}.pause"; then
0155                 echo -n ":pause"
0156         elif xbc_has_key "${1}.continue"; then
0157                 echo -n ":continue"
0158         elif xbc_has_key "${1}.clear"; then
0159                 echo -n ":clear"
0160         fi
0161         print_hist_actions ${1} "onmax" "var"
0162         print_hist_actions ${1} "onchange" "var"
0163         print_hist_actions ${1} "onmatch" "event"
0164 
0165         if xbc_has_key "${1}.filter"; then
0166                 echo -n " if "`xbc_get_val ${1}.filter`
0167         fi
0168 }
0169 
0170 setup_one_histogram() { # prefix trigger-file
0171         run_cmd "echo '`print_one_histogram ${1}`' >> ${2}"
0172 }
0173 
0174 setup_histograms() { # prefix trigger-file
0175         for __hist in `xbc_subkeys ${1} 1 ".[0-9]"`; do
0176                 setup_one_histogram ${1}.$__hist ${2}
0177         done
0178         if xbc_has_key ${1}.keys; then
0179                 setup_one_histogram ${1} ${2}
0180         fi
0181 }
0182 
0183 setup_event() { # prefix group event [instance]
0184         branch=$1.$2.$3
0185         if [ "$4" ]; then
0186                 eventdir="$TRACEFS/instances/$4/events/$2/$3"
0187         else
0188                 eventdir="$TRACEFS/events/$2/$3"
0189         fi
0190         # group enable
0191         if [ "$3" = "enable" ]; then
0192                 run_cmd "echo 1 > ${eventdir}"
0193                 return
0194         fi
0195 
0196         case $2 in
0197         kprobes)
0198                 xbc_get_val ${branch}.probes | while read line; do
0199                         run_cmd "echo 'p:kprobes/$3 $line' >> $TRACEFS/kprobe_events"
0200                 done
0201                 ;;
0202         synthetic)
0203                 run_cmd "echo '`compose_synth $3 ${branch}.fields`' >> $TRACEFS/synthetic_events"
0204                 ;;
0205         esac
0206 
0207         set_value_of ${branch}.filter ${eventdir}/filter
0208         set_array_of ${branch}.actions ${eventdir}/trigger
0209 
0210         setup_histograms ${branch}.hist ${eventdir}/trigger
0211 
0212         if xbc_has_key ${branch}.enable; then
0213                 run_cmd "echo 1 > ${eventdir}/enable"
0214         fi
0215 }
0216 
0217 setup_events() { # prefix("ftrace" or "ftrace.instance.INSTANCE") [instance]
0218         prefix="${1}.event"
0219         if xbc_has_branch ${1}.event; then
0220                 for grpev in `xbc_subkeys ${1}.event 2`; do
0221                         setup_event $prefix ${grpev%.*} ${grpev#*.} $2
0222                 done
0223         fi
0224         if xbc_has_branch ${1}.event.enable; then
0225                 if [ "$2" ]; then
0226                         run_cmd "echo 1 > $TRACEFS/instances/$2/events/enable"
0227                 else
0228                         run_cmd "echo 1 > $TRACEFS/events/enable"
0229                 fi
0230         fi
0231 }
0232 
0233 size2kb() { # size[KB|MB]
0234         case $1 in
0235         *KB)
0236                 echo ${1%KB};;
0237         *MB)
0238                 expr ${1%MB} \* 1024;;
0239         *)
0240                 expr $1 / 1024 ;;
0241         esac
0242 }
0243 
0244 setup_instance() { # [instance]
0245         if [ "$1" ]; then
0246                 instance="ftrace.instance.${1}"
0247                 instancedir=$TRACEFS/instances/$1
0248         else
0249                 instance="ftrace"
0250                 instancedir=$TRACEFS
0251         fi
0252 
0253         set_array_of ${instance}.options ${instancedir}/trace_options
0254         set_value_of ${instance}.trace_clock ${instancedir}/trace_clock
0255         set_value_of ${instance}.cpumask ${instancedir}/tracing_cpumask
0256         set_value_of ${instance}.tracing_on ${instancedir}/tracing_on
0257         set_value_of ${instance}.tracer ${instancedir}/current_tracer
0258         set_array_of ${instance}.ftrace.filters \
0259                 ${instancedir}/set_ftrace_filter
0260         set_array_of ${instance}.ftrace.notrace \
0261                 ${instancedir}/set_ftrace_notrace
0262 
0263         if xbc_has_key ${instance}.alloc_snapshot; then
0264                 run_cmd "echo 1 > ${instancedir}/snapshot"
0265         fi
0266 
0267         if xbc_has_key ${instance}.buffer_size; then
0268                 size=`xbc_get_val ${instance}.buffer_size 1`
0269                 size=`eval size2kb $size`
0270                 run_cmd "echo $size >> ${instancedir}/buffer_size_kb"
0271         fi
0272 
0273         setup_events ${instance} $1
0274         set_array_of ${instance}.events ${instancedir}/set_event
0275 }
0276 
0277 # ftrace global configs (kernel.*)
0278 if xbc_has_key "kernel.dump_on_oops"; then
0279         dump_mode=`xbc_get_val "kernel.dump_on_oops" 1`
0280         [ "$dump_mode" ] && dump_mode=`eval echo $dump_mode` || dump_mode=1
0281         run_cmd "echo \"$dump_mode\" > /proc/sys/kernel/ftrace_dump_on_oops"
0282 fi
0283 
0284 set_value_of kernel.fgraph_max_depth $TRACEFS/max_graph_depth
0285 set_array_of kernel.fgraph_filters $TRACEFS/set_graph_function
0286 set_array_of kernel.fgraph_notraces $TRACEFS/set_graph_notrace
0287 
0288 # Per-instance/per-event configs
0289 if ! xbc_has_branch "ftrace" ; then
0290         exit 0
0291 fi
0292 
0293 setup_instance # root instance
0294 
0295 if xbc_has_branch "ftrace.instance"; then
0296         for i in `xbc_subkeys "ftrace.instance" 1`; do
0297                 run_cmd "mkdir -p $TRACEFS/instances/$i"
0298                 setup_instance $i
0299         done
0300 fi
0301