ftrace.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. #ifndef _LINUX_FTRACE_H
  2. #define _LINUX_FTRACE_H
  3. #include <linux/linkage.h>
  4. #include <linux/fs.h>
  5. #include <linux/ktime.h>
  6. #include <linux/init.h>
  7. #include <linux/types.h>
  8. #include <linux/kallsyms.h>
  9. #ifdef CONFIG_FUNCTION_TRACER
  10. extern int ftrace_enabled;
  11. extern int
  12. ftrace_enable_sysctl(struct ctl_table *table, int write,
  13. struct file *filp, void __user *buffer, size_t *lenp,
  14. loff_t *ppos);
  15. typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
  16. struct ftrace_ops {
  17. ftrace_func_t func;
  18. struct ftrace_ops *next;
  19. };
  20. extern int function_trace_stop;
  21. /*
  22. * Type of the current tracing.
  23. */
  24. enum ftrace_tracing_type_t {
  25. FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
  26. FTRACE_TYPE_RETURN, /* Hook the return of the function */
  27. };
  28. /* Current tracing type, default is FTRACE_TYPE_ENTER */
  29. extern enum ftrace_tracing_type_t ftrace_tracing_type;
  30. /**
  31. * ftrace_stop - stop function tracer.
  32. *
  33. * A quick way to stop the function tracer. Note this an on off switch,
  34. * it is not something that is recursive like preempt_disable.
  35. * This does not disable the calling of mcount, it only stops the
  36. * calling of functions from mcount.
  37. */
  38. static inline void ftrace_stop(void)
  39. {
  40. function_trace_stop = 1;
  41. }
  42. /**
  43. * ftrace_start - start the function tracer.
  44. *
  45. * This function is the inverse of ftrace_stop. This does not enable
  46. * the function tracing if the function tracer is disabled. This only
  47. * sets the function tracer flag to continue calling the functions
  48. * from mcount.
  49. */
  50. static inline void ftrace_start(void)
  51. {
  52. function_trace_stop = 0;
  53. }
  54. /*
  55. * The ftrace_ops must be a static and should also
  56. * be read_mostly. These functions do modify read_mostly variables
  57. * so use them sparely. Never free an ftrace_op or modify the
  58. * next pointer after it has been registered. Even after unregistering
  59. * it, the next pointer may still be used internally.
  60. */
  61. int register_ftrace_function(struct ftrace_ops *ops);
  62. int unregister_ftrace_function(struct ftrace_ops *ops);
  63. void clear_ftrace_function(void);
  64. extern void ftrace_stub(unsigned long a0, unsigned long a1);
  65. #else /* !CONFIG_FUNCTION_TRACER */
  66. # define register_ftrace_function(ops) do { } while (0)
  67. # define unregister_ftrace_function(ops) do { } while (0)
  68. # define clear_ftrace_function(ops) do { } while (0)
  69. static inline void ftrace_kill(void) { }
  70. static inline void ftrace_stop(void) { }
  71. static inline void ftrace_start(void) { }
  72. #endif /* CONFIG_FUNCTION_TRACER */
  73. #ifdef CONFIG_DYNAMIC_FTRACE
  74. /* asm/ftrace.h must be defined for archs supporting dynamic ftrace */
  75. #include <asm/ftrace.h>
  76. enum {
  77. FTRACE_FL_FREE = (1 << 0),
  78. FTRACE_FL_FAILED = (1 << 1),
  79. FTRACE_FL_FILTER = (1 << 2),
  80. FTRACE_FL_ENABLED = (1 << 3),
  81. FTRACE_FL_NOTRACE = (1 << 4),
  82. FTRACE_FL_CONVERTED = (1 << 5),
  83. FTRACE_FL_FROZEN = (1 << 6),
  84. };
  85. struct dyn_ftrace {
  86. struct list_head list;
  87. unsigned long ip; /* address of mcount call-site */
  88. unsigned long flags;
  89. struct dyn_arch_ftrace arch;
  90. };
  91. int ftrace_force_update(void);
  92. void ftrace_set_filter(unsigned char *buf, int len, int reset);
  93. /* defined in arch */
  94. extern int ftrace_ip_converted(unsigned long ip);
  95. extern int ftrace_dyn_arch_init(void *data);
  96. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  97. extern void ftrace_caller(void);
  98. extern void ftrace_call(void);
  99. extern void mcount_call(void);
  100. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  101. extern void ftrace_graph_caller(void);
  102. #endif
  103. /**
  104. * ftrace_make_nop - convert code into top
  105. * @mod: module structure if called by module load initialization
  106. * @rec: the mcount call site record
  107. * @addr: the address that the call site should be calling
  108. *
  109. * This is a very sensitive operation and great care needs
  110. * to be taken by the arch. The operation should carefully
  111. * read the location, check to see if what is read is indeed
  112. * what we expect it to be, and then on success of the compare,
  113. * it should write to the location.
  114. *
  115. * The code segment at @rec->ip should be a caller to @addr
  116. *
  117. * Return must be:
  118. * 0 on success
  119. * -EFAULT on error reading the location
  120. * -EINVAL on a failed compare of the contents
  121. * -EPERM on error writing to the location
  122. * Any other value will be considered a failure.
  123. */
  124. extern int ftrace_make_nop(struct module *mod,
  125. struct dyn_ftrace *rec, unsigned long addr);
  126. /**
  127. * ftrace_make_call - convert a nop call site into a call to addr
  128. * @rec: the mcount call site record
  129. * @addr: the address that the call site should call
  130. *
  131. * This is a very sensitive operation and great care needs
  132. * to be taken by the arch. The operation should carefully
  133. * read the location, check to see if what is read is indeed
  134. * what we expect it to be, and then on success of the compare,
  135. * it should write to the location.
  136. *
  137. * The code segment at @rec->ip should be a nop
  138. *
  139. * Return must be:
  140. * 0 on success
  141. * -EFAULT on error reading the location
  142. * -EINVAL on a failed compare of the contents
  143. * -EPERM on error writing to the location
  144. * Any other value will be considered a failure.
  145. */
  146. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  147. /* May be defined in arch */
  148. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  149. extern int skip_trace(unsigned long ip);
  150. extern void ftrace_release(void *start, unsigned long size);
  151. extern void ftrace_disable_daemon(void);
  152. extern void ftrace_enable_daemon(void);
  153. #else
  154. # define skip_trace(ip) ({ 0; })
  155. # define ftrace_force_update() ({ 0; })
  156. # define ftrace_set_filter(buf, len, reset) do { } while (0)
  157. # define ftrace_disable_daemon() do { } while (0)
  158. # define ftrace_enable_daemon() do { } while (0)
  159. static inline void ftrace_release(void *start, unsigned long size) { }
  160. #endif /* CONFIG_DYNAMIC_FTRACE */
  161. /* totally disable ftrace - can not re-enable after this */
  162. void ftrace_kill(void);
  163. static inline void tracer_disable(void)
  164. {
  165. #ifdef CONFIG_FUNCTION_TRACER
  166. ftrace_enabled = 0;
  167. #endif
  168. }
  169. /*
  170. * Ftrace disable/restore without lock. Some synchronization mechanism
  171. * must be used to prevent ftrace_enabled to be changed between
  172. * disable/restore.
  173. */
  174. static inline int __ftrace_enabled_save(void)
  175. {
  176. #ifdef CONFIG_FUNCTION_TRACER
  177. int saved_ftrace_enabled = ftrace_enabled;
  178. ftrace_enabled = 0;
  179. return saved_ftrace_enabled;
  180. #else
  181. return 0;
  182. #endif
  183. }
  184. static inline void __ftrace_enabled_restore(int enabled)
  185. {
  186. #ifdef CONFIG_FUNCTION_TRACER
  187. ftrace_enabled = enabled;
  188. #endif
  189. }
  190. #ifdef CONFIG_FRAME_POINTER
  191. /* TODO: need to fix this for ARM */
  192. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  193. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  194. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  195. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  196. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  197. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  198. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  199. #else
  200. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  201. # define CALLER_ADDR1 0UL
  202. # define CALLER_ADDR2 0UL
  203. # define CALLER_ADDR3 0UL
  204. # define CALLER_ADDR4 0UL
  205. # define CALLER_ADDR5 0UL
  206. # define CALLER_ADDR6 0UL
  207. #endif
  208. #ifdef CONFIG_IRQSOFF_TRACER
  209. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  210. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  211. #else
  212. # define time_hardirqs_on(a0, a1) do { } while (0)
  213. # define time_hardirqs_off(a0, a1) do { } while (0)
  214. #endif
  215. #ifdef CONFIG_PREEMPT_TRACER
  216. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  217. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  218. #else
  219. # define trace_preempt_on(a0, a1) do { } while (0)
  220. # define trace_preempt_off(a0, a1) do { } while (0)
  221. #endif
  222. #ifdef CONFIG_TRACING
  223. extern int ftrace_dump_on_oops;
  224. extern void tracing_start(void);
  225. extern void tracing_stop(void);
  226. extern void ftrace_off_permanent(void);
  227. extern void
  228. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
  229. /**
  230. * ftrace_printk - printf formatting in the ftrace buffer
  231. * @fmt: the printf format for printing
  232. *
  233. * Note: __ftrace_printk is an internal function for ftrace_printk and
  234. * the @ip is passed in via the ftrace_printk macro.
  235. *
  236. * This function allows a kernel developer to debug fast path sections
  237. * that printk is not appropriate for. By scattering in various
  238. * printk like tracing in the code, a developer can quickly see
  239. * where problems are occurring.
  240. *
  241. * This is intended as a debugging tool for the developer only.
  242. * Please refrain from leaving ftrace_printks scattered around in
  243. * your code.
  244. */
  245. # define ftrace_printk(fmt...) __ftrace_printk(_THIS_IP_, fmt)
  246. extern int
  247. __ftrace_printk(unsigned long ip, const char *fmt, ...)
  248. __attribute__ ((format (printf, 2, 3)));
  249. extern void ftrace_dump(void);
  250. #else
  251. static inline void
  252. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  253. static inline int
  254. ftrace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 0)));
  255. static inline void tracing_start(void) { }
  256. static inline void tracing_stop(void) { }
  257. static inline void ftrace_off_permanent(void) { }
  258. static inline int
  259. ftrace_printk(const char *fmt, ...)
  260. {
  261. return 0;
  262. }
  263. static inline void ftrace_dump(void) { }
  264. #endif
  265. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  266. extern void ftrace_init(void);
  267. extern void ftrace_init_module(struct module *mod,
  268. unsigned long *start, unsigned long *end);
  269. #else
  270. static inline void ftrace_init(void) { }
  271. static inline void
  272. ftrace_init_module(struct module *mod,
  273. unsigned long *start, unsigned long *end) { }
  274. #endif
  275. /*
  276. * Structure that defines an entry function trace.
  277. */
  278. struct ftrace_graph_ent {
  279. unsigned long func; /* Current function */
  280. int depth;
  281. };
  282. /*
  283. * Structure that defines a return function trace.
  284. */
  285. struct ftrace_graph_ret {
  286. unsigned long func; /* Current function */
  287. unsigned long long calltime;
  288. unsigned long long rettime;
  289. /* Number of functions that overran the depth limit for current task */
  290. unsigned long overrun;
  291. int depth;
  292. };
  293. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  294. #define FTRACE_RETFUNC_DEPTH 50
  295. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  296. /* Type of the callback handlers for tracing function graph*/
  297. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  298. typedef void (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  299. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  300. trace_func_graph_ent_t entryfunc);
  301. /* The current handlers in use */
  302. extern trace_func_graph_ret_t ftrace_graph_return;
  303. extern trace_func_graph_ent_t ftrace_graph_entry;
  304. extern void unregister_ftrace_graph(void);
  305. extern void ftrace_graph_init_task(struct task_struct *t);
  306. extern void ftrace_graph_exit_task(struct task_struct *t);
  307. #else
  308. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  309. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  310. #endif
  311. #endif /* _LINUX_FTRACE_H */