ftrace.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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.txt
  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. bool is_ftrace_trampoline(unsigned long addr);
  295. /*
  296. * The dyn_ftrace record's flags field is split into two parts.
  297. * the first part which is '0-FTRACE_REF_MAX' is a counter of
  298. * the number of callbacks that have registered the function that
  299. * the dyn_ftrace descriptor represents.
  300. *
  301. * The second part is a mask:
  302. * ENABLED - the function is being traced
  303. * REGS - the record wants the function to save regs
  304. * REGS_EN - the function is set up to save regs.
  305. * IPMODIFY - the record allows for the IP address to be changed.
  306. * DISABLED - the record is not ready to be touched yet
  307. *
  308. * When a new ftrace_ops is registered and wants a function to save
  309. * pt_regs, the rec->flag REGS is set. When the function has been
  310. * set up to save regs, the REG_EN flag is set. Once a function
  311. * starts saving regs it will do so until all ftrace_ops are removed
  312. * from tracing that function.
  313. */
  314. enum {
  315. FTRACE_FL_ENABLED = (1UL << 31),
  316. FTRACE_FL_REGS = (1UL << 30),
  317. FTRACE_FL_REGS_EN = (1UL << 29),
  318. FTRACE_FL_TRAMP = (1UL << 28),
  319. FTRACE_FL_TRAMP_EN = (1UL << 27),
  320. FTRACE_FL_IPMODIFY = (1UL << 26),
  321. FTRACE_FL_DISABLED = (1UL << 25),
  322. };
  323. #define FTRACE_REF_MAX_SHIFT 25
  324. #define FTRACE_FL_BITS 7
  325. #define FTRACE_FL_MASKED_BITS ((1UL << FTRACE_FL_BITS) - 1)
  326. #define FTRACE_FL_MASK (FTRACE_FL_MASKED_BITS << FTRACE_REF_MAX_SHIFT)
  327. #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
  328. #define ftrace_rec_count(rec) ((rec)->flags & ~FTRACE_FL_MASK)
  329. struct dyn_ftrace {
  330. unsigned long ip; /* address of mcount call-site */
  331. unsigned long flags;
  332. struct dyn_arch_ftrace arch;
  333. };
  334. int ftrace_force_update(void);
  335. int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
  336. int remove, int reset);
  337. int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
  338. int len, int reset);
  339. int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
  340. int len, int reset);
  341. void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
  342. void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
  343. void ftrace_free_filter(struct ftrace_ops *ops);
  344. void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
  345. enum {
  346. FTRACE_UPDATE_CALLS = (1 << 0),
  347. FTRACE_DISABLE_CALLS = (1 << 1),
  348. FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
  349. FTRACE_START_FUNC_RET = (1 << 3),
  350. FTRACE_STOP_FUNC_RET = (1 << 4),
  351. };
  352. /*
  353. * The FTRACE_UPDATE_* enum is used to pass information back
  354. * from the ftrace_update_record() and ftrace_test_record()
  355. * functions. These are called by the code update routines
  356. * to find out what is to be done for a given function.
  357. *
  358. * IGNORE - The function is already what we want it to be
  359. * MAKE_CALL - Start tracing the function
  360. * MODIFY_CALL - Stop saving regs for the function
  361. * MAKE_NOP - Stop tracing the function
  362. */
  363. enum {
  364. FTRACE_UPDATE_IGNORE,
  365. FTRACE_UPDATE_MAKE_CALL,
  366. FTRACE_UPDATE_MODIFY_CALL,
  367. FTRACE_UPDATE_MAKE_NOP,
  368. };
  369. enum {
  370. FTRACE_ITER_FILTER = (1 << 0),
  371. FTRACE_ITER_NOTRACE = (1 << 1),
  372. FTRACE_ITER_PRINTALL = (1 << 2),
  373. FTRACE_ITER_DO_PROBES = (1 << 3),
  374. FTRACE_ITER_PROBE = (1 << 4),
  375. FTRACE_ITER_MOD = (1 << 5),
  376. FTRACE_ITER_ENABLED = (1 << 6),
  377. };
  378. void arch_ftrace_update_code(int command);
  379. struct ftrace_rec_iter;
  380. struct ftrace_rec_iter *ftrace_rec_iter_start(void);
  381. struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
  382. struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
  383. #define for_ftrace_rec_iter(iter) \
  384. for (iter = ftrace_rec_iter_start(); \
  385. iter; \
  386. iter = ftrace_rec_iter_next(iter))
  387. int ftrace_update_record(struct dyn_ftrace *rec, int enable);
  388. int ftrace_test_record(struct dyn_ftrace *rec, int enable);
  389. void ftrace_run_stop_machine(int command);
  390. unsigned long ftrace_location(unsigned long ip);
  391. unsigned long ftrace_location_range(unsigned long start, unsigned long end);
  392. unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
  393. unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
  394. extern ftrace_func_t ftrace_trace_function;
  395. int ftrace_regex_open(struct ftrace_ops *ops, int flag,
  396. struct inode *inode, struct file *file);
  397. ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
  398. size_t cnt, loff_t *ppos);
  399. ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
  400. size_t cnt, loff_t *ppos);
  401. int ftrace_regex_release(struct inode *inode, struct file *file);
  402. void __init
  403. ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
  404. /* defined in arch */
  405. extern int ftrace_ip_converted(unsigned long ip);
  406. extern int ftrace_dyn_arch_init(void);
  407. extern void ftrace_replace_code(int enable);
  408. extern int ftrace_update_ftrace_func(ftrace_func_t func);
  409. extern void ftrace_caller(void);
  410. extern void ftrace_regs_caller(void);
  411. extern void ftrace_call(void);
  412. extern void ftrace_regs_call(void);
  413. extern void mcount_call(void);
  414. void ftrace_modify_all_code(int command);
  415. #ifndef FTRACE_ADDR
  416. #define FTRACE_ADDR ((unsigned long)ftrace_caller)
  417. #endif
  418. #ifndef FTRACE_GRAPH_ADDR
  419. #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
  420. #endif
  421. #ifndef FTRACE_REGS_ADDR
  422. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  423. # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
  424. #else
  425. # define FTRACE_REGS_ADDR FTRACE_ADDR
  426. #endif
  427. #endif
  428. /*
  429. * If an arch would like functions that are only traced
  430. * by the function graph tracer to jump directly to its own
  431. * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
  432. * to be that address to jump to.
  433. */
  434. #ifndef FTRACE_GRAPH_TRAMP_ADDR
  435. #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
  436. #endif
  437. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  438. extern void ftrace_graph_caller(void);
  439. extern int ftrace_enable_ftrace_graph_caller(void);
  440. extern int ftrace_disable_ftrace_graph_caller(void);
  441. #else
  442. static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
  443. static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
  444. #endif
  445. /**
  446. * ftrace_make_nop - convert code into nop
  447. * @mod: module structure if called by module load initialization
  448. * @rec: the mcount call site record
  449. * @addr: the address that the call site should be calling
  450. *
  451. * This is a very sensitive operation and great care needs
  452. * to be taken by the arch. The operation should carefully
  453. * read the location, check to see if what is read is indeed
  454. * what we expect it to be, and then on success of the compare,
  455. * it should write to the location.
  456. *
  457. * The code segment at @rec->ip should be a caller to @addr
  458. *
  459. * Return must be:
  460. * 0 on success
  461. * -EFAULT on error reading the location
  462. * -EINVAL on a failed compare of the contents
  463. * -EPERM on error writing to the location
  464. * Any other value will be considered a failure.
  465. */
  466. extern int ftrace_make_nop(struct module *mod,
  467. struct dyn_ftrace *rec, unsigned long addr);
  468. /**
  469. * ftrace_make_call - convert a nop call site into a call to addr
  470. * @rec: the mcount call site record
  471. * @addr: the address that the call site should call
  472. *
  473. * This is a very sensitive operation and great care needs
  474. * to be taken by the arch. The operation should carefully
  475. * read the location, check to see if what is read is indeed
  476. * what we expect it to be, and then on success of the compare,
  477. * it should write to the location.
  478. *
  479. * The code segment at @rec->ip should be a nop
  480. *
  481. * Return must be:
  482. * 0 on success
  483. * -EFAULT on error reading the location
  484. * -EINVAL on a failed compare of the contents
  485. * -EPERM on error writing to the location
  486. * Any other value will be considered a failure.
  487. */
  488. extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
  489. #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
  490. /**
  491. * ftrace_modify_call - convert from one addr to another (no nop)
  492. * @rec: the mcount call site record
  493. * @old_addr: the address expected to be currently called to
  494. * @addr: the address to change to
  495. *
  496. * This is a very sensitive operation and great care needs
  497. * to be taken by the arch. The operation should carefully
  498. * read the location, check to see if what is read is indeed
  499. * what we expect it to be, and then on success of the compare,
  500. * it should write to the location.
  501. *
  502. * The code segment at @rec->ip should be a caller to @old_addr
  503. *
  504. * Return must be:
  505. * 0 on success
  506. * -EFAULT on error reading the location
  507. * -EINVAL on a failed compare of the contents
  508. * -EPERM on error writing to the location
  509. * Any other value will be considered a failure.
  510. */
  511. extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  512. unsigned long addr);
  513. #else
  514. /* Should never be called */
  515. static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
  516. unsigned long addr)
  517. {
  518. return -EINVAL;
  519. }
  520. #endif
  521. /* May be defined in arch */
  522. extern int ftrace_arch_read_dyn_info(char *buf, int size);
  523. extern int skip_trace(unsigned long ip);
  524. extern void ftrace_module_init(struct module *mod);
  525. extern void ftrace_module_enable(struct module *mod);
  526. extern void ftrace_release_mod(struct module *mod);
  527. extern void ftrace_disable_daemon(void);
  528. extern void ftrace_enable_daemon(void);
  529. #else /* CONFIG_DYNAMIC_FTRACE */
  530. static inline int skip_trace(unsigned long ip) { return 0; }
  531. static inline int ftrace_force_update(void) { return 0; }
  532. static inline void ftrace_disable_daemon(void) { }
  533. static inline void ftrace_enable_daemon(void) { }
  534. static inline void ftrace_module_init(struct module *mod) { }
  535. static inline void ftrace_module_enable(struct module *mod) { }
  536. static inline void ftrace_release_mod(struct module *mod) { }
  537. static inline int ftrace_text_reserved(const void *start, const void *end)
  538. {
  539. return 0;
  540. }
  541. static inline unsigned long ftrace_location(unsigned long ip)
  542. {
  543. return 0;
  544. }
  545. /*
  546. * Again users of functions that have ftrace_ops may not
  547. * have them defined when ftrace is not enabled, but these
  548. * functions may still be called. Use a macro instead of inline.
  549. */
  550. #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
  551. #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
  552. #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
  553. #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
  554. #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
  555. #define ftrace_free_filter(ops) do { } while (0)
  556. #define ftrace_ops_set_global_filter(ops) do { } while (0)
  557. static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
  558. size_t cnt, loff_t *ppos) { return -ENODEV; }
  559. static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
  560. size_t cnt, loff_t *ppos) { return -ENODEV; }
  561. static inline int
  562. ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
  563. static inline bool is_ftrace_trampoline(unsigned long addr)
  564. {
  565. return false;
  566. }
  567. #endif /* CONFIG_DYNAMIC_FTRACE */
  568. /* totally disable ftrace - can not re-enable after this */
  569. void ftrace_kill(void);
  570. static inline void tracer_disable(void)
  571. {
  572. #ifdef CONFIG_FUNCTION_TRACER
  573. ftrace_enabled = 0;
  574. #endif
  575. }
  576. /*
  577. * Ftrace disable/restore without lock. Some synchronization mechanism
  578. * must be used to prevent ftrace_enabled to be changed between
  579. * disable/restore.
  580. */
  581. static inline int __ftrace_enabled_save(void)
  582. {
  583. #ifdef CONFIG_FUNCTION_TRACER
  584. int saved_ftrace_enabled = ftrace_enabled;
  585. ftrace_enabled = 0;
  586. return saved_ftrace_enabled;
  587. #else
  588. return 0;
  589. #endif
  590. }
  591. static inline void __ftrace_enabled_restore(int enabled)
  592. {
  593. #ifdef CONFIG_FUNCTION_TRACER
  594. ftrace_enabled = enabled;
  595. #endif
  596. }
  597. /* All archs should have this, but we define it for consistency */
  598. #ifndef ftrace_return_address0
  599. # define ftrace_return_address0 __builtin_return_address(0)
  600. #endif
  601. /* Archs may use other ways for ADDR1 and beyond */
  602. #ifndef ftrace_return_address
  603. # ifdef CONFIG_FRAME_POINTER
  604. # define ftrace_return_address(n) __builtin_return_address(n)
  605. # else
  606. # define ftrace_return_address(n) 0UL
  607. # endif
  608. #endif
  609. #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
  610. #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
  611. #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
  612. #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
  613. #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
  614. #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
  615. #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
  616. static inline unsigned long get_lock_parent_ip(void)
  617. {
  618. unsigned long addr = CALLER_ADDR0;
  619. if (!in_lock_functions(addr))
  620. return addr;
  621. addr = CALLER_ADDR1;
  622. if (!in_lock_functions(addr))
  623. return addr;
  624. return CALLER_ADDR2;
  625. }
  626. #ifdef CONFIG_IRQSOFF_TRACER
  627. extern void time_hardirqs_on(unsigned long a0, unsigned long a1);
  628. extern void time_hardirqs_off(unsigned long a0, unsigned long a1);
  629. #else
  630. static inline void time_hardirqs_on(unsigned long a0, unsigned long a1) { }
  631. static inline void time_hardirqs_off(unsigned long a0, unsigned long a1) { }
  632. #endif
  633. #if defined(CONFIG_PREEMPT_TRACER) || \
  634. (defined(CONFIG_DEBUG_PREEMPT) && defined(CONFIG_PREEMPTIRQ_EVENTS))
  635. extern void trace_preempt_on(unsigned long a0, unsigned long a1);
  636. extern void trace_preempt_off(unsigned long a0, unsigned long a1);
  637. #else
  638. /*
  639. * Use defines instead of static inlines because some arches will make code out
  640. * of the CALLER_ADDR, when we really want these to be a real nop.
  641. */
  642. # define trace_preempt_on(a0, a1) do { } while (0)
  643. # define trace_preempt_off(a0, a1) do { } while (0)
  644. #endif
  645. #ifdef CONFIG_FTRACE_MCOUNT_RECORD
  646. extern void ftrace_init(void);
  647. #else
  648. static inline void ftrace_init(void) { }
  649. #endif
  650. /*
  651. * Structure that defines an entry function trace.
  652. * It's already packed but the attribute "packed" is needed
  653. * to remove extra padding at the end.
  654. */
  655. struct ftrace_graph_ent {
  656. unsigned long func; /* Current function */
  657. int depth;
  658. } __packed;
  659. /*
  660. * Structure that defines a return function trace.
  661. * It's already packed but the attribute "packed" is needed
  662. * to remove extra padding at the end.
  663. */
  664. struct ftrace_graph_ret {
  665. unsigned long func; /* Current function */
  666. /* Number of functions that overran the depth limit for current task */
  667. unsigned long overrun;
  668. unsigned long long calltime;
  669. unsigned long long rettime;
  670. int depth;
  671. } __packed;
  672. /* Type of the callback handlers for tracing function graph*/
  673. typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
  674. typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
  675. #ifdef CONFIG_FUNCTION_GRAPH_TRACER
  676. /* for init task */
  677. #define INIT_FTRACE_GRAPH .ret_stack = NULL,
  678. /*
  679. * Stack of return addresses for functions
  680. * of a thread.
  681. * Used in struct thread_info
  682. */
  683. struct ftrace_ret_stack {
  684. unsigned long ret;
  685. unsigned long func;
  686. unsigned long long calltime;
  687. #ifdef CONFIG_FUNCTION_PROFILER
  688. unsigned long long subtime;
  689. #endif
  690. #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
  691. unsigned long fp;
  692. #endif
  693. #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
  694. unsigned long *retp;
  695. #endif
  696. };
  697. /*
  698. * Primary handler of a function return.
  699. * It relays on ftrace_return_to_handler.
  700. * Defined in entry_32/64.S
  701. */
  702. extern void return_to_handler(void);
  703. extern int
  704. ftrace_push_return_trace(unsigned long ret, unsigned long func, int *depth,
  705. unsigned long frame_pointer, unsigned long *retp);
  706. unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
  707. unsigned long ret, unsigned long *retp);
  708. /*
  709. * Sometimes we don't want to trace a function with the function
  710. * graph tracer but we want them to keep traced by the usual function
  711. * tracer if the function graph tracer is not configured.
  712. */
  713. #define __notrace_funcgraph notrace
  714. #define FTRACE_NOTRACE_DEPTH 65536
  715. #define FTRACE_RETFUNC_DEPTH 50
  716. #define FTRACE_RETSTACK_ALLOC_SIZE 32
  717. extern int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  718. trace_func_graph_ent_t entryfunc);
  719. extern bool ftrace_graph_is_dead(void);
  720. extern void ftrace_graph_stop(void);
  721. /* The current handlers in use */
  722. extern trace_func_graph_ret_t ftrace_graph_return;
  723. extern trace_func_graph_ent_t ftrace_graph_entry;
  724. extern void unregister_ftrace_graph(void);
  725. extern void ftrace_graph_init_task(struct task_struct *t);
  726. extern void ftrace_graph_exit_task(struct task_struct *t);
  727. extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
  728. static inline int task_curr_ret_stack(struct task_struct *t)
  729. {
  730. return t->curr_ret_stack;
  731. }
  732. static inline void pause_graph_tracing(void)
  733. {
  734. atomic_inc(&current->tracing_graph_pause);
  735. }
  736. static inline void unpause_graph_tracing(void)
  737. {
  738. atomic_dec(&current->tracing_graph_pause);
  739. }
  740. #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
  741. #define __notrace_funcgraph
  742. #define INIT_FTRACE_GRAPH
  743. static inline void ftrace_graph_init_task(struct task_struct *t) { }
  744. static inline void ftrace_graph_exit_task(struct task_struct *t) { }
  745. static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
  746. static inline int register_ftrace_graph(trace_func_graph_ret_t retfunc,
  747. trace_func_graph_ent_t entryfunc)
  748. {
  749. return -1;
  750. }
  751. static inline void unregister_ftrace_graph(void) { }
  752. static inline int task_curr_ret_stack(struct task_struct *tsk)
  753. {
  754. return -1;
  755. }
  756. static inline unsigned long
  757. ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
  758. unsigned long *retp)
  759. {
  760. return ret;
  761. }
  762. static inline void pause_graph_tracing(void) { }
  763. static inline void unpause_graph_tracing(void) { }
  764. #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
  765. #ifdef CONFIG_TRACING
  766. /* flags for current->trace */
  767. enum {
  768. TSK_TRACE_FL_TRACE_BIT = 0,
  769. TSK_TRACE_FL_GRAPH_BIT = 1,
  770. };
  771. enum {
  772. TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
  773. TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
  774. };
  775. static inline void set_tsk_trace_trace(struct task_struct *tsk)
  776. {
  777. set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  778. }
  779. static inline void clear_tsk_trace_trace(struct task_struct *tsk)
  780. {
  781. clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
  782. }
  783. static inline int test_tsk_trace_trace(struct task_struct *tsk)
  784. {
  785. return tsk->trace & TSK_TRACE_FL_TRACE;
  786. }
  787. static inline void set_tsk_trace_graph(struct task_struct *tsk)
  788. {
  789. set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  790. }
  791. static inline void clear_tsk_trace_graph(struct task_struct *tsk)
  792. {
  793. clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
  794. }
  795. static inline int test_tsk_trace_graph(struct task_struct *tsk)
  796. {
  797. return tsk->trace & TSK_TRACE_FL_GRAPH;
  798. }
  799. enum ftrace_dump_mode;
  800. extern enum ftrace_dump_mode ftrace_dump_on_oops;
  801. extern int tracepoint_printk;
  802. extern void disable_trace_on_warning(void);
  803. extern int __disable_trace_on_warning;
  804. #ifdef CONFIG_PREEMPT
  805. #define INIT_TRACE_RECURSION .trace_recursion = 0,
  806. #endif
  807. int tracepoint_printk_sysctl(struct ctl_table *table, int write,
  808. void __user *buffer, size_t *lenp,
  809. loff_t *ppos);
  810. #else /* CONFIG_TRACING */
  811. static inline void disable_trace_on_warning(void) { }
  812. #endif /* CONFIG_TRACING */
  813. #ifndef INIT_TRACE_RECURSION
  814. #define INIT_TRACE_RECURSION
  815. #endif
  816. #ifdef CONFIG_FTRACE_SYSCALLS
  817. unsigned long arch_syscall_addr(int nr);
  818. #endif /* CONFIG_FTRACE_SYSCALLS */
  819. #endif /* _LINUX_FTRACE_H */