ftrace.h 30 KB

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