ftrace.h 15 KB

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