ftrace.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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_FILTER = (1 << 1),
  127. FTRACE_FL_ENABLED = (1 << 2),
  128. };
  129. struct dyn_ftrace {
  130. union {
  131. unsigned long ip; /* address of mcount call-site */
  132. struct dyn_ftrace *freelist;
  133. };
  134. union {
  135. unsigned long flags;
  136. struct dyn_ftrace *newlist;
  137. };
  138. struct dyn_arch_ftrace arch;
  139. };
  140. int ftrace_force_update(void);
  141. void ftrace_set_filter(unsigned char *buf, int len, int reset);
  142. int register_ftrace_command(struct ftrace_func_command *cmd);
  143. int unregister_ftrace_command(struct ftrace_func_command *cmd);
  144. /* defined in arch */
  145. extern int ftrace_ip_converted(unsigned long ip);
  146. extern int ftrace_dyn_arch_init(void *data);
  147. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  148. extern void ftrace_caller(void);
  149. extern void ftrace_call(void);
  150. extern void mcount_call(void);
  151. #ifndef FTRACE_ADDR
  152. #define FTRACE_ADDR ((unsigned long)ftrace_caller)
  153. #endif
  154. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  155. extern void ftrace_graph_caller(void);
  156. extern int ftrace_enable_ftrace_graph_caller(void);
  157. extern int ftrace_disable_ftrace_graph_caller(void);
  158. #else
  159. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  160. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  161. #endif
  162. /**
  163. * ftrace_make_nop - convert code into nop
  164. * @mod: module structure if called by module load initialization
  165. * @rec: the mcount call site record
  166. * @addr: the address that the call site should be calling
  167. *
  168. * This is a very sensitive operation and great care needs
  169. * to be taken by the arch. The operation should carefully
  170. * read the location, check to see if what is read is indeed
  171. * what we expect it to be, and then on success of the compare,
  172. * it should write to the location.
  173. *
  174. * The code segment at @rec->ip should be a caller to @addr
  175. *
  176. * Return must be:
  177. * 0 on success
  178. * -EFAULT on error reading the location
  179. * -EINVAL on a failed compare of the contents
  180. * -EPERM on error writing to the location
  181. * Any other value will be considered a failure.
  182. */
  183. extern int ftrace_make_nop(struct module *mod,
  184. struct dyn_ftrace *rec, unsigned long addr);
  185. /**
  186. * ftrace_make_call - convert a nop call site into a call to addr
  187. * @rec: the mcount call site record
  188. * @addr: the address that the call site should call
  189. *
  190. * This is a very sensitive operation and great care needs
  191. * to be taken by the arch. The operation should carefully
  192. * read the location, check to see if what is read is indeed
  193. * what we expect it to be, and then on success of the compare,
  194. * it should write to the location.
  195. *
  196. * The code segment at @rec->ip should be a nop
  197. *
  198. * Return must be:
  199. * 0 on success
  200. * -EFAULT on error reading the location
  201. * -EINVAL on a failed compare of the contents
  202. * -EPERM on error writing to the location
  203. * Any other value will be considered a failure.
  204. */
  205. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  206. /* May be defined in arch */
  207. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  208. extern int skip_trace(unsigned long ip);
  209. extern void ftrace_disable_daemon(void);
  210. extern void ftrace_enable_daemon(void);
  211. #else
  212. static inline int skip_trace(unsigned long ip) { return 0; }
  213. static inline int ftrace_force_update(void) { return 0; }
  214. static inline void ftrace_set_filter(unsigned char *buf, int len, int reset)
  215. {
  216. }
  217. static inline void ftrace_disable_daemon(void) { }
  218. static inline void ftrace_enable_daemon(void) { }
  219. static inline void ftrace_release_mod(struct module *mod) {}
  220. static inline int register_ftrace_command(struct ftrace_func_command *cmd)
  221. {
  222. return -EINVAL;
  223. }
  224. static inline int unregister_ftrace_command(char *cmd_name)
  225. {
  226. return -EINVAL;
  227. }
  228. static inline int ftrace_text_reserved(void *start, void *end)
  229. {
  230. return 0;
  231. }
  232. #endif /* CONFIG_DYNAMIC_FTRACE */
  233. /* totally disable ftrace - can not re-enable after this */
  234. void ftrace_kill(void);
  235. static inline void tracer_disable(void)
  236. {
  237. #ifdef CONFIG_FUNCTION_TRACER
  238. ftrace_enabled = 0;
  239. #endif
  240. }
  241. /*
  242. * Ftrace disable/restore without lock. Some synchronization mechanism
  243. * must be used to prevent ftrace_enabled to be changed between
  244. * disable/restore.
  245. */
  246. static inline int __ftrace_enabled_save(void)
  247. {
  248. #ifdef CONFIG_FUNCTION_TRACER
  249. int saved_ftrace_enabled = ftrace_enabled;
  250. ftrace_enabled = 0;
  251. return saved_ftrace_enabled;
  252. #else
  253. return 0;
  254. #endif
  255. }
  256. static inline void __ftrace_enabled_restore(int enabled)
  257. {
  258. #ifdef CONFIG_FUNCTION_TRACER
  259. ftrace_enabled = enabled;
  260. #endif
  261. }
  262. #ifndef HAVE_ARCH_CALLER_ADDR
  263. # ifdef CONFIG_FRAME_POINTER
  264. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  265. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  266. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  267. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  268. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  269. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  270. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  271. # else
  272. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  273. # define CALLER_ADDR1 0UL
  274. # define CALLER_ADDR2 0UL
  275. # define CALLER_ADDR3 0UL
  276. # define CALLER_ADDR4 0UL
  277. # define CALLER_ADDR5 0UL
  278. # define CALLER_ADDR6 0UL
  279. # endif
  280. #endif /* ifndef HAVE_ARCH_CALLER_ADDR */
  281. #ifdef CONFIG_IRQSOFF_TRACER
  282. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  283. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  284. #else
  285. static inline void time_hardirqs_on(unsigned long a0, unsigned long a1) { }
  286. static inline void time_hardirqs_off(unsigned long a0, unsigned long a1) { }
  287. #endif
  288. #ifdef CONFIG_PREEMPT_TRACER
  289. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  290. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  291. #else
  292. static inline void trace_preempt_on(unsigned long a0, unsigned long a1) { }
  293. static inline void trace_preempt_off(unsigned long a0, unsigned long a1) { }
  294. #endif
  295. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  296. extern void ftrace_init(void);
  297. #else
  298. static inline void ftrace_init(void) { }
  299. #endif
  300. /*
  301. * Structure that defines an entry function trace.
  302. */
  303. struct ftrace_graph_ent {
  304. unsigned long func; /* Current function */
  305. int depth;
  306. };
  307. /*
  308. * Structure that defines a return function trace.
  309. */
  310. struct ftrace_graph_ret {
  311. unsigned long func; /* Current function */
  312. unsigned long long calltime;
  313. unsigned long long rettime;
  314. /* Number of functions that overran the depth limit for current task */
  315. unsigned long overrun;
  316. int depth;
  317. };
  318. /* Type of the callback handlers for tracing function graph*/
  319. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  320. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  321. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  322. /* for init task */
  323. #define INIT_FTRACE_GRAPH .ret_stack = NULL,
  324. /*
  325. * Stack of return addresses for functions
  326. * of a thread.
  327. * Used in struct thread_info
  328. */
  329. struct ftrace_ret_stack {
  330. unsigned long ret;
  331. unsigned long func;
  332. unsigned long long calltime;
  333. unsigned long long subtime;
  334. unsigned long fp;
  335. };
  336. /*
  337. * Primary handler of a function return.
  338. * It relays on ftrace_return_to_handler.
  339. * Defined in entry_32/64.S
  340. */
  341. extern void return_to_handler(void);
  342. extern int
  343. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  344. unsigned long frame_pointer);
  345. /*
  346. * Sometimes we don't want to trace a function with the function
  347. * graph tracer but we want them to keep traced by the usual function
  348. * tracer if the function graph tracer is not configured.
  349. */
  350. #define __notrace_funcgraph notrace
  351. /*
  352. * We want to which function is an entrypoint of a hardirq.
  353. * That will help us to put a signal on output.
  354. */
  355. #define __irq_entry __attribute__((__section__(".irqentry.text")))
  356. /* Limits of hardirq entrypoints */
  357. extern char __irqentry_text_start[];
  358. extern char __irqentry_text_end[];
  359. #define FTRACE_RETFUNC_DEPTH 50
  360. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  361. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  362. trace_func_graph_ent_t entryfunc);
  363. extern void ftrace_graph_stop(void);
  364. /* The current handlers in use */
  365. extern trace_func_graph_ret_t ftrace_graph_return;
  366. extern trace_func_graph_ent_t ftrace_graph_entry;
  367. extern void unregister_ftrace_graph(void);
  368. extern void ftrace_graph_init_task(struct task_struct *t);
  369. extern void ftrace_graph_exit_task(struct task_struct *t);
  370. extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
  371. static inline int task_curr_ret_stack(struct task_struct *t)
  372. {
  373. return t->curr_ret_stack;
  374. }
  375. static inline void pause_graph_tracing(void)
  376. {
  377. atomic_inc(&current->tracing_graph_pause);
  378. }
  379. static inline void unpause_graph_tracing(void)
  380. {
  381. atomic_dec(&current->tracing_graph_pause);
  382. }
  383. #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
  384. #define __notrace_funcgraph
  385. #define __irq_entry
  386. #define INIT_FTRACE_GRAPH
  387. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  388. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  389. static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
  390. static inline int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  391. trace_func_graph_ent_t entryfunc)
  392. {
  393. return -1;
  394. }
  395. static inline void unregister_ftrace_graph(void) { }
  396. static inline int task_curr_ret_stack(struct task_struct *tsk)
  397. {
  398. return -1;
  399. }
  400. static inline void pause_graph_tracing(void) { }
  401. static inline void unpause_graph_tracing(void) { }
  402. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  403. #ifdef CONFIG_TRACING
  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. enum ftrace_dump_mode;
  438. extern enum ftrace_dump_mode ftrace_dump_on_oops;
  439. #ifdef CONFIG_PREEMPT
  440. #define INIT_TRACE_RECURSION .trace_recursion = 0,
  441. #endif
  442. #endif /* CONFIG_TRACING */
  443. #ifndef INIT_TRACE_RECURSION
  444. #define INIT_TRACE_RECURSION
  445. #endif
  446. #ifdef CONFIG_FTRACE_SYSCALLS
  447. unsigned long arch_syscall_addr(int nr);
  448. #endif /* CONFIG_FTRACE_SYSCALLS */
  449. #endif /* _LINUX_FTRACE_H */