ftrace.h 30 KB

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