File: //opt/perf/bin/init_ftrace.sh
#!/bin/bash
########################################################################################
#
# This script will be run at startup of glance/xglance. This will check whether ftrace
# is already and debugfs is mounted, else it will enable ftrace and mount debugfs
#
########################################################################################
FILE1=/proc/sys/kernel/ftrace_enabled
FILE2=/tracing/tracing_enabled
FILE3=/tracing/tracing_on
rc=0
dbg=0
if [[ $EUID -ne 0 ]]; then
echo "This script must be run as root" 1>&2
exit 1
fi
get_opt (){
rc="0"
while getopts fsd ARG; do
case "$ARG" in
f) rc="1"
;;
s) rc="2"
;;
d) dbg="1"
;;
*) rc="3"
;;
esac
done
return $rc
}
get_opt "${@}"
ret_val=$?
if [ "$ret_val" = "3" ]
then
echo " Usage:$(basename $0) -f -s -d"
echo " -f Force reset the FTRACE interface for use with midaemon"
echo " -s Force reset the FTRACE interface for use with midaemon with verbose silent"
echo " -d show debug messages"
exit 1
fi
reset_mi_interface (){
if [ "$dbg" = "1" ]
then
if ! [ -f $FILE1 ]
then echo "File $FILE1 does not exists";
fi
fi
if cat /proc/mounts |grep -q debugfs
then mountpoint=`cat /proc/mounts |grep debugfs`
else
mount -t debugfs nodev /sys/kernel/debug
rc=$?
if [ "$rc" != "0" ]
then echo "debugfs mounting failed!!!"
echo "FTRACE interface cannot be reset for use with midaemon."
exit
fi
if [ "$ret_val" = "0" ]||[ "$ret_val" = "1" ]
then
echo "debugfs is mounted at /sys/kernel/debug"
fi
fi
if [ -f $FILE1 ]
then
if [ "$val1" = "0" ]
then
echo "1" > $FILE1
rc=$?
if [ "$rc" != "0" ]
then echo "failed to write to $FILE1"
fi
if [ "$rc" = "0" ] && (["$ret_val" = "0" ]||[ "$ret_val" = "1" ])
then
echo "FTRACE is enabled in the system ($FILE1 == 1)"
fi
fi
fi
mountpoint=`cat /proc/mounts |grep debugfs`
arr=($mountpoint)
if [ "$dbg" = "1" ]
then
if ! [ -f ${arr[1]}$FILE2 ]
then echo "File ${arr[1]}$FILE2 does not exists"
fi
fi
if ! [ -f ${arr[1]}$FILE3 ]
then echo "File ${arr[1]}$FILE3 does not exists"
echo "FTRACE interface cannot be reset for use with midaemon.";
fi
if [ -f ${arr[1]}$FILE2 ]
then
echo "0" > ${arr[1]}$FILE2
rc=$?
if [ "$rc" != "0" ]
then echo "failed to write to ${arr[1]}$FILE2"
fi
fi
echo "0" > ${arr[1]}$FILE3
rc=$?
if [ "$rc" != "0" ]
then echo "failed to write to ${arr[1]}$FILE3"
echo "FTRACE interface cannot be reset for use with midaemon.";
fi
if [ "$rc" = "0" ] && ([ "$ret_val" = "0" ]||[ "$ret_val" = "1" ])
then
echo "FTRACE tracing is reset."
fi
}
if cat /proc/mounts |grep -q debugfs
then
mountpoint=`cat /proc/mounts |grep debugfs`
arr=($mountpoint)
if [ "$ret_val" = "0" ]
then echo debugfs is mounted at ${arr[1]}
fi
else
if [ "$ret_val" = "0" ]
then
echo debugfs is not mounted in the system
fi
fi
if [ -f $FILE1 ]
then
val1=`cat $FILE1`
if [ "$val1" != "0" ]
then
if [ "$ret_val" = "0" ]
then echo "FTRACE is enabled in the system ($FILE1 == 1)"
fi
else
if [ "$ret_val" = "0" ]
then
echo "FTRACE is disabled in the system ($FILE1 == 0)"
fi
fi
fi
if [ "$ret_val" = "0" ]
then
if ps cax | grep -q midaemon
then
echo "WARNING : MIDAEMON IS ALREADY RUNNING !!"
fi
echo "Do you want to reset the FTRACE interface for use with midaemon? (Y/N)"
while read input;
do
case "$input" in
"y"|"Y")
reset_mi_interface
if ps cax | grep -q midaemon
then
echo "Please do PA restart !!"
fi
exit
;;
"n"|"N")
exit
;;
*)
echo "Incorrect option !!"
echo "Do you want to reset the FTRACE interface for use with midaemon? (Y/N)"
;;
esac
done
else
reset_mi_interface
exit
fi