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 */
6
7#ifndef _LINUX_FTRACE_H
8#define _LINUX_FTRACE_H
9
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>
22
23#include <asm/ftrace.h>
24
25/*
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.
29 */
30#ifndef ARCH_SUPPORTS_FTRACE_OPS
31#define ARCH_SUPPORTS_FTRACE_OPS 0
32#endif
33
34#ifdef CONFIG_TRACING
35extern void ftrace_boot_snapshot(void);
36#else
37static inline void ftrace_boot_snapshot(void) { }
38#endif
39
40#ifdef CONFIG_FUNCTION_TRACER
41struct ftrace_ops;
42struct ftrace_regs;
43/*
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.
47 *
48 * Also define the function prototype that these architectures use
49 * to call the ftrace_ops_list_func().
50 */
51#if !ARCH_SUPPORTS_FTRACE_OPS
52# define FTRACE_FORCE_LIST_FUNC 1
53void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
54#else
55# define FTRACE_FORCE_LIST_FUNC 0
56void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
57 struct ftrace_ops *op, struct ftrace_regs *fregs);
58#endif
59#endif /* CONFIG_FUNCTION_TRACER */
60
61/* Main tracing buffer and events set up */
62#ifdef CONFIG_TRACING
63void trace_init(void);
64void early_trace_init(void);
65#else
66static inline void trace_init(void) { }
67static inline void early_trace_init(void) { }
68#endif
69
70struct module;
71struct ftrace_hash;
72struct ftrace_direct_func;
73
74#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
75 defined(CONFIG_DYNAMIC_FTRACE)
76const char *
77ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
78 unsigned long *off, char **modname, char *sym);
79#else
80static inline const char *
81ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
82 unsigned long *off, char **modname, char *sym)
83{
84 return NULL;
85}
86#endif
87
88#if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
89int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
90 char *type, char *name,
91 char *module_name, int *exported);
92#else
93static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
94 char *type, char *name,
95 char *module_name, int *exported)
96{
97 return -1;
98}
99#endif
100
101#ifdef CONFIG_FUNCTION_TRACER
102
103extern int ftrace_enabled;
104
105#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
106
107struct ftrace_regs {
108 struct pt_regs regs;
109};
110#define arch_ftrace_get_regs(fregs) (&(fregs)->regs)
111
112/*
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.
117 */
118#define ftrace_instruction_pointer_set(fregs, ip) do { } while (0)
119#endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
120
121static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
122{
123 if (!fregs)
124 return NULL;
125
126 return arch_ftrace_get_regs(fregs);
127}
128
129typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
130 struct ftrace_ops *op, struct ftrace_regs *fregs);
131
132ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
133
134/*
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.
142 *
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
151 * is set.
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
186 * ftrace_enabled.
187 * DIRECT - Used by the direct ftrace_ops helper for direct functions
188 * (internal ftrace only, should not be used by others)
189 */
190enum {
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),
209};
210
211/*
212 * FTRACE_OPS_CMD_* commands allow the ftrace core logic to request changes
213 * to a ftrace_ops. Note, the requests may fail.
214 *
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.
220 *
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.
226 *
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.
232 */
233enum 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,
237};
238
239/*
240 * For most ftrace_ops_cmd,
241 * Returns:
242 * 0 - Success.
243 * Negative on failure. The return value is dependent on the
244 * callback.
245 */
246typedef int (*ftrace_ops_func_t)(struct ftrace_ops *op, enum ftrace_ops_cmd cmd);
247
248#ifdef CONFIG_DYNAMIC_FTRACE
249/* The hash used to know what functions callbacks trace */
250struct ftrace_ops_hash {
251 struct ftrace_hash __rcu *notrace_hash;
252 struct ftrace_hash __rcu *filter_hash;
253 struct mutex regex_lock;
254};
255
256void ftrace_free_init_mem(void);
257void ftrace_free_mem(struct module *mod, void *start, void *end);
258#else
259static inline void ftrace_free_init_mem(void)
260{
261 ftrace_boot_snapshot();
262}
263static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
264#endif
265
266/*
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.
272 *
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.
276 */
277struct ftrace_ops {
278 ftrace_func_t func;
279 struct ftrace_ops __rcu *next;
280 unsigned long flags;
281 void *private;
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;
291#endif
292};
293
294extern struct ftrace_ops __rcu *ftrace_ops_list;
295extern struct ftrace_ops ftrace_list_end;
296
297/*
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.
303 *
304 * Silly Alpha and silly pointer-speculation compiler optimizations!
305 */
306#define do_for_each_ftrace_op(op, list) \
307 op = rcu_dereference_raw_check(list); \
308 do
309
310/*
311 * Optimized for just a single item in the list (as that is the normal case).
312 */
313#define while_for_each_ftrace_op(op) \
314 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
315 unlikely((op) != &ftrace_list_end))
316
317/*
318 * Type of the current tracing.
319 */
320enum 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 */
323};
324
325/* Current tracing type, default is FTRACE_TYPE_ENTER */
326extern enum ftrace_tracing_type_t ftrace_tracing_type;
327
328/*
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.
334 */
335int register_ftrace_function(struct ftrace_ops *ops);
336int unregister_ftrace_function(struct ftrace_ops *ops);
337
338extern void ftrace_stub(unsigned long a0, unsigned long a1,
339 struct ftrace_ops *op, struct ftrace_regs *fregs);
340
341
342int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs);
343#else /* !CONFIG_FUNCTION_TRACER */
344/*
345 * (un)register_ftrace_function must be a macro since the ops parameter
346 * must not be evaluated.
347 */
348#define register_ftrace_function(ops) ({ 0; })
349#define unregister_ftrace_function(ops) ({ 0; })
350static inline void ftrace_kill(void) { }
351static inline void ftrace_free_init_mem(void) { }
352static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
353static inline int ftrace_lookup_symbols(const char **sorted_syms, size_t cnt, unsigned long *addrs)
354{
355 return -EOPNOTSUPP;
356}
357#endif /* CONFIG_FUNCTION_TRACER */
358
359struct ftrace_func_entry {
360 struct hlist_node hlist;
361 unsigned long ip;
362 unsigned long direct; /* for direct lookup only */
363};
364
365struct dyn_ftrace;
366
367#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
368extern int ftrace_direct_func_count;
369int register_ftrace_direct(unsigned long ip, unsigned long addr);
370int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
371int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
372struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
373int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
374 struct dyn_ftrace *rec,
375 unsigned long old_addr,
376 unsigned long new_addr);
377unsigned long ftrace_find_rec_direct(unsigned long ip);
378int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
379int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
380int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
381int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr);
382
383#else
384struct ftrace_ops;
385# define ftrace_direct_func_count 0
386static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
387{
388 return -ENOTSUPP;
389}
390static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
391{
392 return -ENOTSUPP;
393}
394static inline int modify_ftrace_direct(unsigned long ip,
395 unsigned long old_addr, unsigned long new_addr)
396{
397 return -ENOTSUPP;
398}
399static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
400{
401 return NULL;
402}
403static 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)
407{
408 return -ENODEV;
409}
410static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
411{
412 return 0;
413}
414static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
415{
416 return -ENODEV;
417}
418static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
419{
420 return -ENODEV;
421}
422static inline int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
423{
424 return -ENODEV;
425}
426static inline int modify_ftrace_direct_multi_nolock(struct ftrace_ops *ops, unsigned long addr)
427{
428 return -ENODEV;
429}
430#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
431
432#ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
433/*
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.
439 *
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.
445 */
446static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
447 unsigned long addr) { }
448#endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
449
450#ifdef CONFIG_STACK_TRACER
451
452extern int stack_tracer_enabled;
453
454int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
455 size_t *lenp, loff_t *ppos);
456
457/* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
458DECLARE_PER_CPU(int, disable_stack_tracer);
459
460/**
461 * stack_tracer_disable - temporarily disable the stack tracer
462 *
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.
466 *
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.
470 */
471static inline void stack_tracer_disable(void)
472{
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);
477}
478
479/**
480 * stack_tracer_enable - re-enable the stack tracer
481 *
482 * After stack_tracer_disable() is called, stack_tracer_enable()
483 * must be called shortly afterward.
484 */
485static inline void stack_tracer_enable(void)
486{
487 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
488 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
489 this_cpu_dec(disable_stack_tracer);
490}
491#else
492static inline void stack_tracer_disable(void) { }
493static inline void stack_tracer_enable(void) { }
494#endif
495
496#ifdef CONFIG_DYNAMIC_FTRACE
497
498void ftrace_arch_code_modify_prepare(void);
499void ftrace_arch_code_modify_post_process(void);
500
501enum ftrace_bug_type {
502 FTRACE_BUG_UNKNOWN,
503 FTRACE_BUG_INIT,
504 FTRACE_BUG_NOP,
505 FTRACE_BUG_CALL,
506 FTRACE_BUG_UPDATE,
507};
508extern enum ftrace_bug_type ftrace_bug_type;
509
510/*
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().
513 */
514extern const void *ftrace_expected;
515
516void ftrace_bug(int err, struct dyn_ftrace *rec);
517
518struct seq_file;
519
520extern int ftrace_text_reserved(const void *start, const void *end);
521
522struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
523
524bool is_ftrace_trampoline(unsigned long addr);
525
526/*
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.
531 *
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
539 *
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.
545 */
546enum {
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),
556};
557
558#define FTRACE_REF_MAX_SHIFT 23
559#define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
560
561#define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
562
563struct dyn_ftrace {
564 unsigned long ip; /* address of mcount call-site */
565 unsigned long flags;
566 struct dyn_arch_ftrace arch;
567};
568
569int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
570 int remove, int reset);
571int ftrace_set_filter_ips(struct ftrace_ops *ops, unsigned long *ips,
572 unsigned int cnt, int remove, int reset);
573int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
574 int len, int reset);
575int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
576 int len, int reset);
577void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
578void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
579void ftrace_free_filter(struct ftrace_ops *ops);
580void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
581
582enum {
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),
589};
590
591/*
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.
596 *
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
601 */
602enum {
603 FTRACE_UPDATE_IGNORE,
604 FTRACE_UPDATE_MAKE_CALL,
605 FTRACE_UPDATE_MODIFY_CALL,
606 FTRACE_UPDATE_MAKE_NOP,
607};
608
609enum {
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),
617};
618
619void arch_ftrace_update_code(int command);
620void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
621void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
622void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
623
624struct ftrace_rec_iter;
625
626struct ftrace_rec_iter *ftrace_rec_iter_start(void);
627struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
628struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
629
630#define for_ftrace_rec_iter(iter) \
631 for (iter = ftrace_rec_iter_start(); \
632 iter; \
633 iter = ftrace_rec_iter_next(iter))
634
635
636int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
637int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
638void ftrace_run_stop_machine(int command);
639unsigned long ftrace_location(unsigned long ip);
640unsigned long ftrace_location_range(unsigned long start, unsigned long end);
641unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
642unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
643
644extern ftrace_func_t ftrace_trace_function;
645
646int ftrace_regex_open(struct ftrace_ops *ops, int flag,
647 struct inode *inode, struct file *file);
648ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
649 size_t cnt, loff_t *ppos);
650ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
651 size_t cnt, loff_t *ppos);
652int ftrace_regex_release(struct inode *inode, struct file *file);
653
654void __init
655ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
656
657/* defined in arch */
658extern int ftrace_ip_converted(unsigned long ip);
659extern int ftrace_dyn_arch_init(void);
660extern void ftrace_replace_code(int enable);
661extern int ftrace_update_ftrace_func(ftrace_func_t func);
662extern void ftrace_caller(void);
663extern void ftrace_regs_caller(void);
664extern void ftrace_call(void);
665extern void ftrace_regs_call(void);
666extern void mcount_call(void);
667
668void ftrace_modify_all_code(int command);
669
670#ifndef FTRACE_ADDR
671#define FTRACE_ADDR ((unsigned long)ftrace_caller)
672#endif
673
674#ifndef FTRACE_GRAPH_ADDR
675#define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
676#endif
677
678#ifndef FTRACE_REGS_ADDR
679#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
680# define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
681#else
682# define FTRACE_REGS_ADDR FTRACE_ADDR
683#endif
684#endif
685
686/*
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.
691 */
692#ifndef FTRACE_GRAPH_TRAMP_ADDR
693#define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
694#endif
695
696#ifdef CONFIG_FUNCTION_GRAPH_TRACER
697extern void ftrace_graph_caller(void);
698extern int ftrace_enable_ftrace_graph_caller(void);
699extern int ftrace_disable_ftrace_graph_caller(void);
700#else
701static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
702static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
703#endif
704
705/**
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
710 *
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.
716 *
717 * The code segment at @rec->ip should be a caller to @addr
718 *
719 * Return must be:
720 * 0 on success
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.
725 */
726extern int ftrace_make_nop(struct module *mod,
727 struct dyn_ftrace *rec, unsigned long addr);
728
729/**
730 * ftrace_need_init_nop - return whether nop call sites should be initialized
731 *
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
735 * condition.
736 *
737 * Return must be:
738 * 0 if ftrace_init_nop() should be called
739 * Nonzero if ftrace_init_nop() should not be called
740 */
741
742#ifndef ftrace_need_init_nop
743#define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
744#endif
745
746/**
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)
750 *
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.
756 *
757 * The code segment at @rec->ip should contain the contents created by
758 * the compiler
759 *
760 * Return must be:
761 * 0 on success
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.
766 */
767#ifndef ftrace_init_nop
768static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
769{
770 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
771}
772#endif
773
774/**
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
778 *
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.
784 *
785 * The code segment at @rec->ip should be a nop
786 *
787 * Return must be:
788 * 0 on success
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.
793 */
794extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
795
796#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
797/**
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
802 *
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.
808 *
809 * The code segment at @rec->ip should be a caller to @old_addr
810 *
811 * Return must be:
812 * 0 on success
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.
817 */
818extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
819 unsigned long addr);
820#else
821/* Should never be called */
822static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
823 unsigned long addr)
824{
825 return -EINVAL;
826}
827#endif
828
829/* May be defined in arch */
830extern int ftrace_arch_read_dyn_info(char *buf, int size);
831
832extern int skip_trace(unsigned long ip);
833extern void ftrace_module_init(struct module *mod);
834extern void ftrace_module_enable(struct module *mod);
835extern void ftrace_release_mod(struct module *mod);
836
837extern void ftrace_disable_daemon(void);
838extern void ftrace_enable_daemon(void);
839#else /* CONFIG_DYNAMIC_FTRACE */
840static inline int skip_trace(unsigned long ip) { return 0; }
841static inline void ftrace_disable_daemon(void) { }
842static inline void ftrace_enable_daemon(void) { }
843static inline void ftrace_module_init(struct module *mod) { }
844static inline void ftrace_module_enable(struct module *mod) { }
845static inline void ftrace_release_mod(struct module *mod) { }
846static inline int ftrace_text_reserved(const void *start, const void *end)
847{
848 return 0;
849}
850static inline unsigned long ftrace_location(unsigned long ip)
851{
852 return 0;
853}
854
855/*
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.
859 */
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)
868
869static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
870 size_t cnt, loff_t *ppos) { return -ENODEV; }
871static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
872 size_t cnt, loff_t *ppos) { return -ENODEV; }
873static inline int
874ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
875
876static inline bool is_ftrace_trampoline(unsigned long addr)
877{
878 return false;
879}
880#endif /* CONFIG_DYNAMIC_FTRACE */
881
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
886#else
887#define FTRACE_OPS_GRAPH_STUB 0
888#endif
889#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
890
891/* totally disable ftrace - can not re-enable after this */
892void ftrace_kill(void);
893
894static inline void tracer_disable(void)
895{
896#ifdef CONFIG_FUNCTION_TRACER
897 ftrace_enabled = 0;
898#endif
899}
900
901/*
902 * Ftrace disable/restore without lock. Some synchronization mechanism
903 * must be used to prevent ftrace_enabled to be changed between
904 * disable/restore.
905 */
906static inline int __ftrace_enabled_save(void)
907{
908#ifdef CONFIG_FUNCTION_TRACER
909 int saved_ftrace_enabled = ftrace_enabled;
910 ftrace_enabled = 0;
911 return saved_ftrace_enabled;
912#else
913 return 0;
914#endif
915}
916
917static inline void __ftrace_enabled_restore(int enabled)
918{
919#ifdef CONFIG_FUNCTION_TRACER
920 ftrace_enabled = enabled;
921#endif
922}
923
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)
927#endif
928
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)
933# else
934# define ftrace_return_address(n) 0UL
935# endif
936#endif
937
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))
945
946static inline unsigned long get_lock_parent_ip(void)
947{
948 unsigned long addr = CALLER_ADDR0;
949
950 if (!in_lock_functions(addr))
951 return addr;
952 addr = CALLER_ADDR1;
953 if (!in_lock_functions(addr))
954 return addr;
955 return CALLER_ADDR2;
956}
957
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);
961#else
962/*
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.
965 */
966# define trace_preempt_on(a0, a1) do { } while (0)
967# define trace_preempt_off(a0, a1) do { } while (0)
968#endif
969
970#ifdef CONFIG_FTRACE_MCOUNT_RECORD
971extern void ftrace_init(void);
972#ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
973#define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
974#else
975#define FTRACE_CALLSITE_SECTION "__mcount_loc"
976#endif
977#else
978static inline void ftrace_init(void) { }
979#endif
980
981/*
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.
985 */
986struct ftrace_graph_ent {
987 unsigned long func; /* Current function */
988 int depth;
989} __packed;
990
991/*
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.
995 */
996struct ftrace_graph_ret {
997 unsigned long func; /* Current function */
998 int depth;
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;
1003} __packed;
1004
1005/* Type of the callback handlers for tracing function graph*/
1006typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
1007typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
1008
1009extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
1010
1011#ifdef CONFIG_FUNCTION_GRAPH_TRACER
1012
1013struct fgraph_ops {
1014 trace_func_graph_ent_t entryfunc;
1015 trace_func_graph_ret_t retfunc;
1016};
1017
1018/*
1019 * Stack of return addresses for functions
1020 * of a thread.
1021 * Used in struct thread_info
1022 */
1023struct ftrace_ret_stack {
1024 unsigned long ret;
1025 unsigned long func;
1026 unsigned long long calltime;
1027#ifdef CONFIG_FUNCTION_PROFILER
1028 unsigned long long subtime;
1029#endif
1030#ifdef HAVE_FUNCTION_GRAPH_FP_TEST
1031 unsigned long fp;
1032#endif
1033#ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
1034 unsigned long *retp;
1035#endif
1036};
1037
1038/*
1039 * Primary handler of a function return.
1040 * It relays on ftrace_return_to_handler.
1041 * Defined in entry_32/64.S
1042 */
1043extern void return_to_handler(void);
1044
1045extern int
1046function_graph_enter(unsigned long ret, unsigned long func,
1047 unsigned long frame_pointer, unsigned long *retp);
1048
1049struct ftrace_ret_stack *
1050ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
1051
1052unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
1053 unsigned long ret, unsigned long *retp);
1054
1055/*
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.
1059 */
1060#define __notrace_funcgraph notrace
1061
1062#define FTRACE_RETFUNC_DEPTH 50
1063#define FTRACE_RETSTACK_ALLOC_SIZE 32
1064
1065extern int register_ftrace_graph(struct fgraph_ops *ops);
1066extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1067
1068/**
1069 * ftrace_graph_is_dead - returns true if ftrace_graph_stop() was called
1070 *
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.
1074 */
1075DECLARE_STATIC_KEY_FALSE(kill_ftrace_graph);
1076
1077static inline bool ftrace_graph_is_dead(void)
1078{
1079 return static_branch_unlikely(&kill_ftrace_graph);
1080}
1081
1082extern void ftrace_graph_stop(void);
1083
1084/* The current handlers in use */
1085extern trace_func_graph_ret_t ftrace_graph_return;
1086extern trace_func_graph_ent_t ftrace_graph_entry;
1087
1088extern void ftrace_graph_init_task(struct task_struct *t);
1089extern void ftrace_graph_exit_task(struct task_struct *t);
1090extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1091
1092static inline void pause_graph_tracing(void)
1093{
1094 atomic_inc(&current->tracing_graph_pause);
1095}
1096
1097static inline void unpause_graph_tracing(void)
1098{
1099 atomic_dec(&current->tracing_graph_pause);
1100}
1101#else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1102
1103#define __notrace_funcgraph
1104
1105static inline void ftrace_graph_init_task(struct task_struct *t) { }
1106static inline void ftrace_graph_exit_task(struct task_struct *t) { }
1107static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1108
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)
1112
1113static inline unsigned long
1114ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1115 unsigned long *retp)
1116{
1117 return ret;
1118}
1119
1120static inline void pause_graph_tracing(void) { }
1121static inline void unpause_graph_tracing(void) { }
1122#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1123
1124#ifdef CONFIG_TRACING
1125
1126/* flags for current->trace */
1127enum {
1128 TSK_TRACE_FL_TRACE_BIT = 0,
1129 TSK_TRACE_FL_GRAPH_BIT = 1,
1130};
1131enum {
1132 TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
1133 TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
1134};
1135
1136static inline void set_tsk_trace_trace(struct task_struct *tsk)
1137{
1138 set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1139}
1140
1141static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1142{
1143 clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1144}
1145
1146static inline int test_tsk_trace_trace(struct task_struct *tsk)
1147{
1148 return tsk->trace & TSK_TRACE_FL_TRACE;
1149}
1150
1151static inline void set_tsk_trace_graph(struct task_struct *tsk)
1152{
1153 set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1154}
1155
1156static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1157{
1158 clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1159}
1160
1161static inline int test_tsk_trace_graph(struct task_struct *tsk)
1162{
1163 return tsk->trace & TSK_TRACE_FL_GRAPH;
1164}
1165
1166enum ftrace_dump_mode;
1167
1168extern enum ftrace_dump_mode ftrace_dump_on_oops;
1169extern int tracepoint_printk;
1170
1171extern void disable_trace_on_warning(void);
1172extern int __disable_trace_on_warning;
1173
1174int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1175 void *buffer, size_t *lenp, loff_t *ppos);
1176
1177#else /* CONFIG_TRACING */
1178static inline void disable_trace_on_warning(void) { }
1179#endif /* CONFIG_TRACING */
1180
1181#ifdef CONFIG_FTRACE_SYSCALLS
1182
1183unsigned long arch_syscall_addr(int nr);
1184
1185#endif /* CONFIG_FTRACE_SYSCALLS */
1186
1187#endif /* _LINUX_FTRACE_H */
1188

source code of linux/include/linux/ftrace.h