ftrace.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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/module.h>
  9. #include <linux/kallsyms.h>
  10. #include <linux/bitops.h>
  11. #include <linux/sched.h>
  12. #ifdef CONFIG_FUNCTION_TRACER
  13. extern int ftrace_enabled;
  14. extern int
  15. ftrace_enable_sysctl(struct ctl_table *table, int write,
  16. struct file *filp, void __user *buffer, size_t *lenp,
  17. loff_t *ppos);
  18. typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
  19. struct ftrace_ops {
  20. ftrace_func_t func;
  21. struct ftrace_ops *next;
  22. };
  23. extern int function_trace_stop;
  24. /*
  25. * Type of the current tracing.
  26. */
  27. enum ftrace_tracing_type_t {
  28. FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
  29. FTRACE_TYPE_RETURN, /* Hook the return of the function */
  30. };
  31. /* Current tracing type, default is FTRACE_TYPE_ENTER */
  32. extern enum ftrace_tracing_type_t ftrace_tracing_type;
  33. /**
  34. * ftrace_stop - stop function tracer.
  35. *
  36. * A quick way to stop the function tracer. Note this an on off switch,
  37. * it is not something that is recursive like preempt_disable.
  38. * This does not disable the calling of mcount, it only stops the
  39. * calling of functions from mcount.
  40. */
  41. static inline void ftrace_stop(void)
  42. {
  43. function_trace_stop = 1;
  44. }
  45. /**
  46. * ftrace_start - start the function tracer.
  47. *
  48. * This function is the inverse of ftrace_stop. This does not enable
  49. * the function tracing if the function tracer is disabled. This only
  50. * sets the function tracer flag to continue calling the functions
  51. * from mcount.
  52. */
  53. static inline void ftrace_start(void)
  54. {
  55. function_trace_stop = 0;
  56. }
  57. /*
  58. * The ftrace_ops must be a static and should also
  59. * be read_mostly. These functions do modify read_mostly variables
  60. * so use them sparely. Never free an ftrace_op or modify the
  61. * next pointer after it has been registered. Even after unregistering
  62. * it, the next pointer may still be used internally.
  63. */
  64. int register_ftrace_function(struct ftrace_ops *ops);
  65. int unregister_ftrace_function(struct ftrace_ops *ops);
  66. void clear_ftrace_function(void);
  67. extern void ftrace_stub(unsigned long a0, unsigned long a1);
  68. #else /* !CONFIG_FUNCTION_TRACER */
  69. # define register_ftrace_function(ops) do { } while (0)
  70. # define unregister_ftrace_function(ops) do { } while (0)
  71. # define clear_ftrace_function(ops) do { } while (0)
  72. static inline void ftrace_kill(void) { }
  73. static inline void ftrace_stop(void) { }
  74. static inline void ftrace_start(void) { }
  75. #endif /* CONFIG_FUNCTION_TRACER */
  76. #ifdef CONFIG_STACK_TRACER
  77. extern int stack_tracer_enabled;
  78. int
  79. stack_trace_sysctl(struct ctl_table *table, int write,
  80. struct file *file, void __user *buffer, size_t *lenp,
  81. loff_t *ppos);
  82. #endif
  83. struct ftrace_func_command {
  84. struct list_head list;
  85. char *name;
  86. int (*func)(char *func, char *cmd,
  87. char *params, int enable);
  88. };
  89. #ifdef CONFIG_DYNAMIC_FTRACE
  90. /* asm/ftrace.h must be defined for archs supporting dynamic ftrace */
  91. #include <asm/ftrace.h>
  92. struct ftrace_hook_ops {
  93. void (*func)(unsigned long ip,
  94. unsigned long parent_ip,
  95. void **data);
  96. int (*callback)(unsigned long ip, void **data);
  97. void (*free)(void **data);
  98. };
  99. extern int
  100. register_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
  101. void *data);
  102. extern void
  103. unregister_ftrace_function_hook(char *glob, struct ftrace_hook_ops *ops,
  104. void *data);
  105. extern void
  106. unregister_ftrace_function_hook_func(char *glob, struct ftrace_hook_ops *ops);
  107. extern void unregister_ftrace_function_hook_all(char *glob);
  108. enum {
  109. FTRACE_FL_FREE = (1 << 0),
  110. FTRACE_FL_FAILED = (1 << 1),
  111. FTRACE_FL_FILTER = (1 << 2),
  112. FTRACE_FL_ENABLED = (1 << 3),
  113. FTRACE_FL_NOTRACE = (1 << 4),
  114. FTRACE_FL_CONVERTED = (1 << 5),
  115. FTRACE_FL_FROZEN = (1 << 6),
  116. };
  117. struct dyn_ftrace {
  118. struct list_head list;
  119. unsigned long ip; /* address of mcount call-site */
  120. unsigned long flags;
  121. struct dyn_arch_ftrace arch;
  122. };
  123. int ftrace_force_update(void);
  124. void ftrace_set_filter(unsigned char *buf, int len, int reset);
  125. int register_ftrace_command(struct ftrace_func_command *cmd);
  126. int unregister_ftrace_command(struct ftrace_func_command *cmd);
  127. /* defined in arch */
  128. extern int ftrace_ip_converted(unsigned long ip);
  129. extern int ftrace_dyn_arch_init(void *data);
  130. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  131. extern void ftrace_caller(void);
  132. extern void ftrace_call(void);
  133. extern void mcount_call(void);
  134. #ifndef FTRACE_ADDR
  135. #define FTRACE_ADDR ((unsigned long)ftrace_caller)
  136. #endif
  137. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  138. extern void ftrace_graph_caller(void);
  139. extern int ftrace_enable_ftrace_graph_caller(void);
  140. extern int ftrace_disable_ftrace_graph_caller(void);
  141. #else
  142. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  143. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  144. #endif
  145. /**
  146. * ftrace_make_nop - convert code into nop
  147. * @mod: module structure if called by module load initialization
  148. * @rec: the mcount call site record
  149. * @addr: the address that the call site should be calling
  150. *
  151. * This is a very sensitive operation and great care needs
  152. * to be taken by the arch. The operation should carefully
  153. * read the location, check to see if what is read is indeed
  154. * what we expect it to be, and then on success of the compare,
  155. * it should write to the location.
  156. *
  157. * The code segment at @rec->ip should be a caller to @addr
  158. *
  159. * Return must be:
  160. * 0 on success
  161. * -EFAULT on error reading the location
  162. * -EINVAL on a failed compare of the contents
  163. * -EPERM on error writing to the location
  164. * Any other value will be considered a failure.
  165. */
  166. extern int ftrace_make_nop(struct module *mod,
  167. struct dyn_ftrace *rec, unsigned long addr);
  168. /**
  169. * ftrace_make_call - convert a nop call site into a call to addr
  170. * @rec: the mcount call site record
  171. * @addr: the address that the call site should call
  172. *
  173. * This is a very sensitive operation and great care needs
  174. * to be taken by the arch. The operation should carefully
  175. * read the location, check to see if what is read is indeed
  176. * what we expect it to be, and then on success of the compare,
  177. * it should write to the location.
  178. *
  179. * The code segment at @rec->ip should be a nop
  180. *
  181. * Return must be:
  182. * 0 on success
  183. * -EFAULT on error reading the location
  184. * -EINVAL on a failed compare of the contents
  185. * -EPERM on error writing to the location
  186. * Any other value will be considered a failure.
  187. */
  188. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  189. /* May be defined in arch */
  190. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  191. extern int skip_trace(unsigned long ip);
  192. extern void ftrace_release(void *start, unsigned long size);
  193. extern void ftrace_disable_daemon(void);
  194. extern void ftrace_enable_daemon(void);
  195. #else
  196. # define skip_trace(ip) ({ 0; })
  197. # define ftrace_force_update() ({ 0; })
  198. # define ftrace_set_filter(buf, len, reset) do { } while (0)
  199. # define ftrace_disable_daemon() do { } while (0)
  200. # define ftrace_enable_daemon() do { } while (0)
  201. static inline void ftrace_release(void *start, unsigned long size) { }
  202. static inline int register_ftrace_command(struct ftrace_func_command *cmd)
  203. {
  204. }
  205. static inline int unregister_ftrace_command(char *cmd_name)
  206. {
  207. }
  208. #endif /* CONFIG_DYNAMIC_FTRACE */
  209. /* totally disable ftrace - can not re-enable after this */
  210. void ftrace_kill(void);
  211. static inline void tracer_disable(void)
  212. {
  213. #ifdef CONFIG_FUNCTION_TRACER
  214. ftrace_enabled = 0;
  215. #endif
  216. }
  217. /*
  218. * Ftrace disable/restore without lock. Some synchronization mechanism
  219. * must be used to prevent ftrace_enabled to be changed between
  220. * disable/restore.
  221. */
  222. static inline int __ftrace_enabled_save(void)
  223. {
  224. #ifdef CONFIG_FUNCTION_TRACER
  225. int saved_ftrace_enabled = ftrace_enabled;
  226. ftrace_enabled = 0;
  227. return saved_ftrace_enabled;
  228. #else
  229. return 0;
  230. #endif
  231. }
  232. static inline void __ftrace_enabled_restore(int enabled)
  233. {
  234. #ifdef CONFIG_FUNCTION_TRACER
  235. ftrace_enabled = enabled;
  236. #endif
  237. }
  238. #ifdef CONFIG_FRAME_POINTER
  239. /* TODO: need to fix this for ARM */
  240. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  241. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  242. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  243. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  244. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  245. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  246. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  247. #else
  248. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  249. # define CALLER_ADDR1 0UL
  250. # define CALLER_ADDR2 0UL
  251. # define CALLER_ADDR3 0UL
  252. # define CALLER_ADDR4 0UL
  253. # define CALLER_ADDR5 0UL
  254. # define CALLER_ADDR6 0UL
  255. #endif
  256. #ifdef CONFIG_IRQSOFF_TRACER
  257. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  258. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  259. #else
  260. # define time_hardirqs_on(a0, a1) do { } while (0)
  261. # define time_hardirqs_off(a0, a1) do { } while (0)
  262. #endif
  263. #ifdef CONFIG_PREEMPT_TRACER
  264. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  265. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  266. #else
  267. # define trace_preempt_on(a0, a1) do { } while (0)
  268. # define trace_preempt_off(a0, a1) do { } while (0)
  269. #endif
  270. #ifdef CONFIG_TRACING
  271. extern int ftrace_dump_on_oops;
  272. extern void tracing_start(void);
  273. extern void tracing_stop(void);
  274. extern void ftrace_off_permanent(void);
  275. extern void
  276. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
  277. /**
  278. * ftrace_printk - printf formatting in the ftrace buffer
  279. * @fmt: the printf format for printing
  280. *
  281. * Note: __ftrace_printk is an internal function for ftrace_printk and
  282. * the @ip is passed in via the ftrace_printk macro.
  283. *
  284. * This function allows a kernel developer to debug fast path sections
  285. * that printk is not appropriate for. By scattering in various
  286. * printk like tracing in the code, a developer can quickly see
  287. * where problems are occurring.
  288. *
  289. * This is intended as a debugging tool for the developer only.
  290. * Please refrain from leaving ftrace_printks scattered around in
  291. * your code.
  292. */
  293. # define ftrace_printk(fmt...) __ftrace_printk(_THIS_IP_, fmt)
  294. extern int
  295. __ftrace_printk(unsigned long ip, const char *fmt, ...)
  296. __attribute__ ((format (printf, 2, 3)));
  297. # define ftrace_vprintk(fmt, ap) __ftrace_printk(_THIS_IP_, fmt, ap)
  298. extern int
  299. __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
  300. extern void ftrace_dump(void);
  301. #else
  302. static inline void
  303. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  304. static inline int
  305. ftrace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  306. static inline void tracing_start(void) { }
  307. static inline void tracing_stop(void) { }
  308. static inline void ftrace_off_permanent(void) { }
  309. static inline int
  310. ftrace_printk(const char *fmt, ...)
  311. {
  312. return 0;
  313. }
  314. static inline int
  315. ftrace_vprintk(const char *fmt, va_list ap)
  316. {
  317. return 0;
  318. }
  319. static inline void ftrace_dump(void) { }
  320. #endif
  321. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  322. extern void ftrace_init(void);
  323. extern void ftrace_init_module(struct module *mod,
  324. unsigned long *start, unsigned long *end);
  325. #else
  326. static inline void ftrace_init(void) { }
  327. static inline void
  328. ftrace_init_module(struct module *mod,
  329. unsigned long *start, unsigned long *end) { }
  330. #endif
  331. /*
  332. * Structure that defines an entry function trace.
  333. */
  334. struct ftrace_graph_ent {
  335. unsigned long func; /* Current function */
  336. int depth;
  337. };
  338. /*
  339. * Structure that defines a return function trace.
  340. */
  341. struct ftrace_graph_ret {
  342. unsigned long func; /* Current function */
  343. unsigned long long calltime;
  344. unsigned long long rettime;
  345. /* Number of functions that overran the depth limit for current task */
  346. unsigned long overrun;
  347. int depth;
  348. };
  349. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  350. /*
  351. * Sometimes we don't want to trace a function with the function
  352. * graph tracer but we want them to keep traced by the usual function
  353. * tracer if the function graph tracer is not configured.
  354. */
  355. #define __notrace_funcgraph notrace
  356. /*
  357. * We want to which function is an entrypoint of a hardirq.
  358. * That will help us to put a signal on output.
  359. */
  360. #define __irq_entry __attribute__((__section__(".irqentry.text")))
  361. /* Limits of hardirq entrypoints */
  362. extern char __irqentry_text_start[];
  363. extern char __irqentry_text_end[];
  364. #define FTRACE_RETFUNC_DEPTH 50
  365. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  366. /* Type of the callback handlers for tracing function graph*/
  367. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  368. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  369. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  370. trace_func_graph_ent_t entryfunc);
  371. extern void ftrace_graph_stop(void);
  372. /* The current handlers in use */
  373. extern trace_func_graph_ret_t ftrace_graph_return;
  374. extern trace_func_graph_ent_t ftrace_graph_entry;
  375. extern void unregister_ftrace_graph(void);
  376. extern void ftrace_graph_init_task(struct task_struct *t);
  377. extern void ftrace_graph_exit_task(struct task_struct *t);
  378. static inline int task_curr_ret_stack(struct task_struct *t)
  379. {
  380. return t->curr_ret_stack;
  381. }
  382. static inline void pause_graph_tracing(void)
  383. {
  384. atomic_inc(&current->tracing_graph_pause);
  385. }
  386. static inline void unpause_graph_tracing(void)
  387. {
  388. atomic_dec(&current->tracing_graph_pause);
  389. }
  390. #else
  391. #define __notrace_funcgraph
  392. #define __irq_entry
  393. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  394. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  395. static inline int task_curr_ret_stack(struct task_struct *tsk)
  396. {
  397. return -1;
  398. }
  399. static inline void pause_graph_tracing(void) { }
  400. static inline void unpause_graph_tracing(void) { }
  401. #endif
  402. #ifdef CONFIG_TRACING
  403. #include <linux/sched.h>
  404. /* flags for current->trace */
  405. enum {
  406. TSK_TRACE_FL_TRACE_BIT = 0,
  407. TSK_TRACE_FL_GRAPH_BIT = 1,
  408. };
  409. enum {
  410. TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
  411. TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
  412. };
  413. static inline void set_tsk_trace_trace(struct task_struct *tsk)
  414. {
  415. set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  416. }
  417. static inline void clear_tsk_trace_trace(struct task_struct *tsk)
  418. {
  419. clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  420. }
  421. static inline int test_tsk_trace_trace(struct task_struct *tsk)
  422. {
  423. return tsk->trace & TSK_TRACE_FL_TRACE;
  424. }
  425. static inline void set_tsk_trace_graph(struct task_struct *tsk)
  426. {
  427. set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  428. }
  429. static inline void clear_tsk_trace_graph(struct task_struct *tsk)
  430. {
  431. clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  432. }
  433. static inline int test_tsk_trace_graph(struct task_struct *tsk)
  434. {
  435. return tsk->trace & TSK_TRACE_FL_GRAPH;
  436. }
  437. #endif /* CONFIG_TRACING */
  438. #ifdef CONFIG_HW_BRANCH_TRACER
  439. void trace_hw_branch(u64 from, u64 to);
  440. void trace_hw_branch_oops(void);
  441. #else /* CONFIG_HW_BRANCH_TRACER */
  442. static inline void trace_hw_branch(u64 from, u64 to) {}
  443. static inline void trace_hw_branch_oops(void) {}
  444. #endif /* CONFIG_HW_BRANCH_TRACER */
  445. #endif /* _LINUX_FTRACE_H */