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),
212 * FTRACE_OPS_CMD_* commands allow the ftrace core logic to request changes
213 * to a ftrace_ops. Note, the requests may fail.
215 * ENABLE_SHARE_IPMODIFY_SELF - enable a DIRECT ops to work on the same
216 * function as an ops with IPMODIFY. Called
217 * when the DIRECT ops is being registered.
218 * This is called with both direct_mutex and
219 * ftrace_lock are locked.
221 * ENABLE_SHARE_IPMODIFY_PEER - enable a DIRECT ops to work on the same
222 * function as an ops with IPMODIFY. Called
223 * when the other ops (the one with IPMODIFY)
224 * is being registered.
225 * This is called with direct_mutex locked.
227 * DISABLE_SHARE_IPMODIFY_PEER - disable a DIRECT ops to work on the same
228 * function as an ops with IPMODIFY. Called
229 * when the other ops (the one with IPMODIFY)
230 * is being unregistered.
231 * This is called with direct_mutex locked.
233 enum ftrace_ops_cmd {
234 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF,
235 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER,
236 FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER,
240 * For most ftrace_ops_cmd,
243 * Negative on failure. The return value is dependent on the
246 typedef int (*ftrace_ops_func_t)(struct ftrace_ops *op, enum ftrace_ops_cmd cmd);
248 #ifdef CONFIG_DYNAMIC_FTRACE
249 /* The hash used to know what functions callbacks trace */
250 struct ftrace_ops_hash {
251 struct ftrace_hash __rcu *notrace_hash;
252 struct ftrace_hash __rcu *filter_hash;
253 struct mutex regex_lock;
256 void ftrace_free_init_mem(void);
257 void ftrace_free_mem(struct module *mod, void *start, void *end);
259 static inline void ftrace_free_init_mem(void)
261 ftrace_boot_snapshot();
263 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
267 * Note, ftrace_ops can be referenced outside of RCU protection, unless
268 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
269 * core data, the unregistering of it will perform a scheduling on all CPUs
270 * to make sure that there are no more users. Depending on the load of the
271 * system that may take a bit of time.
273 * Any private data added must also take care not to be freed and if private
274 * data is added to a ftrace_ops that is in core code, the user of the
275 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
279 struct ftrace_ops __rcu *next;
282 ftrace_func_t saved_func;
283 #ifdef CONFIG_DYNAMIC_FTRACE
284 struct ftrace_ops_hash local_hash;
285 struct ftrace_ops_hash *func_hash;
286 struct ftrace_ops_hash old_hash;
287 unsigned long trampoline;
288 unsigned long trampoline_size;
289 struct list_head list;
290 ftrace_ops_func_t ops_func;
294 extern struct ftrace_ops __rcu *ftrace_ops_list;
295 extern struct ftrace_ops ftrace_list_end;
298 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
299 * can use rcu_dereference_raw_check() is that elements removed from this list
300 * are simply leaked, so there is no need to interact with a grace-period
301 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
302 * concurrent insertions into the ftrace_ops_list.
304 * Silly Alpha and silly pointer-speculation compiler optimizations!
306 #define do_for_each_ftrace_op(op, list) \
307 op = rcu_dereference_raw_check(list); \
311 * Optimized for just a single item in the list (as that is the normal case).
313 #define while_for_each_ftrace_op(op) \
314 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
315 unlikely((op) != &ftrace_list_end))
318 * Type of the current tracing.
320 enum ftrace_tracing_type_t {
321 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
322 FTRACE_TYPE_RETURN, /* Hook the return of the function */
325 /* Current tracing type, default is FTRACE_TYPE_ENTER */
326 extern enum ftrace_tracing_type_t ftrace_tracing_type;
329 * The ftrace_ops must be a static and should also
330 * be read_mostly. These functions do modify read_mostly variables
331 * so use them sparely. Never free an ftrace_op or modify the
332 * next pointer after it has been registered. Even after unregistering
333 * it, the next pointer may still be used internally.
335 int register_ftrace_function(struct ftrace_ops *ops);
336 int unregister_ftrace_function(struct ftrace_ops *ops);
338 extern void ftrace_stub(unsigned long a0, unsigned long a1,
339 struct ftrace_ops *op, struct ftrace_regs *fregs);
342 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
343 #else /* !CONFIG_FUNCTION_TRACER */
345 * (un)register_ftrace_function must be a macro since the ops parameter
346 * must not be evaluated.
348 #define register_ftrace_function(ops) ({ 0; })
349 #define unregister_ftrace_function(ops) ({ 0; })
350 static inline void ftrace_kill(void) { }
351 static inline void ftrace_free_init_mem(void) { }
352 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
353 static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
357 #endif /* CONFIG_FUNCTION_TRACER */
359 struct ftrace_func_entry {
360 struct hlist_node hlist;
362 unsigned long direct; /* for direct lookup only */
367 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
368 extern int ftrace_direct_func_count;
369 int register_ftrace_direct(unsigned long ip, unsigned long addr);
370 int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
371 int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
372 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
373 int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
374 struct dyn_ftrace *rec,
375 unsigned long old_addr,
376 unsigned long new_addr);
377 unsigned long ftrace_find_rec_direct(unsigned long ip);
378 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
379 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
380 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
381 int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr);
385 # define ftrace_direct_func_count 0
386 static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
390 static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
394 static inline int modify_ftrace_direct(unsigned long ip,
395 unsigned long old_addr, unsigned long new_addr)
399 static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
403 static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
404 struct dyn_ftrace *rec,
405 unsigned long old_addr,
406 unsigned long new_addr)
410 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
414 static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
418 static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
422 static inline int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
426 static inline int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr)
430 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
432 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
434 * This must be implemented by the architecture.
435 * It is the way the ftrace direct_ops helper, when called
436 * via ftrace (because there's other callbacks besides the
437 * direct call), can inform the architecture's trampoline that this
438 * routine has a direct caller, and what the caller is.
440 * For example, in x86, it returns the direct caller
441 * callback function via the regs->orig_ax parameter.
442 * Then in the ftrace trampoline, if this is set, it makes
443 * the return from the trampoline jump to the direct caller
444 * instead of going back to the function it just traced.
446 static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
447 unsigned long addr) { }
448 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
450 #ifdef CONFIG_STACK_TRACER
452 extern int stack_tracer_enabled;
454 int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
455 size_t *lenp, loff_t *ppos);
457 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
458 DECLARE_PER_CPU(int, disable_stack_tracer);
461 * stack_tracer_disable - temporarily disable the stack tracer
463 * There's a few locations (namely in RCU) where stack tracing
464 * cannot be executed. This function is used to disable stack
465 * tracing during those critical sections.
467 * This function must be called with preemption or interrupts
468 * disabled and stack_tracer_enable() must be called shortly after
469 * while preemption or interrupts are still disabled.
471 static inline void stack_tracer_disable(void)
473 /* Preemption or interrupts must be disabled */
474 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
475 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
476 this_cpu_inc(disable_stack_tracer);
480 * stack_tracer_enable - re-enable the stack tracer
482 * After stack_tracer_disable() is called, stack_tracer_enable()
483 * must be called shortly afterward.
485 static inline void stack_tracer_enable(void)
487 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
488 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
489 this_cpu_dec(disable_stack_tracer);
492 static inline void stack_tracer_disable(void) { }
493 static inline void stack_tracer_enable(void) { }
496 #ifdef CONFIG_DYNAMIC_FTRACE
498 void ftrace_arch_code_modify_prepare(void);
499 void ftrace_arch_code_modify_post_process(void);
501 enum ftrace_bug_type {
508 extern enum ftrace_bug_type ftrace_bug_type;
511 * Archs can set this to point to a variable that holds the value that was
512 * expected at the call site before calling ftrace_bug().
514 extern const void *ftrace_expected;
516 void ftrace_bug(int err, struct dyn_ftrace *rec);
520 extern int ftrace_text_reserved(const void *start, const void *end);
522 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
524 bool is_ftrace_trampoline(unsigned long addr);
527 * The dyn_ftrace record's flags field is split into two parts.
528 * the first part which is '0-FTRACE_REF_MAX' is a counter of
529 * the number of callbacks that have registered the function that
530 * the dyn_ftrace descriptor represents.
532 * The second part is a mask:
533 * ENABLED - the function is being traced
534 * REGS - the record wants the function to save regs
535 * REGS_EN - the function is set up to save regs.
536 * IPMODIFY - the record allows for the IP address to be changed.
537 * DISABLED - the record is not ready to be touched yet
538 * DIRECT - there is a direct function to call
540 * When a new ftrace_ops is registered and wants a function to save
541 * pt_regs, the rec->flags REGS is set. When the function has been
542 * set up to save regs, the REG_EN flag is set. Once a function
543 * starts saving regs it will do so until all ftrace_ops are removed
544 * from tracing that function.
547 FTRACE_FL_ENABLED = (1UL << 31),
548 FTRACE_FL_REGS = (1UL << 30),
549 FTRACE_FL_REGS_EN = (1UL << 29),
550 FTRACE_FL_TRAMP = (1UL << 28),
551 FTRACE_FL_TRAMP_EN = (1UL << 27),
552 FTRACE_FL_IPMODIFY = (1UL << 26),
553 FTRACE_FL_DISABLED = (1UL << 25),
554 FTRACE_FL_DIRECT = (1UL << 24),
555 FTRACE_FL_DIRECT_EN = (1UL << 23),
558 #define FTRACE_REF_MAX_SHIFT 23
559 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
561 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
564 unsigned long ip; /* address of mcount call-site */
566 struct dyn_arch_ftrace arch;
569 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
570 int remove, int reset);
571 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
572 unsigned int cnt, int remove, int reset);
573 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
575 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
577 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
578 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
579 void ftrace_free_filter(struct ftrace_ops *ops);
580 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
583 FTRACE_UPDATE_CALLS = (1 << 0),
584 FTRACE_DISABLE_CALLS = (1 << 1),
585 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
586 FTRACE_START_FUNC_RET = (1 << 3),
587 FTRACE_STOP_FUNC_RET = (1 << 4),
588 FTRACE_MAY_SLEEP = (1 << 5),
592 * The FTRACE_UPDATE_* enum is used to pass information back
593 * from the ftrace_update_record() and ftrace_test_record()
594 * functions. These are called by the code update routines
595 * to find out what is to be done for a given function.
597 * IGNORE - The function is already what we want it to be
598 * MAKE_CALL - Start tracing the function
599 * MODIFY_CALL - Stop saving regs for the function
600 * MAKE_NOP - Stop tracing the function
603 FTRACE_UPDATE_IGNORE,
604 FTRACE_UPDATE_MAKE_CALL,
605 FTRACE_UPDATE_MODIFY_CALL,
606 FTRACE_UPDATE_MAKE_NOP,
610 FTRACE_ITER_FILTER = (1 << 0),
611 FTRACE_ITER_NOTRACE = (1 << 1),
612 FTRACE_ITER_PRINTALL = (1 << 2),
613 FTRACE_ITER_DO_PROBES = (1 << 3),
614 FTRACE_ITER_PROBE = (1 << 4),
615 FTRACE_ITER_MOD = (1 << 5),
616 FTRACE_ITER_ENABLED = (1 << 6),
619 void arch_ftrace_update_code(int command);
620 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
621 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
622 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
624 struct ftrace_rec_iter;
626 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
627 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
628 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
630 #define for_ftrace_rec_iter(iter) \
631 for (iter = ftrace_rec_iter_start(); \
633 iter = ftrace_rec_iter_next(iter))
636 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
637 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
638 void ftrace_run_stop_machine(int command);
639 unsigned long ftrace_location(unsigned long ip);
640 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
641 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
642 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
644 extern ftrace_func_t ftrace_trace_function;
646 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
647 struct inode *inode, struct file *file);
648 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
649 size_t cnt, loff_t *ppos);
650 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
651 size_t cnt, loff_t *ppos);
652 int ftrace_regex_release(struct inode *inode, struct file *file);
655 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
657 /* defined in arch */
658 extern int ftrace_ip_converted(unsigned long ip);
659 extern int ftrace_dyn_arch_init(void);
660 extern void ftrace_replace_code(int enable);
661 extern int ftrace_update_ftrace_func(ftrace_func_t func);
662 extern void ftrace_caller(void);
663 extern void ftrace_regs_caller(void);
664 extern void ftrace_call(void);
665 extern void ftrace_regs_call(void);
666 extern void mcount_call(void);
668 void ftrace_modify_all_code(int command);
671 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
674 #ifndef FTRACE_GRAPH_ADDR
675 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
678 #ifndef FTRACE_REGS_ADDR
679 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
680 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
682 # define FTRACE_REGS_ADDR FTRACE_ADDR
687 * If an arch would like functions that are only traced
688 * by the function graph tracer to jump directly to its own
689 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
690 * to be that address to jump to.
692 #ifndef FTRACE_GRAPH_TRAMP_ADDR
693 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
696 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
697 extern void ftrace_graph_caller(void);
698 extern int ftrace_enable_ftrace_graph_caller(void);
699 extern int ftrace_disable_ftrace_graph_caller(void);
701 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
702 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
706 * ftrace_make_nop - convert code into nop
707 * @mod: module structure if called by module load initialization
708 * @rec: the call site record (e.g. mcount/fentry)
709 * @addr: the address that the call site should be calling
711 * This is a very sensitive operation and great care needs
712 * to be taken by the arch. The operation should carefully
713 * read the location, check to see if what is read is indeed
714 * what we expect it to be, and then on success of the compare,
715 * it should write to the location.
717 * The code segment at @rec->ip should be a caller to @addr
721 * -EFAULT on error reading the location
722 * -EINVAL on a failed compare of the contents
723 * -EPERM on error writing to the location
724 * Any other value will be considered a failure.
726 extern int ftrace_make_nop(struct module *mod,
727 struct dyn_ftrace *rec, unsigned long addr);
730 * ftrace_need_init_nop - return whether nop call sites should be initialized
732 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
733 * need to call ftrace_init_nop() if the code is built with that flag.
734 * Architectures where this is not always the case may define their own
738 * 0 if ftrace_init_nop() should be called
739 * Nonzero if ftrace_init_nop() should not be called
742 #ifndef ftrace_need_init_nop
743 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
747 * ftrace_init_nop - initialize a nop call site
748 * @mod: module structure if called by module load initialization
749 * @rec: the call site record (e.g. mcount/fentry)
751 * This is a very sensitive operation and great care needs
752 * to be taken by the arch. The operation should carefully
753 * read the location, check to see if what is read is indeed
754 * what we expect it to be, and then on success of the compare,
755 * it should write to the location.
757 * The code segment at @rec->ip should contain the contents created by
762 * -EFAULT on error reading the location
763 * -EINVAL on a failed compare of the contents
764 * -EPERM on error writing to the location
765 * Any other value will be considered a failure.
767 #ifndef ftrace_init_nop
768 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
770 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
775 * ftrace_make_call - convert a nop call site into a call to addr
776 * @rec: the call site record (e.g. mcount/fentry)
777 * @addr: the address that the call site should call
779 * This is a very sensitive operation and great care needs
780 * to be taken by the arch. The operation should carefully
781 * read the location, check to see if what is read is indeed
782 * what we expect it to be, and then on success of the compare,
783 * it should write to the location.
785 * The code segment at @rec->ip should be a nop
789 * -EFAULT on error reading the location
790 * -EINVAL on a failed compare of the contents
791 * -EPERM on error writing to the location
792 * Any other value will be considered a failure.
794 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
796 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
798 * ftrace_modify_call - convert from one addr to another (no nop)
799 * @rec: the call site record (e.g. mcount/fentry)
800 * @old_addr: the address expected to be currently called to
801 * @addr: the address to change to
803 * This is a very sensitive operation and great care needs
804 * to be taken by the arch. The operation should carefully
805 * read the location, check to see if what is read is indeed
806 * what we expect it to be, and then on success of the compare,
807 * it should write to the location.
809 * The code segment at @rec->ip should be a caller to @old_addr
813 * -EFAULT on error reading the location
814 * -EINVAL on a failed compare of the contents
815 * -EPERM on error writing to the location
816 * Any other value will be considered a failure.
818 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
821 /* Should never be called */
822 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
829 /* May be defined in arch */
830 extern int ftrace_arch_read_dyn_info(char *buf, int size);
832 extern int skip_trace(unsigned long ip);
833 extern void ftrace_module_init(struct module *mod);
834 extern void ftrace_module_enable(struct module *mod);
835 extern void ftrace_release_mod(struct module *mod);
837 extern void ftrace_disable_daemon(void);
838 extern void ftrace_enable_daemon(void);
839 #else /* CONFIG_DYNAMIC_FTRACE */
840 static inline int skip_trace(unsigned long ip) { return 0; }
841 static inline void ftrace_disable_daemon(void) { }
842 static inline void ftrace_enable_daemon(void) { }
843 static inline void ftrace_module_init(struct module *mod) { }
844 static inline void ftrace_module_enable(struct module *mod) { }
845 static inline void ftrace_release_mod(struct module *mod) { }
846 static inline int ftrace_text_reserved(const void *start, const void *end)
850 static inline unsigned long ftrace_location(unsigned long ip)
856 * Again users of functions that have ftrace_ops may not
857 * have them defined when ftrace is not enabled, but these
858 * functions may still be called. Use a macro instead of inline.
860 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
861 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
862 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
863 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
864 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
865 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
866 #define ftrace_free_filter(ops) do { } while (0)
867 #define ftrace_ops_set_global_filter(ops) do { } while (0)
869 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
870 size_t cnt, loff_t *ppos) { return -ENODEV; }
871 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
872 size_t cnt, loff_t *ppos) { return -ENODEV; }
874 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
876 static inline bool is_ftrace_trampoline(unsigned long addr)
880 #endif /* CONFIG_DYNAMIC_FTRACE */
882 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
883 #ifndef ftrace_graph_func
884 #define ftrace_graph_func ftrace_stub
885 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
887 #define FTRACE_OPS_GRAPH_STUB 0
889 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
891 /* totally disable ftrace - can not re-enable after this */
892 void ftrace_kill(void);
894 static inline void tracer_disable(void)
896 #ifdef CONFIG_FUNCTION_TRACER
902 * Ftrace disable/restore without lock. Some synchronization mechanism
903 * must be used to prevent ftrace_enabled to be changed between
906 static inline int __ftrace_enabled_save(void)
908 #ifdef CONFIG_FUNCTION_TRACER
909 int saved_ftrace_enabled = ftrace_enabled;
911 return saved_ftrace_enabled;
917 static inline void __ftrace_enabled_restore(int enabled)
919 #ifdef CONFIG_FUNCTION_TRACER
920 ftrace_enabled = enabled;
924 /* All archs should have this, but we define it for consistency */
925 #ifndef ftrace_return_address0
926 # define ftrace_return_address0 __builtin_return_address(0)
929 /* Archs may use other ways for ADDR1 and beyond */
930 #ifndef ftrace_return_address
931 # ifdef CONFIG_FRAME_POINTER
932 # define ftrace_return_address(n) __builtin_return_address(n)
934 # define ftrace_return_address(n) 0UL
938 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
939 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
940 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
941 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
942 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
943 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
944 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
946 static inline unsigned long get_lock_parent_ip(void)
948 unsigned long addr = CALLER_ADDR0;
950 if (!in_lock_functions(addr))
953 if (!in_lock_functions(addr))
958 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
959 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
960 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
963 * Use defines instead of static inlines because some arches will make code out
964 * of the CALLER_ADDR, when we really want these to be a real nop.
966 # define trace_preempt_on(a0, a1) do { } while (0)
967 # define trace_preempt_off(a0, a1) do { } while (0)
970 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
971 extern void ftrace_init(void);
972 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
973 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
975 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
978 static inline void ftrace_init(void) { }
982 * Structure that defines an entry function trace.
983 * It's already packed but the attribute "packed" is needed
984 * to remove extra padding at the end.
986 struct ftrace_graph_ent {
987 unsigned long func; /* Current function */
992 * Structure that defines a return function trace.
993 * It's already packed but the attribute "packed" is needed
994 * to remove extra padding at the end.
996 struct ftrace_graph_ret {
997 unsigned long func; /* Current function */
999 /* Number of functions that overran the depth limit for current task */
1000 unsigned int overrun;
1001 unsigned long long calltime;
1002 unsigned long long rettime;
1005 /* Type of the callback handlers for tracing function graph*/
1006 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
1007 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
1009 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
1011 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1014 trace_func_graph_ent_t entryfunc;
1015 trace_func_graph_ret_t retfunc;
1019 * Stack of return addresses for functions
1021 * Used in struct thread_info
1023 struct ftrace_ret_stack {
1026 unsigned long long calltime;
1027 #ifdef CONFIG_FUNCTION_PROFILER
1028 unsigned long long subtime;
1030 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
1033 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
1034 unsigned long *retp;
1039 * Primary handler of a function return.
1040 * It relays on ftrace_return_to_handler.
1041 * Defined in entry_32/64.S
1043 extern void return_to_handler(void);
1046 function_graph_enter(unsigned long ret, unsigned long func,
1047 unsigned long frame_pointer, unsigned long *retp);
1049 struct ftrace_ret_stack *
1050 ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
1052 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1053 unsigned long ret, unsigned long *retp);
1056 * Sometimes we don't want to trace a function with the function
1057 * graph tracer but we want them to keep traced by the usual function
1058 * tracer if the function graph tracer is not configured.
1060 #define __notrace_funcgraph notrace
1062 #define FTRACE_RETFUNC_DEPTH 50
1063 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1065 extern int register_ftrace_graph(struct fgraph_ops *ops);
1066 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1069 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1071 * ftrace_graph_stop() is called when a severe error is detected in
1072 * the function graph tracing. This function is called by the critical
1073 * paths of function graph to keep those paths from doing any more harm.
1075 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1077 static inline bool ftrace_graph_is_dead(void)
1079 return static_branch_unlikely(&kill_ftrace_graph);
1082 extern void ftrace_graph_stop(void);
1084 /* The current handlers in use */
1085 extern trace_func_graph_ret_t ftrace_graph_return;
1086 extern trace_func_graph_ent_t ftrace_graph_entry;
1088 extern void ftrace_graph_init_task(struct task_struct *t);
1089 extern void ftrace_graph_exit_task(struct task_struct *t);
1090 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1092 static inline void pause_graph_tracing(void)
1094 atomic_inc(¤t->tracing_graph_pause);
1097 static inline void unpause_graph_tracing(void)
1099 atomic_dec(¤t->tracing_graph_pause);
1101 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1103 #define __notrace_funcgraph
1105 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1106 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1107 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1109 /* Define as macros as fgraph_ops may not be defined */
1110 #define register_ftrace_graph(ops) ({ -1; })
1111 #define unregister_ftrace_graph(ops) do { } while (0)
1113 static inline unsigned long
1114 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1115 unsigned long *retp)
1120 static inline void pause_graph_tracing(void) { }
1121 static inline void unpause_graph_tracing(void) { }
1122 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1124 #ifdef CONFIG_TRACING
1126 /* flags for current->trace */
1128 TSK_TRACE_FL_TRACE_BIT = 0,
1129 TSK_TRACE_FL_GRAPH_BIT = 1,
1132 TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
1133 TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
1136 static inline void set_tsk_trace_trace(struct task_struct *tsk)
1138 set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1141 static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1143 clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1146 static inline int test_tsk_trace_trace(struct task_struct *tsk)
1148 return tsk->trace & TSK_TRACE_FL_TRACE;
1151 static inline void set_tsk_trace_graph(struct task_struct *tsk)
1153 set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1156 static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1158 clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1161 static inline int test_tsk_trace_graph(struct task_struct *tsk)
1163 return tsk->trace & TSK_TRACE_FL_GRAPH;
1166 enum ftrace_dump_mode;
1168 extern enum ftrace_dump_mode ftrace_dump_on_oops;
1169 extern int tracepoint_printk;
1171 extern void disable_trace_on_warning(void);
1172 extern int __disable_trace_on_warning;
1174 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1175 void *buffer, size_t *lenp, loff_t *ppos);
1177 #else /* CONFIG_TRACING */
1178 static inline void disable_trace_on_warning(void) { }
1179 #endif /* CONFIG_TRACING */
1181 #ifdef CONFIG_FTRACE_SYSCALLS
1183 unsigned long arch_syscall_addr(int nr);
1185 #endif /* CONFIG_FTRACE_SYSCALLS */
1187 #endif /* _LINUX_FTRACE_H */