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 ftrace_enable_sysctl(struct ctl_table *table, int write,
106 void *buffer, size_t *lenp, loff_t *ppos);
108 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
113 #define arch_ftrace_get_regs(fregs) (&(fregs)->regs)
116 * ftrace_instruction_pointer_set() is to be defined by the architecture
117 * if to allow setting of the instruction pointer from the ftrace_regs
118 * when HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports
119 * live kernel patching.
121 #define ftrace_instruction_pointer_set(fregs, ip) do { } while (0)
122 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
124 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
129 return arch_ftrace_get_regs(fregs);
132 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
133 struct ftrace_ops *op, struct ftrace_regs *fregs);
135 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
138 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
139 * set in the flags member.
140 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
141 * IPMODIFY are a kind of attribute flags which can be set only before
142 * registering the ftrace_ops, and can not be modified while registered.
143 * Changing those attribute flags after registering ftrace_ops will
144 * cause unexpected results.
146 * ENABLED - set/unset when ftrace_ops is registered/unregistered
147 * DYNAMIC - set when ftrace_ops is registered to denote dynamically
148 * allocated ftrace_ops which need special care
149 * SAVE_REGS - The ftrace_ops wants regs saved at each function called
150 * and passed to the callback. If this flag is set, but the
151 * architecture does not support passing regs
152 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
153 * ftrace_ops will fail to register, unless the next flag
155 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
156 * handler can handle an arch that does not save regs
157 * (the handler tests if regs == NULL), then it can set
158 * this flag instead. It will not fail registering the ftrace_ops
159 * but, the regs field will be NULL if the arch does not support
160 * passing regs to the handler.
161 * Note, if this flag is set, the SAVE_REGS flag will automatically
162 * get set upon registering the ftrace_ops, if the arch supports it.
163 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
164 * that the call back needs recursion protection. If it does
165 * not set this, then the ftrace infrastructure will assume
166 * that the callback can handle recursion on its own.
167 * STUB - The ftrace_ops is just a place holder.
168 * INITIALIZED - The ftrace_ops has already been initialized (first use time
169 * register_ftrace_function() is called, it will initialized the ops)
170 * DELETED - The ops are being deleted, do not let them be registered again.
171 * ADDING - The ops is in the process of being added.
172 * REMOVING - The ops is in the process of being removed.
173 * MODIFYING - The ops is in the process of changing its filter functions.
174 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
175 * The arch specific code sets this flag when it allocated a
176 * trampoline. This lets the arch know that it can update the
177 * trampoline in case the callback function changes.
178 * The ftrace_ops trampoline can be set by the ftrace users, and
179 * in such cases the arch must not modify it. Only the arch ftrace
180 * core code should set this flag.
181 * IPMODIFY - The ops can modify the IP register. This can only be set with
182 * SAVE_REGS. If another ops with this flag set is already registered
183 * for any of the functions that this ops will be registered for, then
184 * this ops will fail to register or set_filter_ip.
185 * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
186 * RCU - Set when the ops can only be called when RCU is watching.
187 * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
188 * PERMANENT - Set when the ops is permanent and should not be affected by
190 * DIRECT - Used by the direct ftrace_ops helper for direct functions
191 * (internal ftrace only, should not be used by others)
194 FTRACE_OPS_FL_ENABLED = BIT(0),
195 FTRACE_OPS_FL_DYNAMIC = BIT(1),
196 FTRACE_OPS_FL_SAVE_REGS = BIT(2),
197 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3),
198 FTRACE_OPS_FL_RECURSION = BIT(4),
199 FTRACE_OPS_FL_STUB = BIT(5),
200 FTRACE_OPS_FL_INITIALIZED = BIT(6),
201 FTRACE_OPS_FL_DELETED = BIT(7),
202 FTRACE_OPS_FL_ADDING = BIT(8),
203 FTRACE_OPS_FL_REMOVING = BIT(9),
204 FTRACE_OPS_FL_MODIFYING = BIT(10),
205 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11),
206 FTRACE_OPS_FL_IPMODIFY = BIT(12),
207 FTRACE_OPS_FL_PID = BIT(13),
208 FTRACE_OPS_FL_RCU = BIT(14),
209 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15),
210 FTRACE_OPS_FL_PERMANENT = BIT(16),
211 FTRACE_OPS_FL_DIRECT = BIT(17),
214 #ifdef CONFIG_DYNAMIC_FTRACE
215 /* The hash used to know what functions callbacks trace */
216 struct ftrace_ops_hash {
217 struct ftrace_hash __rcu *notrace_hash;
218 struct ftrace_hash __rcu *filter_hash;
219 struct mutex regex_lock;
222 void ftrace_free_init_mem(void);
223 void ftrace_free_mem(struct module *mod, void *start, void *end);
225 static inline void ftrace_free_init_mem(void)
227 ftrace_boot_snapshot();
229 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
233 * Note, ftrace_ops can be referenced outside of RCU protection, unless
234 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
235 * core data, the unregistering of it will perform a scheduling on all CPUs
236 * to make sure that there are no more users. Depending on the load of the
237 * system that may take a bit of time.
239 * Any private data added must also take care not to be freed and if private
240 * data is added to a ftrace_ops that is in core code, the user of the
241 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
245 struct ftrace_ops __rcu *next;
248 ftrace_func_t saved_func;
249 #ifdef CONFIG_DYNAMIC_FTRACE
250 struct ftrace_ops_hash local_hash;
251 struct ftrace_ops_hash *func_hash;
252 struct ftrace_ops_hash old_hash;
253 unsigned long trampoline;
254 unsigned long trampoline_size;
255 struct list_head list;
259 extern struct ftrace_ops __rcu *ftrace_ops_list;
260 extern struct ftrace_ops ftrace_list_end;
263 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
264 * can use rcu_dereference_raw_check() is that elements removed from this list
265 * are simply leaked, so there is no need to interact with a grace-period
266 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
267 * concurrent insertions into the ftrace_ops_list.
269 * Silly Alpha and silly pointer-speculation compiler optimizations!
271 #define do_for_each_ftrace_op(op, list) \
272 op = rcu_dereference_raw_check(list); \
276 * Optimized for just a single item in the list (as that is the normal case).
278 #define while_for_each_ftrace_op(op) \
279 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
280 unlikely((op) != &ftrace_list_end))
283 * Type of the current tracing.
285 enum ftrace_tracing_type_t {
286 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
287 FTRACE_TYPE_RETURN, /* Hook the return of the function */
290 /* Current tracing type, default is FTRACE_TYPE_ENTER */
291 extern enum ftrace_tracing_type_t ftrace_tracing_type;
294 * The ftrace_ops must be a static and should also
295 * be read_mostly. These functions do modify read_mostly variables
296 * so use them sparely. Never free an ftrace_op or modify the
297 * next pointer after it has been registered. Even after unregistering
298 * it, the next pointer may still be used internally.
300 int register_ftrace_function(struct ftrace_ops *ops);
301 int unregister_ftrace_function(struct ftrace_ops *ops);
303 extern void ftrace_stub(unsigned long a0, unsigned long a1,
304 struct ftrace_ops *op, struct ftrace_regs *fregs);
306 #else /* !CONFIG_FUNCTION_TRACER */
308 * (un)register_ftrace_function must be a macro since the ops parameter
309 * must not be evaluated.
311 #define register_ftrace_function(ops) ({ 0; })
312 #define unregister_ftrace_function(ops) ({ 0; })
313 static inline void ftrace_kill(void) { }
314 static inline void ftrace_free_init_mem(void) { }
315 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
316 #endif /* CONFIG_FUNCTION_TRACER */
318 struct ftrace_func_entry {
319 struct hlist_node hlist;
321 unsigned long direct; /* for direct lookup only */
326 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
327 extern int ftrace_direct_func_count;
328 int register_ftrace_direct(unsigned long ip, unsigned long addr);
329 int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
330 int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
331 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
332 int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
333 struct dyn_ftrace *rec,
334 unsigned long old_addr,
335 unsigned long new_addr);
336 unsigned long ftrace_find_rec_direct(unsigned long ip);
337 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
338 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
339 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
343 # define ftrace_direct_func_count 0
344 static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
348 static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
352 static inline int modify_ftrace_direct(unsigned long ip,
353 unsigned long old_addr, unsigned long new_addr)
357 static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
361 static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
362 struct dyn_ftrace *rec,
363 unsigned long old_addr,
364 unsigned long new_addr)
368 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
372 static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
376 static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
380 static inline int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
384 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
386 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
388 * This must be implemented by the architecture.
389 * It is the way the ftrace direct_ops helper, when called
390 * via ftrace (because there's other callbacks besides the
391 * direct call), can inform the architecture's trampoline that this
392 * routine has a direct caller, and what the caller is.
394 * For example, in x86, it returns the direct caller
395 * callback function via the regs->orig_ax parameter.
396 * Then in the ftrace trampoline, if this is set, it makes
397 * the return from the trampoline jump to the direct caller
398 * instead of going back to the function it just traced.
400 static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
401 unsigned long addr) { }
402 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
404 #ifdef CONFIG_STACK_TRACER
406 extern int stack_tracer_enabled;
408 int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
409 size_t *lenp, loff_t *ppos);
411 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
412 DECLARE_PER_CPU(int, disable_stack_tracer);
415 * stack_tracer_disable - temporarily disable the stack tracer
417 * There's a few locations (namely in RCU) where stack tracing
418 * cannot be executed. This function is used to disable stack
419 * tracing during those critical sections.
421 * This function must be called with preemption or interrupts
422 * disabled and stack_tracer_enable() must be called shortly after
423 * while preemption or interrupts are still disabled.
425 static inline void stack_tracer_disable(void)
427 /* Preemption or interrupts must be disabled */
428 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
429 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
430 this_cpu_inc(disable_stack_tracer);
434 * stack_tracer_enable - re-enable the stack tracer
436 * After stack_tracer_disable() is called, stack_tracer_enable()
437 * must be called shortly afterward.
439 static inline void stack_tracer_enable(void)
441 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
442 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
443 this_cpu_dec(disable_stack_tracer);
446 static inline void stack_tracer_disable(void) { }
447 static inline void stack_tracer_enable(void) { }
450 #ifdef CONFIG_DYNAMIC_FTRACE
452 int ftrace_arch_code_modify_prepare(void);
453 int ftrace_arch_code_modify_post_process(void);
455 enum ftrace_bug_type {
462 extern enum ftrace_bug_type ftrace_bug_type;
465 * Archs can set this to point to a variable that holds the value that was
466 * expected at the call site before calling ftrace_bug().
468 extern const void *ftrace_expected;
470 void ftrace_bug(int err, struct dyn_ftrace *rec);
474 extern int ftrace_text_reserved(const void *start, const void *end);
476 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
478 bool is_ftrace_trampoline(unsigned long addr);
481 * The dyn_ftrace record's flags field is split into two parts.
482 * the first part which is '0-FTRACE_REF_MAX' is a counter of
483 * the number of callbacks that have registered the function that
484 * the dyn_ftrace descriptor represents.
486 * The second part is a mask:
487 * ENABLED - the function is being traced
488 * REGS - the record wants the function to save regs
489 * REGS_EN - the function is set up to save regs.
490 * IPMODIFY - the record allows for the IP address to be changed.
491 * DISABLED - the record is not ready to be touched yet
492 * DIRECT - there is a direct function to call
494 * When a new ftrace_ops is registered and wants a function to save
495 * pt_regs, the rec->flags REGS is set. When the function has been
496 * set up to save regs, the REG_EN flag is set. Once a function
497 * starts saving regs it will do so until all ftrace_ops are removed
498 * from tracing that function.
501 FTRACE_FL_ENABLED = (1UL << 31),
502 FTRACE_FL_REGS = (1UL << 30),
503 FTRACE_FL_REGS_EN = (1UL << 29),
504 FTRACE_FL_TRAMP = (1UL << 28),
505 FTRACE_FL_TRAMP_EN = (1UL << 27),
506 FTRACE_FL_IPMODIFY = (1UL << 26),
507 FTRACE_FL_DISABLED = (1UL << 25),
508 FTRACE_FL_DIRECT = (1UL << 24),
509 FTRACE_FL_DIRECT_EN = (1UL << 23),
512 #define FTRACE_REF_MAX_SHIFT 23
513 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
515 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
518 unsigned long ip; /* address of mcount call-site */
520 struct dyn_arch_ftrace arch;
523 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
524 int remove, int reset);
525 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
526 unsigned int cnt, int remove, int reset);
527 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
529 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
531 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
532 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
533 void ftrace_free_filter(struct ftrace_ops *ops);
534 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
537 FTRACE_UPDATE_CALLS = (1 << 0),
538 FTRACE_DISABLE_CALLS = (1 << 1),
539 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
540 FTRACE_START_FUNC_RET = (1 << 3),
541 FTRACE_STOP_FUNC_RET = (1 << 4),
542 FTRACE_MAY_SLEEP = (1 << 5),
546 * The FTRACE_UPDATE_* enum is used to pass information back
547 * from the ftrace_update_record() and ftrace_test_record()
548 * functions. These are called by the code update routines
549 * to find out what is to be done for a given function.
551 * IGNORE - The function is already what we want it to be
552 * MAKE_CALL - Start tracing the function
553 * MODIFY_CALL - Stop saving regs for the function
554 * MAKE_NOP - Stop tracing the function
557 FTRACE_UPDATE_IGNORE,
558 FTRACE_UPDATE_MAKE_CALL,
559 FTRACE_UPDATE_MODIFY_CALL,
560 FTRACE_UPDATE_MAKE_NOP,
564 FTRACE_ITER_FILTER = (1 << 0),
565 FTRACE_ITER_NOTRACE = (1 << 1),
566 FTRACE_ITER_PRINTALL = (1 << 2),
567 FTRACE_ITER_DO_PROBES = (1 << 3),
568 FTRACE_ITER_PROBE = (1 << 4),
569 FTRACE_ITER_MOD = (1 << 5),
570 FTRACE_ITER_ENABLED = (1 << 6),
573 void arch_ftrace_update_code(int command);
574 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
575 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
576 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
578 struct ftrace_rec_iter;
580 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
581 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
582 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
584 #define for_ftrace_rec_iter(iter) \
585 for (iter = ftrace_rec_iter_start(); \
587 iter = ftrace_rec_iter_next(iter))
590 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
591 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
592 void ftrace_run_stop_machine(int command);
593 unsigned long ftrace_location(unsigned long ip);
594 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
595 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
596 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
598 extern ftrace_func_t ftrace_trace_function;
600 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
601 struct inode *inode, struct file *file);
602 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
603 size_t cnt, loff_t *ppos);
604 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
605 size_t cnt, loff_t *ppos);
606 int ftrace_regex_release(struct inode *inode, struct file *file);
609 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
611 /* defined in arch */
612 extern int ftrace_ip_converted(unsigned long ip);
613 extern int ftrace_dyn_arch_init(void);
614 extern void ftrace_replace_code(int enable);
615 extern int ftrace_update_ftrace_func(ftrace_func_t func);
616 extern void ftrace_caller(void);
617 extern void ftrace_regs_caller(void);
618 extern void ftrace_call(void);
619 extern void ftrace_regs_call(void);
620 extern void mcount_call(void);
622 void ftrace_modify_all_code(int command);
625 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
628 #ifndef FTRACE_GRAPH_ADDR
629 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
632 #ifndef FTRACE_REGS_ADDR
633 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
634 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
636 # define FTRACE_REGS_ADDR FTRACE_ADDR
641 * If an arch would like functions that are only traced
642 * by the function graph tracer to jump directly to its own
643 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
644 * to be that address to jump to.
646 #ifndef FTRACE_GRAPH_TRAMP_ADDR
647 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
650 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
651 extern void ftrace_graph_caller(void);
652 extern int ftrace_enable_ftrace_graph_caller(void);
653 extern int ftrace_disable_ftrace_graph_caller(void);
655 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
656 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
660 * ftrace_make_nop - convert code into nop
661 * @mod: module structure if called by module load initialization
662 * @rec: the call site record (e.g. mcount/fentry)
663 * @addr: the address that the call site should be calling
665 * This is a very sensitive operation and great care needs
666 * to be taken by the arch. The operation should carefully
667 * read the location, check to see if what is read is indeed
668 * what we expect it to be, and then on success of the compare,
669 * it should write to the location.
671 * The code segment at @rec->ip should be a caller to @addr
675 * -EFAULT on error reading the location
676 * -EINVAL on a failed compare of the contents
677 * -EPERM on error writing to the location
678 * Any other value will be considered a failure.
680 extern int ftrace_make_nop(struct module *mod,
681 struct dyn_ftrace *rec, unsigned long addr);
684 * ftrace_need_init_nop - return whether nop call sites should be initialized
686 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
687 * need to call ftrace_init_nop() if the code is built with that flag.
688 * Architectures where this is not always the case may define their own
692 * 0 if ftrace_init_nop() should be called
693 * Nonzero if ftrace_init_nop() should not be called
696 #ifndef ftrace_need_init_nop
697 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
701 * ftrace_init_nop - initialize a nop call site
702 * @mod: module structure if called by module load initialization
703 * @rec: the call site record (e.g. mcount/fentry)
705 * This is a very sensitive operation and great care needs
706 * to be taken by the arch. The operation should carefully
707 * read the location, check to see if what is read is indeed
708 * what we expect it to be, and then on success of the compare,
709 * it should write to the location.
711 * The code segment at @rec->ip should contain the contents created by
716 * -EFAULT on error reading the location
717 * -EINVAL on a failed compare of the contents
718 * -EPERM on error writing to the location
719 * Any other value will be considered a failure.
721 #ifndef ftrace_init_nop
722 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
724 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
729 * ftrace_make_call - convert a nop call site into a call to addr
730 * @rec: the call site record (e.g. mcount/fentry)
731 * @addr: the address that the call site should call
733 * This is a very sensitive operation and great care needs
734 * to be taken by the arch. The operation should carefully
735 * read the location, check to see if what is read is indeed
736 * what we expect it to be, and then on success of the compare,
737 * it should write to the location.
739 * The code segment at @rec->ip should be a nop
743 * -EFAULT on error reading the location
744 * -EINVAL on a failed compare of the contents
745 * -EPERM on error writing to the location
746 * Any other value will be considered a failure.
748 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
750 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
752 * ftrace_modify_call - convert from one addr to another (no nop)
753 * @rec: the call site record (e.g. mcount/fentry)
754 * @old_addr: the address expected to be currently called to
755 * @addr: the address to change to
757 * This is a very sensitive operation and great care needs
758 * to be taken by the arch. The operation should carefully
759 * read the location, check to see if what is read is indeed
760 * what we expect it to be, and then on success of the compare,
761 * it should write to the location.
763 * The code segment at @rec->ip should be a caller to @old_addr
767 * -EFAULT on error reading the location
768 * -EINVAL on a failed compare of the contents
769 * -EPERM on error writing to the location
770 * Any other value will be considered a failure.
772 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
775 /* Should never be called */
776 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
783 /* May be defined in arch */
784 extern int ftrace_arch_read_dyn_info(char *buf, int size);
786 extern int skip_trace(unsigned long ip);
787 extern void ftrace_module_init(struct module *mod);
788 extern void ftrace_module_enable(struct module *mod);
789 extern void ftrace_release_mod(struct module *mod);
791 extern void ftrace_disable_daemon(void);
792 extern void ftrace_enable_daemon(void);
793 #else /* CONFIG_DYNAMIC_FTRACE */
794 static inline int skip_trace(unsigned long ip) { return 0; }
795 static inline void ftrace_disable_daemon(void) { }
796 static inline void ftrace_enable_daemon(void) { }
797 static inline void ftrace_module_init(struct module *mod) { }
798 static inline void ftrace_module_enable(struct module *mod) { }
799 static inline void ftrace_release_mod(struct module *mod) { }
800 static inline int ftrace_text_reserved(const void *start, const void *end)
804 static inline unsigned long ftrace_location(unsigned long ip)
810 * Again users of functions that have ftrace_ops may not
811 * have them defined when ftrace is not enabled, but these
812 * functions may still be called. Use a macro instead of inline.
814 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
815 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
816 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
817 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
818 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
819 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
820 #define ftrace_free_filter(ops) do { } while (0)
821 #define ftrace_ops_set_global_filter(ops) do { } while (0)
823 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
824 size_t cnt, loff_t *ppos) { return -ENODEV; }
825 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
826 size_t cnt, loff_t *ppos) { return -ENODEV; }
828 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
830 static inline bool is_ftrace_trampoline(unsigned long addr)
834 #endif /* CONFIG_DYNAMIC_FTRACE */
836 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
837 #ifndef ftrace_graph_func
838 #define ftrace_graph_func ftrace_stub
839 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
841 #define FTRACE_OPS_GRAPH_STUB 0
843 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
845 /* totally disable ftrace - can not re-enable after this */
846 void ftrace_kill(void);
848 static inline void tracer_disable(void)
850 #ifdef CONFIG_FUNCTION_TRACER
856 * Ftrace disable/restore without lock. Some synchronization mechanism
857 * must be used to prevent ftrace_enabled to be changed between
860 static inline int __ftrace_enabled_save(void)
862 #ifdef CONFIG_FUNCTION_TRACER
863 int saved_ftrace_enabled = ftrace_enabled;
865 return saved_ftrace_enabled;
871 static inline void __ftrace_enabled_restore(int enabled)
873 #ifdef CONFIG_FUNCTION_TRACER
874 ftrace_enabled = enabled;
878 /* All archs should have this, but we define it for consistency */
879 #ifndef ftrace_return_address0
880 # define ftrace_return_address0 __builtin_return_address(0)
883 /* Archs may use other ways for ADDR1 and beyond */
884 #ifndef ftrace_return_address
885 # ifdef CONFIG_FRAME_POINTER
886 # define ftrace_return_address(n) __builtin_return_address(n)
888 # define ftrace_return_address(n) 0UL
892 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
893 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
894 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
895 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
896 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
897 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
898 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
900 static inline unsigned long get_lock_parent_ip(void)
902 unsigned long addr = CALLER_ADDR0;
904 if (!in_lock_functions(addr))
907 if (!in_lock_functions(addr))
912 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
913 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
914 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
917 * Use defines instead of static inlines because some arches will make code out
918 * of the CALLER_ADDR, when we really want these to be a real nop.
920 # define trace_preempt_on(a0, a1) do { } while (0)
921 # define trace_preempt_off(a0, a1) do { } while (0)
924 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
925 extern void ftrace_init(void);
926 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
927 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
929 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
932 static inline void ftrace_init(void) { }
936 * Structure that defines an entry function trace.
937 * It's already packed but the attribute "packed" is needed
938 * to remove extra padding at the end.
940 struct ftrace_graph_ent {
941 unsigned long func; /* Current function */
946 * Structure that defines a return function trace.
947 * It's already packed but the attribute "packed" is needed
948 * to remove extra padding at the end.
950 struct ftrace_graph_ret {
951 unsigned long func; /* Current function */
953 /* Number of functions that overran the depth limit for current task */
954 unsigned int overrun;
955 unsigned long long calltime;
956 unsigned long long rettime;
959 /* Type of the callback handlers for tracing function graph*/
960 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
961 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
963 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
965 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
968 trace_func_graph_ent_t entryfunc;
969 trace_func_graph_ret_t retfunc;
973 * Stack of return addresses for functions
975 * Used in struct thread_info
977 struct ftrace_ret_stack {
980 unsigned long long calltime;
981 #ifdef CONFIG_FUNCTION_PROFILER
982 unsigned long long subtime;
984 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
987 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
993 * Primary handler of a function return.
994 * It relays on ftrace_return_to_handler.
995 * Defined in entry_32/64.S
997 extern void return_to_handler(void);
1000 function_graph_enter(unsigned long ret, unsigned long func,
1001 unsigned long frame_pointer, unsigned long *retp);
1003 struct ftrace_ret_stack *
1004 ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
1006 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1007 unsigned long ret, unsigned long *retp);
1010 * Sometimes we don't want to trace a function with the function
1011 * graph tracer but we want them to keep traced by the usual function
1012 * tracer if the function graph tracer is not configured.
1014 #define __notrace_funcgraph notrace
1016 #define FTRACE_RETFUNC_DEPTH 50
1017 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1019 extern int register_ftrace_graph(struct fgraph_ops *ops);
1020 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1023 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1025 * ftrace_graph_stop() is called when a severe error is detected in
1026 * the function graph tracing. This function is called by the critical
1027 * paths of function graph to keep those paths from doing any more harm.
1029 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1031 static inline bool ftrace_graph_is_dead(void)
1033 return static_branch_unlikely(&kill_ftrace_graph);
1036 extern void ftrace_graph_stop(void);
1038 /* The current handlers in use */
1039 extern trace_func_graph_ret_t ftrace_graph_return;
1040 extern trace_func_graph_ent_t ftrace_graph_entry;
1042 extern void ftrace_graph_init_task(struct task_struct *t);
1043 extern void ftrace_graph_exit_task(struct task_struct *t);
1044 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1046 static inline void pause_graph_tracing(void)
1048 atomic_inc(¤t->tracing_graph_pause);
1051 static inline void unpause_graph_tracing(void)
1053 atomic_dec(¤t->tracing_graph_pause);
1055 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1057 #define __notrace_funcgraph
1059 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1060 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1061 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1063 /* Define as macros as fgraph_ops may not be defined */
1064 #define register_ftrace_graph(ops) ({ -1; })
1065 #define unregister_ftrace_graph(ops) do { } while (0)
1067 static inline unsigned long
1068 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1069 unsigned long *retp)
1074 static inline void pause_graph_tracing(void) { }
1075 static inline void unpause_graph_tracing(void) { }
1076 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1078 #ifdef CONFIG_TRACING
1080 /* flags for current->trace */
1082 TSK_TRACE_FL_TRACE_BIT = 0,
1083 TSK_TRACE_FL_GRAPH_BIT = 1,
1086 TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
1087 TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
1090 static inline void set_tsk_trace_trace(struct task_struct *tsk)
1092 set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1095 static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1097 clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1100 static inline int test_tsk_trace_trace(struct task_struct *tsk)
1102 return tsk->trace & TSK_TRACE_FL_TRACE;
1105 static inline void set_tsk_trace_graph(struct task_struct *tsk)
1107 set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1110 static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1112 clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1115 static inline int test_tsk_trace_graph(struct task_struct *tsk)
1117 return tsk->trace & TSK_TRACE_FL_GRAPH;
1120 enum ftrace_dump_mode;
1122 extern enum ftrace_dump_mode ftrace_dump_on_oops;
1123 extern int tracepoint_printk;
1125 extern void disable_trace_on_warning(void);
1126 extern int __disable_trace_on_warning;
1128 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1129 void *buffer, size_t *lenp, loff_t *ppos);
1131 #else /* CONFIG_TRACING */
1132 static inline void disable_trace_on_warning(void) { }
1133 #endif /* CONFIG_TRACING */
1135 #ifdef CONFIG_FTRACE_SYSCALLS
1137 unsigned long arch_syscall_addr(int nr);
1139 #endif /* CONFIG_FTRACE_SYSCALLS */
1141 #endif /* _LINUX_FTRACE_H */