ftrace.h 15 KB

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