ftrace.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. extern int ftrace_enable_ftrace_graph_caller(void);
  103. extern int ftrace_disable_ftrace_graph_caller(void);
  104. #else
  105. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  106. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  107. #endif
  108. /**
  109. * ftrace_make_nop - convert code into top
  110. * @mod: module structure if called by module load initialization
  111. * @rec: the mcount call site record
  112. * @addr: the address that the call site should be calling
  113. *
  114. * This is a very sensitive operation and great care needs
  115. * to be taken by the arch. The operation should carefully
  116. * read the location, check to see if what is read is indeed
  117. * what we expect it to be, and then on success of the compare,
  118. * it should write to the location.
  119. *
  120. * The code segment at @rec->ip should be a caller to @addr
  121. *
  122. * Return must be:
  123. * 0 on success
  124. * -EFAULT on error reading the location
  125. * -EINVAL on a failed compare of the contents
  126. * -EPERM on error writing to the location
  127. * Any other value will be considered a failure.
  128. */
  129. extern int ftrace_make_nop(struct module *mod,
  130. struct dyn_ftrace *rec, unsigned long addr);
  131. /**
  132. * ftrace_make_call - convert a nop call site into a call to addr
  133. * @rec: the mcount call site record
  134. * @addr: the address that the call site should call
  135. *
  136. * This is a very sensitive operation and great care needs
  137. * to be taken by the arch. The operation should carefully
  138. * read the location, check to see if what is read is indeed
  139. * what we expect it to be, and then on success of the compare,
  140. * it should write to the location.
  141. *
  142. * The code segment at @rec->ip should be a nop
  143. *
  144. * Return must be:
  145. * 0 on success
  146. * -EFAULT on error reading the location
  147. * -EINVAL on a failed compare of the contents
  148. * -EPERM on error writing to the location
  149. * Any other value will be considered a failure.
  150. */
  151. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  152. /* May be defined in arch */
  153. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  154. extern int skip_trace(unsigned long ip);
  155. extern void ftrace_release(void *start, unsigned long size);
  156. extern void ftrace_disable_daemon(void);
  157. extern void ftrace_enable_daemon(void);
  158. #else
  159. # define skip_trace(ip) ({ 0; })
  160. # define ftrace_force_update() ({ 0; })
  161. # define ftrace_set_filter(buf, len, reset) do { } while (0)
  162. # define ftrace_disable_daemon() do { } while (0)
  163. # define ftrace_enable_daemon() do { } while (0)
  164. static inline void ftrace_release(void *start, unsigned long size) { }
  165. #endif /* CONFIG_DYNAMIC_FTRACE */
  166. /* totally disable ftrace - can not re-enable after this */
  167. void ftrace_kill(void);
  168. static inline void tracer_disable(void)
  169. {
  170. #ifdef CONFIG_FUNCTION_TRACER
  171. ftrace_enabled = 0;
  172. #endif
  173. }
  174. /*
  175. * Ftrace disable/restore without lock. Some synchronization mechanism
  176. * must be used to prevent ftrace_enabled to be changed between
  177. * disable/restore.
  178. */
  179. static inline int __ftrace_enabled_save(void)
  180. {
  181. #ifdef CONFIG_FUNCTION_TRACER
  182. int saved_ftrace_enabled = ftrace_enabled;
  183. ftrace_enabled = 0;
  184. return saved_ftrace_enabled;
  185. #else
  186. return 0;
  187. #endif
  188. }
  189. static inline void __ftrace_enabled_restore(int enabled)
  190. {
  191. #ifdef CONFIG_FUNCTION_TRACER
  192. ftrace_enabled = enabled;
  193. #endif
  194. }
  195. #ifdef CONFIG_FRAME_POINTER
  196. /* TODO: need to fix this for ARM */
  197. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  198. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  199. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  200. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  201. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  202. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  203. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  204. #else
  205. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  206. # define CALLER_ADDR1 0UL
  207. # define CALLER_ADDR2 0UL
  208. # define CALLER_ADDR3 0UL
  209. # define CALLER_ADDR4 0UL
  210. # define CALLER_ADDR5 0UL
  211. # define CALLER_ADDR6 0UL
  212. #endif
  213. #ifdef CONFIG_IRQSOFF_TRACER
  214. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  215. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  216. #else
  217. # define time_hardirqs_on(a0, a1) do { } while (0)
  218. # define time_hardirqs_off(a0, a1) do { } while (0)
  219. #endif
  220. #ifdef CONFIG_PREEMPT_TRACER
  221. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  222. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  223. #else
  224. # define trace_preempt_on(a0, a1) do { } while (0)
  225. # define trace_preempt_off(a0, a1) do { } while (0)
  226. #endif
  227. #ifdef CONFIG_TRACING
  228. extern int ftrace_dump_on_oops;
  229. extern void tracing_start(void);
  230. extern void tracing_stop(void);
  231. extern void ftrace_off_permanent(void);
  232. extern void
  233. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
  234. /**
  235. * ftrace_printk - printf formatting in the ftrace buffer
  236. * @fmt: the printf format for printing
  237. *
  238. * Note: __ftrace_printk is an internal function for ftrace_printk and
  239. * the @ip is passed in via the ftrace_printk macro.
  240. *
  241. * This function allows a kernel developer to debug fast path sections
  242. * that printk is not appropriate for. By scattering in various
  243. * printk like tracing in the code, a developer can quickly see
  244. * where problems are occurring.
  245. *
  246. * This is intended as a debugging tool for the developer only.
  247. * Please refrain from leaving ftrace_printks scattered around in
  248. * your code.
  249. */
  250. # define ftrace_printk(fmt...) __ftrace_printk(_THIS_IP_, fmt)
  251. extern int
  252. __ftrace_printk(unsigned long ip, const char *fmt, ...)
  253. __attribute__ ((format (printf, 2, 3)));
  254. extern void ftrace_dump(void);
  255. #else
  256. static inline void
  257. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  258. static inline int
  259. ftrace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 0)));
  260. static inline void tracing_start(void) { }
  261. static inline void tracing_stop(void) { }
  262. static inline void ftrace_off_permanent(void) { }
  263. static inline int
  264. ftrace_printk(const char *fmt, ...)
  265. {
  266. return 0;
  267. }
  268. static inline void ftrace_dump(void) { }
  269. #endif
  270. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  271. extern void ftrace_init(void);
  272. extern void ftrace_init_module(struct module *mod,
  273. unsigned long *start, unsigned long *end);
  274. #else
  275. static inline void ftrace_init(void) { }
  276. static inline void
  277. ftrace_init_module(struct module *mod,
  278. unsigned long *start, unsigned long *end) { }
  279. #endif
  280. enum {
  281. POWER_NONE = 0,
  282. POWER_CSTATE = 1,
  283. POWER_PSTATE = 2,
  284. };
  285. struct power_trace {
  286. #ifdef CONFIG_POWER_TRACER
  287. ktime_t stamp;
  288. ktime_t end;
  289. int type;
  290. int state;
  291. #endif
  292. };
  293. #ifdef CONFIG_POWER_TRACER
  294. extern void trace_power_start(struct power_trace *it, unsigned int type,
  295. unsigned int state);
  296. extern void trace_power_mark(struct power_trace *it, unsigned int type,
  297. unsigned int state);
  298. extern void trace_power_end(struct power_trace *it);
  299. #else
  300. static inline void trace_power_start(struct power_trace *it, unsigned int type,
  301. unsigned int state) { }
  302. static inline void trace_power_mark(struct power_trace *it, unsigned int type,
  303. unsigned int state) { }
  304. static inline void trace_power_end(struct power_trace *it) { }
  305. #endif
  306. /*
  307. * Structure that defines an entry function trace.
  308. */
  309. struct ftrace_graph_ent {
  310. unsigned long func; /* Current function */
  311. int depth;
  312. };
  313. /*
  314. * Structure that defines a return function trace.
  315. */
  316. struct ftrace_graph_ret {
  317. unsigned long func; /* Current function */
  318. unsigned long long calltime;
  319. unsigned long long rettime;
  320. /* Number of functions that overran the depth limit for current task */
  321. unsigned long overrun;
  322. int depth;
  323. };
  324. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  325. #define FTRACE_RETFUNC_DEPTH 50
  326. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  327. /* Type of the callback handlers for tracing function graph*/
  328. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  329. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  330. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  331. trace_func_graph_ent_t entryfunc);
  332. extern void ftrace_graph_stop(void);
  333. /* The current handlers in use */
  334. extern trace_func_graph_ret_t ftrace_graph_return;
  335. extern trace_func_graph_ent_t ftrace_graph_entry;
  336. extern void unregister_ftrace_graph(void);
  337. extern void ftrace_graph_init_task(struct task_struct *t);
  338. extern void ftrace_graph_exit_task(struct task_struct *t);
  339. #else
  340. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  341. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  342. #endif
  343. #endif /* _LINUX_FTRACE_H */