ftrace.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970
  1. /*
  2. * Ftrace header. For implementation details beyond the random comments
  3. * scattered below, see: Documentation/trace/ftrace-design.txt
  4. */
  5. #ifndef _LINUX_FTRACE_H
  6. #define _LINUX_FTRACE_H
  7. #include <linux/trace_clock.h>
  8. #include <linux/kallsyms.h>
  9. #include <linux/linkage.h>
  10. #include <linux/bitops.h>
  11. #include <linux/ptrace.h>
  12. #include <linux/ktime.h>
  13. #include <linux/sched.h>
  14. #include <linux/types.h>
  15. #include <linux/init.h>
  16. #include <linux/fs.h>
  17. #include <asm/ftrace.h>
  18. /*
  19. * If the arch supports passing the variable contents of
  20. * function_trace_op as the third parameter back from the
  21. * mcount call, then the arch should define this as 1.
  22. */
  23. #ifndef ARCH_SUPPORTS_FTRACE_OPS
  24. #define ARCH_SUPPORTS_FTRACE_OPS 0
  25. #endif
  26. /*
  27. * If the arch's mcount caller does not support all of ftrace's
  28. * features, then it must call an indirect function that
  29. * does. Or at least does enough to prevent any unwelcomed side effects.
  30. */
  31. #if !ARCH_SUPPORTS_FTRACE_OPS
  32. # define FTRACE_FORCE_LIST_FUNC 1
  33. #else
  34. # define FTRACE_FORCE_LIST_FUNC 0
  35. #endif
  36. /* Main tracing buffer and events set up */
  37. #ifdef CONFIG_TRACING
  38. void trace_init(void);
  39. void early_trace_init(void);
  40. #else
  41. static inline void trace_init(void) { }
  42. static inline void early_trace_init(void) { }
  43. #endif
  44. struct module;
  45. struct ftrace_hash;
  46. #ifdef CONFIG_FUNCTION_TRACER
  47. extern int ftrace_enabled;
  48. extern int
  49. ftrace_enable_sysctl(struct ctl_table *table, int write,
  50. void __user *buffer, size_t *lenp,
  51. loff_t *ppos);
  52. struct ftrace_ops;
  53. typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
  54. struct ftrace_ops *op, struct pt_regs *regs);
  55. ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
  56. /*
  57. * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
  58. * set in the flags member.
  59. * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION_SAFE, STUB and
  60. * IPMODIFY are a kind of attribute flags which can be set only before
  61. * registering the ftrace_ops, and can not be modified while registered.
  62. * Changing those attribute flags after registering ftrace_ops will
  63. * cause unexpected results.
  64. *
  65. * ENABLED - set/unset when ftrace_ops is registered/unregistered
  66. * DYNAMIC - set when ftrace_ops is registered to denote dynamically
  67. * allocated ftrace_ops which need special care
  68. * PER_CPU - set manualy by ftrace_ops user to denote the ftrace_ops
  69. * could be controlled by following calls:
  70. * ftrace_function_local_enable
  71. * ftrace_function_local_disable
  72. * SAVE_REGS - The ftrace_ops wants regs saved at each function called
  73. * and passed to the callback. If this flag is set, but the
  74. * architecture does not support passing regs
  75. * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
  76. * ftrace_ops will fail to register, unless the next flag
  77. * is set.
  78. * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
  79. * handler can handle an arch that does not save regs
  80. * (the handler tests if regs == NULL), then it can set
  81. * this flag instead. It will not fail registering the ftrace_ops
  82. * but, the regs field will be NULL if the arch does not support
  83. * passing regs to the handler.
  84. * Note, if this flag is set, the SAVE_REGS flag will automatically
  85. * get set upon registering the ftrace_ops, if the arch supports it.
  86. * RECURSION_SAFE - The ftrace_ops can set this to tell the ftrace infrastructure
  87. * that the call back has its own recursion protection. If it does
  88. * not set this, then the ftrace infrastructure will add recursion
  89. * protection for the caller.
  90. * STUB - The ftrace_ops is just a place holder.
  91. * INITIALIZED - The ftrace_ops has already been initialized (first use time
  92. * register_ftrace_function() is called, it will initialized the ops)
  93. * DELETED - The ops are being deleted, do not let them be registered again.
  94. * ADDING - The ops is in the process of being added.
  95. * REMOVING - The ops is in the process of being removed.
  96. * MODIFYING - The ops is in the process of changing its filter functions.
  97. * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
  98. * The arch specific code sets this flag when it allocated a
  99. * trampoline. This lets the arch know that it can update the
  100. * trampoline in case the callback function changes.
  101. * The ftrace_ops trampoline can be set by the ftrace users, and
  102. * in such cases the arch must not modify it. Only the arch ftrace
  103. * core code should set this flag.
  104. * IPMODIFY - The ops can modify the IP register. This can only be set with
  105. * SAVE_REGS. If another ops with this flag set is already registered
  106. * for any of the functions that this ops will be registered for, then
  107. * this ops will fail to register or set_filter_ip.
  108. * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
  109. */
  110. enum {
  111. FTRACE_OPS_FL_ENABLED = 1 << 0,
  112. FTRACE_OPS_FL_DYNAMIC = 1 << 1,
  113. FTRACE_OPS_FL_PER_CPU = 1 << 2,
  114. FTRACE_OPS_FL_SAVE_REGS = 1 << 3,
  115. FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = 1 << 4,
  116. FTRACE_OPS_FL_RECURSION_SAFE = 1 << 5,
  117. FTRACE_OPS_FL_STUB = 1 << 6,
  118. FTRACE_OPS_FL_INITIALIZED = 1 << 7,
  119. FTRACE_OPS_FL_DELETED = 1 << 8,
  120. FTRACE_OPS_FL_ADDING = 1 << 9,
  121. FTRACE_OPS_FL_REMOVING = 1 << 10,
  122. FTRACE_OPS_FL_MODIFYING = 1 << 11,
  123. FTRACE_OPS_FL_ALLOC_TRAMP = 1 << 12,
  124. FTRACE_OPS_FL_IPMODIFY = 1 << 13,
  125. FTRACE_OPS_FL_PID = 1 << 14,
  126. FTRACE_OPS_FL_RCU = 1 << 15,
  127. };
  128. #ifdef CONFIG_DYNAMIC_FTRACE
  129. /* The hash used to know what functions callbacks trace */
  130. struct ftrace_ops_hash {
  131. struct ftrace_hash *notrace_hash;
  132. struct ftrace_hash *filter_hash;
  133. struct mutex regex_lock;
  134. };
  135. void ftrace_free_init_mem(void);
  136. #else
  137. static inline void ftrace_free_init_mem(void) { }
  138. #endif
  139. /*
  140. * Note, ftrace_ops can be referenced outside of RCU protection, unless
  141. * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
  142. * core data, the unregistering of it will perform a scheduling on all CPUs
  143. * to make sure that there are no more users. Depending on the load of the
  144. * system that may take a bit of time.
  145. *
  146. * Any private data added must also take care not to be freed and if private
  147. * data is added to a ftrace_ops that is in core code, the user of the
  148. * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
  149. */
  150. struct ftrace_ops {
  151. ftrace_func_t func;
  152. struct ftrace_ops *next;
  153. unsigned long flags;
  154. void *private;
  155. ftrace_func_t saved_func;
  156. int __percpu *disabled;
  157. #ifdef CONFIG_DYNAMIC_FTRACE
  158. struct ftrace_ops_hash local_hash;
  159. struct ftrace_ops_hash *func_hash;
  160. struct ftrace_ops_hash old_hash;
  161. unsigned long trampoline;
  162. unsigned long trampoline_size;
  163. #endif
  164. };
  165. /*
  166. * Type of the current tracing.
  167. */
  168. enum ftrace_tracing_type_t {
  169. FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
  170. FTRACE_TYPE_RETURN, /* Hook the return of the function */
  171. };
  172. /* Current tracing type, default is FTRACE_TYPE_ENTER */
  173. extern enum ftrace_tracing_type_t ftrace_tracing_type;
  174. /*
  175. * The ftrace_ops must be a static and should also
  176. * be read_mostly. These functions do modify read_mostly variables
  177. * so use them sparely. Never free an ftrace_op or modify the
  178. * next pointer after it has been registered. Even after unregistering
  179. * it, the next pointer may still be used internally.
  180. */
  181. int register_ftrace_function(struct ftrace_ops *ops);
  182. int unregister_ftrace_function(struct ftrace_ops *ops);
  183. void clear_ftrace_function(void);
  184. /**
  185. * ftrace_function_local_enable - enable ftrace_ops on current cpu
  186. *
  187. * This function enables tracing on current cpu by decreasing
  188. * the per cpu control variable.
  189. * It must be called with preemption disabled and only on ftrace_ops
  190. * registered with FTRACE_OPS_FL_PER_CPU. If called without preemption
  191. * disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
  192. */
  193. static inline void ftrace_function_local_enable(struct ftrace_ops *ops)
  194. {
  195. if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
  196. return;
  197. (*this_cpu_ptr(ops->disabled))--;
  198. }
  199. /**
  200. * ftrace_function_local_disable - disable ftrace_ops on current cpu
  201. *
  202. * This function disables tracing on current cpu by increasing
  203. * the per cpu control variable.
  204. * It must be called with preemption disabled and only on ftrace_ops
  205. * registered with FTRACE_OPS_FL_PER_CPU. If called without preemption
  206. * disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
  207. */
  208. static inline void ftrace_function_local_disable(struct ftrace_ops *ops)
  209. {
  210. if (WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU)))
  211. return;
  212. (*this_cpu_ptr(ops->disabled))++;
  213. }
  214. /**
  215. * ftrace_function_local_disabled - returns ftrace_ops disabled value
  216. * on current cpu
  217. *
  218. * This function returns value of ftrace_ops::disabled on current cpu.
  219. * It must be called with preemption disabled and only on ftrace_ops
  220. * registered with FTRACE_OPS_FL_PER_CPU. If called without preemption
  221. * disabled, this_cpu_ptr will complain when CONFIG_DEBUG_PREEMPT is enabled.
  222. */
  223. static inline int ftrace_function_local_disabled(struct ftrace_ops *ops)
  224. {
  225. WARN_ON_ONCE(!(ops->flags & FTRACE_OPS_FL_PER_CPU));
  226. return *this_cpu_ptr(ops->disabled);
  227. }
  228. extern void ftrace_stub(unsigned long a0, unsigned long a1,
  229. struct ftrace_ops *op, struct pt_regs *regs);
  230. #else /* !CONFIG_FUNCTION_TRACER */
  231. /*
  232. * (un)register_ftrace_function must be a macro since the ops parameter
  233. * must not be evaluated.
  234. */
  235. #define register_ftrace_function(ops) ({ 0; })
  236. #define unregister_ftrace_function(ops) ({ 0; })
  237. static inline int ftrace_nr_registered_ops(void)
  238. {
  239. return 0;
  240. }
  241. static inline void clear_ftrace_function(void) { }
  242. static inline void ftrace_kill(void) { }
  243. static inline void ftrace_free_init_mem(void) { }
  244. #endif /* CONFIG_FUNCTION_TRACER */
  245. #ifdef CONFIG_STACK_TRACER
  246. #define STACK_TRACE_ENTRIES 500
  247. struct stack_trace;
  248. extern unsigned stack_trace_index[];
  249. extern struct stack_trace stack_trace_max;
  250. extern unsigned long stack_trace_max_size;
  251. extern arch_spinlock_t stack_trace_max_lock;
  252. extern int stack_tracer_enabled;
  253. void stack_trace_print(void);
  254. int
  255. stack_trace_sysctl(struct ctl_table *table, int write,
  256. void __user *buffer, size_t *lenp,
  257. loff_t *ppos);
  258. /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
  259. DECLARE_PER_CPU(int, disable_stack_tracer);
  260. /**
  261. * stack_tracer_disable - temporarily disable the stack tracer
  262. *
  263. * There's a few locations (namely in RCU) where stack tracing
  264. * cannot be executed. This function is used to disable stack
  265. * tracing during those critical sections.
  266. *
  267. * This function must be called with preemption or interrupts
  268. * disabled and stack_tracer_enable() must be called shortly after
  269. * while preemption or interrupts are still disabled.
  270. */
  271. static inline void stack_tracer_disable(void)
  272. {
  273. /* Preemption or interupts must be disabled */
  274. if (IS_ENABLED(CONFIG_PREEMPT_DEBUG))
  275. WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
  276. this_cpu_inc(disable_stack_tracer);
  277. }
  278. /**
  279. * stack_tracer_enable - re-enable the stack tracer
  280. *
  281. * After stack_tracer_disable() is called, stack_tracer_enable()
  282. * must be called shortly afterward.
  283. */
  284. static inline void stack_tracer_enable(void)
  285. {
  286. if (IS_ENABLED(CONFIG_PREEMPT_DEBUG))
  287. WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
  288. this_cpu_dec(disable_stack_tracer);
  289. }
  290. #else
  291. static inline void stack_tracer_disable(void) { }
  292. static inline void stack_tracer_enable(void) { }
  293. #endif
  294. #ifdef CONFIG_DYNAMIC_FTRACE
  295. int ftrace_arch_code_modify_prepare(void);
  296. int ftrace_arch_code_modify_post_process(void);
  297. struct dyn_ftrace;
  298. enum ftrace_bug_type {
  299. FTRACE_BUG_UNKNOWN,
  300. FTRACE_BUG_INIT,
  301. FTRACE_BUG_NOP,
  302. FTRACE_BUG_CALL,
  303. FTRACE_BUG_UPDATE,
  304. };
  305. extern enum ftrace_bug_type ftrace_bug_type;
  306. /*
  307. * Archs can set this to point to a variable that holds the value that was
  308. * expected at the call site before calling ftrace_bug().
  309. */
  310. extern const void *ftrace_expected;
  311. void ftrace_bug(int err, struct dyn_ftrace *rec);
  312. struct seq_file;
  313. extern int ftrace_text_reserved(const void *start, const void *end);
  314. extern int ftrace_nr_registered_ops(void);
  315. bool is_ftrace_trampoline(unsigned long addr);
  316. /*
  317. * The dyn_ftrace record's flags field is split into two parts.
  318. * the first part which is '0-FTRACE_REF_MAX' is a counter of
  319. * the number of callbacks that have registered the function that
  320. * the dyn_ftrace descriptor represents.
  321. *
  322. * The second part is a mask:
  323. * ENABLED - the function is being traced
  324. * REGS - the record wants the function to save regs
  325. * REGS_EN - the function is set up to save regs.
  326. * IPMODIFY - the record allows for the IP address to be changed.
  327. * DISABLED - the record is not ready to be touched yet
  328. *
  329. * When a new ftrace_ops is registered and wants a function to save
  330. * pt_regs, the rec->flag REGS is set. When the function has been
  331. * set up to save regs, the REG_EN flag is set. Once a function
  332. * starts saving regs it will do so until all ftrace_ops are removed
  333. * from tracing that function.
  334. */
  335. enum {
  336. FTRACE_FL_ENABLED = (1UL << 31),
  337. FTRACE_FL_REGS = (1UL << 30),
  338. FTRACE_FL_REGS_EN = (1UL << 29),
  339. FTRACE_FL_TRAMP = (1UL << 28),
  340. FTRACE_FL_TRAMP_EN = (1UL << 27),
  341. FTRACE_FL_IPMODIFY = (1UL << 26),
  342. FTRACE_FL_DISABLED = (1UL << 25),
  343. };
  344. #define FTRACE_REF_MAX_SHIFT 25
  345. #define FTRACE_FL_BITS 7
  346. #define FTRACE_FL_MASKED_BITS ((1UL << FTRACE_FL_BITS) - 1)
  347. #define FTRACE_FL_MASK (FTRACE_FL_MASKED_BITS << FTRACE_REF_MAX_SHIFT)
  348. #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
  349. #define ftrace_rec_count(rec) ((rec)->flags & ~FTRACE_FL_MASK)
  350. struct dyn_ftrace {
  351. unsigned long ip; /* address of mcount call-site */
  352. unsigned long flags;
  353. struct dyn_arch_ftrace arch;
  354. };
  355. int ftrace_force_update(void);
  356. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  357. int remove, int reset);
  358. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  359. int len, int reset);
  360. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  361. int len, int reset);
  362. void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
  363. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
  364. void ftrace_free_filter(struct ftrace_ops *ops);
  365. void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
  366. enum {
  367. FTRACE_UPDATE_CALLS = (1 << 0),
  368. FTRACE_DISABLE_CALLS = (1 << 1),
  369. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  370. FTRACE_START_FUNC_RET = (1 << 3),
  371. FTRACE_STOP_FUNC_RET = (1 << 4),
  372. };
  373. /*
  374. * The FTRACE_UPDATE_* enum is used to pass information back
  375. * from the ftrace_update_record() and ftrace_test_record()
  376. * functions. These are called by the code update routines
  377. * to find out what is to be done for a given function.
  378. *
  379. * IGNORE - The function is already what we want it to be
  380. * MAKE_CALL - Start tracing the function
  381. * MODIFY_CALL - Stop saving regs for the function
  382. * MAKE_NOP - Stop tracing the function
  383. */
  384. enum {
  385. FTRACE_UPDATE_IGNORE,
  386. FTRACE_UPDATE_MAKE_CALL,
  387. FTRACE_UPDATE_MODIFY_CALL,
  388. FTRACE_UPDATE_MAKE_NOP,
  389. };
  390. enum {
  391. FTRACE_ITER_FILTER = (1 << 0),
  392. FTRACE_ITER_NOTRACE = (1 << 1),
  393. FTRACE_ITER_PRINTALL = (1 << 2),
  394. FTRACE_ITER_DO_PROBES = (1 << 3),
  395. FTRACE_ITER_PROBE = (1 << 4),
  396. FTRACE_ITER_ENABLED = (1 << 5),
  397. };
  398. void arch_ftrace_update_code(int command);
  399. struct ftrace_rec_iter;
  400. struct ftrace_rec_iter *ftrace_rec_iter_start(void);
  401. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
  402. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
  403. #define for_ftrace_rec_iter(iter) \
  404. for (iter = ftrace_rec_iter_start(); \
  405. iter; \
  406. iter = ftrace_rec_iter_next(iter))
  407. int ftrace_update_record(struct dyn_ftrace *rec, int enable);
  408. int ftrace_test_record(struct dyn_ftrace *rec, int enable);
  409. void ftrace_run_stop_machine(int command);
  410. unsigned long ftrace_location(unsigned long ip);
  411. unsigned long ftrace_location_range(unsigned long start, unsigned long end);
  412. unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
  413. unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
  414. extern ftrace_func_t ftrace_trace_function;
  415. int ftrace_regex_open(struct ftrace_ops *ops, int flag,
  416. struct inode *inode, struct file *file);
  417. ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
  418. size_t cnt, loff_t *ppos);
  419. ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
  420. size_t cnt, loff_t *ppos);
  421. int ftrace_regex_release(struct inode *inode, struct file *file);
  422. void __init
  423. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
  424. /* defined in arch */
  425. extern int ftrace_ip_converted(unsigned long ip);
  426. extern int ftrace_dyn_arch_init(void);
  427. extern void ftrace_replace_code(int enable);
  428. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  429. extern void ftrace_caller(void);
  430. extern void ftrace_regs_caller(void);
  431. extern void ftrace_call(void);
  432. extern void ftrace_regs_call(void);
  433. extern void mcount_call(void);
  434. void ftrace_modify_all_code(int command);
  435. #ifndef FTRACE_ADDR
  436. #define FTRACE_ADDR ((unsigned long)ftrace_caller)
  437. #endif
  438. #ifndef FTRACE_GRAPH_ADDR
  439. #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
  440. #endif
  441. #ifndef FTRACE_REGS_ADDR
  442. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  443. # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
  444. #else
  445. # define FTRACE_REGS_ADDR FTRACE_ADDR
  446. #endif
  447. #endif
  448. /*
  449. * If an arch would like functions that are only traced
  450. * by the function graph tracer to jump directly to its own
  451. * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
  452. * to be that address to jump to.
  453. */
  454. #ifndef FTRACE_GRAPH_TRAMP_ADDR
  455. #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
  456. #endif
  457. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  458. extern void ftrace_graph_caller(void);
  459. extern int ftrace_enable_ftrace_graph_caller(void);
  460. extern int ftrace_disable_ftrace_graph_caller(void);
  461. #else
  462. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  463. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  464. #endif
  465. /**
  466. * ftrace_make_nop - convert code into nop
  467. * @mod: module structure if called by module load initialization
  468. * @rec: the mcount call site record
  469. * @addr: the address that the call site should be calling
  470. *
  471. * This is a very sensitive operation and great care needs
  472. * to be taken by the arch. The operation should carefully
  473. * read the location, check to see if what is read is indeed
  474. * what we expect it to be, and then on success of the compare,
  475. * it should write to the location.
  476. *
  477. * The code segment at @rec->ip should be a caller to @addr
  478. *
  479. * Return must be:
  480. * 0 on success
  481. * -EFAULT on error reading the location
  482. * -EINVAL on a failed compare of the contents
  483. * -EPERM on error writing to the location
  484. * Any other value will be considered a failure.
  485. */
  486. extern int ftrace_make_nop(struct module *mod,
  487. struct dyn_ftrace *rec, unsigned long addr);
  488. /**
  489. * ftrace_make_call - convert a nop call site into a call to addr
  490. * @rec: the mcount call site record
  491. * @addr: the address that the call site should call
  492. *
  493. * This is a very sensitive operation and great care needs
  494. * to be taken by the arch. The operation should carefully
  495. * read the location, check to see if what is read is indeed
  496. * what we expect it to be, and then on success of the compare,
  497. * it should write to the location.
  498. *
  499. * The code segment at @rec->ip should be a nop
  500. *
  501. * Return must be:
  502. * 0 on success
  503. * -EFAULT on error reading the location
  504. * -EINVAL on a failed compare of the contents
  505. * -EPERM on error writing to the location
  506. * Any other value will be considered a failure.
  507. */
  508. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  509. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  510. /**
  511. * ftrace_modify_call - convert from one addr to another (no nop)
  512. * @rec: the mcount call site record
  513. * @old_addr: the address expected to be currently called to
  514. * @addr: the address to change to
  515. *
  516. * This is a very sensitive operation and great care needs
  517. * to be taken by the arch. The operation should carefully
  518. * read the location, check to see if what is read is indeed
  519. * what we expect it to be, and then on success of the compare,
  520. * it should write to the location.
  521. *
  522. * The code segment at @rec->ip should be a caller to @old_addr
  523. *
  524. * Return must be:
  525. * 0 on success
  526. * -EFAULT on error reading the location
  527. * -EINVAL on a failed compare of the contents
  528. * -EPERM on error writing to the location
  529. * Any other value will be considered a failure.
  530. */
  531. extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  532. unsigned long addr);
  533. #else
  534. /* Should never be called */
  535. static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  536. unsigned long addr)
  537. {
  538. return -EINVAL;
  539. }
  540. #endif
  541. /* May be defined in arch */
  542. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  543. extern int skip_trace(unsigned long ip);
  544. extern void ftrace_module_init(struct module *mod);
  545. extern void ftrace_module_enable(struct module *mod);
  546. extern void ftrace_release_mod(struct module *mod);
  547. extern void ftrace_disable_daemon(void);
  548. extern void ftrace_enable_daemon(void);
  549. #else /* CONFIG_DYNAMIC_FTRACE */
  550. static inline int skip_trace(unsigned long ip) { return 0; }
  551. static inline int ftrace_force_update(void) { return 0; }
  552. static inline void ftrace_disable_daemon(void) { }
  553. static inline void ftrace_enable_daemon(void) { }
  554. static inline void ftrace_module_init(struct module *mod) { }
  555. static inline void ftrace_module_enable(struct module *mod) { }
  556. static inline void ftrace_release_mod(struct module *mod) { }
  557. static inline int ftrace_text_reserved(const void *start, const void *end)
  558. {
  559. return 0;
  560. }
  561. static inline unsigned long ftrace_location(unsigned long ip)
  562. {
  563. return 0;
  564. }
  565. /*
  566. * Again users of functions that have ftrace_ops may not
  567. * have them defined when ftrace is not enabled, but these
  568. * functions may still be called. Use a macro instead of inline.
  569. */
  570. #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
  571. #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
  572. #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
  573. #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
  574. #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
  575. #define ftrace_free_filter(ops) do { } while (0)
  576. #define ftrace_ops_set_global_filter(ops) do { } while (0)
  577. static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
  578. size_t cnt, loff_t *ppos) { return -ENODEV; }
  579. static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
  580. size_t cnt, loff_t *ppos) { return -ENODEV; }
  581. static inline int
  582. ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
  583. static inline bool is_ftrace_trampoline(unsigned long addr)
  584. {
  585. return false;
  586. }
  587. #endif /* CONFIG_DYNAMIC_FTRACE */
  588. /* totally disable ftrace - can not re-enable after this */
  589. void ftrace_kill(void);
  590. static inline void tracer_disable(void)
  591. {
  592. #ifdef CONFIG_FUNCTION_TRACER
  593. ftrace_enabled = 0;
  594. #endif
  595. }
  596. /*
  597. * Ftrace disable/restore without lock. Some synchronization mechanism
  598. * must be used to prevent ftrace_enabled to be changed between
  599. * disable/restore.
  600. */
  601. static inline int __ftrace_enabled_save(void)
  602. {
  603. #ifdef CONFIG_FUNCTION_TRACER
  604. int saved_ftrace_enabled = ftrace_enabled;
  605. ftrace_enabled = 0;
  606. return saved_ftrace_enabled;
  607. #else
  608. return 0;
  609. #endif
  610. }
  611. static inline void __ftrace_enabled_restore(int enabled)
  612. {
  613. #ifdef CONFIG_FUNCTION_TRACER
  614. ftrace_enabled = enabled;
  615. #endif
  616. }
  617. /* All archs should have this, but we define it for consistency */
  618. #ifndef ftrace_return_address0
  619. # define ftrace_return_address0 __builtin_return_address(0)
  620. #endif
  621. /* Archs may use other ways for ADDR1 and beyond */
  622. #ifndef ftrace_return_address
  623. # ifdef CONFIG_FRAME_POINTER
  624. # define ftrace_return_address(n) __builtin_return_address(n)
  625. # else
  626. # define ftrace_return_address(n) 0UL
  627. # endif
  628. #endif
  629. #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
  630. #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
  631. #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
  632. #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
  633. #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
  634. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  635. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  636. static inline unsigned long get_lock_parent_ip(void)
  637. {
  638. unsigned long addr = CALLER_ADDR0;
  639. if (!in_lock_functions(addr))
  640. return addr;
  641. addr = CALLER_ADDR1;
  642. if (!in_lock_functions(addr))
  643. return addr;
  644. return CALLER_ADDR2;
  645. }
  646. #ifdef CONFIG_IRQSOFF_TRACER
  647. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  648. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  649. #else
  650. static inline void time_hardirqs_on(unsigned long a0, unsigned long a1) { }
  651. static inline void time_hardirqs_off(unsigned long a0, unsigned long a1) { }
  652. #endif
  653. #ifdef CONFIG_PREEMPT_TRACER
  654. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  655. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  656. #else
  657. /*
  658. * Use defines instead of static inlines because some arches will make code out
  659. * of the CALLER_ADDR, when we really want these to be a real nop.
  660. */
  661. # define trace_preempt_on(a0, a1) do { } while (0)
  662. # define trace_preempt_off(a0, a1) do { } while (0)
  663. #endif
  664. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  665. extern void ftrace_init(void);
  666. #else
  667. static inline void ftrace_init(void) { }
  668. #endif
  669. /*
  670. * Structure that defines an entry function trace.
  671. * It's already packed but the attribute "packed" is needed
  672. * to remove extra padding at the end.
  673. */
  674. struct ftrace_graph_ent {
  675. unsigned long func; /* Current function */
  676. int depth;
  677. } __packed;
  678. /*
  679. * Structure that defines a return function trace.
  680. * It's already packed but the attribute "packed" is needed
  681. * to remove extra padding at the end.
  682. */
  683. struct ftrace_graph_ret {
  684. unsigned long func; /* Current function */
  685. /* Number of functions that overran the depth limit for current task */
  686. unsigned long overrun;
  687. unsigned long long calltime;
  688. unsigned long long rettime;
  689. int depth;
  690. } __packed;
  691. /* Type of the callback handlers for tracing function graph*/
  692. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  693. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  694. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  695. /* for init task */
  696. #define INIT_FTRACE_GRAPH .ret_stack = NULL,
  697. /*
  698. * Stack of return addresses for functions
  699. * of a thread.
  700. * Used in struct thread_info
  701. */
  702. struct ftrace_ret_stack {
  703. unsigned long ret;
  704. unsigned long func;
  705. unsigned long long calltime;
  706. #ifdef CONFIG_FUNCTION_PROFILER
  707. unsigned long long subtime;
  708. #endif
  709. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  710. unsigned long fp;
  711. #endif
  712. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  713. unsigned long *retp;
  714. #endif
  715. };
  716. /*
  717. * Primary handler of a function return.
  718. * It relays on ftrace_return_to_handler.
  719. * Defined in entry_32/64.S
  720. */
  721. extern void return_to_handler(void);
  722. extern int
  723. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  724. unsigned long frame_pointer, unsigned long *retp);
  725. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  726. unsigned long ret, unsigned long *retp);
  727. /*
  728. * Sometimes we don't want to trace a function with the function
  729. * graph tracer but we want them to keep traced by the usual function
  730. * tracer if the function graph tracer is not configured.
  731. */
  732. #define __notrace_funcgraph notrace
  733. #define FTRACE_NOTRACE_DEPTH 65536
  734. #define FTRACE_RETFUNC_DEPTH 50
  735. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  736. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  737. trace_func_graph_ent_t entryfunc);
  738. extern bool ftrace_graph_is_dead(void);
  739. extern void ftrace_graph_stop(void);
  740. /* The current handlers in use */
  741. extern trace_func_graph_ret_t ftrace_graph_return;
  742. extern trace_func_graph_ent_t ftrace_graph_entry;
  743. extern void unregister_ftrace_graph(void);
  744. extern void ftrace_graph_init_task(struct task_struct *t);
  745. extern void ftrace_graph_exit_task(struct task_struct *t);
  746. extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
  747. static inline int task_curr_ret_stack(struct task_struct *t)
  748. {
  749. return t->curr_ret_stack;
  750. }
  751. static inline void pause_graph_tracing(void)
  752. {
  753. atomic_inc(&current->tracing_graph_pause);
  754. }
  755. static inline void unpause_graph_tracing(void)
  756. {
  757. atomic_dec(&current->tracing_graph_pause);
  758. }
  759. #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
  760. #define __notrace_funcgraph
  761. #define INIT_FTRACE_GRAPH
  762. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  763. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  764. static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
  765. static inline int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  766. trace_func_graph_ent_t entryfunc)
  767. {
  768. return -1;
  769. }
  770. static inline void unregister_ftrace_graph(void) { }
  771. static inline int task_curr_ret_stack(struct task_struct *tsk)
  772. {
  773. return -1;
  774. }
  775. static inline unsigned long
  776. ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
  777. unsigned long *retp)
  778. {
  779. return ret;
  780. }
  781. static inline void pause_graph_tracing(void) { }
  782. static inline void unpause_graph_tracing(void) { }
  783. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  784. #ifdef CONFIG_TRACING
  785. /* flags for current->trace */
  786. enum {
  787. TSK_TRACE_FL_TRACE_BIT = 0,
  788. TSK_TRACE_FL_GRAPH_BIT = 1,
  789. };
  790. enum {
  791. TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
  792. TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
  793. };
  794. static inline void set_tsk_trace_trace(struct task_struct *tsk)
  795. {
  796. set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  797. }
  798. static inline void clear_tsk_trace_trace(struct task_struct *tsk)
  799. {
  800. clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  801. }
  802. static inline int test_tsk_trace_trace(struct task_struct *tsk)
  803. {
  804. return tsk->trace & TSK_TRACE_FL_TRACE;
  805. }
  806. static inline void set_tsk_trace_graph(struct task_struct *tsk)
  807. {
  808. set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  809. }
  810. static inline void clear_tsk_trace_graph(struct task_struct *tsk)
  811. {
  812. clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  813. }
  814. static inline int test_tsk_trace_graph(struct task_struct *tsk)
  815. {
  816. return tsk->trace & TSK_TRACE_FL_GRAPH;
  817. }
  818. enum ftrace_dump_mode;
  819. extern enum ftrace_dump_mode ftrace_dump_on_oops;
  820. extern int tracepoint_printk;
  821. extern void disable_trace_on_warning(void);
  822. extern int __disable_trace_on_warning;
  823. #ifdef CONFIG_PREEMPT
  824. #define INIT_TRACE_RECURSION .trace_recursion = 0,
  825. #endif
  826. int tracepoint_printk_sysctl(struct ctl_table *table, int write,
  827. void __user *buffer, size_t *lenp,
  828. loff_t *ppos);
  829. #else /* CONFIG_TRACING */
  830. static inline void disable_trace_on_warning(void) { }
  831. #endif /* CONFIG_TRACING */
  832. #ifndef INIT_TRACE_RECURSION
  833. #define INIT_TRACE_RECURSION
  834. #endif
  835. #ifdef CONFIG_FTRACE_SYSCALLS
  836. unsigned long arch_syscall_addr(int nr);
  837. #endif /* CONFIG_FTRACE_SYSCALLS */
  838. #endif /* _LINUX_FTRACE_H */