ftrace.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543
  1. #ifndef _LINUX_FTRACE_H
  2. #define _LINUX_FTRACE_H
  3. #include <linux/trace_clock.h>
  4. #include <linux/kallsyms.h>
  5. #include <linux/linkage.h>
  6. #include <linux/bitops.h>
  7. #include <linux/module.h>
  8. #include <linux/ktime.h>
  9. #include <linux/sched.h>
  10. #include <linux/types.h>
  11. #include <linux/init.h>
  12. #include <linux/fs.h>
  13. #include <asm/ftrace.h>
  14. #ifdef CONFIG_FUNCTION_TRACER
  15. extern int ftrace_enabled;
  16. extern int
  17. ftrace_enable_sysctl(struct ctl_table *table, int write,
  18. struct file *filp, void __user *buffer, size_t *lenp,
  19. loff_t *ppos);
  20. typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip);
  21. struct ftrace_ops {
  22. ftrace_func_t func;
  23. struct ftrace_ops *next;
  24. };
  25. extern int function_trace_stop;
  26. /*
  27. * Type of the current tracing.
  28. */
  29. enum ftrace_tracing_type_t {
  30. FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
  31. FTRACE_TYPE_RETURN, /* Hook the return of the function */
  32. };
  33. /* Current tracing type, default is FTRACE_TYPE_ENTER */
  34. extern enum ftrace_tracing_type_t ftrace_tracing_type;
  35. /**
  36. * ftrace_stop - stop function tracer.
  37. *
  38. * A quick way to stop the function tracer. Note this an on off switch,
  39. * it is not something that is recursive like preempt_disable.
  40. * This does not disable the calling of mcount, it only stops the
  41. * calling of functions from mcount.
  42. */
  43. static inline void ftrace_stop(void)
  44. {
  45. function_trace_stop = 1;
  46. }
  47. /**
  48. * ftrace_start - start the function tracer.
  49. *
  50. * This function is the inverse of ftrace_stop. This does not enable
  51. * the function tracing if the function tracer is disabled. This only
  52. * sets the function tracer flag to continue calling the functions
  53. * from mcount.
  54. */
  55. static inline void ftrace_start(void)
  56. {
  57. function_trace_stop = 0;
  58. }
  59. /*
  60. * The ftrace_ops must be a static and should also
  61. * be read_mostly. These functions do modify read_mostly variables
  62. * so use them sparely. Never free an ftrace_op or modify the
  63. * next pointer after it has been registered. Even after unregistering
  64. * it, the next pointer may still be used internally.
  65. */
  66. int register_ftrace_function(struct ftrace_ops *ops);
  67. int unregister_ftrace_function(struct ftrace_ops *ops);
  68. void clear_ftrace_function(void);
  69. extern void ftrace_stub(unsigned long a0, unsigned long a1);
  70. #else /* !CONFIG_FUNCTION_TRACER */
  71. # define register_ftrace_function(ops) do { } while (0)
  72. # define unregister_ftrace_function(ops) do { } while (0)
  73. # define clear_ftrace_function(ops) do { } while (0)
  74. static inline void ftrace_kill(void) { }
  75. static inline void ftrace_stop(void) { }
  76. static inline void ftrace_start(void) { }
  77. #endif /* CONFIG_FUNCTION_TRACER */
  78. #ifdef CONFIG_STACK_TRACER
  79. extern int stack_tracer_enabled;
  80. int
  81. stack_trace_sysctl(struct ctl_table *table, int write,
  82. struct file *file, void __user *buffer, size_t *lenp,
  83. loff_t *ppos);
  84. #endif
  85. struct ftrace_func_command {
  86. struct list_head list;
  87. char *name;
  88. int (*func)(char *func, char *cmd,
  89. char *params, int enable);
  90. };
  91. #ifdef CONFIG_DYNAMIC_FTRACE
  92. int ftrace_arch_code_modify_prepare(void);
  93. int ftrace_arch_code_modify_post_process(void);
  94. struct seq_file;
  95. struct ftrace_probe_ops {
  96. void (*func)(unsigned long ip,
  97. unsigned long parent_ip,
  98. void **data);
  99. int (*callback)(unsigned long ip, void **data);
  100. void (*free)(void **data);
  101. int (*print)(struct seq_file *m,
  102. unsigned long ip,
  103. struct ftrace_probe_ops *ops,
  104. void *data);
  105. };
  106. extern int
  107. register_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  108. void *data);
  109. extern void
  110. unregister_ftrace_function_probe(char *glob, struct ftrace_probe_ops *ops,
  111. void *data);
  112. extern void
  113. unregister_ftrace_function_probe_func(char *glob, struct ftrace_probe_ops *ops);
  114. extern void unregister_ftrace_function_probe_all(char *glob);
  115. enum {
  116. FTRACE_FL_FREE = (1 << 0),
  117. FTRACE_FL_FAILED = (1 << 1),
  118. FTRACE_FL_FILTER = (1 << 2),
  119. FTRACE_FL_ENABLED = (1 << 3),
  120. FTRACE_FL_NOTRACE = (1 << 4),
  121. FTRACE_FL_CONVERTED = (1 << 5),
  122. FTRACE_FL_FROZEN = (1 << 6),
  123. };
  124. struct dyn_ftrace {
  125. union {
  126. unsigned long ip; /* address of mcount call-site */
  127. struct dyn_ftrace *freelist;
  128. };
  129. union {
  130. unsigned long flags;
  131. struct dyn_ftrace *newlist;
  132. };
  133. struct dyn_arch_ftrace arch;
  134. };
  135. int ftrace_force_update(void);
  136. void ftrace_set_filter(unsigned char *buf, int len, int reset);
  137. int register_ftrace_command(struct ftrace_func_command *cmd);
  138. int unregister_ftrace_command(struct ftrace_func_command *cmd);
  139. /* defined in arch */
  140. extern int ftrace_ip_converted(unsigned long ip);
  141. extern int ftrace_dyn_arch_init(void *data);
  142. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  143. extern void ftrace_caller(void);
  144. extern void ftrace_call(void);
  145. extern void mcount_call(void);
  146. #ifndef FTRACE_ADDR
  147. #define FTRACE_ADDR ((unsigned long)ftrace_caller)
  148. #endif
  149. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  150. extern void ftrace_graph_caller(void);
  151. extern int ftrace_enable_ftrace_graph_caller(void);
  152. extern int ftrace_disable_ftrace_graph_caller(void);
  153. #else
  154. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  155. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  156. #endif
  157. /**
  158. * ftrace_make_nop - convert code into nop
  159. * @mod: module structure if called by module load initialization
  160. * @rec: the mcount call site record
  161. * @addr: the address that the call site should be calling
  162. *
  163. * This is a very sensitive operation and great care needs
  164. * to be taken by the arch. The operation should carefully
  165. * read the location, check to see if what is read is indeed
  166. * what we expect it to be, and then on success of the compare,
  167. * it should write to the location.
  168. *
  169. * The code segment at @rec->ip should be a caller to @addr
  170. *
  171. * Return must be:
  172. * 0 on success
  173. * -EFAULT on error reading the location
  174. * -EINVAL on a failed compare of the contents
  175. * -EPERM on error writing to the location
  176. * Any other value will be considered a failure.
  177. */
  178. extern int ftrace_make_nop(struct module *mod,
  179. struct dyn_ftrace *rec, unsigned long addr);
  180. /**
  181. * ftrace_make_call - convert a nop call site into a call to addr
  182. * @rec: the mcount call site record
  183. * @addr: the address that the call site should call
  184. *
  185. * This is a very sensitive operation and great care needs
  186. * to be taken by the arch. The operation should carefully
  187. * read the location, check to see if what is read is indeed
  188. * what we expect it to be, and then on success of the compare,
  189. * it should write to the location.
  190. *
  191. * The code segment at @rec->ip should be a nop
  192. *
  193. * Return must be:
  194. * 0 on success
  195. * -EFAULT on error reading the location
  196. * -EINVAL on a failed compare of the contents
  197. * -EPERM on error writing to the location
  198. * Any other value will be considered a failure.
  199. */
  200. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  201. /* May be defined in arch */
  202. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  203. extern int skip_trace(unsigned long ip);
  204. extern void ftrace_release(void *start, unsigned long size);
  205. extern void ftrace_disable_daemon(void);
  206. extern void ftrace_enable_daemon(void);
  207. #else
  208. # define skip_trace(ip) ({ 0; })
  209. # define ftrace_force_update() ({ 0; })
  210. # define ftrace_set_filter(buf, len, reset) do { } while (0)
  211. # define ftrace_disable_daemon() do { } while (0)
  212. # define ftrace_enable_daemon() do { } while (0)
  213. static inline void ftrace_release(void *start, unsigned long size) { }
  214. static inline int register_ftrace_command(struct ftrace_func_command *cmd)
  215. {
  216. return -EINVAL;
  217. }
  218. static inline int unregister_ftrace_command(char *cmd_name)
  219. {
  220. return -EINVAL;
  221. }
  222. #endif /* CONFIG_DYNAMIC_FTRACE */
  223. /* totally disable ftrace - can not re-enable after this */
  224. void ftrace_kill(void);
  225. static inline void tracer_disable(void)
  226. {
  227. #ifdef CONFIG_FUNCTION_TRACER
  228. ftrace_enabled = 0;
  229. #endif
  230. }
  231. /*
  232. * Ftrace disable/restore without lock. Some synchronization mechanism
  233. * must be used to prevent ftrace_enabled to be changed between
  234. * disable/restore.
  235. */
  236. static inline int __ftrace_enabled_save(void)
  237. {
  238. #ifdef CONFIG_FUNCTION_TRACER
  239. int saved_ftrace_enabled = ftrace_enabled;
  240. ftrace_enabled = 0;
  241. return saved_ftrace_enabled;
  242. #else
  243. return 0;
  244. #endif
  245. }
  246. static inline void __ftrace_enabled_restore(int enabled)
  247. {
  248. #ifdef CONFIG_FUNCTION_TRACER
  249. ftrace_enabled = enabled;
  250. #endif
  251. }
  252. #ifndef HAVE_ARCH_CALLER_ADDR
  253. # ifdef CONFIG_FRAME_POINTER
  254. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  255. # define CALLER_ADDR1 ((unsigned long)__builtin_return_address(1))
  256. # define CALLER_ADDR2 ((unsigned long)__builtin_return_address(2))
  257. # define CALLER_ADDR3 ((unsigned long)__builtin_return_address(3))
  258. # define CALLER_ADDR4 ((unsigned long)__builtin_return_address(4))
  259. # define CALLER_ADDR5 ((unsigned long)__builtin_return_address(5))
  260. # define CALLER_ADDR6 ((unsigned long)__builtin_return_address(6))
  261. # else
  262. # define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0))
  263. # define CALLER_ADDR1 0UL
  264. # define CALLER_ADDR2 0UL
  265. # define CALLER_ADDR3 0UL
  266. # define CALLER_ADDR4 0UL
  267. # define CALLER_ADDR5 0UL
  268. # define CALLER_ADDR6 0UL
  269. # endif
  270. #endif /* ifndef HAVE_ARCH_CALLER_ADDR */
  271. #ifdef CONFIG_IRQSOFF_TRACER
  272. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  273. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  274. #else
  275. # define time_hardirqs_on(a0, a1) do { } while (0)
  276. # define time_hardirqs_off(a0, a1) do { } while (0)
  277. #endif
  278. #ifdef CONFIG_PREEMPT_TRACER
  279. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  280. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  281. #else
  282. # define trace_preempt_on(a0, a1) do { } while (0)
  283. # define trace_preempt_off(a0, a1) do { } while (0)
  284. #endif
  285. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  286. extern void ftrace_init(void);
  287. extern void ftrace_init_module(struct module *mod,
  288. unsigned long *start, unsigned long *end);
  289. #else
  290. static inline void ftrace_init(void) { }
  291. static inline void
  292. ftrace_init_module(struct module *mod,
  293. unsigned long *start, unsigned long *end) { }
  294. #endif
  295. /*
  296. * Structure that defines an entry function trace.
  297. */
  298. struct ftrace_graph_ent {
  299. unsigned long func; /* Current function */
  300. int depth;
  301. };
  302. /*
  303. * Structure that defines a return function trace.
  304. */
  305. struct ftrace_graph_ret {
  306. unsigned long func; /* Current function */
  307. unsigned long long calltime;
  308. unsigned long long rettime;
  309. /* Number of functions that overran the depth limit for current task */
  310. unsigned long overrun;
  311. int depth;
  312. };
  313. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  314. /* for init task */
  315. #define INIT_FTRACE_GRAPH .ret_stack = NULL
  316. /*
  317. * Stack of return addresses for functions
  318. * of a thread.
  319. * Used in struct thread_info
  320. */
  321. struct ftrace_ret_stack {
  322. unsigned long ret;
  323. unsigned long func;
  324. unsigned long long calltime;
  325. };
  326. /*
  327. * Primary handler of a function return.
  328. * It relays on ftrace_return_to_handler.
  329. * Defined in entry_32/64.S
  330. */
  331. extern void return_to_handler(void);
  332. extern int
  333. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth);
  334. extern void
  335. ftrace_pop_return_trace(struct ftrace_graph_ret *trace, unsigned long *ret);
  336. /*
  337. * Sometimes we don't want to trace a function with the function
  338. * graph tracer but we want them to keep traced by the usual function
  339. * tracer if the function graph tracer is not configured.
  340. */
  341. #define __notrace_funcgraph notrace
  342. /*
  343. * We want to which function is an entrypoint of a hardirq.
  344. * That will help us to put a signal on output.
  345. */
  346. #define __irq_entry __attribute__((__section__(".irqentry.text")))
  347. /* Limits of hardirq entrypoints */
  348. extern char __irqentry_text_start[];
  349. extern char __irqentry_text_end[];
  350. #define FTRACE_RETFUNC_DEPTH 50
  351. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  352. /* Type of the callback handlers for tracing function graph*/
  353. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  354. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  355. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  356. trace_func_graph_ent_t entryfunc);
  357. extern void ftrace_graph_stop(void);
  358. /* The current handlers in use */
  359. extern trace_func_graph_ret_t ftrace_graph_return;
  360. extern trace_func_graph_ent_t ftrace_graph_entry;
  361. extern void unregister_ftrace_graph(void);
  362. extern void ftrace_graph_init_task(struct task_struct *t);
  363. extern void ftrace_graph_exit_task(struct task_struct *t);
  364. static inline int task_curr_ret_stack(struct task_struct *t)
  365. {
  366. return t->curr_ret_stack;
  367. }
  368. static inline void pause_graph_tracing(void)
  369. {
  370. atomic_inc(&current->tracing_graph_pause);
  371. }
  372. static inline void unpause_graph_tracing(void)
  373. {
  374. atomic_dec(&current->tracing_graph_pause);
  375. }
  376. #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
  377. #define __notrace_funcgraph
  378. #define __irq_entry
  379. #define INIT_FTRACE_GRAPH
  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 /* CONFIG_FUNCTION_GRAPH_TRACER */
  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. extern int ftrace_dump_on_oops;
  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. /*
  434. * A syscall entry in the ftrace syscalls array.
  435. *
  436. * @name: name of the syscall
  437. * @nb_args: number of parameters it takes
  438. * @types: list of types as strings
  439. * @args: list of args as strings (args[i] matches types[i])
  440. */
  441. struct syscall_metadata {
  442. const char *name;
  443. int nb_args;
  444. const char **types;
  445. const char **args;
  446. };
  447. #ifdef CONFIG_FTRACE_SYSCALLS
  448. extern void arch_init_ftrace_syscalls(void);
  449. extern struct syscall_metadata *syscall_nr_to_meta(int nr);
  450. extern void start_ftrace_syscalls(void);
  451. extern void stop_ftrace_syscalls(void);
  452. extern void ftrace_syscall_enter(struct pt_regs *regs);
  453. extern void ftrace_syscall_exit(struct pt_regs *regs);
  454. #else
  455. static inline void start_ftrace_syscalls(void) { }
  456. static inline void stop_ftrace_syscalls(void) { }
  457. static inline void ftrace_syscall_enter(struct pt_regs *regs) { }
  458. static inline void ftrace_syscall_exit(struct pt_regs *regs) { }
  459. #endif
  460. #endif /* _LINUX_FTRACE_H */