ftrace.h 15 KB

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