7 Here we will cover the architecture pieces that the common function tracing
8 code relies on for proper functioning. Things are broken down into increasing
9 complexity so that you can start simple and at least get basic functionality.
11 Note that this focuses on architecture implementation details only. If you
12 want more explanation of a feature in terms of common code, review the common
19 Ftrace relies on these features being implemented:
20 STACKTRACE_SUPPORT - implement save_stack_trace()
21 TRACE_IRQFLAGS_SUPPORT - implement include/asm/irqflags.h
27 You will need to implement the mcount and the ftrace_stub functions.
29 The exact mcount symbol name will depend on your toolchain. Some call it
30 "mcount", "_mcount", or even "__mcount". You can probably figure it out by
31 running something like:
32 $ echo 'main(){}' | gcc -x c -S -o - - -pg | grep mcount
34 We'll make the assumption below that the symbol is "mcount" just to keep things
35 nice and simple in the examples.
37 Keep in mind that the ABI that is in effect inside of the mcount function is
38 *highly* architecture/toolchain specific. We cannot help you in this regard,
39 sorry. Dig up some old documentation and/or find someone more familiar than
40 you to bang ideas off of. Typically, register usage (argument/scratch/etc...)
41 is a major issue at this point, especially in relation to the location of the
42 mcount call (before/after function prologue). You might also want to look at
43 how glibc has implemented the mcount function for your architecture. It might
46 The mcount function should check the function pointer ftrace_trace_function
47 to see if it is set to ftrace_stub. If it is, there is nothing for you to do,
48 so return immediately. If it isn't, then call that function in the same way
49 the mcount function normally calls __mcount_internal -- the first argument is
50 the "frompc" while the second argument is the "selfpc" (adjusted to remove the
51 size of the mcount call that is embedded in the function).
53 For example, if the function foo() calls bar(), when the bar() function calls
54 mcount(), the arguments mcount() will pass to the tracer are:
55 "frompc" - the address bar() will use to return to foo()
56 "selfpc" - the address bar() (with _mcount() size adjustment)
58 Also keep in mind that this mcount function will be called *a lot*, so
59 optimizing for the default case of no tracer will help the smooth running of
60 your system when tracing is disabled. So the start of the mcount function is
61 typically the bare min with checking things before returning. That also means
62 the code flow should usually kept linear (i.e. no branching in the nop case).
63 This is of course an optimization and not a hard requirement.
65 Here is some pseudo code that should help (these functions should actually be
66 implemented in assembly):
68 void ftrace_stub(void)
75 /* save any bare state needed in order to do initial checking */
77 extern void (*ftrace_trace_function)(unsigned long, unsigned long);
78 if (ftrace_trace_function != ftrace_stub)
81 /* restore any bare state */
87 /* save all state needed by the ABI (see paragraph above) */
89 unsigned long frompc = ...;
90 unsigned long selfpc = <return address> - MCOUNT_INSN_SIZE;
91 ftrace_trace_function(frompc, selfpc);
93 /* restore all state needed by the ABI */
96 Don't forget to export mcount for modules !
97 extern void mcount(void);
98 EXPORT_SYMBOL(mcount);
101 HAVE_FUNCTION_TRACE_MCOUNT_TEST
102 -------------------------------
104 This is an optional optimization for the normal case when tracing is turned off
105 in the system. If you do not enable this Kconfig option, the common ftrace
106 code will take care of doing the checking for you.
108 To support this feature, you only need to check the function_trace_stop
109 variable in the mcount function. If it is non-zero, there is no tracing to be
110 done at all, so you can return.
112 This additional pseudo code would simply be:
115 /* save any bare state needed in order to do initial checking */
117 + if (function_trace_stop)
120 extern void (*ftrace_trace_function)(unsigned long, unsigned long);
121 if (ftrace_trace_function != ftrace_stub)
125 HAVE_FUNCTION_GRAPH_TRACER
126 --------------------------
128 Deep breath ... time to do some real work. Here you will need to update the
129 mcount function to check ftrace graph function pointers, as well as implement
130 some functions to save (hijack) and restore the return address.
132 The mcount function should check the function pointers ftrace_graph_return
133 (compare to ftrace_stub) and ftrace_graph_entry (compare to
134 ftrace_graph_entry_stub). If either of those are not set to the relevant stub
135 function, call the arch-specific function ftrace_graph_caller which in turn
136 calls the arch-specific function prepare_ftrace_return. Neither of these
137 function names are strictly required, but you should use them anyways to stay
138 consistent across the architecture ports -- easier to compare & contrast
141 The arguments to prepare_ftrace_return are slightly different than what are
142 passed to ftrace_trace_function. The second argument "selfpc" is the same,
143 but the first argument should be a pointer to the "frompc". Typically this is
144 located on the stack. This allows the function to hijack the return address
145 temporarily to have it point to the arch-specific function return_to_handler.
146 That function will simply call the common ftrace_return_to_handler function and
147 that will return the original return address with which, you can return to the
150 Here is the updated mcount pseudo code:
154 if (ftrace_trace_function != ftrace_stub)
157 +#ifdef CONFIG_FUNCTION_GRAPH_TRACER
158 + extern void (*ftrace_graph_return)(...);
159 + extern void (*ftrace_graph_entry)(...);
160 + if (ftrace_graph_return != ftrace_stub ||
161 + ftrace_graph_entry != ftrace_graph_entry_stub)
162 + ftrace_graph_caller();
165 /* restore any bare state */
168 Here is the pseudo code for the new ftrace_graph_caller assembly function:
169 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
170 void ftrace_graph_caller(void)
172 /* save all state needed by the ABI */
174 unsigned long *frompc = &...;
175 unsigned long selfpc = <return address> - MCOUNT_INSN_SIZE;
176 prepare_ftrace_return(frompc, selfpc);
178 /* restore all state needed by the ABI */
182 For information on how to implement prepare_ftrace_return(), simply look at
183 the x86 version. The only architecture-specific piece in it is the setup of
184 the fault recovery table (the asm(...) code). The rest should be the same
185 across architectures.
187 Here is the pseudo code for the new return_to_handler assembly function. Note
188 that the ABI that applies here is different from what applies to the mcount
189 code. Since you are returning from a function (after the epilogue), you might
190 be able to skimp on things saved/restored (usually just registers used to pass
193 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
194 void return_to_handler(void)
196 /* save all state needed by the ABI (see paragraph above) */
198 void (*original_return_point)(void) = ftrace_return_to_handler();
200 /* restore all state needed by the ABI */
202 /* this is usually either a return or a jump */
203 original_return_point();
208 HAVE_FTRACE_NMI_ENTER
209 ---------------------
211 If you can't trace NMI functions, then skip this option.
213 <details to be filled>
217 ---------------------
219 <details to be filled>
222 HAVE_FTRACE_MCOUNT_RECORD
223 -------------------------
225 See scripts/recordmcount.pl for more info.
227 <details to be filled>
231 ---------------------
233 <details to be filled>