ftrace.h 14 KB

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