linuxcpftrace

How to trace all events for a shell command using ftrace?


How can I trace the the command like cp file1 file2 using the ftrace tool?

I want to see all the functions that been called when I used cp file1 file2 but I don't know how to do it. Can anyone help me and write down the exact command in the terminal that does it?


Solution

  • Take the following script, you can use it to ftrace any command you want. Taken from here, I've modified slightly, you can get traced output at /tmp/mytrace.txt. Example usage: script.sh cp file1 file2

    #!/bin/bash     
    DPATH="/sys/kernel/debug/tracing"
    PID=$$
    TEMP="/tmp/mytrace.txt"
    ## Quick basic checks
    [ `id -u` -ne 0  ]  &&  { echo "needs to be root" ; exit 1; }  # check for root permissions
    [ -z $1 ] && { echo "needs process name as argument" ; exit 1; } # check for args to this function
    mount | grep -i debugfs &> /dev/null
    [ $? -ne 0 ] && { echo "debugfs not mounted, mount it first"; exit 1; } #checks for debugfs mount
    
    # flush existing trace data
    echo nop > $DPATH/current_tracer
    
    # set function tracer
    echo function_graph > $DPATH/current_tracer
    
    # enable the current tracer
    #echo 1 > $DPATH/tracing_on
    
    # write current process id to set_ftrace_pid file
    echo $PID > $DPATH/set_ftrace_pid
    
    # start the tracing
    echo 1 > $DPATH/tracing_on
    # execute the process
    exec $* > /dev/null 2>&1 &
    #echo "$*"
    
    `cat $DPATH/trace > $TEMP`
    
    echo 0 > $DPATH/tracing_on
    
    echo nop > $DPATH/current_tracer