~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/trace/ftrace-design.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/trace/ftrace-design.rst (Version linux-6.12-rc7) and /Documentation/trace/ftrace-design.rst (Version linux-4.12.14)


  1 ======================                            
  2 Function Tracer Design                            
  3 ======================                            
  4                                                   
  5 :Author: Mike Frysinger                           
  6                                                   
  7 .. caution::                                      
  8         This document is out of date. Some of     
  9         match current implementation now.         
 10                                                   
 11 Introduction                                      
 12 ------------                                      
 13                                                   
 14 Here we will cover the architecture pieces tha    
 15 code relies on for proper functioning.  Things    
 16 complexity so that you can start simple and at    
 17                                                   
 18 Note that this focuses on architecture impleme    
 19 want more explanation of a feature in terms of    
 20 ftrace.txt file.                                  
 21                                                   
 22 Ideally, everyone who wishes to retain perform    
 23 their kernel should make it all the way to dyn    
 24                                                   
 25                                                   
 26 Prerequisites                                     
 27 -------------                                     
 28                                                   
 29 Ftrace relies on these features being implemen    
 30   - STACKTRACE_SUPPORT - implement save_stack_    
 31   - TRACE_IRQFLAGS_SUPPORT - implement include    
 32                                                   
 33                                                   
 34 HAVE_FUNCTION_TRACER                              
 35 --------------------                              
 36                                                   
 37 You will need to implement the mcount and the     
 38                                                   
 39 The exact mcount symbol name will depend on yo    
 40 "mcount", "_mcount", or even "__mcount".  You     
 41 running something like::                          
 42                                                   
 43         $ echo 'main(){}' | gcc -x c -S -o - -    
 44                 call    mcount                    
 45                                                   
 46 We'll make the assumption below that the symbo    
 47 nice and simple in the examples.                  
 48                                                   
 49 Keep in mind that the ABI that is in effect in    
 50 *highly* architecture/toolchain specific.  We     
 51 sorry.  Dig up some old documentation and/or f    
 52 you to bang ideas off of.  Typically, register    
 53 is a major issue at this point, especially in     
 54 mcount call (before/after function prologue).     
 55 how glibc has implemented the mcount function     
 56 be (semi-)relevant.                               
 57                                                   
 58 The mcount function should check the function     
 59 to see if it is set to ftrace_stub.  If it is,    
 60 so return immediately.  If it isn't, then call    
 61 the mcount function normally calls __mcount_in    
 62 the "frompc" while the second argument is the     
 63 size of the mcount call that is embedded in th    
 64                                                   
 65 For example, if the function foo() calls bar()    
 66 mcount(), the arguments mcount() will pass to     
 67                                                   
 68   - "frompc" - the address bar() will use to r    
 69   - "selfpc" - the address bar() (with mcount(    
 70                                                   
 71 Also keep in mind that this mcount function wi    
 72 optimizing for the default case of no tracer w    
 73 your system when tracing is disabled.  So the     
 74 typically the bare minimum with checking thing    
 75 means the code flow should usually be kept lin    
 76 case).  This is of course an optimization and     
 77                                                   
 78 Here is some pseudo code that should help (the    
 79 implemented in assembly)::                        
 80                                                   
 81         void ftrace_stub(void)                    
 82         {                                         
 83                 return;                           
 84         }                                         
 85                                                   
 86         void mcount(void)                         
 87         {                                         
 88                 /* save any bare state needed     
 89                                                   
 90                 extern void (*ftrace_trace_fun    
 91                 if (ftrace_trace_function != f    
 92                         goto do_trace;            
 93                                                   
 94                 /* restore any bare state */      
 95                                                   
 96                 return;                           
 97                                                   
 98         do_trace:                                 
 99                                                   
100                 /* save all state needed by th    
101                                                   
102                 unsigned long frompc = ...;       
103                 unsigned long selfpc = <return    
104                 ftrace_trace_function(frompc,     
105                                                   
106                 /* restore all state needed by    
107         }                                         
108                                                   
109 Don't forget to export mcount for modules !       
110 ::                                                
111                                                   
112         extern void mcount(void);                 
113         EXPORT_SYMBOL(mcount);                    
114                                                   
115                                                   
116 HAVE_FUNCTION_GRAPH_TRACER                        
117 --------------------------                        
118                                                   
119 Deep breath ... time to do some real work.  He    
120 mcount function to check ftrace graph function    
121 some functions to save (hijack) and restore th    
122                                                   
123 The mcount function should check the function     
124 (compare to ftrace_stub) and ftrace_graph_entr    
125 ftrace_graph_entry_stub).  If either of those     
126 function, call the arch-specific function ftra    
127 calls the arch-specific function prepare_ftrac    
128 function names is strictly required, but you s    
129 consistent across the architecture ports -- ea    
130 things.                                           
131                                                   
132 The arguments to prepare_ftrace_return are sli    
133 passed to ftrace_trace_function.  The second a    
134 but the first argument should be a pointer to     
135 located on the stack.  This allows the functio    
136 temporarily to have it point to the arch-speci    
137 That function will simply call the common ftra    
138 that will return the original return address w    
139 original call site.                               
140                                                   
141 Here is the updated mcount pseudo code::          
142                                                   
143         void mcount(void)                         
144         {                                         
145         ...                                       
146                 if (ftrace_trace_function != f    
147                         goto do_trace;            
148                                                   
149         +#ifdef CONFIG_FUNCTION_GRAPH_TRACER      
150         +       extern void (*ftrace_graph_ret    
151         +       extern void (*ftrace_graph_ent    
152         +       if (ftrace_graph_return != ftr    
153         +           ftrace_graph_entry != ftra    
154         +               ftrace_graph_caller();    
155         +#endif                                   
156                                                   
157                 /* restore any bare state */      
158         ...                                       
159                                                   
160 Here is the pseudo code for the new ftrace_gra    
161                                                   
162         #ifdef CONFIG_FUNCTION_GRAPH_TRACER       
163         void ftrace_graph_caller(void)            
164         {                                         
165                 /* save all state needed by th    
166                                                   
167                 unsigned long *frompc = &...;     
168                 unsigned long selfpc = <return    
169                 /* passing frame pointer up is    
170                 prepare_ftrace_return(frompc,     
171                                                   
172                 /* restore all state needed by    
173         }                                         
174         #endif                                    
175                                                   
176 For information on how to implement prepare_ft    
177 x86 version (the frame pointer passing is opti    
178 more information).  The only architecture-spec    
179 the fault recovery table (the asm(...) code).     
180 across architectures.                             
181                                                   
182 Here is the pseudo code for the new return_to_    
183 that the ABI that applies here is different fr    
184 code.  Since you are returning from a function    
185 be able to skimp on things saved/restored (usu    
186 return values).                                   
187 ::                                                
188                                                   
189         #ifdef CONFIG_FUNCTION_GRAPH_TRACER       
190         void return_to_handler(void)              
191         {                                         
192                 /* save all state needed by th    
193                                                   
194                 void (*original_return_point)(    
195                                                   
196                 /* restore all state needed by    
197                                                   
198                 /* this is usually either a re    
199                 original_return_point();          
200         }                                         
201         #endif                                    
202                                                   
203                                                   
204 HAVE_FUNCTION_GRAPH_FP_TEST                       
205 ---------------------------                       
206                                                   
207 An arch may pass in a unique value (frame poin    
208 exiting of a function.  On exit, the value is     
209 match, then it will panic the kernel.  This is    
210 code generation with gcc.  If gcc for your por    
211 pointer under different optimization levels, t    
212                                                   
213 However, adding support for it isn't terribly     
214 that calls prepare_ftrace_return(), pass the f    
215 Then in the C version of that function, do wha    
216 along to ftrace_push_return_trace() instead of    
217                                                   
218 Similarly, when you call ftrace_return_to_hand    
219                                                   
220 HAVE_SYSCALL_TRACEPOINTS                          
221 ------------------------                          
222                                                   
223 You need very few things to get the syscalls t    
224                                                   
225   - Support HAVE_ARCH_TRACEHOOK (see arch/Kcon    
226   - Have a NR_syscalls variable in <asm/unistd    
227     of syscalls supported by the arch.            
228   - Support the TIF_SYSCALL_TRACEPOINT thread     
229   - Put the trace_sys_enter() and trace_sys_ex    
230     in the ptrace syscalls tracing path.          
231   - If the system call table on this arch is m    
232     of addresses of the system calls, implemen    
233     the address of a given system call.           
234   - If the symbol names of the system calls do    
235     this arch, define ARCH_HAS_SYSCALL_MATCH_S    
236     implement arch_syscall_match_sym_name with    
237     true if the function name corresponds with    
238   - Tag this arch as HAVE_SYSCALL_TRACEPOINTS.    
239                                                   
240                                                   
241 HAVE_FTRACE_MCOUNT_RECORD                         
242 -------------------------                         
243                                                   
244 See scripts/recordmcount.pl for more info.  Ju    
245 details for how to locate the addresses of mco    
246 This option doesn't make much sense without al    
247                                                   
248                                                   
249 HAVE_DYNAMIC_FTRACE                               
250 -------------------                               
251                                                   
252 You will first need HAVE_FTRACE_MCOUNT_RECORD     
253 scroll your reader back up if you got over eag    
254                                                   
255 Once those are out of the way, you will need t    
256         - asm/ftrace.h:                           
257                 - MCOUNT_ADDR                     
258                 - ftrace_call_adjust()            
259                 - struct dyn_arch_ftrace{}        
260         - asm code:                               
261                 - mcount() (new stub)             
262                 - ftrace_caller()                 
263                 - ftrace_call()                   
264                 - ftrace_stub()                   
265         - C code:                                 
266                 - ftrace_dyn_arch_init()          
267                 - ftrace_make_nop()               
268                 - ftrace_make_call()              
269                 - ftrace_update_ftrace_func()     
270                                                   
271 First you will need to fill out some arch deta    
272                                                   
273 Define MCOUNT_ADDR as the address of your mcou    
274                                                   
275         #define MCOUNT_ADDR ((unsigned long)mc    
276                                                   
277 Since no one else will have a decl for that fu    
278                                                   
279         extern void mcount(void);                 
280                                                   
281 You will also need the helper function ftrace_    
282 will be able to stub it out like so::             
283                                                   
284         static inline unsigned long ftrace_cal    
285         {                                         
286                 return addr;                      
287         }                                         
288                                                   
289 <details to be filled>                            
290                                                   
291 Lastly you will need the custom dyn_arch_ftrac    
292 some extra state when runtime patching arbitra    
293 place.  For now though, create an empty struct    
294                                                   
295         struct dyn_arch_ftrace {                  
296                 /* No extra data needed */        
297         };                                        
298                                                   
299 With the header out of the way, we can fill ou    
300 did already create a mcount() function earlier    
301 stub function.  This is because the mcount() w    
302 and then all references to it will be patched     
303 the guts of the old mcount() will be used to c    
304 function.  Because the two are hard to merge,     
305 easier to have two separate definitions split     
306 the ftrace_stub() as that will now be inlined     
307                                                   
308 Before we get confused anymore, let's check ou    
309 implement your own stuff in assembly::            
310                                                   
311         void mcount(void)                         
312         {                                         
313                 return;                           
314         }                                         
315                                                   
316         void ftrace_caller(void)                  
317         {                                         
318                 /* save all state needed by th    
319                                                   
320                 unsigned long frompc = ...;       
321                 unsigned long selfpc = <return    
322                                                   
323         ftrace_call:                              
324                 ftrace_stub(frompc, selfpc);      
325                                                   
326                 /* restore all state needed by    
327                                                   
328         ftrace_stub:                              
329                 return;                           
330         }                                         
331                                                   
332 This might look a little odd at first, but kee    
333 patching multiple things.  First, only functio    
334 will be patched to call ftrace_caller().  Seco    
335 active at a time, we will patch the ftrace_cal    
336 specific tracer in question.  That is the poin    
337                                                   
338 With that in mind, let's move on to the C code    
339 runtime patching.  You'll need a little knowle    
340 order to make it through the next section.        
341                                                   
342 Every arch has an init callback function.  If     
343 to initialize some state, this is the time to     
344 function below should be sufficient for most p    
345                                                   
346         int __init ftrace_dyn_arch_init(void)     
347         {                                         
348                 return 0;                         
349         }                                         
350                                                   
351 There are two functions that are used to do ru    
352 functions.  The first is used to turn the mcou    
353 is what helps us retain runtime performance wh    
354 used to turn the mcount call site into a call     
355 typically that is ftracer_caller()).  See the     
356 linux/ftrace.h for the functions::                
357                                                   
358         ftrace_make_nop()                         
359         ftrace_make_call()                        
360                                                   
361 The rec->ip value is the address of the mcount    
362 by the scripts/recordmcount.pl during build ti    
363                                                   
364 The last function is used to do runtime patchi    
365 will be modifying the assembly code at the loc    
366 inside of the ftrace_caller() function.  So yo    
367 at that location to support the new function c    
368 people will be using a "call" type instruction    
369 "branch" type instruction.  Specifically, the     
370                                                   
371         ftrace_update_ftrace_func()               
372                                                   
373                                                   
374 HAVE_DYNAMIC_FTRACE + HAVE_FUNCTION_GRAPH_TRAC    
375 ----------------------------------------------    
376                                                   
377 The function grapher needs a few tweaks in ord    
378 Basically, you will need to:                      
379                                                   
380         - update:                                 
381                 - ftrace_caller()                 
382                 - ftrace_graph_call()             
383                 - ftrace_graph_caller()           
384         - implement:                              
385                 - ftrace_enable_ftrace_graph_c    
386                 - ftrace_disable_ftrace_graph_    
387                                                   
388 <details to be filled>                            
389                                                   
390 Quick notes:                                      
391                                                   
392         - add a nop stub after the ftrace_call    
393           stub needs to be large enough to sup    
394         - update ftrace_graph_caller() to work    
395           ftrace_caller() since some semantics    
396         - ftrace_enable_ftrace_graph_caller()     
397           ftrace_graph_call location with a ca    
398         - ftrace_disable_ftrace_graph_caller()    
399           ftrace_graph_call location with nops    
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php