ftrace.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  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 top
  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. extern void ftrace_dump(void);
  268. #else
  269. static inline void
  270. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  271. static inline int
  272. ftrace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  273. static inline void tracing_start(void) { }
  274. static inline void tracing_stop(void) { }
  275. static inline void ftrace_off_permanent(void) { }
  276. static inline int
  277. ftrace_printk(const char *fmt, ...)
  278. {
  279. return 0;
  280. }
  281. static inline void ftrace_dump(void) { }
  282. #endif
  283. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  284. extern void ftrace_init(void);
  285. extern void ftrace_init_module(struct module *mod,
  286. unsigned long *start, unsigned long *end);
  287. #else
  288. static inline void ftrace_init(void) { }
  289. static inline void
  290. ftrace_init_module(struct module *mod,
  291. unsigned long *start, unsigned long *end) { }
  292. #endif
  293. enum {
  294. POWER_NONE = 0,
  295. POWER_CSTATE = 1,
  296. POWER_PSTATE = 2,
  297. };
  298. struct power_trace {
  299. #ifdef CONFIG_POWER_TRACER
  300. ktime_t stamp;
  301. ktime_t end;
  302. int type;
  303. int state;
  304. #endif
  305. };
  306. #ifdef CONFIG_POWER_TRACER
  307. extern void trace_power_start(struct power_trace *it, unsigned int type,
  308. unsigned int state);
  309. extern void trace_power_mark(struct power_trace *it, unsigned int type,
  310. unsigned int state);
  311. extern void trace_power_end(struct power_trace *it);
  312. #else
  313. static inline void trace_power_start(struct power_trace *it, unsigned int type,
  314. unsigned int state) { }
  315. static inline void trace_power_mark(struct power_trace *it, unsigned int type,
  316. unsigned int state) { }
  317. static inline void trace_power_end(struct power_trace *it) { }
  318. #endif
  319. /*
  320. * Structure that defines an entry function trace.
  321. */
  322. struct ftrace_graph_ent {
  323. unsigned long func; /* Current function */
  324. int depth;
  325. };
  326. /*
  327. * Structure that defines a return function trace.
  328. */
  329. struct ftrace_graph_ret {
  330. unsigned long func; /* Current function */
  331. unsigned long long calltime;
  332. unsigned long long rettime;
  333. /* Number of functions that overran the depth limit for current task */
  334. unsigned long overrun;
  335. int depth;
  336. };
  337. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  338. /*
  339. * Sometimes we don't want to trace a function with the function
  340. * graph tracer but we want them to keep traced by the usual function
  341. * tracer if the function graph tracer is not configured.
  342. */
  343. #define __notrace_funcgraph notrace
  344. /*
  345. * We want to which function is an entrypoint of a hardirq.
  346. * That will help us to put a signal on output.
  347. */
  348. #define __irq_entry __attribute__((__section__(".irqentry.text")))
  349. /* Limits of hardirq entrypoints */
  350. extern char __irqentry_text_start[];
  351. extern char __irqentry_text_end[];
  352. #define FTRACE_RETFUNC_DEPTH 50
  353. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  354. /* Type of the callback handlers for tracing function graph*/
  355. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  356. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  357. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  358. trace_func_graph_ent_t entryfunc);
  359. extern void ftrace_graph_stop(void);
  360. /* The current handlers in use */
  361. extern trace_func_graph_ret_t ftrace_graph_return;
  362. extern trace_func_graph_ent_t ftrace_graph_entry;
  363. extern void unregister_ftrace_graph(void);
  364. extern void ftrace_graph_init_task(struct task_struct *t);
  365. extern void ftrace_graph_exit_task(struct task_struct *t);
  366. static inline int task_curr_ret_stack(struct task_struct *t)
  367. {
  368. return t->curr_ret_stack;
  369. }
  370. static inline void pause_graph_tracing(void)
  371. {
  372. atomic_inc(&current->tracing_graph_pause);
  373. }
  374. static inline void unpause_graph_tracing(void)
  375. {
  376. atomic_dec(&current->tracing_graph_pause);
  377. }
  378. #else
  379. #define __notrace_funcgraph
  380. #define __irq_entry
  381. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  382. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  383. static inline int task_curr_ret_stack(struct task_struct *tsk)
  384. {
  385. return -1;
  386. }
  387. static inline void pause_graph_tracing(void) { }
  388. static inline void unpause_graph_tracing(void) { }
  389. #endif
  390. #ifdef CONFIG_TRACING
  391. #include <linux/sched.h>
  392. /* flags for current->trace */
  393. enum {
  394. TSK_TRACE_FL_TRACE_BIT = 0,
  395. TSK_TRACE_FL_GRAPH_BIT = 1,
  396. };
  397. enum {
  398. TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
  399. TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
  400. };
  401. static inline void set_tsk_trace_trace(struct task_struct *tsk)
  402. {
  403. set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  404. }
  405. static inline void clear_tsk_trace_trace(struct task_struct *tsk)
  406. {
  407. clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  408. }
  409. static inline int test_tsk_trace_trace(struct task_struct *tsk)
  410. {
  411. return tsk->trace & TSK_TRACE_FL_TRACE;
  412. }
  413. static inline void set_tsk_trace_graph(struct task_struct *tsk)
  414. {
  415. set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  416. }
  417. static inline void clear_tsk_trace_graph(struct task_struct *tsk)
  418. {
  419. clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  420. }
  421. static inline int test_tsk_trace_graph(struct task_struct *tsk)
  422. {
  423. return tsk->trace & TSK_TRACE_FL_GRAPH;
  424. }
  425. #endif /* CONFIG_TRACING */
  426. #ifdef CONFIG_HW_BRANCH_TRACER
  427. void trace_hw_branch(u64 from, u64 to);
  428. void trace_hw_branch_oops(void);
  429. #else /* CONFIG_HW_BRANCH_TRACER */
  430. static inline void trace_hw_branch(u64 from, u64 to) {}
  431. static inline void trace_hw_branch_oops(void) {}
  432. #endif /* CONFIG_HW_BRANCH_TRACER */
  433. #endif /* _LINUX_FTRACE_H */