ftrace.h 14 KB

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