ftrace.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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. int ftrace_arch_code_modify_prepare(void);
  87. int ftrace_arch_code_modify_post_process(void);
  88. enum {
  89. FTRACE_FL_FREE = (1 << 0),
  90. FTRACE_FL_FAILED = (1 << 1),
  91. FTRACE_FL_FILTER = (1 << 2),
  92. FTRACE_FL_ENABLED = (1 << 3),
  93. FTRACE_FL_NOTRACE = (1 << 4),
  94. FTRACE_FL_CONVERTED = (1 << 5),
  95. FTRACE_FL_FROZEN = (1 << 6),
  96. };
  97. struct dyn_ftrace {
  98. struct list_head list;
  99. unsigned long ip; /* address of mcount call-site */
  100. unsigned long flags;
  101. struct dyn_arch_ftrace arch;
  102. };
  103. int ftrace_force_update(void);
  104. void ftrace_set_filter(unsigned char *buf, int len, int reset);
  105. /* defined in arch */
  106. extern int ftrace_ip_converted(unsigned long ip);
  107. extern int ftrace_dyn_arch_init(void *data);
  108. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  109. extern void ftrace_caller(void);
  110. extern void ftrace_call(void);
  111. extern void mcount_call(void);
  112. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  113. extern void ftrace_graph_caller(void);
  114. extern int ftrace_enable_ftrace_graph_caller(void);
  115. extern int ftrace_disable_ftrace_graph_caller(void);
  116. #else
  117. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  118. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  119. #endif
  120. /**
  121. * ftrace_make_nop - convert code into top
  122. * @mod: module structure if called by module load initialization
  123. * @rec: the mcount call site record
  124. * @addr: the address that the call site should be calling
  125. *
  126. * This is a very sensitive operation and great care needs
  127. * to be taken by the arch. The operation should carefully
  128. * read the location, check to see if what is read is indeed
  129. * what we expect it to be, and then on success of the compare,
  130. * it should write to the location.
  131. *
  132. * The code segment at @rec->ip should be a caller to @addr
  133. *
  134. * Return must be:
  135. * 0 on success
  136. * -EFAULT on error reading the location
  137. * -EINVAL on a failed compare of the contents
  138. * -EPERM on error writing to the location
  139. * Any other value will be considered a failure.
  140. */
  141. extern int ftrace_make_nop(struct module *mod,
  142. struct dyn_ftrace *rec, unsigned long addr);
  143. /**
  144. * ftrace_make_call - convert a nop call site into a call to addr
  145. * @rec: the mcount call site record
  146. * @addr: the address that the call site should call
  147. *
  148. * This is a very sensitive operation and great care needs
  149. * to be taken by the arch. The operation should carefully
  150. * read the location, check to see if what is read is indeed
  151. * what we expect it to be, and then on success of the compare,
  152. * it should write to the location.
  153. *
  154. * The code segment at @rec->ip should be a nop
  155. *
  156. * Return must be:
  157. * 0 on success
  158. * -EFAULT on error reading the location
  159. * -EINVAL on a failed compare of the contents
  160. * -EPERM on error writing to the location
  161. * Any other value will be considered a failure.
  162. */
  163. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  164. /* May be defined in arch */
  165. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  166. extern int skip_trace(unsigned long ip);
  167. extern void ftrace_release(void *start, unsigned long size);
  168. extern void ftrace_disable_daemon(void);
  169. extern void ftrace_enable_daemon(void);
  170. #else
  171. # define skip_trace(ip) ({ 0; })
  172. # define ftrace_force_update() ({ 0; })
  173. # define ftrace_set_filter(buf, len, reset) do { } while (0)
  174. # define ftrace_disable_daemon() do { } while (0)
  175. # define ftrace_enable_daemon() do { } while (0)
  176. static inline void ftrace_release(void *start, unsigned long size) { }
  177. #endif /* CONFIG_DYNAMIC_FTRACE */
  178. /* totally disable ftrace - can not re-enable after this */
  179. void ftrace_kill(void);
  180. static inline void tracer_disable(void)
  181. {
  182. #ifdef CONFIG_FUNCTION_TRACER
  183. ftrace_enabled = 0;
  184. #endif
  185. }
  186. /*
  187. * Ftrace disable/restore without lock. Some synchronization mechanism
  188. * must be used to prevent ftrace_enabled to be changed between
  189. * disable/restore.
  190. */
  191. static inline int __ftrace_enabled_save(void)
  192. {
  193. #ifdef CONFIG_FUNCTION_TRACER
  194. int saved_ftrace_enabled = ftrace_enabled;
  195. ftrace_enabled = 0;
  196. return saved_ftrace_enabled;
  197. #else
  198. return 0;
  199. #endif
  200. }
  201. static inline void __ftrace_enabled_restore(int enabled)
  202. {
  203. #ifdef CONFIG_FUNCTION_TRACER
  204. ftrace_enabled = enabled;
  205. #endif
  206. }
  207. #ifdef CONFIG_FRAME_POINTER
  208. /* TODO: need to fix this for ARM */
  209. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  210. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  211. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  212. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  213. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  214. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  215. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  216. #else
  217. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  218. # define CALLER_ADDR1 0UL
  219. # define CALLER_ADDR2 0UL
  220. # define CALLER_ADDR3 0UL
  221. # define CALLER_ADDR4 0UL
  222. # define CALLER_ADDR5 0UL
  223. # define CALLER_ADDR6 0UL
  224. #endif
  225. #ifdef CONFIG_IRQSOFF_TRACER
  226. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  227. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  228. #else
  229. # define time_hardirqs_on(a0, a1) do { } while (0)
  230. # define time_hardirqs_off(a0, a1) do { } while (0)
  231. #endif
  232. #ifdef CONFIG_PREEMPT_TRACER
  233. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  234. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  235. #else
  236. # define trace_preempt_on(a0, a1) do { } while (0)
  237. # define trace_preempt_off(a0, a1) do { } while (0)
  238. #endif
  239. #ifdef CONFIG_TRACING
  240. extern int ftrace_dump_on_oops;
  241. extern void tracing_start(void);
  242. extern void tracing_stop(void);
  243. extern void ftrace_off_permanent(void);
  244. extern void
  245. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
  246. /**
  247. * ftrace_printk - printf formatting in the ftrace buffer
  248. * @fmt: the printf format for printing
  249. *
  250. * Note: __ftrace_printk is an internal function for ftrace_printk and
  251. * the @ip is passed in via the ftrace_printk macro.
  252. *
  253. * This function allows a kernel developer to debug fast path sections
  254. * that printk is not appropriate for. By scattering in various
  255. * printk like tracing in the code, a developer can quickly see
  256. * where problems are occurring.
  257. *
  258. * This is intended as a debugging tool for the developer only.
  259. * Please refrain from leaving ftrace_printks scattered around in
  260. * your code.
  261. */
  262. # define ftrace_printk(fmt...) __ftrace_printk(_THIS_IP_, fmt)
  263. extern int
  264. __ftrace_printk(unsigned long ip, const char *fmt, ...)
  265. __attribute__ ((format (printf, 2, 3)));
  266. extern void ftrace_dump(void);
  267. #else
  268. static inline void
  269. ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3) { }
  270. static inline int
  271. ftrace_printk(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
  272. static inline void tracing_start(void) { }
  273. static inline void tracing_stop(void) { }
  274. static inline void ftrace_off_permanent(void) { }
  275. static inline int
  276. ftrace_printk(const char *fmt, ...)
  277. {
  278. return 0;
  279. }
  280. static inline void ftrace_dump(void) { }
  281. #endif
  282. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  283. extern void ftrace_init(void);
  284. extern void ftrace_init_module(struct module *mod,
  285. unsigned long *start, unsigned long *end);
  286. #else
  287. static inline void ftrace_init(void) { }
  288. static inline void
  289. ftrace_init_module(struct module *mod,
  290. unsigned long *start, unsigned long *end) { }
  291. #endif
  292. enum {
  293. POWER_NONE = 0,
  294. POWER_CSTATE = 1,
  295. POWER_PSTATE = 2,
  296. };
  297. struct power_trace {
  298. #ifdef CONFIG_POWER_TRACER
  299. ktime_t stamp;
  300. ktime_t end;
  301. int type;
  302. int state;
  303. #endif
  304. };
  305. #ifdef CONFIG_POWER_TRACER
  306. extern void trace_power_start(struct power_trace *it, unsigned int type,
  307. unsigned int state);
  308. extern void trace_power_mark(struct power_trace *it, unsigned int type,
  309. unsigned int state);
  310. extern void trace_power_end(struct power_trace *it);
  311. #else
  312. static inline void trace_power_start(struct power_trace *it, unsigned int type,
  313. unsigned int state) { }
  314. static inline void trace_power_mark(struct power_trace *it, unsigned int type,
  315. unsigned int state) { }
  316. static inline void trace_power_end(struct power_trace *it) { }
  317. #endif
  318. /*
  319. * Structure that defines an entry function trace.
  320. */
  321. struct ftrace_graph_ent {
  322. unsigned long func; /* Current function */
  323. int depth;
  324. };
  325. /*
  326. * Structure that defines a return function trace.
  327. */
  328. struct ftrace_graph_ret {
  329. unsigned long func; /* Current function */
  330. unsigned long long calltime;
  331. unsigned long long rettime;
  332. /* Number of functions that overran the depth limit for current task */
  333. unsigned long overrun;
  334. int depth;
  335. };
  336. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  337. /*
  338. * Sometimes we don't want to trace a function with the function
  339. * graph tracer but we want them to keep traced by the usual function
  340. * tracer if the function graph tracer is not configured.
  341. */
  342. #define __notrace_funcgraph notrace
  343. /*
  344. * We want to which function is an entrypoint of a hardirq.
  345. * That will help us to put a signal on output.
  346. */
  347. #define __irq_entry __attribute__((__section__(".irqentry.text")))
  348. /* Limits of hardirq entrypoints */
  349. extern char __irqentry_text_start[];
  350. extern char __irqentry_text_end[];
  351. #define FTRACE_RETFUNC_DEPTH 50
  352. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  353. /* Type of the callback handlers for tracing function graph*/
  354. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  355. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  356. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  357. trace_func_graph_ent_t entryfunc);
  358. extern void ftrace_graph_stop(void);
  359. /* The current handlers in use */
  360. extern trace_func_graph_ret_t ftrace_graph_return;
  361. extern trace_func_graph_ent_t ftrace_graph_entry;
  362. extern void unregister_ftrace_graph(void);
  363. extern void ftrace_graph_init_task(struct task_struct *t);
  364. extern void ftrace_graph_exit_task(struct task_struct *t);
  365. static inline int task_curr_ret_stack(struct task_struct *t)
  366. {
  367. return t->curr_ret_stack;
  368. }
  369. static inline void pause_graph_tracing(void)
  370. {
  371. atomic_inc(&current->tracing_graph_pause);
  372. }
  373. static inline void unpause_graph_tracing(void)
  374. {
  375. atomic_dec(&current->tracing_graph_pause);
  376. }
  377. #else
  378. #define __notrace_funcgraph
  379. #define __irq_entry
  380. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  381. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  382. static inline int task_curr_ret_stack(struct task_struct *tsk)
  383. {
  384. return -1;
  385. }
  386. static inline void pause_graph_tracing(void) { }
  387. static inline void unpause_graph_tracing(void) { }
  388. #endif
  389. #ifdef CONFIG_TRACING
  390. #include <linux/sched.h>
  391. /* flags for current->trace */
  392. enum {
  393. TSK_TRACE_FL_TRACE_BIT = 0,
  394. TSK_TRACE_FL_GRAPH_BIT = 1,
  395. };
  396. enum {
  397. TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
  398. TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
  399. };
  400. static inline void set_tsk_trace_trace(struct task_struct *tsk)
  401. {
  402. set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  403. }
  404. static inline void clear_tsk_trace_trace(struct task_struct *tsk)
  405. {
  406. clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  407. }
  408. static inline int test_tsk_trace_trace(struct task_struct *tsk)
  409. {
  410. return tsk->trace & TSK_TRACE_FL_TRACE;
  411. }
  412. static inline void set_tsk_trace_graph(struct task_struct *tsk)
  413. {
  414. set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  415. }
  416. static inline void clear_tsk_trace_graph(struct task_struct *tsk)
  417. {
  418. clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  419. }
  420. static inline int test_tsk_trace_graph(struct task_struct *tsk)
  421. {
  422. return tsk->trace & TSK_TRACE_FL_GRAPH;
  423. }
  424. #endif /* CONFIG_TRACING */
  425. #endif /* _LINUX_FTRACE_H */