ftrace-design.txt 7.6 KB

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