1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Ftrace header. For implementation details beyond the random comments
4 * scattered below, see: Documentation/trace/ftrace-design.rst
7 #ifndef _LINUX_FTRACE_H
8 #define _LINUX_FTRACE_H
10 #include <linux/trace_recursion.h>
11 #include <linux/trace_clock.h>
12 #include <linux/jump_label.h>
13 #include <linux/kallsyms.h>
14 #include <linux/linkage.h>
15 #include <linux/bitops.h>
16 #include <linux/ptrace.h>
17 #include <linux/ktime.h>
18 #include <linux/sched.h>
19 #include <linux/types.h>
20 #include <linux/init.h>
23 #include <asm/ftrace.h>
26 * If the arch supports passing the variable contents of
27 * function_trace_op as the third parameter back from the
28 * mcount call, then the arch should define this as 1.
30 #ifndef ARCH_SUPPORTS_FTRACE_OPS
31 #define ARCH_SUPPORTS_FTRACE_OPS 0
35 extern void ftrace_boot_snapshot(void);
37 static inline void ftrace_boot_snapshot(void) { }
40 #ifdef CONFIG_FUNCTION_TRACER
44 * If the arch's mcount caller does not support all of ftrace's
45 * features, then it must call an indirect function that
46 * does. Or at least does enough to prevent any unwelcome side effects.
48 * Also define the function prototype that these architectures use
49 * to call the ftrace_ops_list_func().
51 #if !ARCH_SUPPORTS_FTRACE_OPS
52 # define FTRACE_FORCE_LIST_FUNC 1
53 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
55 # define FTRACE_FORCE_LIST_FUNC 0
56 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
57 struct ftrace_ops *op, struct ftrace_regs *fregs);
59 #endif /* CONFIG_FUNCTION_TRACER */
61 /* Main tracing buffer and events set up */
63 void trace_init(void);
64 void early_trace_init(void);
66 static inline void trace_init(void) { }
67 static inline void early_trace_init(void) { }
72 struct ftrace_direct_func;
74 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
75 defined(CONFIG_DYNAMIC_FTRACE)
77 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
78 unsigned long *off, char **modname, char *sym);
80 static inline const char *
81 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
82 unsigned long *off, char **modname, char *sym)
88 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
89 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
90 char *type, char *name,
91 char *module_name, int *exported);
93 static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
94 char *type, char *name,
95 char *module_name, int *exported)
101 #ifdef CONFIG_FUNCTION_TRACER
103 extern int ftrace_enabled;
105 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
110 #define arch_ftrace_get_regs(fregs) (&(fregs)->regs)
113 * ftrace_instruction_pointer_set() is to be defined by the architecture
114 * if to allow setting of the instruction pointer from the ftrace_regs
115 * when HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports
116 * live kernel patching.
118 #define ftrace_instruction_pointer_set(fregs, ip) do { } while (0)
119 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
121 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
126 return arch_ftrace_get_regs(fregs);
129 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
130 struct ftrace_ops *op, struct ftrace_regs *fregs);
132 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
135 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
136 * set in the flags member.
137 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
138 * IPMODIFY are a kind of attribute flags which can be set only before
139 * registering the ftrace_ops, and can not be modified while registered.
140 * Changing those attribute flags after registering ftrace_ops will
141 * cause unexpected results.
143 * ENABLED - set/unset when ftrace_ops is registered/unregistered
144 * DYNAMIC - set when ftrace_ops is registered to denote dynamically
145 * allocated ftrace_ops which need special care
146 * SAVE_REGS - The ftrace_ops wants regs saved at each function called
147 * and passed to the callback. If this flag is set, but the
148 * architecture does not support passing regs
149 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
150 * ftrace_ops will fail to register, unless the next flag
152 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
153 * handler can handle an arch that does not save regs
154 * (the handler tests if regs == NULL), then it can set
155 * this flag instead. It will not fail registering the ftrace_ops
156 * but, the regs field will be NULL if the arch does not support
157 * passing regs to the handler.
158 * Note, if this flag is set, the SAVE_REGS flag will automatically
159 * get set upon registering the ftrace_ops, if the arch supports it.
160 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
161 * that the call back needs recursion protection. If it does
162 * not set this, then the ftrace infrastructure will assume
163 * that the callback can handle recursion on its own.
164 * STUB - The ftrace_ops is just a place holder.
165 * INITIALIZED - The ftrace_ops has already been initialized (first use time
166 * register_ftrace_function() is called, it will initialized the ops)
167 * DELETED - The ops are being deleted, do not let them be registered again.
168 * ADDING - The ops is in the process of being added.
169 * REMOVING - The ops is in the process of being removed.
170 * MODIFYING - The ops is in the process of changing its filter functions.
171 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
172 * The arch specific code sets this flag when it allocated a
173 * trampoline. This lets the arch know that it can update the
174 * trampoline in case the callback function changes.
175 * The ftrace_ops trampoline can be set by the ftrace users, and
176 * in such cases the arch must not modify it. Only the arch ftrace
177 * core code should set this flag.
178 * IPMODIFY - The ops can modify the IP register. This can only be set with
179 * SAVE_REGS. If another ops with this flag set is already registered
180 * for any of the functions that this ops will be registered for, then
181 * this ops will fail to register or set_filter_ip.
182 * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
183 * RCU - Set when the ops can only be called when RCU is watching.
184 * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
185 * PERMANENT - Set when the ops is permanent and should not be affected by
187 * DIRECT - Used by the direct ftrace_ops helper for direct functions
188 * (internal ftrace only, should not be used by others)
191 FTRACE_OPS_FL_ENABLED = BIT(0),
192 FTRACE_OPS_FL_DYNAMIC = BIT(1),
193 FTRACE_OPS_FL_SAVE_REGS = BIT(2),
194 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3),
195 FTRACE_OPS_FL_RECURSION = BIT(4),
196 FTRACE_OPS_FL_STUB = BIT(5),
197 FTRACE_OPS_FL_INITIALIZED = BIT(6),
198 FTRACE_OPS_FL_DELETED = BIT(7),
199 FTRACE_OPS_FL_ADDING = BIT(8),
200 FTRACE_OPS_FL_REMOVING = BIT(9),
201 FTRACE_OPS_FL_MODIFYING = BIT(10),
202 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11),
203 FTRACE_OPS_FL_IPMODIFY = BIT(12),
204 FTRACE_OPS_FL_PID = BIT(13),
205 FTRACE_OPS_FL_RCU = BIT(14),
206 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15),
207 FTRACE_OPS_FL_PERMANENT = BIT(16),
208 FTRACE_OPS_FL_DIRECT = BIT(17),
211 #ifdef CONFIG_DYNAMIC_FTRACE
212 /* The hash used to know what functions callbacks trace */
213 struct ftrace_ops_hash {
214 struct ftrace_hash __rcu *notrace_hash;
215 struct ftrace_hash __rcu *filter_hash;
216 struct mutex regex_lock;
219 void ftrace_free_init_mem(void);
220 void ftrace_free_mem(struct module *mod, void *start, void *end);
222 static inline void ftrace_free_init_mem(void)
224 ftrace_boot_snapshot();
226 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
230 * Note, ftrace_ops can be referenced outside of RCU protection, unless
231 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
232 * core data, the unregistering of it will perform a scheduling on all CPUs
233 * to make sure that there are no more users. Depending on the load of the
234 * system that may take a bit of time.
236 * Any private data added must also take care not to be freed and if private
237 * data is added to a ftrace_ops that is in core code, the user of the
238 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
242 struct ftrace_ops __rcu *next;
245 ftrace_func_t saved_func;
246 #ifdef CONFIG_DYNAMIC_FTRACE
247 struct ftrace_ops_hash local_hash;
248 struct ftrace_ops_hash *func_hash;
249 struct ftrace_ops_hash old_hash;
250 unsigned long trampoline;
251 unsigned long trampoline_size;
252 struct list_head list;
256 extern struct ftrace_ops __rcu *ftrace_ops_list;
257 extern struct ftrace_ops ftrace_list_end;
260 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
261 * can use rcu_dereference_raw_check() is that elements removed from this list
262 * are simply leaked, so there is no need to interact with a grace-period
263 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
264 * concurrent insertions into the ftrace_ops_list.
266 * Silly Alpha and silly pointer-speculation compiler optimizations!
268 #define do_for_each_ftrace_op(op, list) \
269 op = rcu_dereference_raw_check(list); \
273 * Optimized for just a single item in the list (as that is the normal case).
275 #define while_for_each_ftrace_op(op) \
276 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
277 unlikely((op) != &ftrace_list_end))
280 * Type of the current tracing.
282 enum ftrace_tracing_type_t {
283 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
284 FTRACE_TYPE_RETURN, /* Hook the return of the function */
287 /* Current tracing type, default is FTRACE_TYPE_ENTER */
288 extern enum ftrace_tracing_type_t ftrace_tracing_type;
291 * The ftrace_ops must be a static and should also
292 * be read_mostly. These functions do modify read_mostly variables
293 * so use them sparely. Never free an ftrace_op or modify the
294 * next pointer after it has been registered. Even after unregistering
295 * it, the next pointer may still be used internally.
297 int register_ftrace_function(struct ftrace_ops *ops);
298 int unregister_ftrace_function(struct ftrace_ops *ops);
300 extern void ftrace_stub(unsigned long a0, unsigned long a1,
301 struct ftrace_ops *op, struct ftrace_regs *fregs);
304 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
305 #else /* !CONFIG_FUNCTION_TRACER */
307 * (un)register_ftrace_function must be a macro since the ops parameter
308 * must not be evaluated.
310 #define register_ftrace_function(ops) ({ 0; })
311 #define unregister_ftrace_function(ops) ({ 0; })
312 static inline void ftrace_kill(void) { }
313 static inline void ftrace_free_init_mem(void) { }
314 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
315 static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
319 #endif /* CONFIG_FUNCTION_TRACER */
321 struct ftrace_func_entry {
322 struct hlist_node hlist;
324 unsigned long direct; /* for direct lookup only */
329 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
330 extern int ftrace_direct_func_count;
331 int register_ftrace_direct(unsigned long ip, unsigned long addr);
332 int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
333 int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
334 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
335 int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
336 struct dyn_ftrace *rec,
337 unsigned long old_addr,
338 unsigned long new_addr);
339 unsigned long ftrace_find_rec_direct(unsigned long ip);
340 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
341 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
342 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
346 # define ftrace_direct_func_count 0
347 static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
351 static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
355 static inline int modify_ftrace_direct(unsigned long ip,
356 unsigned long old_addr, unsigned long new_addr)
360 static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
364 static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
365 struct dyn_ftrace *rec,
366 unsigned long old_addr,
367 unsigned long new_addr)
371 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
375 static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
379 static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
383 static inline int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
387 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
389 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
391 * This must be implemented by the architecture.
392 * It is the way the ftrace direct_ops helper, when called
393 * via ftrace (because there's other callbacks besides the
394 * direct call), can inform the architecture's trampoline that this
395 * routine has a direct caller, and what the caller is.
397 * For example, in x86, it returns the direct caller
398 * callback function via the regs->orig_ax parameter.
399 * Then in the ftrace trampoline, if this is set, it makes
400 * the return from the trampoline jump to the direct caller
401 * instead of going back to the function it just traced.
403 static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
404 unsigned long addr) { }
405 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
407 #ifdef CONFIG_STACK_TRACER
409 extern int stack_tracer_enabled;
411 int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
412 size_t *lenp, loff_t *ppos);
414 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
415 DECLARE_PER_CPU(int, disable_stack_tracer);
418 * stack_tracer_disable - temporarily disable the stack tracer
420 * There's a few locations (namely in RCU) where stack tracing
421 * cannot be executed. This function is used to disable stack
422 * tracing during those critical sections.
424 * This function must be called with preemption or interrupts
425 * disabled and stack_tracer_enable() must be called shortly after
426 * while preemption or interrupts are still disabled.
428 static inline void stack_tracer_disable(void)
430 /* Preemption or interrupts must be disabled */
431 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
432 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
433 this_cpu_inc(disable_stack_tracer);
437 * stack_tracer_enable - re-enable the stack tracer
439 * After stack_tracer_disable() is called, stack_tracer_enable()
440 * must be called shortly afterward.
442 static inline void stack_tracer_enable(void)
444 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
445 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
446 this_cpu_dec(disable_stack_tracer);
449 static inline void stack_tracer_disable(void) { }
450 static inline void stack_tracer_enable(void) { }
453 #ifdef CONFIG_DYNAMIC_FTRACE
455 int ftrace_arch_code_modify_prepare(void);
456 int ftrace_arch_code_modify_post_process(void);
458 enum ftrace_bug_type {
465 extern enum ftrace_bug_type ftrace_bug_type;
468 * Archs can set this to point to a variable that holds the value that was
469 * expected at the call site before calling ftrace_bug().
471 extern const void *ftrace_expected;
473 void ftrace_bug(int err, struct dyn_ftrace *rec);
477 extern int ftrace_text_reserved(const void *start, const void *end);
479 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
481 bool is_ftrace_trampoline(unsigned long addr);
484 * The dyn_ftrace record's flags field is split into two parts.
485 * the first part which is '0-FTRACE_REF_MAX' is a counter of
486 * the number of callbacks that have registered the function that
487 * the dyn_ftrace descriptor represents.
489 * The second part is a mask:
490 * ENABLED - the function is being traced
491 * REGS - the record wants the function to save regs
492 * REGS_EN - the function is set up to save regs.
493 * IPMODIFY - the record allows for the IP address to be changed.
494 * DISABLED - the record is not ready to be touched yet
495 * DIRECT - there is a direct function to call
497 * When a new ftrace_ops is registered and wants a function to save
498 * pt_regs, the rec->flags REGS is set. When the function has been
499 * set up to save regs, the REG_EN flag is set. Once a function
500 * starts saving regs it will do so until all ftrace_ops are removed
501 * from tracing that function.
504 FTRACE_FL_ENABLED = (1UL << 31),
505 FTRACE_FL_REGS = (1UL << 30),
506 FTRACE_FL_REGS_EN = (1UL << 29),
507 FTRACE_FL_TRAMP = (1UL << 28),
508 FTRACE_FL_TRAMP_EN = (1UL << 27),
509 FTRACE_FL_IPMODIFY = (1UL << 26),
510 FTRACE_FL_DISABLED = (1UL << 25),
511 FTRACE_FL_DIRECT = (1UL << 24),
512 FTRACE_FL_DIRECT_EN = (1UL << 23),
515 #define FTRACE_REF_MAX_SHIFT 23
516 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
518 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
521 unsigned long ip; /* address of mcount call-site */
523 struct dyn_arch_ftrace arch;
526 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
527 int remove, int reset);
528 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
529 unsigned int cnt, int remove, int reset);
530 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
532 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
534 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
535 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
536 void ftrace_free_filter(struct ftrace_ops *ops);
537 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
540 FTRACE_UPDATE_CALLS = (1 << 0),
541 FTRACE_DISABLE_CALLS = (1 << 1),
542 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
543 FTRACE_START_FUNC_RET = (1 << 3),
544 FTRACE_STOP_FUNC_RET = (1 << 4),
545 FTRACE_MAY_SLEEP = (1 << 5),
549 * The FTRACE_UPDATE_* enum is used to pass information back
550 * from the ftrace_update_record() and ftrace_test_record()
551 * functions. These are called by the code update routines
552 * to find out what is to be done for a given function.
554 * IGNORE - The function is already what we want it to be
555 * MAKE_CALL - Start tracing the function
556 * MODIFY_CALL - Stop saving regs for the function
557 * MAKE_NOP - Stop tracing the function
560 FTRACE_UPDATE_IGNORE,
561 FTRACE_UPDATE_MAKE_CALL,
562 FTRACE_UPDATE_MODIFY_CALL,
563 FTRACE_UPDATE_MAKE_NOP,
567 FTRACE_ITER_FILTER = (1 << 0),
568 FTRACE_ITER_NOTRACE = (1 << 1),
569 FTRACE_ITER_PRINTALL = (1 << 2),
570 FTRACE_ITER_DO_PROBES = (1 << 3),
571 FTRACE_ITER_PROBE = (1 << 4),
572 FTRACE_ITER_MOD = (1 << 5),
573 FTRACE_ITER_ENABLED = (1 << 6),
576 void arch_ftrace_update_code(int command);
577 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
578 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
579 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
581 struct ftrace_rec_iter;
583 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
584 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
585 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
587 #define for_ftrace_rec_iter(iter) \
588 for (iter = ftrace_rec_iter_start(); \
590 iter = ftrace_rec_iter_next(iter))
593 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
594 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
595 void ftrace_run_stop_machine(int command);
596 unsigned long ftrace_location(unsigned long ip);
597 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
598 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
599 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
601 extern ftrace_func_t ftrace_trace_function;
603 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
604 struct inode *inode, struct file *file);
605 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
606 size_t cnt, loff_t *ppos);
607 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
608 size_t cnt, loff_t *ppos);
609 int ftrace_regex_release(struct inode *inode, struct file *file);
612 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
614 /* defined in arch */
615 extern int ftrace_ip_converted(unsigned long ip);
616 extern int ftrace_dyn_arch_init(void);
617 extern void ftrace_replace_code(int enable);
618 extern int ftrace_update_ftrace_func(ftrace_func_t func);
619 extern void ftrace_caller(void);
620 extern void ftrace_regs_caller(void);
621 extern void ftrace_call(void);
622 extern void ftrace_regs_call(void);
623 extern void mcount_call(void);
625 void ftrace_modify_all_code(int command);
628 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
631 #ifndef FTRACE_GRAPH_ADDR
632 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
635 #ifndef FTRACE_REGS_ADDR
636 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
637 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
639 # define FTRACE_REGS_ADDR FTRACE_ADDR
644 * If an arch would like functions that are only traced
645 * by the function graph tracer to jump directly to its own
646 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
647 * to be that address to jump to.
649 #ifndef FTRACE_GRAPH_TRAMP_ADDR
650 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
653 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
654 extern void ftrace_graph_caller(void);
655 extern int ftrace_enable_ftrace_graph_caller(void);
656 extern int ftrace_disable_ftrace_graph_caller(void);
658 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
659 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
663 * ftrace_make_nop - convert code into nop
664 * @mod: module structure if called by module load initialization
665 * @rec: the call site record (e.g. mcount/fentry)
666 * @addr: the address that the call site should be calling
668 * This is a very sensitive operation and great care needs
669 * to be taken by the arch. The operation should carefully
670 * read the location, check to see if what is read is indeed
671 * what we expect it to be, and then on success of the compare,
672 * it should write to the location.
674 * The code segment at @rec->ip should be a caller to @addr
678 * -EFAULT on error reading the location
679 * -EINVAL on a failed compare of the contents
680 * -EPERM on error writing to the location
681 * Any other value will be considered a failure.
683 extern int ftrace_make_nop(struct module *mod,
684 struct dyn_ftrace *rec, unsigned long addr);
687 * ftrace_need_init_nop - return whether nop call sites should be initialized
689 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
690 * need to call ftrace_init_nop() if the code is built with that flag.
691 * Architectures where this is not always the case may define their own
695 * 0 if ftrace_init_nop() should be called
696 * Nonzero if ftrace_init_nop() should not be called
699 #ifndef ftrace_need_init_nop
700 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
704 * ftrace_init_nop - initialize a nop call site
705 * @mod: module structure if called by module load initialization
706 * @rec: the call site record (e.g. mcount/fentry)
708 * This is a very sensitive operation and great care needs
709 * to be taken by the arch. The operation should carefully
710 * read the location, check to see if what is read is indeed
711 * what we expect it to be, and then on success of the compare,
712 * it should write to the location.
714 * The code segment at @rec->ip should contain the contents created by
719 * -EFAULT on error reading the location
720 * -EINVAL on a failed compare of the contents
721 * -EPERM on error writing to the location
722 * Any other value will be considered a failure.
724 #ifndef ftrace_init_nop
725 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
727 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
732 * ftrace_make_call - convert a nop call site into a call to addr
733 * @rec: the call site record (e.g. mcount/fentry)
734 * @addr: the address that the call site should call
736 * This is a very sensitive operation and great care needs
737 * to be taken by the arch. The operation should carefully
738 * read the location, check to see if what is read is indeed
739 * what we expect it to be, and then on success of the compare,
740 * it should write to the location.
742 * The code segment at @rec->ip should be a nop
746 * -EFAULT on error reading the location
747 * -EINVAL on a failed compare of the contents
748 * -EPERM on error writing to the location
749 * Any other value will be considered a failure.
751 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
753 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
755 * ftrace_modify_call - convert from one addr to another (no nop)
756 * @rec: the call site record (e.g. mcount/fentry)
757 * @old_addr: the address expected to be currently called to
758 * @addr: the address to change to
760 * This is a very sensitive operation and great care needs
761 * to be taken by the arch. The operation should carefully
762 * read the location, check to see if what is read is indeed
763 * what we expect it to be, and then on success of the compare,
764 * it should write to the location.
766 * The code segment at @rec->ip should be a caller to @old_addr
770 * -EFAULT on error reading the location
771 * -EINVAL on a failed compare of the contents
772 * -EPERM on error writing to the location
773 * Any other value will be considered a failure.
775 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
778 /* Should never be called */
779 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
786 /* May be defined in arch */
787 extern int ftrace_arch_read_dyn_info(char *buf, int size);
789 extern int skip_trace(unsigned long ip);
790 extern void ftrace_module_init(struct module *mod);
791 extern void ftrace_module_enable(struct module *mod);
792 extern void ftrace_release_mod(struct module *mod);
794 extern void ftrace_disable_daemon(void);
795 extern void ftrace_enable_daemon(void);
796 #else /* CONFIG_DYNAMIC_FTRACE */
797 static inline int skip_trace(unsigned long ip) { return 0; }
798 static inline void ftrace_disable_daemon(void) { }
799 static inline void ftrace_enable_daemon(void) { }
800 static inline void ftrace_module_init(struct module *mod) { }
801 static inline void ftrace_module_enable(struct module *mod) { }
802 static inline void ftrace_release_mod(struct module *mod) { }
803 static inline int ftrace_text_reserved(const void *start, const void *end)
807 static inline unsigned long ftrace_location(unsigned long ip)
813 * Again users of functions that have ftrace_ops may not
814 * have them defined when ftrace is not enabled, but these
815 * functions may still be called. Use a macro instead of inline.
817 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
818 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
819 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
820 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
821 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
822 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
823 #define ftrace_free_filter(ops) do { } while (0)
824 #define ftrace_ops_set_global_filter(ops) do { } while (0)
826 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
827 size_t cnt, loff_t *ppos) { return -ENODEV; }
828 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
829 size_t cnt, loff_t *ppos) { return -ENODEV; }
831 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
833 static inline bool is_ftrace_trampoline(unsigned long addr)
837 #endif /* CONFIG_DYNAMIC_FTRACE */
839 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
840 #ifndef ftrace_graph_func
841 #define ftrace_graph_func ftrace_stub
842 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
844 #define FTRACE_OPS_GRAPH_STUB 0
846 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
848 /* totally disable ftrace - can not re-enable after this */
849 void ftrace_kill(void);
851 static inline void tracer_disable(void)
853 #ifdef CONFIG_FUNCTION_TRACER
859 * Ftrace disable/restore without lock. Some synchronization mechanism
860 * must be used to prevent ftrace_enabled to be changed between
863 static inline int __ftrace_enabled_save(void)
865 #ifdef CONFIG_FUNCTION_TRACER
866 int saved_ftrace_enabled = ftrace_enabled;
868 return saved_ftrace_enabled;
874 static inline void __ftrace_enabled_restore(int enabled)
876 #ifdef CONFIG_FUNCTION_TRACER
877 ftrace_enabled = enabled;
881 /* All archs should have this, but we define it for consistency */
882 #ifndef ftrace_return_address0
883 # define ftrace_return_address0 __builtin_return_address(0)
886 /* Archs may use other ways for ADDR1 and beyond */
887 #ifndef ftrace_return_address
888 # ifdef CONFIG_FRAME_POINTER
889 # define ftrace_return_address(n) __builtin_return_address(n)
891 # define ftrace_return_address(n) 0UL
895 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
896 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
897 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
898 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
899 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
900 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
901 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
903 static inline unsigned long get_lock_parent_ip(void)
905 unsigned long addr = CALLER_ADDR0;
907 if (!in_lock_functions(addr))
910 if (!in_lock_functions(addr))
915 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
916 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
917 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
920 * Use defines instead of static inlines because some arches will make code out
921 * of the CALLER_ADDR, when we really want these to be a real nop.
923 # define trace_preempt_on(a0, a1) do { } while (0)
924 # define trace_preempt_off(a0, a1) do { } while (0)
927 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
928 extern void ftrace_init(void);
929 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
930 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
932 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
935 static inline void ftrace_init(void) { }
939 * Structure that defines an entry function trace.
940 * It's already packed but the attribute "packed" is needed
941 * to remove extra padding at the end.
943 struct ftrace_graph_ent {
944 unsigned long func; /* Current function */
949 * Structure that defines a return function trace.
950 * It's already packed but the attribute "packed" is needed
951 * to remove extra padding at the end.
953 struct ftrace_graph_ret {
954 unsigned long func; /* Current function */
956 /* Number of functions that overran the depth limit for current task */
957 unsigned int overrun;
958 unsigned long long calltime;
959 unsigned long long rettime;
962 /* Type of the callback handlers for tracing function graph*/
963 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
964 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
966 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
968 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
971 trace_func_graph_ent_t entryfunc;
972 trace_func_graph_ret_t retfunc;
976 * Stack of return addresses for functions
978 * Used in struct thread_info
980 struct ftrace_ret_stack {
983 unsigned long long calltime;
984 #ifdef CONFIG_FUNCTION_PROFILER
985 unsigned long long subtime;
987 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
990 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
996 * Primary handler of a function return.
997 * It relays on ftrace_return_to_handler.
998 * Defined in entry_32/64.S
1000 extern void return_to_handler(void);
1003 function_graph_enter(unsigned long ret, unsigned long func,
1004 unsigned long frame_pointer, unsigned long *retp);
1006 struct ftrace_ret_stack *
1007 ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
1009 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1010 unsigned long ret, unsigned long *retp);
1013 * Sometimes we don't want to trace a function with the function
1014 * graph tracer but we want them to keep traced by the usual function
1015 * tracer if the function graph tracer is not configured.
1017 #define __notrace_funcgraph notrace
1019 #define FTRACE_RETFUNC_DEPTH 50
1020 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1022 extern int register_ftrace_graph(struct fgraph_ops *ops);
1023 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1026 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1028 * ftrace_graph_stop() is called when a severe error is detected in
1029 * the function graph tracing. This function is called by the critical
1030 * paths of function graph to keep those paths from doing any more harm.
1032 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1034 static inline bool ftrace_graph_is_dead(void)
1036 return static_branch_unlikely(&kill_ftrace_graph);
1039 extern void ftrace_graph_stop(void);
1041 /* The current handlers in use */
1042 extern trace_func_graph_ret_t ftrace_graph_return;
1043 extern trace_func_graph_ent_t ftrace_graph_entry;
1045 extern void ftrace_graph_init_task(struct task_struct *t);
1046 extern void ftrace_graph_exit_task(struct task_struct *t);
1047 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1049 static inline void pause_graph_tracing(void)
1051 atomic_inc(¤t->tracing_graph_pause);
1054 static inline void unpause_graph_tracing(void)
1056 atomic_dec(¤t->tracing_graph_pause);
1058 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1060 #define __notrace_funcgraph
1062 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1063 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1064 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1066 /* Define as macros as fgraph_ops may not be defined */
1067 #define register_ftrace_graph(ops) ({ -1; })
1068 #define unregister_ftrace_graph(ops) do { } while (0)
1070 static inline unsigned long
1071 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1072 unsigned long *retp)
1077 static inline void pause_graph_tracing(void) { }
1078 static inline void unpause_graph_tracing(void) { }
1079 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1081 #ifdef CONFIG_TRACING
1083 /* flags for current->trace */
1085 TSK_TRACE_FL_TRACE_BIT = 0,
1086 TSK_TRACE_FL_GRAPH_BIT = 1,
1089 TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
1090 TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
1093 static inline void set_tsk_trace_trace(struct task_struct *tsk)
1095 set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1098 static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1100 clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1103 static inline int test_tsk_trace_trace(struct task_struct *tsk)
1105 return tsk->trace & TSK_TRACE_FL_TRACE;
1108 static inline void set_tsk_trace_graph(struct task_struct *tsk)
1110 set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1113 static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1115 clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1118 static inline int test_tsk_trace_graph(struct task_struct *tsk)
1120 return tsk->trace & TSK_TRACE_FL_GRAPH;
1123 enum ftrace_dump_mode;
1125 extern enum ftrace_dump_mode ftrace_dump_on_oops;
1126 extern int tracepoint_printk;
1128 extern void disable_trace_on_warning(void);
1129 extern int __disable_trace_on_warning;
1131 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1132 void *buffer, size_t *lenp, loff_t *ppos);
1134 #else /* CONFIG_TRACING */
1135 static inline void disable_trace_on_warning(void) { }
1136 #endif /* CONFIG_TRACING */
1138 #ifdef CONFIG_FTRACE_SYSCALLS
1140 unsigned long arch_syscall_addr(int nr);
1142 #endif /* CONFIG_FTRACE_SYSCALLS */
1144 #endif /* _LINUX_FTRACE_H */