ftrace.h 14 KB

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