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) { }
44 char *arch_ftrace_match_adjust(char *str, const char *search);
46 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_FREGS
47 unsigned long ftrace_return_to_handler(struct ftrace_regs *fregs);
49 unsigned long ftrace_return_to_handler(unsigned long frame_pointer);
52 #ifdef CONFIG_FUNCTION_TRACER
54 * If the arch's mcount caller does not support all of ftrace's
55 * features, then it must call an indirect function that
56 * does. Or at least does enough to prevent any unwelcome side effects.
58 * Also define the function prototype that these architectures use
59 * to call the ftrace_ops_list_func().
61 #if !ARCH_SUPPORTS_FTRACE_OPS
62 # define FTRACE_FORCE_LIST_FUNC 1
63 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
65 # define FTRACE_FORCE_LIST_FUNC 0
66 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
67 struct ftrace_ops *op, struct ftrace_regs *fregs);
69 extern const struct ftrace_ops ftrace_nop_ops;
70 extern const struct ftrace_ops ftrace_list_ops;
71 struct ftrace_ops *ftrace_find_unique_ops(struct dyn_ftrace *rec);
72 #endif /* CONFIG_FUNCTION_TRACER */
74 /* Main tracing buffer and events set up */
76 void trace_init(void);
77 void early_trace_init(void);
79 static inline void trace_init(void) { }
80 static inline void early_trace_init(void) { }
86 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
87 defined(CONFIG_DYNAMIC_FTRACE)
89 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
90 unsigned long *off, char **modname, char *sym);
93 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
94 unsigned long *off, char **modname, char *sym)
100 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
101 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
102 char *type, char *name,
103 char *module_name, int *exported);
105 static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
106 char *type, char *name,
107 char *module_name, int *exported)
113 #ifdef CONFIG_FUNCTION_TRACER
115 #include <linux/ftrace_regs.h>
117 extern int ftrace_enabled;
120 * ftrace_regs - ftrace partial/optimal register set
122 * ftrace_regs represents a group of registers which is used at the
123 * function entry and exit. There are three types of registers.
125 * - Registers for passing the parameters to callee, including the stack
126 * pointer. (e.g. rcx, rdx, rdi, rsi, r8, r9 and rsp on x86_64)
127 * - Registers for passing the return values to caller.
128 * (e.g. rax and rdx on x86_64)
129 * - Registers for hooking the function call and return including the
130 * frame pointer (the frame pointer is architecture/config dependent)
131 * (e.g. rip, rbp and rsp for x86_64)
133 * Also, architecture dependent fields can be used for internal process.
134 * (e.g. orig_ax on x86_64)
136 * Basically, ftrace_regs stores the registers related to the context.
137 * On function entry, registers for function parameters and hooking the
138 * function call are stored, and on function exit, registers for function
139 * return value and frame pointers are stored.
141 * And also, it dpends on the context that which registers are restored
142 * from the ftrace_regs.
143 * On the function entry, those registers will be restored except for
144 * the stack pointer, so that user can change the function parameters
145 * and instruction pointer (e.g. live patching.)
146 * On the function exit, only registers which is used for return values
149 * NOTE: user *must not* access regs directly, only do it via APIs, because
150 * the member can be changed according to the architecture.
151 * This is why the structure is empty here, so that nothing accesses
152 * the ftrace_regs directly.
155 /* Nothing to see here, use the accessor functions! */
158 #define ftrace_regs_size() sizeof(struct __arch_ftrace_regs)
160 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
162 * Architectures that define HAVE_DYNAMIC_FTRACE_WITH_ARGS must define their own
163 * arch_ftrace_get_regs() where it only returns pt_regs *if* it is fully
164 * populated. It should return NULL otherwise.
166 static inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs *fregs)
168 return &arch_ftrace_regs(fregs)->regs;
172 * ftrace_regs_set_instruction_pointer() is to be defined by the architecture
173 * if to allow setting of the instruction pointer from the ftrace_regs when
174 * HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports live kernel patching.
176 #define ftrace_regs_set_instruction_pointer(fregs, ip) do { } while (0)
177 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
179 #ifdef CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS
181 static_assert(sizeof(struct pt_regs) == ftrace_regs_size());
183 #endif /* CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */
185 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
190 return arch_ftrace_get_regs(fregs);
193 #if !defined(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS) || \
194 defined(CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS)
196 static __always_inline struct pt_regs *
197 ftrace_partial_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
200 * If CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS=y, ftrace_regs memory
201 * layout is including pt_regs. So always returns that address.
202 * Since arch_ftrace_get_regs() will check some members and may return
203 * NULL, we can not use it.
205 return &arch_ftrace_regs(fregs)->regs;
208 #endif /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS || CONFIG_HAVE_FTRACE_REGS_HAVING_PT_REGS */
210 #ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
213 * Please define arch dependent pt_regs which compatible to the
214 * perf_arch_fetch_caller_regs() but based on ftrace_regs.
216 * - user_mode(_regs) returns false (always kernel mode).
217 * - able to use the _regs for stack trace.
219 #ifndef arch_ftrace_fill_perf_regs
220 /* As same as perf_arch_fetch_caller_regs(), do nothing by default */
221 #define arch_ftrace_fill_perf_regs(fregs, _regs) do {} while (0)
224 static __always_inline struct pt_regs *
225 ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
227 arch_ftrace_fill_perf_regs(fregs, regs);
231 #else /* !CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
233 static __always_inline struct pt_regs *
234 ftrace_fill_perf_regs(struct ftrace_regs *fregs, struct pt_regs *regs)
236 return &arch_ftrace_regs(fregs)->regs;
242 * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs.
243 * Note: this can be true even when ftrace_get_regs() cannot provide a pt_regs.
245 static __always_inline bool ftrace_regs_has_args(struct ftrace_regs *fregs)
247 if (IS_ENABLED(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS))
250 return ftrace_get_regs(fregs) != NULL;
253 #ifdef CONFIG_HAVE_REGS_AND_STACK_ACCESS_API
254 static __always_inline unsigned long
255 ftrace_regs_get_kernel_stack_nth(struct ftrace_regs *fregs, unsigned int nth)
257 unsigned long *stackp;
259 stackp = (unsigned long *)ftrace_regs_get_stack_pointer(fregs);
260 if (((unsigned long)(stackp + nth) & ~(THREAD_SIZE - 1)) ==
261 ((unsigned long)stackp & ~(THREAD_SIZE - 1)))
262 return *(stackp + nth);
266 #else /* !CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */
267 #define ftrace_regs_get_kernel_stack_nth(fregs, nth) (0L)
268 #endif /* CONFIG_HAVE_REGS_AND_STACK_ACCESS_API */
270 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
271 struct ftrace_ops *op, struct ftrace_regs *fregs);
273 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
276 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
277 * set in the flags member.
278 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
279 * IPMODIFY are a kind of attribute flags which can be set only before
280 * registering the ftrace_ops, and can not be modified while registered.
281 * Changing those attribute flags after registering ftrace_ops will
282 * cause unexpected results.
284 * ENABLED - set/unset when ftrace_ops is registered/unregistered
285 * DYNAMIC - set when ftrace_ops is registered to denote dynamically
286 * allocated ftrace_ops which need special care
287 * SAVE_REGS - The ftrace_ops wants regs saved at each function called
288 * and passed to the callback. If this flag is set, but the
289 * architecture does not support passing regs
290 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
291 * ftrace_ops will fail to register, unless the next flag
293 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
294 * handler can handle an arch that does not save regs
295 * (the handler tests if regs == NULL), then it can set
296 * this flag instead. It will not fail registering the ftrace_ops
297 * but, the regs field will be NULL if the arch does not support
298 * passing regs to the handler.
299 * Note, if this flag is set, the SAVE_REGS flag will automatically
300 * get set upon registering the ftrace_ops, if the arch supports it.
301 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
302 * that the call back needs recursion protection. If it does
303 * not set this, then the ftrace infrastructure will assume
304 * that the callback can handle recursion on its own.
305 * STUB - The ftrace_ops is just a place holder.
306 * INITIALIZED - The ftrace_ops has already been initialized (first use time
307 * register_ftrace_function() is called, it will initialized the ops)
308 * DELETED - The ops are being deleted, do not let them be registered again.
309 * ADDING - The ops is in the process of being added.
310 * REMOVING - The ops is in the process of being removed.
311 * MODIFYING - The ops is in the process of changing its filter functions.
312 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
313 * The arch specific code sets this flag when it allocated a
314 * trampoline. This lets the arch know that it can update the
315 * trampoline in case the callback function changes.
316 * The ftrace_ops trampoline can be set by the ftrace users, and
317 * in such cases the arch must not modify it. Only the arch ftrace
318 * core code should set this flag.
319 * IPMODIFY - The ops can modify the IP register. This can only be set with
320 * SAVE_REGS. If another ops with this flag set is already registered
321 * for any of the functions that this ops will be registered for, then
322 * this ops will fail to register or set_filter_ip.
323 * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
324 * RCU - Set when the ops can only be called when RCU is watching.
325 * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
326 * PERMANENT - Set when the ops is permanent and should not be affected by
328 * DIRECT - Used by the direct ftrace_ops helper for direct functions
329 * (internal ftrace only, should not be used by others)
330 * SUBOP - Is controlled by another op in field managed.
331 * GRAPH - Is a component of the fgraph_ops structure
334 FTRACE_OPS_FL_ENABLED = BIT(0),
335 FTRACE_OPS_FL_DYNAMIC = BIT(1),
336 FTRACE_OPS_FL_SAVE_REGS = BIT(2),
337 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3),
338 FTRACE_OPS_FL_RECURSION = BIT(4),
339 FTRACE_OPS_FL_STUB = BIT(5),
340 FTRACE_OPS_FL_INITIALIZED = BIT(6),
341 FTRACE_OPS_FL_DELETED = BIT(7),
342 FTRACE_OPS_FL_ADDING = BIT(8),
343 FTRACE_OPS_FL_REMOVING = BIT(9),
344 FTRACE_OPS_FL_MODIFYING = BIT(10),
345 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11),
346 FTRACE_OPS_FL_IPMODIFY = BIT(12),
347 FTRACE_OPS_FL_PID = BIT(13),
348 FTRACE_OPS_FL_RCU = BIT(14),
349 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15),
350 FTRACE_OPS_FL_PERMANENT = BIT(16),
351 FTRACE_OPS_FL_DIRECT = BIT(17),
352 FTRACE_OPS_FL_SUBOP = BIT(18),
353 FTRACE_OPS_FL_GRAPH = BIT(19),
356 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
357 #define FTRACE_OPS_FL_SAVE_ARGS FTRACE_OPS_FL_SAVE_REGS
359 #define FTRACE_OPS_FL_SAVE_ARGS 0
363 * FTRACE_OPS_CMD_* commands allow the ftrace core logic to request changes
364 * to a ftrace_ops. Note, the requests may fail.
366 * ENABLE_SHARE_IPMODIFY_SELF - enable a DIRECT ops to work on the same
367 * function as an ops with IPMODIFY. Called
368 * when the DIRECT ops is being registered.
369 * This is called with both direct_mutex and
370 * ftrace_lock are locked.
372 * ENABLE_SHARE_IPMODIFY_PEER - enable a DIRECT ops to work on the same
373 * function as an ops with IPMODIFY. Called
374 * when the other ops (the one with IPMODIFY)
375 * is being registered.
376 * This is called with direct_mutex locked.
378 * DISABLE_SHARE_IPMODIFY_PEER - disable a DIRECT ops to work on the same
379 * function as an ops with IPMODIFY. Called
380 * when the other ops (the one with IPMODIFY)
381 * is being unregistered.
382 * This is called with direct_mutex locked.
384 enum ftrace_ops_cmd {
385 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF,
386 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER,
387 FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER,
391 * For most ftrace_ops_cmd,
394 * Negative on failure. The return value is dependent on the
397 typedef int (*ftrace_ops_func_t)(struct ftrace_ops *op, enum ftrace_ops_cmd cmd);
399 #ifdef CONFIG_DYNAMIC_FTRACE
400 /* The hash used to know what functions callbacks trace */
401 struct ftrace_ops_hash {
402 struct ftrace_hash __rcu *notrace_hash;
403 struct ftrace_hash __rcu *filter_hash;
404 struct mutex regex_lock;
407 void ftrace_free_init_mem(void);
408 void ftrace_free_mem(struct module *mod, void *start, void *end);
410 static inline void ftrace_free_init_mem(void)
412 ftrace_boot_snapshot();
414 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
418 * Note, ftrace_ops can be referenced outside of RCU protection, unless
419 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
420 * core data, the unregistering of it will perform a scheduling on all CPUs
421 * to make sure that there are no more users. Depending on the load of the
422 * system that may take a bit of time.
424 * Any private data added must also take care not to be freed and if private
425 * data is added to a ftrace_ops that is in core code, the user of the
426 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
430 struct ftrace_ops __rcu *next;
433 ftrace_func_t saved_func;
434 #ifdef CONFIG_DYNAMIC_FTRACE
435 struct ftrace_ops_hash local_hash;
436 struct ftrace_ops_hash *func_hash;
437 struct ftrace_ops_hash old_hash;
438 unsigned long trampoline;
439 unsigned long trampoline_size;
440 struct list_head list;
441 struct list_head subop_list;
442 ftrace_ops_func_t ops_func;
443 struct ftrace_ops *managed;
444 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
445 unsigned long direct_call;
450 extern struct ftrace_ops __rcu *ftrace_ops_list;
451 extern struct ftrace_ops ftrace_list_end;
454 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
455 * can use rcu_dereference_raw_check() is that elements removed from this list
456 * are simply leaked, so there is no need to interact with a grace-period
457 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
458 * concurrent insertions into the ftrace_ops_list.
460 * Silly Alpha and silly pointer-speculation compiler optimizations!
462 #define do_for_each_ftrace_op(op, list) \
463 op = rcu_dereference_raw_check(list); \
467 * Optimized for just a single item in the list (as that is the normal case).
469 #define while_for_each_ftrace_op(op) \
470 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
471 unlikely((op) != &ftrace_list_end))
474 * Type of the current tracing.
476 enum ftrace_tracing_type_t {
477 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
478 FTRACE_TYPE_RETURN, /* Hook the return of the function */
481 /* Current tracing type, default is FTRACE_TYPE_ENTER */
482 extern enum ftrace_tracing_type_t ftrace_tracing_type;
485 * The ftrace_ops must be a static and should also
486 * be read_mostly. These functions do modify read_mostly variables
487 * so use them sparely. Never free an ftrace_op or modify the
488 * next pointer after it has been registered. Even after unregistering
489 * it, the next pointer may still be used internally.
491 int register_ftrace_function(struct ftrace_ops *ops);
492 int unregister_ftrace_function(struct ftrace_ops *ops);
494 extern void ftrace_stub(unsigned long a0, unsigned long a1,
495 struct ftrace_ops *op, struct ftrace_regs *fregs);
498 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
499 #else /* !CONFIG_FUNCTION_TRACER */
501 * (un)register_ftrace_function must be a macro since the ops parameter
502 * must not be evaluated.
504 #define register_ftrace_function(ops) ({ 0; })
505 #define unregister_ftrace_function(ops) ({ 0; })
506 static inline void ftrace_kill(void) { }
507 static inline void ftrace_free_init_mem(void) { }
508 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
509 static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
513 #endif /* CONFIG_FUNCTION_TRACER */
515 struct ftrace_func_entry {
516 struct hlist_node hlist;
518 unsigned long direct; /* for direct lookup only */
521 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
522 unsigned long ftrace_find_rec_direct(unsigned long ip);
523 int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
524 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
526 int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
527 int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr);
529 void ftrace_stub_direct_tramp(void);
533 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
537 static inline int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
541 static inline int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
546 static inline int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
550 static inline int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr)
556 * This must be implemented by the architecture.
557 * It is the way the ftrace direct_ops helper, when called
558 * via ftrace (because there's other callbacks besides the
559 * direct call), can inform the architecture's trampoline that this
560 * routine has a direct caller, and what the caller is.
562 * For example, in x86, it returns the direct caller
563 * callback function via the regs->orig_ax parameter.
564 * Then in the ftrace trampoline, if this is set, it makes
565 * the return from the trampoline jump to the direct caller
566 * instead of going back to the function it just traced.
568 static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,
569 unsigned long addr) { }
570 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
572 #ifdef CONFIG_STACK_TRACER
574 int stack_trace_sysctl(const struct ctl_table *table, int write, void *buffer,
575 size_t *lenp, loff_t *ppos);
577 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
578 DECLARE_PER_CPU(int, disable_stack_tracer);
581 * stack_tracer_disable - temporarily disable the stack tracer
583 * There's a few locations (namely in RCU) where stack tracing
584 * cannot be executed. This function is used to disable stack
585 * tracing during those critical sections.
587 * This function must be called with preemption or interrupts
588 * disabled and stack_tracer_enable() must be called shortly after
589 * while preemption or interrupts are still disabled.
591 static inline void stack_tracer_disable(void)
593 /* Preemption or interrupts must be disabled */
594 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
595 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
596 this_cpu_inc(disable_stack_tracer);
600 * stack_tracer_enable - re-enable the stack tracer
602 * After stack_tracer_disable() is called, stack_tracer_enable()
603 * must be called shortly afterward.
605 static inline void stack_tracer_enable(void)
607 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
608 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
609 this_cpu_dec(disable_stack_tracer);
612 static inline void stack_tracer_disable(void) { }
613 static inline void stack_tracer_enable(void) { }
617 FTRACE_UPDATE_CALLS = (1 << 0),
618 FTRACE_DISABLE_CALLS = (1 << 1),
619 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
620 FTRACE_START_FUNC_RET = (1 << 3),
621 FTRACE_STOP_FUNC_RET = (1 << 4),
622 FTRACE_MAY_SLEEP = (1 << 5),
625 /* Arches can override ftrace_get_symaddr() to convert fentry_ip to symaddr. */
626 #ifndef ftrace_get_symaddr
628 * ftrace_get_symaddr - return the symbol address from fentry_ip
629 * @fentry_ip: the address of ftrace location
631 * Get the symbol address from @fentry_ip (fast path). If there is no fast
632 * search path, this returns 0.
633 * User may need to use kallsyms API to find the symbol address.
635 #define ftrace_get_symaddr(fentry_ip) (0)
638 void ftrace_sync_ipi(void *data);
640 #ifdef CONFIG_DYNAMIC_FTRACE
642 void ftrace_arch_code_modify_prepare(void);
643 void ftrace_arch_code_modify_post_process(void);
645 enum ftrace_bug_type {
652 extern enum ftrace_bug_type ftrace_bug_type;
655 * Archs can set this to point to a variable that holds the value that was
656 * expected at the call site before calling ftrace_bug().
658 extern const void *ftrace_expected;
660 void ftrace_bug(int err, struct dyn_ftrace *rec);
664 extern int ftrace_text_reserved(const void *start, const void *end);
666 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
668 bool is_ftrace_trampoline(unsigned long addr);
671 * The dyn_ftrace record's flags field is split into two parts.
672 * the first part which is '0-FTRACE_REF_MAX' is a counter of
673 * the number of callbacks that have registered the function that
674 * the dyn_ftrace descriptor represents.
676 * The second part is a mask:
677 * ENABLED - the function is being traced
678 * REGS - the record wants the function to save regs
679 * REGS_EN - the function is set up to save regs.
680 * IPMODIFY - the record allows for the IP address to be changed.
681 * DISABLED - the record is not ready to be touched yet
682 * DIRECT - there is a direct function to call
683 * CALL_OPS - the record can use callsite-specific ops
684 * CALL_OPS_EN - the function is set up to use callsite-specific ops
685 * TOUCHED - A callback was added since boot up
686 * MODIFIED - The function had IPMODIFY or DIRECT attached to it
688 * When a new ftrace_ops is registered and wants a function to save
689 * pt_regs, the rec->flags REGS is set. When the function has been
690 * set up to save regs, the REG_EN flag is set. Once a function
691 * starts saving regs it will do so until all ftrace_ops are removed
692 * from tracing that function.
695 FTRACE_FL_ENABLED = (1UL << 31),
696 FTRACE_FL_REGS = (1UL << 30),
697 FTRACE_FL_REGS_EN = (1UL << 29),
698 FTRACE_FL_TRAMP = (1UL << 28),
699 FTRACE_FL_TRAMP_EN = (1UL << 27),
700 FTRACE_FL_IPMODIFY = (1UL << 26),
701 FTRACE_FL_DISABLED = (1UL << 25),
702 FTRACE_FL_DIRECT = (1UL << 24),
703 FTRACE_FL_DIRECT_EN = (1UL << 23),
704 FTRACE_FL_CALL_OPS = (1UL << 22),
705 FTRACE_FL_CALL_OPS_EN = (1UL << 21),
706 FTRACE_FL_TOUCHED = (1UL << 20),
707 FTRACE_FL_MODIFIED = (1UL << 19),
710 #define FTRACE_REF_MAX_SHIFT 19
711 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
713 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
716 unsigned long ip; /* address of mcount call-site */
718 struct dyn_arch_ftrace arch;
721 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
722 int remove, int reset);
723 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
724 unsigned int cnt, int remove, int reset);
725 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
727 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
729 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
730 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
731 void ftrace_free_filter(struct ftrace_ops *ops);
732 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
735 * The FTRACE_UPDATE_* enum is used to pass information back
736 * from the ftrace_update_record() and ftrace_test_record()
737 * functions. These are called by the code update routines
738 * to find out what is to be done for a given function.
740 * IGNORE - The function is already what we want it to be
741 * MAKE_CALL - Start tracing the function
742 * MODIFY_CALL - Stop saving regs for the function
743 * MAKE_NOP - Stop tracing the function
746 FTRACE_UPDATE_IGNORE,
747 FTRACE_UPDATE_MAKE_CALL,
748 FTRACE_UPDATE_MODIFY_CALL,
749 FTRACE_UPDATE_MAKE_NOP,
753 FTRACE_ITER_FILTER = (1 << 0),
754 FTRACE_ITER_NOTRACE = (1 << 1),
755 FTRACE_ITER_PRINTALL = (1 << 2),
756 FTRACE_ITER_DO_PROBES = (1 << 3),
757 FTRACE_ITER_PROBE = (1 << 4),
758 FTRACE_ITER_MOD = (1 << 5),
759 FTRACE_ITER_ENABLED = (1 << 6),
760 FTRACE_ITER_TOUCHED = (1 << 7),
761 FTRACE_ITER_ADDRS = (1 << 8),
764 void arch_ftrace_update_code(int command);
765 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
766 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
767 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
769 struct ftrace_rec_iter;
771 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
772 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
773 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
775 #define for_ftrace_rec_iter(iter) \
776 for (iter = ftrace_rec_iter_start(); \
778 iter = ftrace_rec_iter_next(iter))
781 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
782 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
783 void ftrace_run_stop_machine(int command);
784 unsigned long ftrace_location(unsigned long ip);
785 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
786 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
787 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
789 extern ftrace_func_t ftrace_trace_function;
791 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
792 struct inode *inode, struct file *file);
793 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
794 size_t cnt, loff_t *ppos);
795 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
796 size_t cnt, loff_t *ppos);
797 int ftrace_regex_release(struct inode *inode, struct file *file);
800 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
802 /* defined in arch */
803 extern int ftrace_dyn_arch_init(void);
804 extern void ftrace_replace_code(int enable);
805 extern int ftrace_update_ftrace_func(ftrace_func_t func);
806 extern void ftrace_caller(void);
807 extern void ftrace_regs_caller(void);
808 extern void ftrace_call(void);
809 extern void ftrace_regs_call(void);
810 extern void mcount_call(void);
812 void ftrace_modify_all_code(int command);
815 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
818 #ifndef FTRACE_GRAPH_ADDR
819 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
822 #ifndef FTRACE_REGS_ADDR
823 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
824 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
826 # define FTRACE_REGS_ADDR FTRACE_ADDR
831 * If an arch would like functions that are only traced
832 * by the function graph tracer to jump directly to its own
833 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
834 * to be that address to jump to.
836 #ifndef FTRACE_GRAPH_TRAMP_ADDR
837 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
840 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
841 extern void ftrace_graph_caller(void);
842 extern int ftrace_enable_ftrace_graph_caller(void);
843 extern int ftrace_disable_ftrace_graph_caller(void);
845 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
846 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
850 * ftrace_make_nop - convert code into nop
851 * @mod: module structure if called by module load initialization
852 * @rec: the call site record (e.g. mcount/fentry)
853 * @addr: the address that the call site should be calling
855 * This is a very sensitive operation and great care needs
856 * to be taken by the arch. The operation should carefully
857 * read the location, check to see if what is read is indeed
858 * what we expect it to be, and then on success of the compare,
859 * it should write to the location.
861 * The code segment at @rec->ip should be a caller to @addr
865 * -EFAULT on error reading the location
866 * -EINVAL on a failed compare of the contents
867 * -EPERM on error writing to the location
868 * Any other value will be considered a failure.
870 extern int ftrace_make_nop(struct module *mod,
871 struct dyn_ftrace *rec, unsigned long addr);
874 * ftrace_need_init_nop - return whether nop call sites should be initialized
876 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
877 * need to call ftrace_init_nop() if the code is built with that flag.
878 * Architectures where this is not always the case may define their own
882 * 0 if ftrace_init_nop() should be called
883 * Nonzero if ftrace_init_nop() should not be called
886 #ifndef ftrace_need_init_nop
887 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
891 * ftrace_init_nop - initialize a nop call site
892 * @mod: module structure if called by module load initialization
893 * @rec: the call site record (e.g. mcount/fentry)
895 * This is a very sensitive operation and great care needs
896 * to be taken by the arch. The operation should carefully
897 * read the location, check to see if what is read is indeed
898 * what we expect it to be, and then on success of the compare,
899 * it should write to the location.
901 * The code segment at @rec->ip should contain the contents created by
906 * -EFAULT on error reading the location
907 * -EINVAL on a failed compare of the contents
908 * -EPERM on error writing to the location
909 * Any other value will be considered a failure.
911 #ifndef ftrace_init_nop
912 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
914 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
919 * ftrace_make_call - convert a nop call site into a call to addr
920 * @rec: the call site record (e.g. mcount/fentry)
921 * @addr: the address that the call site should call
923 * This is a very sensitive operation and great care needs
924 * to be taken by the arch. The operation should carefully
925 * read the location, check to see if what is read is indeed
926 * what we expect it to be, and then on success of the compare,
927 * it should write to the location.
929 * The code segment at @rec->ip should be a nop
933 * -EFAULT on error reading the location
934 * -EINVAL on a failed compare of the contents
935 * -EPERM on error writing to the location
936 * Any other value will be considered a failure.
938 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
940 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) || \
941 defined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \
942 defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)
944 * ftrace_modify_call - convert from one addr to another (no nop)
945 * @rec: the call site record (e.g. mcount/fentry)
946 * @old_addr: the address expected to be currently called to
947 * @addr: the address to change to
949 * This is a very sensitive operation and great care needs
950 * to be taken by the arch. The operation should carefully
951 * read the location, check to see if what is read is indeed
952 * what we expect it to be, and then on success of the compare,
953 * it should write to the location.
955 * When using call ops, this is called when the associated ops change, even
956 * when (addr == old_addr).
958 * The code segment at @rec->ip should be a caller to @old_addr
962 * -EFAULT on error reading the location
963 * -EINVAL on a failed compare of the contents
964 * -EPERM on error writing to the location
965 * Any other value will be considered a failure.
967 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
970 /* Should never be called */
971 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
978 extern int skip_trace(unsigned long ip);
979 extern void ftrace_module_init(struct module *mod);
980 extern void ftrace_module_enable(struct module *mod);
981 extern void ftrace_release_mod(struct module *mod);
982 #else /* CONFIG_DYNAMIC_FTRACE */
983 static inline int skip_trace(unsigned long ip) { return 0; }
984 static inline void ftrace_module_init(struct module *mod) { }
985 static inline void ftrace_module_enable(struct module *mod) { }
986 static inline void ftrace_release_mod(struct module *mod) { }
987 static inline int ftrace_text_reserved(const void *start, const void *end)
991 static inline unsigned long ftrace_location(unsigned long ip)
997 * Again users of functions that have ftrace_ops may not
998 * have them defined when ftrace is not enabled, but these
999 * functions may still be called. Use a macro instead of inline.
1001 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
1002 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
1003 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
1004 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
1005 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
1006 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
1007 #define ftrace_free_filter(ops) do { } while (0)
1008 #define ftrace_ops_set_global_filter(ops) do { } while (0)
1010 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
1011 size_t cnt, loff_t *ppos) { return -ENODEV; }
1012 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
1013 size_t cnt, loff_t *ppos) { return -ENODEV; }
1015 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
1017 static inline bool is_ftrace_trampoline(unsigned long addr)
1021 #endif /* CONFIG_DYNAMIC_FTRACE */
1023 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1024 #ifndef ftrace_graph_func
1025 #define ftrace_graph_func ftrace_stub
1026 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
1028 #define FTRACE_OPS_GRAPH_STUB 0
1030 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1032 /* totally disable ftrace - can not re-enable after this */
1033 void ftrace_kill(void);
1035 static inline void tracer_disable(void)
1037 #ifdef CONFIG_FUNCTION_TRACER
1043 * Ftrace disable/restore without lock. Some synchronization mechanism
1044 * must be used to prevent ftrace_enabled to be changed between
1047 static inline int __ftrace_enabled_save(void)
1049 #ifdef CONFIG_FUNCTION_TRACER
1050 int saved_ftrace_enabled = ftrace_enabled;
1052 return saved_ftrace_enabled;
1058 static inline void __ftrace_enabled_restore(int enabled)
1060 #ifdef CONFIG_FUNCTION_TRACER
1061 ftrace_enabled = enabled;
1065 /* All archs should have this, but we define it for consistency */
1066 #ifndef ftrace_return_address0
1067 # define ftrace_return_address0 __builtin_return_address(0)
1070 /* Archs may use other ways for ADDR1 and beyond */
1071 #ifndef ftrace_return_address
1072 # ifdef CONFIG_FRAME_POINTER
1073 # define ftrace_return_address(n) __builtin_return_address(n)
1075 # define ftrace_return_address(n) 0UL
1079 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
1080 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
1081 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
1082 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
1083 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
1084 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
1085 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
1087 static __always_inline unsigned long get_lock_parent_ip(void)
1089 unsigned long addr = CALLER_ADDR0;
1091 if (!in_lock_functions(addr))
1093 addr = CALLER_ADDR1;
1094 if (!in_lock_functions(addr))
1096 return CALLER_ADDR2;
1099 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
1100 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
1101 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
1104 * Use defines instead of static inlines because some arches will make code out
1105 * of the CALLER_ADDR, when we really want these to be a real nop.
1107 # define trace_preempt_on(a0, a1) do { } while (0)
1108 # define trace_preempt_off(a0, a1) do { } while (0)
1111 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
1112 extern void ftrace_init(void);
1113 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
1114 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
1116 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
1119 static inline void ftrace_init(void) { }
1123 * Structure that defines an entry function trace.
1124 * It's already packed but the attribute "packed" is needed
1125 * to remove extra padding at the end.
1127 struct ftrace_graph_ent {
1128 unsigned long func; /* Current function */
1133 * Structure that defines an entry function trace with retaddr.
1134 * It's already packed but the attribute "packed" is needed
1135 * to remove extra padding at the end.
1137 struct fgraph_retaddr_ent {
1138 unsigned long func; /* Current function */
1140 unsigned long retaddr; /* Return address */
1144 * Structure that defines a return function trace.
1145 * It's already packed but the attribute "packed" is needed
1146 * to remove extra padding at the end.
1148 struct ftrace_graph_ret {
1149 unsigned long func; /* Current function */
1150 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
1151 unsigned long retval;
1154 /* Number of functions that overran the depth limit for current task */
1155 unsigned int overrun;
1160 /* Type of the callback handlers for tracing function graph*/
1161 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *,
1162 struct fgraph_ops *,
1163 struct ftrace_regs *); /* return */
1164 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *,
1165 struct fgraph_ops *,
1166 struct ftrace_regs *); /* entry */
1168 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace,
1169 struct fgraph_ops *gops,
1170 struct ftrace_regs *fregs);
1171 bool ftrace_pids_enabled(struct ftrace_ops *ops);
1173 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1176 trace_func_graph_ent_t entryfunc;
1177 trace_func_graph_ret_t retfunc;
1178 struct ftrace_ops ops; /* for the hash lists */
1180 trace_func_graph_ent_t saved_func;
1184 void *fgraph_reserve_data(int idx, int size_bytes);
1185 void *fgraph_retrieve_data(int idx, int *size_bytes);
1186 void *fgraph_retrieve_parent_data(int idx, int *size_bytes, int depth);
1189 * Stack of return addresses for functions
1191 * Used in struct thread_info
1193 struct ftrace_ret_stack {
1196 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
1199 unsigned long *retp;
1203 * Primary handler of a function return.
1204 * It relays on ftrace_return_to_handler.
1205 * Defined in entry_32/64.S
1207 extern void return_to_handler(void);
1210 function_graph_enter_regs(unsigned long ret, unsigned long func,
1211 unsigned long frame_pointer, unsigned long *retp,
1212 struct ftrace_regs *fregs);
1214 static inline int function_graph_enter(unsigned long ret, unsigned long func,
1215 unsigned long fp, unsigned long *retp)
1217 return function_graph_enter_regs(ret, func, fp, retp, NULL);
1220 struct ftrace_ret_stack *
1221 ftrace_graph_get_ret_stack(struct task_struct *task, int skip);
1222 unsigned long ftrace_graph_top_ret_addr(struct task_struct *task);
1224 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1225 unsigned long ret, unsigned long *retp);
1226 unsigned long *fgraph_get_task_var(struct fgraph_ops *gops);
1229 * Sometimes we don't want to trace a function with the function
1230 * graph tracer but we want them to keep traced by the usual function
1231 * tracer if the function graph tracer is not configured.
1233 #define __notrace_funcgraph notrace
1235 #define FTRACE_RETFUNC_DEPTH 50
1236 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1238 extern int register_ftrace_graph(struct fgraph_ops *ops);
1239 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1242 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1244 * ftrace_graph_stop() is called when a severe error is detected in
1245 * the function graph tracing. This function is called by the critical
1246 * paths of function graph to keep those paths from doing any more harm.
1248 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1250 static inline bool ftrace_graph_is_dead(void)
1252 return static_branch_unlikely(&kill_ftrace_graph);
1255 extern void ftrace_graph_stop(void);
1257 /* The current handlers in use */
1258 extern trace_func_graph_ret_t ftrace_graph_return;
1259 extern trace_func_graph_ent_t ftrace_graph_entry;
1261 extern void ftrace_graph_init_task(struct task_struct *t);
1262 extern void ftrace_graph_exit_task(struct task_struct *t);
1263 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1265 /* Used by assembly, but to quiet sparse warnings */
1266 extern struct ftrace_ops *function_trace_op;
1268 static inline void pause_graph_tracing(void)
1270 atomic_inc(¤t->tracing_graph_pause);
1273 static inline void unpause_graph_tracing(void)
1275 atomic_dec(¤t->tracing_graph_pause);
1277 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1279 #define __notrace_funcgraph
1281 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1282 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1283 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1285 /* Define as macros as fgraph_ops may not be defined */
1286 #define register_ftrace_graph(ops) ({ -1; })
1287 #define unregister_ftrace_graph(ops) do { } while (0)
1289 static inline unsigned long
1290 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1291 unsigned long *retp)
1296 static inline void pause_graph_tracing(void) { }
1297 static inline void unpause_graph_tracing(void) { }
1298 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1300 #ifdef CONFIG_TRACING
1301 enum ftrace_dump_mode;
1303 extern int ftrace_dump_on_oops_enabled(void);
1305 extern void disable_trace_on_warning(void);
1307 #else /* CONFIG_TRACING */
1308 static inline void disable_trace_on_warning(void) { }
1309 #endif /* CONFIG_TRACING */
1311 #ifdef CONFIG_FTRACE_SYSCALLS
1313 unsigned long arch_syscall_addr(int nr);
1315 #endif /* CONFIG_FTRACE_SYSCALLS */
1317 #endif /* _LINUX_FTRACE_H */