4 Copyright 2008 (c) Frederic Weisbecker <fweisbec@gmail.com>
5 Licensed under the terms of the GNU GPL License version 2
7 This script parses a trace provided by the function tracer in
8 kernel/trace/trace_functions.c
9 The resulted trace is processed into a tree to produce a more human
10 view of the call stack by drawing textual but hierarchical tree of
11 calls. Only the functions's names and the the call time are provided.
14 Be sure that you have CONFIG_FUNCTION_TRACER
15 # mount -t debugfs nodev /sys/kernel/debug
16 # echo function > /sys/kernel/debug/tracing/current_tracer
17 $ cat /sys/kernel/debug/tracing/trace_pipe > ~/raw_trace_func
18 Wait some times but not too much, the script is a bit slow.
19 Break the pipe (Ctrl + Z)
20 $ scripts/draw_functrace.py < raw_trace_func > draw_functrace
21 Then you have your drawn trace in draw_functrace
28 """ This class provides a tree representation of the functions
29 call stack. If a function has no parent in the kernel (interrupt,
30 syscall, kernel thread...) then it is attached to a virtual parent
35 def __init__(self
, func
, time
= None, parent
= None):
39 self
._parent
= CallTree
.ROOT
44 def calls(self
, func
, calltime
):
45 """ If a function calls another one, call this method to insert it
46 into the tree at the appropriate place.
47 @return: A reference to the newly created child node.
49 child
= CallTree(func
, calltime
, self
)
50 self
._children
.append(child
)
53 def getParent(self
, func
):
54 """ Retrieve the last parent of the current node that
55 has the name given by func. If this function is not
56 on a parent, then create it as new child of root
57 @return: A reference to the parent.
60 while tree
!= CallTree
.ROOT
and tree
._func
!= func
:
62 if tree
== CallTree
.ROOT
:
63 child
= CallTree
.ROOT
.calls(func
, None)
68 return self
.__toString
("", True)
70 def __toString(self
, branch
, lastChild
):
71 if self
._time
is not None:
72 s
= "%s----%s (%s)\n" % (branch
, self
._func
, self
._time
)
74 s
= "%s----%s\n" % (branch
, self
._func
)
78 branch
= branch
[:-1] + " "
79 while i
< len(self
._children
):
80 if i
!= len(self
._children
) - 1:
81 s
+= "%s" % self
._children
[i
].__toString
(branch
+\
84 s
+= "%s" % self
._children
[i
].__toString
(branch
+\
89 class BrokenLineException(Exception):
90 """If the last line is not complete because of the pipe breakage,
91 we want to stop the processing and ignore this line.
95 class CommentLineException(Exception):
96 """ If the line is a comment (as in the beginning of the trace file),
104 if line
.startswith("#"):
105 raise CommentLineException
106 m
= re
.match("[^]]+?\\] +([0-9.]+): (\\w+) <-(\\w+)", line
)
108 raise BrokenLineException
109 return (m
.group(1), m
.group(2), m
.group(3))
113 CallTree
.ROOT
= CallTree("Root (Nowhere)", None, None)
116 for line
in sys
.stdin
:
118 calltime
, callee
, caller
= parseLine(line
)
119 except BrokenLineException
:
121 except CommentLineException
:
123 tree
= tree
.getParent(caller
)
124 tree
= tree
.calls(callee
, calltime
)
128 if __name__
== "__main__":