Sunday 14 December 2014

Generating function graph in Linux using ftrace

ftrace is powerful tool used for debugging in Linux. Lets go through a simple example of generating a function graph using ftrace.

Following steps will help in generating function graph of selected linux function.

  1. cd /sys/kernel/debug/tracing
  2. cat /dev/null >  trace
  3. echo generic_make_request > set_graph_function
  4. echo 10 > max_graph_depth
  5. echo function_graph > current_tracer
  6. echo 1 > tracing_on
  7. dd if=/dev/sddk of=~/test bs=512 count=5
  8. cp trace ~/dd_trace_depth10
  9. echo 0 > tracing_on
  10. echo > set_graph_function
  11. echo 0 > max_graph_depth
  12. cat /dev/null >  trace


Enable Tracing:
In this example I have selected the filter function as "generic_make_request". As such the step 3 sets the graph function to "generic_make_request" linux function. Then we have set the graph depth to 10 in step 4. Step 5 sets the current tracer as "function_graph". After all these are set we enable the tracing.

Action:
Now we perform action for which we wanted a linux trace. In this example we perform an I/O with dd command. This command will hit the generic_make_request function and the file  /sys/kernel/debug/tracing/trace will be filled with the function graph.

Disable Tracing:
Steps 9, 10, 11 will disable the tracing

how ftrace works:
http://www.linuxplumbersconf.org/2014/ocw/system/presentations/1773/original/ftrace-kernel-hooks-2014.pdf

References
http://lwn.net/Articles/365835/
http://lwn.net/Articles/366796/
http://lwn.net/Articles/370423/

Stack Tracer:
https://lwn.net/Articles/366796/
To enable the stack tracer, echo 1 into /proc/sys/kernel/stack_tracer_enabled

/sys/kernel/debug/tracing # cat stack_trace
        Depth    Size   Location    (25 entries)
        -----    ----   --------

  0)     2736     120   kmem_cache_alloc_node_trace+0x306/0x5c0
...
...
 23)      240      64   kthread+0x11a/0x130

 24)      176     176   ret_from_fork+0x35/0x40


/sys/kernel/debug/tracing # cat stack_max_size
2736

No comments:

Post a Comment