ftrace.h 16 KB

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