printf: Remove unused 'bprintf'
[drm/drm-misc.git] / include / linux / ftrace.h
blobaa9ddd1e4bb6ab14c2131cb6a9d64551d2aba708
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Ftrace header. For implementation details beyond the random comments
4 * scattered below, see: Documentation/trace/ftrace-design.rst
5 */
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>
21 #include <linux/fs.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
32 #endif
34 #ifdef CONFIG_TRACING
35 extern void ftrace_boot_snapshot(void);
36 #else
37 static inline void ftrace_boot_snapshot(void) { }
38 #endif
40 struct ftrace_ops;
41 struct ftrace_regs;
42 struct dyn_ftrace;
44 char *arch_ftrace_match_adjust(char *str, const char *search);
46 #ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL
47 struct fgraph_ret_regs;
48 unsigned long ftrace_return_to_handler(struct fgraph_ret_regs *ret_regs);
49 #else
50 unsigned long ftrace_return_to_handler(unsigned long frame_pointer);
51 #endif
53 #ifdef CONFIG_FUNCTION_TRACER
55 * If the arch's mcount caller does not support all of ftrace's
56 * features, then it must call an indirect function that
57 * does. Or at least does enough to prevent any unwelcome side effects.
59 * Also define the function prototype that these architectures use
60 * to call the ftrace_ops_list_func().
62 #if !ARCH_SUPPORTS_FTRACE_OPS
63 # define FTRACE_FORCE_LIST_FUNC 1
64 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
65 #else
66 # define FTRACE_FORCE_LIST_FUNC 0
67 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
68 struct ftrace_ops *op, struct ftrace_regs *fregs);
69 #endif
70 extern const struct ftrace_ops ftrace_nop_ops;
71 extern const struct ftrace_ops ftrace_list_ops;
72 struct ftrace_ops *ftrace_find_unique_ops(struct dyn_ftrace *rec);
73 #endif /* CONFIG_FUNCTION_TRACER */
75 /* Main tracing buffer and events set up */
76 #ifdef CONFIG_TRACING
77 void trace_init(void);
78 void early_trace_init(void);
79 #else
80 static inline void trace_init(void) { }
81 static inline void early_trace_init(void) { }
82 #endif
84 struct module;
85 struct ftrace_hash;
87 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
88 defined(CONFIG_DYNAMIC_FTRACE)
89 int
90 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
91 unsigned long *off, char **modname, char *sym);
92 #else
93 static inline int
94 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
95 unsigned long *off, char **modname, char *sym)
97 return 0;
99 #endif
101 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
102 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
103 char *type, char *name,
104 char *module_name, int *exported);
105 #else
106 static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
107 char *type, char *name,
108 char *module_name, int *exported)
110 return -1;
112 #endif
114 #ifdef CONFIG_FUNCTION_TRACER
116 #include <linux/ftrace_regs.h>
118 extern int ftrace_enabled;
121 * ftrace_regs - ftrace partial/optimal register set
123 * ftrace_regs represents a group of registers which is used at the
124 * function entry and exit. There are three types of registers.
126 * - Registers for passing the parameters to callee, including the stack
127 * pointer. (e.g. rcx, rdx, rdi, rsi, r8, r9 and rsp on x86_64)
128 * - Registers for passing the return values to caller.
129 * (e.g. rax and rdx on x86_64)
130 * - Registers for hooking the function call and return including the
131 * frame pointer (the frame pointer is architecture/config dependent)
132 * (e.g. rip, rbp and rsp for x86_64)
134 * Also, architecture dependent fields can be used for internal process.
135 * (e.g. orig_ax on x86_64)
137 * On the function entry, those registers will be restored except for
138 * the stack pointer, so that user can change the function parameters
139 * and instruction pointer (e.g. live patching.)
140 * On the function exit, only registers which is used for return values
141 * are restored.
143 * NOTE: user *must not* access regs directly, only do it via APIs, because
144 * the member can be changed according to the architecture.
145 * This is why the structure is empty here, so that nothing accesses
146 * the ftrace_regs directly.
148 struct ftrace_regs {
149 /* Nothing to see here, use the accessor functions! */
152 #define ftrace_regs_size() sizeof(struct __arch_ftrace_regs)
154 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
156 * Architectures that define HAVE_DYNAMIC_FTRACE_WITH_ARGS must define their own
157 * arch_ftrace_get_regs() where it only returns pt_regs *if* it is fully
158 * populated. It should return NULL otherwise.
160 static inline struct pt_regs *arch_ftrace_get_regs(struct ftrace_regs *fregs)
162 return &arch_ftrace_regs(fregs)->regs;
166 * ftrace_regs_set_instruction_pointer() is to be defined by the architecture
167 * if to allow setting of the instruction pointer from the ftrace_regs when
168 * HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports live kernel patching.
170 #define ftrace_regs_set_instruction_pointer(fregs, ip) do { } while (0)
171 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
173 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
175 if (!fregs)
176 return NULL;
178 return arch_ftrace_get_regs(fregs);
182 * When true, the ftrace_regs_{get,set}_*() functions may be used on fregs.
183 * Note: this can be true even when ftrace_get_regs() cannot provide a pt_regs.
185 static __always_inline bool ftrace_regs_has_args(struct ftrace_regs *fregs)
187 if (IS_ENABLED(CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS))
188 return true;
190 return ftrace_get_regs(fregs) != NULL;
193 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
194 struct ftrace_ops *op, struct ftrace_regs *fregs);
196 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
199 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
200 * set in the flags member.
201 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
202 * IPMODIFY are a kind of attribute flags which can be set only before
203 * registering the ftrace_ops, and can not be modified while registered.
204 * Changing those attribute flags after registering ftrace_ops will
205 * cause unexpected results.
207 * ENABLED - set/unset when ftrace_ops is registered/unregistered
208 * DYNAMIC - set when ftrace_ops is registered to denote dynamically
209 * allocated ftrace_ops which need special care
210 * SAVE_REGS - The ftrace_ops wants regs saved at each function called
211 * and passed to the callback. If this flag is set, but the
212 * architecture does not support passing regs
213 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
214 * ftrace_ops will fail to register, unless the next flag
215 * is set.
216 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
217 * handler can handle an arch that does not save regs
218 * (the handler tests if regs == NULL), then it can set
219 * this flag instead. It will not fail registering the ftrace_ops
220 * but, the regs field will be NULL if the arch does not support
221 * passing regs to the handler.
222 * Note, if this flag is set, the SAVE_REGS flag will automatically
223 * get set upon registering the ftrace_ops, if the arch supports it.
224 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
225 * that the call back needs recursion protection. If it does
226 * not set this, then the ftrace infrastructure will assume
227 * that the callback can handle recursion on its own.
228 * STUB - The ftrace_ops is just a place holder.
229 * INITIALIZED - The ftrace_ops has already been initialized (first use time
230 * register_ftrace_function() is called, it will initialized the ops)
231 * DELETED - The ops are being deleted, do not let them be registered again.
232 * ADDING - The ops is in the process of being added.
233 * REMOVING - The ops is in the process of being removed.
234 * MODIFYING - The ops is in the process of changing its filter functions.
235 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
236 * The arch specific code sets this flag when it allocated a
237 * trampoline. This lets the arch know that it can update the
238 * trampoline in case the callback function changes.
239 * The ftrace_ops trampoline can be set by the ftrace users, and
240 * in such cases the arch must not modify it. Only the arch ftrace
241 * core code should set this flag.
242 * IPMODIFY - The ops can modify the IP register. This can only be set with
243 * SAVE_REGS. If another ops with this flag set is already registered
244 * for any of the functions that this ops will be registered for, then
245 * this ops will fail to register or set_filter_ip.
246 * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
247 * RCU - Set when the ops can only be called when RCU is watching.
248 * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
249 * PERMANENT - Set when the ops is permanent and should not be affected by
250 * ftrace_enabled.
251 * DIRECT - Used by the direct ftrace_ops helper for direct functions
252 * (internal ftrace only, should not be used by others)
253 * SUBOP - Is controlled by another op in field managed.
255 enum {
256 FTRACE_OPS_FL_ENABLED = BIT(0),
257 FTRACE_OPS_FL_DYNAMIC = BIT(1),
258 FTRACE_OPS_FL_SAVE_REGS = BIT(2),
259 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3),
260 FTRACE_OPS_FL_RECURSION = BIT(4),
261 FTRACE_OPS_FL_STUB = BIT(5),
262 FTRACE_OPS_FL_INITIALIZED = BIT(6),
263 FTRACE_OPS_FL_DELETED = BIT(7),
264 FTRACE_OPS_FL_ADDING = BIT(8),
265 FTRACE_OPS_FL_REMOVING = BIT(9),
266 FTRACE_OPS_FL_MODIFYING = BIT(10),
267 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11),
268 FTRACE_OPS_FL_IPMODIFY = BIT(12),
269 FTRACE_OPS_FL_PID = BIT(13),
270 FTRACE_OPS_FL_RCU = BIT(14),
271 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15),
272 FTRACE_OPS_FL_PERMANENT = BIT(16),
273 FTRACE_OPS_FL_DIRECT = BIT(17),
274 FTRACE_OPS_FL_SUBOP = BIT(18),
277 #ifndef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
278 #define FTRACE_OPS_FL_SAVE_ARGS FTRACE_OPS_FL_SAVE_REGS
279 #else
280 #define FTRACE_OPS_FL_SAVE_ARGS 0
281 #endif
284 * FTRACE_OPS_CMD_* commands allow the ftrace core logic to request changes
285 * to a ftrace_ops. Note, the requests may fail.
287 * ENABLE_SHARE_IPMODIFY_SELF - enable a DIRECT ops to work on the same
288 * function as an ops with IPMODIFY. Called
289 * when the DIRECT ops is being registered.
290 * This is called with both direct_mutex and
291 * ftrace_lock are locked.
293 * ENABLE_SHARE_IPMODIFY_PEER - enable a DIRECT ops to work on the same
294 * function as an ops with IPMODIFY. Called
295 * when the other ops (the one with IPMODIFY)
296 * is being registered.
297 * This is called with direct_mutex locked.
299 * DISABLE_SHARE_IPMODIFY_PEER - disable a DIRECT ops to work on the same
300 * function as an ops with IPMODIFY. Called
301 * when the other ops (the one with IPMODIFY)
302 * is being unregistered.
303 * This is called with direct_mutex locked.
305 enum ftrace_ops_cmd {
306 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_SELF,
307 FTRACE_OPS_CMD_ENABLE_SHARE_IPMODIFY_PEER,
308 FTRACE_OPS_CMD_DISABLE_SHARE_IPMODIFY_PEER,
312 * For most ftrace_ops_cmd,
313 * Returns:
314 * 0 - Success.
315 * Negative on failure. The return value is dependent on the
316 * callback.
318 typedef int (*ftrace_ops_func_t)(struct ftrace_ops *op, enum ftrace_ops_cmd cmd);
320 #ifdef CONFIG_DYNAMIC_FTRACE
321 /* The hash used to know what functions callbacks trace */
322 struct ftrace_ops_hash {
323 struct ftrace_hash __rcu *notrace_hash;
324 struct ftrace_hash __rcu *filter_hash;
325 struct mutex regex_lock;
328 void ftrace_free_init_mem(void);
329 void ftrace_free_mem(struct module *mod, void *start, void *end);
330 #else
331 static inline void ftrace_free_init_mem(void)
333 ftrace_boot_snapshot();
335 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
336 #endif
339 * Note, ftrace_ops can be referenced outside of RCU protection, unless
340 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
341 * core data, the unregistering of it will perform a scheduling on all CPUs
342 * to make sure that there are no more users. Depending on the load of the
343 * system that may take a bit of time.
345 * Any private data added must also take care not to be freed and if private
346 * data is added to a ftrace_ops that is in core code, the user of the
347 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
349 struct ftrace_ops {
350 ftrace_func_t func;
351 struct ftrace_ops __rcu *next;
352 unsigned long flags;
353 void *private;
354 ftrace_func_t saved_func;
355 #ifdef CONFIG_DYNAMIC_FTRACE
356 struct ftrace_ops_hash local_hash;
357 struct ftrace_ops_hash *func_hash;
358 struct ftrace_ops_hash old_hash;
359 unsigned long trampoline;
360 unsigned long trampoline_size;
361 struct list_head list;
362 struct list_head subop_list;
363 ftrace_ops_func_t ops_func;
364 struct ftrace_ops *managed;
365 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
366 unsigned long direct_call;
367 #endif
368 #endif
371 extern struct ftrace_ops __rcu *ftrace_ops_list;
372 extern struct ftrace_ops ftrace_list_end;
375 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
376 * can use rcu_dereference_raw_check() is that elements removed from this list
377 * are simply leaked, so there is no need to interact with a grace-period
378 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
379 * concurrent insertions into the ftrace_ops_list.
381 * Silly Alpha and silly pointer-speculation compiler optimizations!
383 #define do_for_each_ftrace_op(op, list) \
384 op = rcu_dereference_raw_check(list); \
388 * Optimized for just a single item in the list (as that is the normal case).
390 #define while_for_each_ftrace_op(op) \
391 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
392 unlikely((op) != &ftrace_list_end))
395 * Type of the current tracing.
397 enum ftrace_tracing_type_t {
398 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
399 FTRACE_TYPE_RETURN, /* Hook the return of the function */
402 /* Current tracing type, default is FTRACE_TYPE_ENTER */
403 extern enum ftrace_tracing_type_t ftrace_tracing_type;
406 * The ftrace_ops must be a static and should also
407 * be read_mostly. These functions do modify read_mostly variables
408 * so use them sparely. Never free an ftrace_op or modify the
409 * next pointer after it has been registered. Even after unregistering
410 * it, the next pointer may still be used internally.
412 int register_ftrace_function(struct ftrace_ops *ops);
413 int unregister_ftrace_function(struct ftrace_ops *ops);
415 extern void ftrace_stub(unsigned long a0, unsigned long a1,
416 struct ftrace_ops *op, struct ftrace_regs *fregs);
419 int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
420 #else /* !CONFIG_FUNCTION_TRACER */
422 * (un)register_ftrace_function must be a macro since the ops parameter
423 * must not be evaluated.
425 #define register_ftrace_function(ops) ({ 0; })
426 #define unregister_ftrace_function(ops) ({ 0; })
427 static inline void ftrace_kill(void) { }
428 static inline void ftrace_free_init_mem(void) { }
429 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
430 static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
432 return -EOPNOTSUPP;
434 #endif /* CONFIG_FUNCTION_TRACER */
436 struct ftrace_func_entry {
437 struct hlist_node hlist;
438 unsigned long ip;
439 unsigned long direct; /* for direct lookup only */
442 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
443 unsigned long ftrace_find_rec_direct(unsigned long ip);
444 int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
445 int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
446 bool free_filters);
447 int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr);
448 int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr);
450 void ftrace_stub_direct_tramp(void);
452 #else
453 struct ftrace_ops;
454 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
456 return 0;
458 static inline int register_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
460 return -ENODEV;
462 static inline int unregister_ftrace_direct(struct ftrace_ops *ops, unsigned long addr,
463 bool free_filters)
465 return -ENODEV;
467 static inline int modify_ftrace_direct(struct ftrace_ops *ops, unsigned long addr)
469 return -ENODEV;
471 static inline int modify_ftrace_direct_nolock(struct ftrace_ops *ops, unsigned long addr)
473 return -ENODEV;
477 * This must be implemented by the architecture.
478 * It is the way the ftrace direct_ops helper, when called
479 * via ftrace (because there's other callbacks besides the
480 * direct call), can inform the architecture's trampoline that this
481 * routine has a direct caller, and what the caller is.
483 * For example, in x86, it returns the direct caller
484 * callback function via the regs->orig_ax parameter.
485 * Then in the ftrace trampoline, if this is set, it makes
486 * the return from the trampoline jump to the direct caller
487 * instead of going back to the function it just traced.
489 static inline void arch_ftrace_set_direct_caller(struct ftrace_regs *fregs,
490 unsigned long addr) { }
491 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
493 #ifdef CONFIG_STACK_TRACER
495 extern int stack_tracer_enabled;
497 int stack_trace_sysctl(const struct ctl_table *table, int write, void *buffer,
498 size_t *lenp, loff_t *ppos);
500 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
501 DECLARE_PER_CPU(int, disable_stack_tracer);
504 * stack_tracer_disable - temporarily disable the stack tracer
506 * There's a few locations (namely in RCU) where stack tracing
507 * cannot be executed. This function is used to disable stack
508 * tracing during those critical sections.
510 * This function must be called with preemption or interrupts
511 * disabled and stack_tracer_enable() must be called shortly after
512 * while preemption or interrupts are still disabled.
514 static inline void stack_tracer_disable(void)
516 /* Preemption or interrupts must be disabled */
517 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
518 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
519 this_cpu_inc(disable_stack_tracer);
523 * stack_tracer_enable - re-enable the stack tracer
525 * After stack_tracer_disable() is called, stack_tracer_enable()
526 * must be called shortly afterward.
528 static inline void stack_tracer_enable(void)
530 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
531 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
532 this_cpu_dec(disable_stack_tracer);
534 #else
535 static inline void stack_tracer_disable(void) { }
536 static inline void stack_tracer_enable(void) { }
537 #endif
539 enum {
540 FTRACE_UPDATE_CALLS = (1 << 0),
541 FTRACE_DISABLE_CALLS = (1 << 1),
542 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
543 FTRACE_START_FUNC_RET = (1 << 3),
544 FTRACE_STOP_FUNC_RET = (1 << 4),
545 FTRACE_MAY_SLEEP = (1 << 5),
548 #ifdef CONFIG_DYNAMIC_FTRACE
550 void ftrace_arch_code_modify_prepare(void);
551 void ftrace_arch_code_modify_post_process(void);
553 enum ftrace_bug_type {
554 FTRACE_BUG_UNKNOWN,
555 FTRACE_BUG_INIT,
556 FTRACE_BUG_NOP,
557 FTRACE_BUG_CALL,
558 FTRACE_BUG_UPDATE,
560 extern enum ftrace_bug_type ftrace_bug_type;
563 * Archs can set this to point to a variable that holds the value that was
564 * expected at the call site before calling ftrace_bug().
566 extern const void *ftrace_expected;
568 void ftrace_bug(int err, struct dyn_ftrace *rec);
570 struct seq_file;
572 extern int ftrace_text_reserved(const void *start, const void *end);
574 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
576 bool is_ftrace_trampoline(unsigned long addr);
579 * The dyn_ftrace record's flags field is split into two parts.
580 * the first part which is '0-FTRACE_REF_MAX' is a counter of
581 * the number of callbacks that have registered the function that
582 * the dyn_ftrace descriptor represents.
584 * The second part is a mask:
585 * ENABLED - the function is being traced
586 * REGS - the record wants the function to save regs
587 * REGS_EN - the function is set up to save regs.
588 * IPMODIFY - the record allows for the IP address to be changed.
589 * DISABLED - the record is not ready to be touched yet
590 * DIRECT - there is a direct function to call
591 * CALL_OPS - the record can use callsite-specific ops
592 * CALL_OPS_EN - the function is set up to use callsite-specific ops
593 * TOUCHED - A callback was added since boot up
594 * MODIFIED - The function had IPMODIFY or DIRECT attached to it
596 * When a new ftrace_ops is registered and wants a function to save
597 * pt_regs, the rec->flags REGS is set. When the function has been
598 * set up to save regs, the REG_EN flag is set. Once a function
599 * starts saving regs it will do so until all ftrace_ops are removed
600 * from tracing that function.
602 enum {
603 FTRACE_FL_ENABLED = (1UL << 31),
604 FTRACE_FL_REGS = (1UL << 30),
605 FTRACE_FL_REGS_EN = (1UL << 29),
606 FTRACE_FL_TRAMP = (1UL << 28),
607 FTRACE_FL_TRAMP_EN = (1UL << 27),
608 FTRACE_FL_IPMODIFY = (1UL << 26),
609 FTRACE_FL_DISABLED = (1UL << 25),
610 FTRACE_FL_DIRECT = (1UL << 24),
611 FTRACE_FL_DIRECT_EN = (1UL << 23),
612 FTRACE_FL_CALL_OPS = (1UL << 22),
613 FTRACE_FL_CALL_OPS_EN = (1UL << 21),
614 FTRACE_FL_TOUCHED = (1UL << 20),
615 FTRACE_FL_MODIFIED = (1UL << 19),
618 #define FTRACE_REF_MAX_SHIFT 19
619 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
621 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
623 struct dyn_ftrace {
624 unsigned long ip; /* address of mcount call-site */
625 unsigned long flags;
626 struct dyn_arch_ftrace arch;
629 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
630 int remove, int reset);
631 int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
632 unsigned int cnt, int remove, int reset);
633 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
634 int len, int reset);
635 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
636 int len, int reset);
637 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
638 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
639 void ftrace_free_filter(struct ftrace_ops *ops);
640 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
643 * The FTRACE_UPDATE_* enum is used to pass information back
644 * from the ftrace_update_record() and ftrace_test_record()
645 * functions. These are called by the code update routines
646 * to find out what is to be done for a given function.
648 * IGNORE - The function is already what we want it to be
649 * MAKE_CALL - Start tracing the function
650 * MODIFY_CALL - Stop saving regs for the function
651 * MAKE_NOP - Stop tracing the function
653 enum {
654 FTRACE_UPDATE_IGNORE,
655 FTRACE_UPDATE_MAKE_CALL,
656 FTRACE_UPDATE_MODIFY_CALL,
657 FTRACE_UPDATE_MAKE_NOP,
660 enum {
661 FTRACE_ITER_FILTER = (1 << 0),
662 FTRACE_ITER_NOTRACE = (1 << 1),
663 FTRACE_ITER_PRINTALL = (1 << 2),
664 FTRACE_ITER_DO_PROBES = (1 << 3),
665 FTRACE_ITER_PROBE = (1 << 4),
666 FTRACE_ITER_MOD = (1 << 5),
667 FTRACE_ITER_ENABLED = (1 << 6),
668 FTRACE_ITER_TOUCHED = (1 << 7),
669 FTRACE_ITER_ADDRS = (1 << 8),
672 void arch_ftrace_update_code(int command);
673 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
674 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
675 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
677 struct ftrace_rec_iter;
679 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
680 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
681 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
683 #define for_ftrace_rec_iter(iter) \
684 for (iter = ftrace_rec_iter_start(); \
685 iter; \
686 iter = ftrace_rec_iter_next(iter))
689 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
690 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
691 void ftrace_run_stop_machine(int command);
692 unsigned long ftrace_location(unsigned long ip);
693 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
694 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
695 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
697 extern ftrace_func_t ftrace_trace_function;
699 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
700 struct inode *inode, struct file *file);
701 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
702 size_t cnt, loff_t *ppos);
703 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
704 size_t cnt, loff_t *ppos);
705 int ftrace_regex_release(struct inode *inode, struct file *file);
707 void __init
708 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
710 /* defined in arch */
711 extern int ftrace_dyn_arch_init(void);
712 extern void ftrace_replace_code(int enable);
713 extern int ftrace_update_ftrace_func(ftrace_func_t func);
714 extern void ftrace_caller(void);
715 extern void ftrace_regs_caller(void);
716 extern void ftrace_call(void);
717 extern void ftrace_regs_call(void);
718 extern void mcount_call(void);
720 void ftrace_modify_all_code(int command);
722 #ifndef FTRACE_ADDR
723 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
724 #endif
726 #ifndef FTRACE_GRAPH_ADDR
727 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
728 #endif
730 #ifndef FTRACE_REGS_ADDR
731 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
732 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
733 #else
734 # define FTRACE_REGS_ADDR FTRACE_ADDR
735 #endif
736 #endif
739 * If an arch would like functions that are only traced
740 * by the function graph tracer to jump directly to its own
741 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
742 * to be that address to jump to.
744 #ifndef FTRACE_GRAPH_TRAMP_ADDR
745 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
746 #endif
748 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
749 extern void ftrace_graph_caller(void);
750 extern int ftrace_enable_ftrace_graph_caller(void);
751 extern int ftrace_disable_ftrace_graph_caller(void);
752 #else
753 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
754 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
755 #endif
758 * ftrace_make_nop - convert code into nop
759 * @mod: module structure if called by module load initialization
760 * @rec: the call site record (e.g. mcount/fentry)
761 * @addr: the address that the call site should be calling
763 * This is a very sensitive operation and great care needs
764 * to be taken by the arch. The operation should carefully
765 * read the location, check to see if what is read is indeed
766 * what we expect it to be, and then on success of the compare,
767 * it should write to the location.
769 * The code segment at @rec->ip should be a caller to @addr
771 * Return must be:
772 * 0 on success
773 * -EFAULT on error reading the location
774 * -EINVAL on a failed compare of the contents
775 * -EPERM on error writing to the location
776 * Any other value will be considered a failure.
778 extern int ftrace_make_nop(struct module *mod,
779 struct dyn_ftrace *rec, unsigned long addr);
782 * ftrace_need_init_nop - return whether nop call sites should be initialized
784 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
785 * need to call ftrace_init_nop() if the code is built with that flag.
786 * Architectures where this is not always the case may define their own
787 * condition.
789 * Return must be:
790 * 0 if ftrace_init_nop() should be called
791 * Nonzero if ftrace_init_nop() should not be called
794 #ifndef ftrace_need_init_nop
795 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
796 #endif
799 * ftrace_init_nop - initialize a nop call site
800 * @mod: module structure if called by module load initialization
801 * @rec: the call site record (e.g. mcount/fentry)
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 contain the contents created by
810 * the compiler
812 * Return must be:
813 * 0 on success
814 * -EFAULT on error reading the location
815 * -EINVAL on a failed compare of the contents
816 * -EPERM on error writing to the location
817 * Any other value will be considered a failure.
819 #ifndef ftrace_init_nop
820 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
822 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
824 #endif
827 * ftrace_make_call - convert a nop call site into a call to addr
828 * @rec: the call site record (e.g. mcount/fentry)
829 * @addr: the address that the call site should call
831 * This is a very sensitive operation and great care needs
832 * to be taken by the arch. The operation should carefully
833 * read the location, check to see if what is read is indeed
834 * what we expect it to be, and then on success of the compare,
835 * it should write to the location.
837 * The code segment at @rec->ip should be a nop
839 * Return must be:
840 * 0 on success
841 * -EFAULT on error reading the location
842 * -EINVAL on a failed compare of the contents
843 * -EPERM on error writing to the location
844 * Any other value will be considered a failure.
846 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
848 #if defined(CONFIG_DYNAMIC_FTRACE_WITH_REGS) || \
849 defined(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS) || \
850 defined(CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS)
852 * ftrace_modify_call - convert from one addr to another (no nop)
853 * @rec: the call site record (e.g. mcount/fentry)
854 * @old_addr: the address expected to be currently called to
855 * @addr: the address to change to
857 * This is a very sensitive operation and great care needs
858 * to be taken by the arch. The operation should carefully
859 * read the location, check to see if what is read is indeed
860 * what we expect it to be, and then on success of the compare,
861 * it should write to the location.
863 * When using call ops, this is called when the associated ops change, even
864 * when (addr == old_addr).
866 * The code segment at @rec->ip should be a caller to @old_addr
868 * Return must be:
869 * 0 on success
870 * -EFAULT on error reading the location
871 * -EINVAL on a failed compare of the contents
872 * -EPERM on error writing to the location
873 * Any other value will be considered a failure.
875 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
876 unsigned long addr);
877 #else
878 /* Should never be called */
879 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
880 unsigned long addr)
882 return -EINVAL;
884 #endif
886 extern int skip_trace(unsigned long ip);
887 extern void ftrace_module_init(struct module *mod);
888 extern void ftrace_module_enable(struct module *mod);
889 extern void ftrace_release_mod(struct module *mod);
890 #else /* CONFIG_DYNAMIC_FTRACE */
891 static inline int skip_trace(unsigned long ip) { return 0; }
892 static inline void ftrace_module_init(struct module *mod) { }
893 static inline void ftrace_module_enable(struct module *mod) { }
894 static inline void ftrace_release_mod(struct module *mod) { }
895 static inline int ftrace_text_reserved(const void *start, const void *end)
897 return 0;
899 static inline unsigned long ftrace_location(unsigned long ip)
901 return 0;
905 * Again users of functions that have ftrace_ops may not
906 * have them defined when ftrace is not enabled, but these
907 * functions may still be called. Use a macro instead of inline.
909 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
910 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
911 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
912 #define ftrace_set_filter_ips(ops, ips, cnt, remove, reset) ({ -ENODEV; })
913 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
914 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
915 #define ftrace_free_filter(ops) do { } while (0)
916 #define ftrace_ops_set_global_filter(ops) do { } while (0)
918 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
919 size_t cnt, loff_t *ppos) { return -ENODEV; }
920 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
921 size_t cnt, loff_t *ppos) { return -ENODEV; }
922 static inline int
923 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
925 static inline bool is_ftrace_trampoline(unsigned long addr)
927 return false;
929 #endif /* CONFIG_DYNAMIC_FTRACE */
931 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
932 #ifndef ftrace_graph_func
933 #define ftrace_graph_func ftrace_stub
934 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
935 #else
936 #define FTRACE_OPS_GRAPH_STUB 0
937 #endif
938 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
940 /* totally disable ftrace - can not re-enable after this */
941 void ftrace_kill(void);
943 static inline void tracer_disable(void)
945 #ifdef CONFIG_FUNCTION_TRACER
946 ftrace_enabled = 0;
947 #endif
951 * Ftrace disable/restore without lock. Some synchronization mechanism
952 * must be used to prevent ftrace_enabled to be changed between
953 * disable/restore.
955 static inline int __ftrace_enabled_save(void)
957 #ifdef CONFIG_FUNCTION_TRACER
958 int saved_ftrace_enabled = ftrace_enabled;
959 ftrace_enabled = 0;
960 return saved_ftrace_enabled;
961 #else
962 return 0;
963 #endif
966 static inline void __ftrace_enabled_restore(int enabled)
968 #ifdef CONFIG_FUNCTION_TRACER
969 ftrace_enabled = enabled;
970 #endif
973 /* All archs should have this, but we define it for consistency */
974 #ifndef ftrace_return_address0
975 # define ftrace_return_address0 __builtin_return_address(0)
976 #endif
978 /* Archs may use other ways for ADDR1 and beyond */
979 #ifndef ftrace_return_address
980 # ifdef CONFIG_FRAME_POINTER
981 # define ftrace_return_address(n) __builtin_return_address(n)
982 # else
983 # define ftrace_return_address(n) 0UL
984 # endif
985 #endif
987 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
988 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
989 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
990 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
991 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
992 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
993 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
995 static __always_inline unsigned long get_lock_parent_ip(void)
997 unsigned long addr = CALLER_ADDR0;
999 if (!in_lock_functions(addr))
1000 return addr;
1001 addr = CALLER_ADDR1;
1002 if (!in_lock_functions(addr))
1003 return addr;
1004 return CALLER_ADDR2;
1007 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
1008 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
1009 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
1010 #else
1012 * Use defines instead of static inlines because some arches will make code out
1013 * of the CALLER_ADDR, when we really want these to be a real nop.
1015 # define trace_preempt_on(a0, a1) do { } while (0)
1016 # define trace_preempt_off(a0, a1) do { } while (0)
1017 #endif
1019 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
1020 extern void ftrace_init(void);
1021 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
1022 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
1023 #else
1024 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
1025 #endif
1026 #else
1027 static inline void ftrace_init(void) { }
1028 #endif
1031 * Structure that defines an entry function trace.
1032 * It's already packed but the attribute "packed" is needed
1033 * to remove extra padding at the end.
1035 struct ftrace_graph_ent {
1036 unsigned long func; /* Current function */
1037 int depth;
1038 } __packed;
1041 * Structure that defines an entry function trace with retaddr.
1042 * It's already packed but the attribute "packed" is needed
1043 * to remove extra padding at the end.
1045 struct fgraph_retaddr_ent {
1046 unsigned long func; /* Current function */
1047 int depth;
1048 unsigned long retaddr; /* Return address */
1049 } __packed;
1052 * Structure that defines a return function trace.
1053 * It's already packed but the attribute "packed" is needed
1054 * to remove extra padding at the end.
1056 struct ftrace_graph_ret {
1057 unsigned long func; /* Current function */
1058 #ifdef CONFIG_FUNCTION_GRAPH_RETVAL
1059 unsigned long retval;
1060 #endif
1061 int depth;
1062 /* Number of functions that overran the depth limit for current task */
1063 unsigned int overrun;
1064 unsigned long long calltime;
1065 unsigned long long rettime;
1066 } __packed;
1068 struct fgraph_ops;
1070 /* Type of the callback handlers for tracing function graph*/
1071 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *,
1072 struct fgraph_ops *); /* return */
1073 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *,
1074 struct fgraph_ops *); /* entry */
1076 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace,
1077 struct fgraph_ops *gops);
1078 bool ftrace_pids_enabled(struct ftrace_ops *ops);
1080 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
1082 struct fgraph_ops {
1083 trace_func_graph_ent_t entryfunc;
1084 trace_func_graph_ret_t retfunc;
1085 struct ftrace_ops ops; /* for the hash lists */
1086 void *private;
1087 trace_func_graph_ent_t saved_func;
1088 int idx;
1091 void *fgraph_reserve_data(int idx, int size_bytes);
1092 void *fgraph_retrieve_data(int idx, int *size_bytes);
1093 void *fgraph_retrieve_parent_data(int idx, int *size_bytes, int depth);
1096 * Stack of return addresses for functions
1097 * of a thread.
1098 * Used in struct thread_info
1100 struct ftrace_ret_stack {
1101 unsigned long ret;
1102 unsigned long func;
1103 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
1104 unsigned long fp;
1105 #endif
1106 unsigned long *retp;
1110 * Primary handler of a function return.
1111 * It relays on ftrace_return_to_handler.
1112 * Defined in entry_32/64.S
1114 extern void return_to_handler(void);
1116 extern int
1117 function_graph_enter(unsigned long ret, unsigned long func,
1118 unsigned long frame_pointer, unsigned long *retp);
1120 struct ftrace_ret_stack *
1121 ftrace_graph_get_ret_stack(struct task_struct *task, int skip);
1122 unsigned long ftrace_graph_top_ret_addr(struct task_struct *task);
1124 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1125 unsigned long ret, unsigned long *retp);
1126 unsigned long *fgraph_get_task_var(struct fgraph_ops *gops);
1129 * Sometimes we don't want to trace a function with the function
1130 * graph tracer but we want them to keep traced by the usual function
1131 * tracer if the function graph tracer is not configured.
1133 #define __notrace_funcgraph notrace
1135 #define FTRACE_RETFUNC_DEPTH 50
1136 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1138 extern int register_ftrace_graph(struct fgraph_ops *ops);
1139 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1142 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1144 * ftrace_graph_stop() is called when a severe error is detected in
1145 * the function graph tracing. This function is called by the critical
1146 * paths of function graph to keep those paths from doing any more harm.
1148 DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1150 static inline bool ftrace_graph_is_dead(void)
1152 return static_branch_unlikely(&kill_ftrace_graph);
1155 extern void ftrace_graph_stop(void);
1157 /* The current handlers in use */
1158 extern trace_func_graph_ret_t ftrace_graph_return;
1159 extern trace_func_graph_ent_t ftrace_graph_entry;
1161 extern void ftrace_graph_init_task(struct task_struct *t);
1162 extern void ftrace_graph_exit_task(struct task_struct *t);
1163 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1165 /* Used by assembly, but to quiet sparse warnings */
1166 extern struct ftrace_ops *function_trace_op;
1168 static inline void pause_graph_tracing(void)
1170 atomic_inc(&current->tracing_graph_pause);
1173 static inline void unpause_graph_tracing(void)
1175 atomic_dec(&current->tracing_graph_pause);
1177 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1179 #define __notrace_funcgraph
1181 static inline void ftrace_graph_init_task(struct task_struct *t) { }
1182 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1183 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1185 /* Define as macros as fgraph_ops may not be defined */
1186 #define register_ftrace_graph(ops) ({ -1; })
1187 #define unregister_ftrace_graph(ops) do { } while (0)
1189 static inline unsigned long
1190 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1191 unsigned long *retp)
1193 return ret;
1196 static inline void pause_graph_tracing(void) { }
1197 static inline void unpause_graph_tracing(void) { }
1198 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1200 #ifdef CONFIG_TRACING
1201 enum ftrace_dump_mode;
1203 #define MAX_TRACER_SIZE 100
1204 extern char ftrace_dump_on_oops[];
1205 extern int ftrace_dump_on_oops_enabled(void);
1206 extern int tracepoint_printk;
1208 extern void disable_trace_on_warning(void);
1209 extern int __disable_trace_on_warning;
1211 int tracepoint_printk_sysctl(const struct ctl_table *table, int write,
1212 void *buffer, size_t *lenp, loff_t *ppos);
1214 #else /* CONFIG_TRACING */
1215 static inline void disable_trace_on_warning(void) { }
1216 #endif /* CONFIG_TRACING */
1218 #ifdef CONFIG_FTRACE_SYSCALLS
1220 unsigned long arch_syscall_addr(int nr);
1222 #endif /* CONFIG_FTRACE_SYSCALLS */
1224 #endif /* _LINUX_FTRACE_H */