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/kallsyms.h>
13 #include <linux/linkage.h>
14 #include <linux/bitops.h>
15 #include <linux/ptrace.h>
16 #include <linux/ktime.h>
17 #include <linux/sched.h>
18 #include <linux/types.h>
19 #include <linux/init.h>
20 #include <linux/fs.h>
21
22 #include <asm/ftrace.h>
23
24 /*
25 * If the arch supports passing the variable contents of
26 * function_trace_op as the third parameter back from the
27 * mcount call, then the arch should define this as 1.
28 */
29 #ifndef ARCH_SUPPORTS_FTRACE_OPS
30 #define ARCH_SUPPORTS_FTRACE_OPS 0
31 #endif
32
33 #ifdef CONFIG_FUNCTION_TRACER
34 struct ftrace_ops;
35 struct ftrace_regs;
36 /*
37 * If the arch's mcount caller does not support all of ftrace's
38 * features, then it must call an indirect function that
39 * does. Or at least does enough to prevent any unwelcome side effects.
40 *
41 * Also define the function prototype that these architectures use
42 * to call the ftrace_ops_list_func().
43 */
44 #if !ARCH_SUPPORTS_FTRACE_OPS
45 # define FTRACE_FORCE_LIST_FUNC 1
46 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip);
47 #else
48 # define FTRACE_FORCE_LIST_FUNC 0
49 void arch_ftrace_ops_list_func(unsigned long ip, unsigned long parent_ip,
50 struct ftrace_ops *op, struct ftrace_regs *fregs);
51 #endif
52 #endif /* CONFIG_FUNCTION_TRACER */
53
54 /* Main tracing buffer and events set up */
55 #ifdef CONFIG_TRACING
56 void trace_init(void);
57 void early_trace_init(void);
58 #else
trace_init(void)59 static inline void trace_init(void) { }
early_trace_init(void)60 static inline void early_trace_init(void) { }
61 #endif
62
63 struct module;
64 struct ftrace_hash;
65 struct ftrace_direct_func;
66
67 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_MODULES) && \
68 defined(CONFIG_DYNAMIC_FTRACE)
69 const char *
70 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
71 unsigned long *off, char **modname, char *sym);
72 #else
73 static inline const char *
ftrace_mod_address_lookup(unsigned long addr,unsigned long * size,unsigned long * off,char ** modname,char * sym)74 ftrace_mod_address_lookup(unsigned long addr, unsigned long *size,
75 unsigned long *off, char **modname, char *sym)
76 {
77 return NULL;
78 }
79 #endif
80
81 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE)
82 int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
83 char *type, char *name,
84 char *module_name, int *exported);
85 #else
ftrace_mod_get_kallsym(unsigned int symnum,unsigned long * value,char * type,char * name,char * module_name,int * exported)86 static inline int ftrace_mod_get_kallsym(unsigned int symnum, unsigned long *value,
87 char *type, char *name,
88 char *module_name, int *exported)
89 {
90 return -1;
91 }
92 #endif
93
94 #ifdef CONFIG_FUNCTION_TRACER
95
96 extern int ftrace_enabled;
97 extern int
98 ftrace_enable_sysctl(struct ctl_table *table, int write,
99 void *buffer, size_t *lenp, loff_t *ppos);
100
101 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
102
103 struct ftrace_regs {
104 struct pt_regs regs;
105 };
106 #define arch_ftrace_get_regs(fregs) (&(fregs)->regs)
107
108 /*
109 * ftrace_instruction_pointer_set() is to be defined by the architecture
110 * if to allow setting of the instruction pointer from the ftrace_regs
111 * when HAVE_DYNAMIC_FTRACE_WITH_ARGS is set and it supports
112 * live kernel patching.
113 */
114 #define ftrace_instruction_pointer_set(fregs, ip) do { } while (0)
115 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS */
116
ftrace_get_regs(struct ftrace_regs * fregs)117 static __always_inline struct pt_regs *ftrace_get_regs(struct ftrace_regs *fregs)
118 {
119 if (!fregs)
120 return NULL;
121
122 return arch_ftrace_get_regs(fregs);
123 }
124
125 typedef void (*ftrace_func_t)(unsigned long ip, unsigned long parent_ip,
126 struct ftrace_ops *op, struct ftrace_regs *fregs);
127
128 ftrace_func_t ftrace_ops_get_func(struct ftrace_ops *ops);
129
130 /*
131 * FTRACE_OPS_FL_* bits denote the state of ftrace_ops struct and are
132 * set in the flags member.
133 * CONTROL, SAVE_REGS, SAVE_REGS_IF_SUPPORTED, RECURSION, STUB and
134 * IPMODIFY are a kind of attribute flags which can be set only before
135 * registering the ftrace_ops, and can not be modified while registered.
136 * Changing those attribute flags after registering ftrace_ops will
137 * cause unexpected results.
138 *
139 * ENABLED - set/unset when ftrace_ops is registered/unregistered
140 * DYNAMIC - set when ftrace_ops is registered to denote dynamically
141 * allocated ftrace_ops which need special care
142 * SAVE_REGS - The ftrace_ops wants regs saved at each function called
143 * and passed to the callback. If this flag is set, but the
144 * architecture does not support passing regs
145 * (CONFIG_DYNAMIC_FTRACE_WITH_REGS is not defined), then the
146 * ftrace_ops will fail to register, unless the next flag
147 * is set.
148 * SAVE_REGS_IF_SUPPORTED - This is the same as SAVE_REGS, but if the
149 * handler can handle an arch that does not save regs
150 * (the handler tests if regs == NULL), then it can set
151 * this flag instead. It will not fail registering the ftrace_ops
152 * but, the regs field will be NULL if the arch does not support
153 * passing regs to the handler.
154 * Note, if this flag is set, the SAVE_REGS flag will automatically
155 * get set upon registering the ftrace_ops, if the arch supports it.
156 * RECURSION - The ftrace_ops can set this to tell the ftrace infrastructure
157 * that the call back needs recursion protection. If it does
158 * not set this, then the ftrace infrastructure will assume
159 * that the callback can handle recursion on its own.
160 * STUB - The ftrace_ops is just a place holder.
161 * INITIALIZED - The ftrace_ops has already been initialized (first use time
162 * register_ftrace_function() is called, it will initialized the ops)
163 * DELETED - The ops are being deleted, do not let them be registered again.
164 * ADDING - The ops is in the process of being added.
165 * REMOVING - The ops is in the process of being removed.
166 * MODIFYING - The ops is in the process of changing its filter functions.
167 * ALLOC_TRAMP - A dynamic trampoline was allocated by the core code.
168 * The arch specific code sets this flag when it allocated a
169 * trampoline. This lets the arch know that it can update the
170 * trampoline in case the callback function changes.
171 * The ftrace_ops trampoline can be set by the ftrace users, and
172 * in such cases the arch must not modify it. Only the arch ftrace
173 * core code should set this flag.
174 * IPMODIFY - The ops can modify the IP register. This can only be set with
175 * SAVE_REGS. If another ops with this flag set is already registered
176 * for any of the functions that this ops will be registered for, then
177 * this ops will fail to register or set_filter_ip.
178 * PID - Is affected by set_ftrace_pid (allows filtering on those pids)
179 * RCU - Set when the ops can only be called when RCU is watching.
180 * TRACE_ARRAY - The ops->private points to a trace_array descriptor.
181 * PERMANENT - Set when the ops is permanent and should not be affected by
182 * ftrace_enabled.
183 * DIRECT - Used by the direct ftrace_ops helper for direct functions
184 * (internal ftrace only, should not be used by others)
185 */
186 enum {
187 FTRACE_OPS_FL_ENABLED = BIT(0),
188 FTRACE_OPS_FL_DYNAMIC = BIT(1),
189 FTRACE_OPS_FL_SAVE_REGS = BIT(2),
190 FTRACE_OPS_FL_SAVE_REGS_IF_SUPPORTED = BIT(3),
191 FTRACE_OPS_FL_RECURSION = BIT(4),
192 FTRACE_OPS_FL_STUB = BIT(5),
193 FTRACE_OPS_FL_INITIALIZED = BIT(6),
194 FTRACE_OPS_FL_DELETED = BIT(7),
195 FTRACE_OPS_FL_ADDING = BIT(8),
196 FTRACE_OPS_FL_REMOVING = BIT(9),
197 FTRACE_OPS_FL_MODIFYING = BIT(10),
198 FTRACE_OPS_FL_ALLOC_TRAMP = BIT(11),
199 FTRACE_OPS_FL_IPMODIFY = BIT(12),
200 FTRACE_OPS_FL_PID = BIT(13),
201 FTRACE_OPS_FL_RCU = BIT(14),
202 FTRACE_OPS_FL_TRACE_ARRAY = BIT(15),
203 FTRACE_OPS_FL_PERMANENT = BIT(16),
204 FTRACE_OPS_FL_DIRECT = BIT(17),
205 };
206
207 #ifdef CONFIG_DYNAMIC_FTRACE
208 /* The hash used to know what functions callbacks trace */
209 struct ftrace_ops_hash {
210 struct ftrace_hash __rcu *notrace_hash;
211 struct ftrace_hash __rcu *filter_hash;
212 struct mutex regex_lock;
213 };
214
215 void ftrace_free_init_mem(void);
216 void ftrace_free_mem(struct module *mod, void *start, void *end);
217 #else
ftrace_free_init_mem(void)218 static inline void ftrace_free_init_mem(void) { }
ftrace_free_mem(struct module * mod,void * start,void * end)219 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
220 #endif
221
222 /*
223 * Note, ftrace_ops can be referenced outside of RCU protection, unless
224 * the RCU flag is set. If ftrace_ops is allocated and not part of kernel
225 * core data, the unregistering of it will perform a scheduling on all CPUs
226 * to make sure that there are no more users. Depending on the load of the
227 * system that may take a bit of time.
228 *
229 * Any private data added must also take care not to be freed and if private
230 * data is added to a ftrace_ops that is in core code, the user of the
231 * ftrace_ops must perform a schedule_on_each_cpu() before freeing it.
232 */
233 struct ftrace_ops {
234 ftrace_func_t func;
235 struct ftrace_ops __rcu *next;
236 unsigned long flags;
237 void *private;
238 ftrace_func_t saved_func;
239 #ifdef CONFIG_DYNAMIC_FTRACE
240 struct ftrace_ops_hash local_hash;
241 struct ftrace_ops_hash *func_hash;
242 struct ftrace_ops_hash old_hash;
243 unsigned long trampoline;
244 unsigned long trampoline_size;
245 struct list_head list;
246 #endif
247 };
248
249 extern struct ftrace_ops __rcu *ftrace_ops_list;
250 extern struct ftrace_ops ftrace_list_end;
251
252 /*
253 * Traverse the ftrace_ops_list, invoking all entries. The reason that we
254 * can use rcu_dereference_raw_check() is that elements removed from this list
255 * are simply leaked, so there is no need to interact with a grace-period
256 * mechanism. The rcu_dereference_raw_check() calls are needed to handle
257 * concurrent insertions into the ftrace_ops_list.
258 *
259 * Silly Alpha and silly pointer-speculation compiler optimizations!
260 */
261 #define do_for_each_ftrace_op(op, list) \
262 op = rcu_dereference_raw_check(list); \
263 do
264
265 /*
266 * Optimized for just a single item in the list (as that is the normal case).
267 */
268 #define while_for_each_ftrace_op(op) \
269 while (likely(op = rcu_dereference_raw_check((op)->next)) && \
270 unlikely((op) != &ftrace_list_end))
271
272 /*
273 * Type of the current tracing.
274 */
275 enum ftrace_tracing_type_t {
276 FTRACE_TYPE_ENTER = 0, /* Hook the call of the function */
277 FTRACE_TYPE_RETURN, /* Hook the return of the function */
278 };
279
280 /* Current tracing type, default is FTRACE_TYPE_ENTER */
281 extern enum ftrace_tracing_type_t ftrace_tracing_type;
282
283 /*
284 * The ftrace_ops must be a static and should also
285 * be read_mostly. These functions do modify read_mostly variables
286 * so use them sparely. Never free an ftrace_op or modify the
287 * next pointer after it has been registered. Even after unregistering
288 * it, the next pointer may still be used internally.
289 */
290 int register_ftrace_function(struct ftrace_ops *ops);
291 int unregister_ftrace_function(struct ftrace_ops *ops);
292
293 extern void ftrace_stub(unsigned long a0, unsigned long a1,
294 struct ftrace_ops *op, struct ftrace_regs *fregs);
295
296 #else /* !CONFIG_FUNCTION_TRACER */
297 /*
298 * (un)register_ftrace_function must be a macro since the ops parameter
299 * must not be evaluated.
300 */
301 #define register_ftrace_function(ops) ({ 0; })
302 #define unregister_ftrace_function(ops) ({ 0; })
ftrace_kill(void)303 static inline void ftrace_kill(void) { }
ftrace_free_init_mem(void)304 static inline void ftrace_free_init_mem(void) { }
ftrace_free_mem(struct module * mod,void * start,void * end)305 static inline void ftrace_free_mem(struct module *mod, void *start, void *end) { }
306 #endif /* CONFIG_FUNCTION_TRACER */
307
308 struct ftrace_func_entry {
309 struct hlist_node hlist;
310 unsigned long ip;
311 unsigned long direct; /* for direct lookup only */
312 };
313
314 struct dyn_ftrace;
315
316 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
317 extern int ftrace_direct_func_count;
318 int register_ftrace_direct(unsigned long ip, unsigned long addr);
319 int unregister_ftrace_direct(unsigned long ip, unsigned long addr);
320 int modify_ftrace_direct(unsigned long ip, unsigned long old_addr, unsigned long new_addr);
321 struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr);
322 int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
323 struct dyn_ftrace *rec,
324 unsigned long old_addr,
325 unsigned long new_addr);
326 unsigned long ftrace_find_rec_direct(unsigned long ip);
327 int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
328 int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
329 int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr);
330
331 #else
332 struct ftrace_ops;
333 # define ftrace_direct_func_count 0
register_ftrace_direct(unsigned long ip,unsigned long addr)334 static inline int register_ftrace_direct(unsigned long ip, unsigned long addr)
335 {
336 return -ENOTSUPP;
337 }
unregister_ftrace_direct(unsigned long ip,unsigned long addr)338 static inline int unregister_ftrace_direct(unsigned long ip, unsigned long addr)
339 {
340 return -ENOTSUPP;
341 }
modify_ftrace_direct(unsigned long ip,unsigned long old_addr,unsigned long new_addr)342 static inline int modify_ftrace_direct(unsigned long ip,
343 unsigned long old_addr, unsigned long new_addr)
344 {
345 return -ENOTSUPP;
346 }
ftrace_find_direct_func(unsigned long addr)347 static inline struct ftrace_direct_func *ftrace_find_direct_func(unsigned long addr)
348 {
349 return NULL;
350 }
ftrace_modify_direct_caller(struct ftrace_func_entry * entry,struct dyn_ftrace * rec,unsigned long old_addr,unsigned long new_addr)351 static inline int ftrace_modify_direct_caller(struct ftrace_func_entry *entry,
352 struct dyn_ftrace *rec,
353 unsigned long old_addr,
354 unsigned long new_addr)
355 {
356 return -ENODEV;
357 }
ftrace_find_rec_direct(unsigned long ip)358 static inline unsigned long ftrace_find_rec_direct(unsigned long ip)
359 {
360 return 0;
361 }
register_ftrace_direct_multi(struct ftrace_ops * ops,unsigned long addr)362 static inline int register_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
363 {
364 return -ENODEV;
365 }
unregister_ftrace_direct_multi(struct ftrace_ops * ops,unsigned long addr)366 static inline int unregister_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
367 {
368 return -ENODEV;
369 }
modify_ftrace_direct_multi(struct ftrace_ops * ops,unsigned long addr)370 static inline int modify_ftrace_direct_multi(struct ftrace_ops *ops, unsigned long addr)
371 {
372 return -ENODEV;
373 }
374 #endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
375
376 #ifndef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
377 /*
378 * This must be implemented by the architecture.
379 * It is the way the ftrace direct_ops helper, when called
380 * via ftrace (because there's other callbacks besides the
381 * direct call), can inform the architecture's trampoline that this
382 * routine has a direct caller, and what the caller is.
383 *
384 * For example, in x86, it returns the direct caller
385 * callback function via the regs->orig_ax parameter.
386 * Then in the ftrace trampoline, if this is set, it makes
387 * the return from the trampoline jump to the direct caller
388 * instead of going back to the function it just traced.
389 */
arch_ftrace_set_direct_caller(struct pt_regs * regs,unsigned long addr)390 static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
391 unsigned long addr) { }
392 #endif /* CONFIG_HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
393
394 #ifdef CONFIG_STACK_TRACER
395
396 extern int stack_tracer_enabled;
397
398 int stack_trace_sysctl(struct ctl_table *table, int write, void *buffer,
399 size_t *lenp, loff_t *ppos);
400
401 /* DO NOT MODIFY THIS VARIABLE DIRECTLY! */
402 DECLARE_PER_CPU(int, disable_stack_tracer);
403
404 /**
405 * stack_tracer_disable - temporarily disable the stack tracer
406 *
407 * There's a few locations (namely in RCU) where stack tracing
408 * cannot be executed. This function is used to disable stack
409 * tracing during those critical sections.
410 *
411 * This function must be called with preemption or interrupts
412 * disabled and stack_tracer_enable() must be called shortly after
413 * while preemption or interrupts are still disabled.
414 */
stack_tracer_disable(void)415 static inline void stack_tracer_disable(void)
416 {
417 /* Preemption or interrupts must be disabled */
418 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
419 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
420 this_cpu_inc(disable_stack_tracer);
421 }
422
423 /**
424 * stack_tracer_enable - re-enable the stack tracer
425 *
426 * After stack_tracer_disable() is called, stack_tracer_enable()
427 * must be called shortly afterward.
428 */
stack_tracer_enable(void)429 static inline void stack_tracer_enable(void)
430 {
431 if (IS_ENABLED(CONFIG_DEBUG_PREEMPT))
432 WARN_ON_ONCE(!preempt_count() || !irqs_disabled());
433 this_cpu_dec(disable_stack_tracer);
434 }
435 #else
stack_tracer_disable(void)436 static inline void stack_tracer_disable(void) { }
stack_tracer_enable(void)437 static inline void stack_tracer_enable(void) { }
438 #endif
439
440 #ifdef CONFIG_DYNAMIC_FTRACE
441
442 int ftrace_arch_code_modify_prepare(void);
443 int ftrace_arch_code_modify_post_process(void);
444
445 enum ftrace_bug_type {
446 FTRACE_BUG_UNKNOWN,
447 FTRACE_BUG_INIT,
448 FTRACE_BUG_NOP,
449 FTRACE_BUG_CALL,
450 FTRACE_BUG_UPDATE,
451 };
452 extern enum ftrace_bug_type ftrace_bug_type;
453
454 /*
455 * Archs can set this to point to a variable that holds the value that was
456 * expected at the call site before calling ftrace_bug().
457 */
458 extern const void *ftrace_expected;
459
460 void ftrace_bug(int err, struct dyn_ftrace *rec);
461
462 struct seq_file;
463
464 extern int ftrace_text_reserved(const void *start, const void *end);
465
466 struct ftrace_ops *ftrace_ops_trampoline(unsigned long addr);
467
468 bool is_ftrace_trampoline(unsigned long addr);
469
470 /*
471 * The dyn_ftrace record's flags field is split into two parts.
472 * the first part which is '0-FTRACE_REF_MAX' is a counter of
473 * the number of callbacks that have registered the function that
474 * the dyn_ftrace descriptor represents.
475 *
476 * The second part is a mask:
477 * ENABLED - the function is being traced
478 * REGS - the record wants the function to save regs
479 * REGS_EN - the function is set up to save regs.
480 * IPMODIFY - the record allows for the IP address to be changed.
481 * DISABLED - the record is not ready to be touched yet
482 * DIRECT - there is a direct function to call
483 *
484 * When a new ftrace_ops is registered and wants a function to save
485 * pt_regs, the rec->flags REGS is set. When the function has been
486 * set up to save regs, the REG_EN flag is set. Once a function
487 * starts saving regs it will do so until all ftrace_ops are removed
488 * from tracing that function.
489 */
490 enum {
491 FTRACE_FL_ENABLED = (1UL << 31),
492 FTRACE_FL_REGS = (1UL << 30),
493 FTRACE_FL_REGS_EN = (1UL << 29),
494 FTRACE_FL_TRAMP = (1UL << 28),
495 FTRACE_FL_TRAMP_EN = (1UL << 27),
496 FTRACE_FL_IPMODIFY = (1UL << 26),
497 FTRACE_FL_DISABLED = (1UL << 25),
498 FTRACE_FL_DIRECT = (1UL << 24),
499 FTRACE_FL_DIRECT_EN = (1UL << 23),
500 };
501
502 #define FTRACE_REF_MAX_SHIFT 23
503 #define FTRACE_REF_MAX ((1UL << FTRACE_REF_MAX_SHIFT) - 1)
504
505 #define ftrace_rec_count(rec) ((rec)->flags & FTRACE_REF_MAX)
506
507 struct dyn_ftrace {
508 unsigned long ip; /* address of mcount call-site */
509 unsigned long flags;
510 struct dyn_arch_ftrace arch;
511 };
512
513 int ftrace_set_filter_ip(struct ftrace_ops *ops, unsigned long ip,
514 int remove, int reset);
515 int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf,
516 int len, int reset);
517 int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf,
518 int len, int reset);
519 void ftrace_set_global_filter(unsigned char *buf, int len, int reset);
520 void ftrace_set_global_notrace(unsigned char *buf, int len, int reset);
521 void ftrace_free_filter(struct ftrace_ops *ops);
522 void ftrace_ops_set_global_filter(struct ftrace_ops *ops);
523
524 enum {
525 FTRACE_UPDATE_CALLS = (1 << 0),
526 FTRACE_DISABLE_CALLS = (1 << 1),
527 FTRACE_UPDATE_TRACE_FUNC = (1 << 2),
528 FTRACE_START_FUNC_RET = (1 << 3),
529 FTRACE_STOP_FUNC_RET = (1 << 4),
530 FTRACE_MAY_SLEEP = (1 << 5),
531 };
532
533 /*
534 * The FTRACE_UPDATE_* enum is used to pass information back
535 * from the ftrace_update_record() and ftrace_test_record()
536 * functions. These are called by the code update routines
537 * to find out what is to be done for a given function.
538 *
539 * IGNORE - The function is already what we want it to be
540 * MAKE_CALL - Start tracing the function
541 * MODIFY_CALL - Stop saving regs for the function
542 * MAKE_NOP - Stop tracing the function
543 */
544 enum {
545 FTRACE_UPDATE_IGNORE,
546 FTRACE_UPDATE_MAKE_CALL,
547 FTRACE_UPDATE_MODIFY_CALL,
548 FTRACE_UPDATE_MAKE_NOP,
549 };
550
551 enum {
552 FTRACE_ITER_FILTER = (1 << 0),
553 FTRACE_ITER_NOTRACE = (1 << 1),
554 FTRACE_ITER_PRINTALL = (1 << 2),
555 FTRACE_ITER_DO_PROBES = (1 << 3),
556 FTRACE_ITER_PROBE = (1 << 4),
557 FTRACE_ITER_MOD = (1 << 5),
558 FTRACE_ITER_ENABLED = (1 << 6),
559 };
560
561 void arch_ftrace_update_code(int command);
562 void arch_ftrace_update_trampoline(struct ftrace_ops *ops);
563 void *arch_ftrace_trampoline_func(struct ftrace_ops *ops, struct dyn_ftrace *rec);
564 void arch_ftrace_trampoline_free(struct ftrace_ops *ops);
565
566 struct ftrace_rec_iter;
567
568 struct ftrace_rec_iter *ftrace_rec_iter_start(void);
569 struct ftrace_rec_iter *ftrace_rec_iter_next(struct ftrace_rec_iter *iter);
570 struct dyn_ftrace *ftrace_rec_iter_record(struct ftrace_rec_iter *iter);
571
572 #define for_ftrace_rec_iter(iter) \
573 for (iter = ftrace_rec_iter_start(); \
574 iter; \
575 iter = ftrace_rec_iter_next(iter))
576
577
578 int ftrace_update_record(struct dyn_ftrace *rec, bool enable);
579 int ftrace_test_record(struct dyn_ftrace *rec, bool enable);
580 void ftrace_run_stop_machine(int command);
581 unsigned long ftrace_location(unsigned long ip);
582 unsigned long ftrace_location_range(unsigned long start, unsigned long end);
583 unsigned long ftrace_get_addr_new(struct dyn_ftrace *rec);
584 unsigned long ftrace_get_addr_curr(struct dyn_ftrace *rec);
585
586 extern ftrace_func_t ftrace_trace_function;
587
588 int ftrace_regex_open(struct ftrace_ops *ops, int flag,
589 struct inode *inode, struct file *file);
590 ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
591 size_t cnt, loff_t *ppos);
592 ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
593 size_t cnt, loff_t *ppos);
594 int ftrace_regex_release(struct inode *inode, struct file *file);
595
596 void __init
597 ftrace_set_early_filter(struct ftrace_ops *ops, char *buf, int enable);
598
599 /* defined in arch */
600 extern int ftrace_ip_converted(unsigned long ip);
601 extern int ftrace_dyn_arch_init(void);
602 extern void ftrace_replace_code(int enable);
603 extern int ftrace_update_ftrace_func(ftrace_func_t func);
604 extern void ftrace_caller(void);
605 extern void ftrace_regs_caller(void);
606 extern void ftrace_call(void);
607 extern void ftrace_regs_call(void);
608 extern void mcount_call(void);
609
610 void ftrace_modify_all_code(int command);
611
612 #ifndef FTRACE_ADDR
613 #define FTRACE_ADDR ((unsigned long)ftrace_caller)
614 #endif
615
616 #ifndef FTRACE_GRAPH_ADDR
617 #define FTRACE_GRAPH_ADDR ((unsigned long)ftrace_graph_caller)
618 #endif
619
620 #ifndef FTRACE_REGS_ADDR
621 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
622 # define FTRACE_REGS_ADDR ((unsigned long)ftrace_regs_caller)
623 #else
624 # define FTRACE_REGS_ADDR FTRACE_ADDR
625 #endif
626 #endif
627
628 /*
629 * If an arch would like functions that are only traced
630 * by the function graph tracer to jump directly to its own
631 * trampoline, then they can define FTRACE_GRAPH_TRAMP_ADDR
632 * to be that address to jump to.
633 */
634 #ifndef FTRACE_GRAPH_TRAMP_ADDR
635 #define FTRACE_GRAPH_TRAMP_ADDR ((unsigned long) 0)
636 #endif
637
638 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
639 extern void ftrace_graph_caller(void);
640 extern int ftrace_enable_ftrace_graph_caller(void);
641 extern int ftrace_disable_ftrace_graph_caller(void);
642 #else
ftrace_enable_ftrace_graph_caller(void)643 static inline int ftrace_enable_ftrace_graph_caller(void) { return 0; }
ftrace_disable_ftrace_graph_caller(void)644 static inline int ftrace_disable_ftrace_graph_caller(void) { return 0; }
645 #endif
646
647 /**
648 * ftrace_make_nop - convert code into nop
649 * @mod: module structure if called by module load initialization
650 * @rec: the call site record (e.g. mcount/fentry)
651 * @addr: the address that the call site should be calling
652 *
653 * This is a very sensitive operation and great care needs
654 * to be taken by the arch. The operation should carefully
655 * read the location, check to see if what is read is indeed
656 * what we expect it to be, and then on success of the compare,
657 * it should write to the location.
658 *
659 * The code segment at @rec->ip should be a caller to @addr
660 *
661 * Return must be:
662 * 0 on success
663 * -EFAULT on error reading the location
664 * -EINVAL on a failed compare of the contents
665 * -EPERM on error writing to the location
666 * Any other value will be considered a failure.
667 */
668 extern int ftrace_make_nop(struct module *mod,
669 struct dyn_ftrace *rec, unsigned long addr);
670
671 /**
672 * ftrace_need_init_nop - return whether nop call sites should be initialized
673 *
674 * Normally the compiler's -mnop-mcount generates suitable nops, so we don't
675 * need to call ftrace_init_nop() if the code is built with that flag.
676 * Architectures where this is not always the case may define their own
677 * condition.
678 *
679 * Return must be:
680 * 0 if ftrace_init_nop() should be called
681 * Nonzero if ftrace_init_nop() should not be called
682 */
683
684 #ifndef ftrace_need_init_nop
685 #define ftrace_need_init_nop() (!__is_defined(CC_USING_NOP_MCOUNT))
686 #endif
687
688 /**
689 * ftrace_init_nop - initialize a nop call site
690 * @mod: module structure if called by module load initialization
691 * @rec: the call site record (e.g. mcount/fentry)
692 *
693 * This is a very sensitive operation and great care needs
694 * to be taken by the arch. The operation should carefully
695 * read the location, check to see if what is read is indeed
696 * what we expect it to be, and then on success of the compare,
697 * it should write to the location.
698 *
699 * The code segment at @rec->ip should contain the contents created by
700 * the compiler
701 *
702 * Return must be:
703 * 0 on success
704 * -EFAULT on error reading the location
705 * -EINVAL on a failed compare of the contents
706 * -EPERM on error writing to the location
707 * Any other value will be considered a failure.
708 */
709 #ifndef ftrace_init_nop
ftrace_init_nop(struct module * mod,struct dyn_ftrace * rec)710 static inline int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
711 {
712 return ftrace_make_nop(mod, rec, MCOUNT_ADDR);
713 }
714 #endif
715
716 /**
717 * ftrace_make_call - convert a nop call site into a call to addr
718 * @rec: the call site record (e.g. mcount/fentry)
719 * @addr: the address that the call site should call
720 *
721 * This is a very sensitive operation and great care needs
722 * to be taken by the arch. The operation should carefully
723 * read the location, check to see if what is read is indeed
724 * what we expect it to be, and then on success of the compare,
725 * it should write to the location.
726 *
727 * The code segment at @rec->ip should be a nop
728 *
729 * Return must be:
730 * 0 on success
731 * -EFAULT on error reading the location
732 * -EINVAL on a failed compare of the contents
733 * -EPERM on error writing to the location
734 * Any other value will be considered a failure.
735 */
736 extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
737
738 #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
739 /**
740 * ftrace_modify_call - convert from one addr to another (no nop)
741 * @rec: the call site record (e.g. mcount/fentry)
742 * @old_addr: the address expected to be currently called to
743 * @addr: the address to change to
744 *
745 * This is a very sensitive operation and great care needs
746 * to be taken by the arch. The operation should carefully
747 * read the location, check to see if what is read is indeed
748 * what we expect it to be, and then on success of the compare,
749 * it should write to the location.
750 *
751 * The code segment at @rec->ip should be a caller to @old_addr
752 *
753 * Return must be:
754 * 0 on success
755 * -EFAULT on error reading the location
756 * -EINVAL on a failed compare of the contents
757 * -EPERM on error writing to the location
758 * Any other value will be considered a failure.
759 */
760 extern int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
761 unsigned long addr);
762 #else
763 /* Should never be called */
ftrace_modify_call(struct dyn_ftrace * rec,unsigned long old_addr,unsigned long addr)764 static inline int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
765 unsigned long addr)
766 {
767 return -EINVAL;
768 }
769 #endif
770
771 /* May be defined in arch */
772 extern int ftrace_arch_read_dyn_info(char *buf, int size);
773
774 extern int skip_trace(unsigned long ip);
775 extern void ftrace_module_init(struct module *mod);
776 extern void ftrace_module_enable(struct module *mod);
777 extern void ftrace_release_mod(struct module *mod);
778
779 extern void ftrace_disable_daemon(void);
780 extern void ftrace_enable_daemon(void);
781 #else /* CONFIG_DYNAMIC_FTRACE */
skip_trace(unsigned long ip)782 static inline int skip_trace(unsigned long ip) { return 0; }
ftrace_disable_daemon(void)783 static inline void ftrace_disable_daemon(void) { }
ftrace_enable_daemon(void)784 static inline void ftrace_enable_daemon(void) { }
ftrace_module_init(struct module * mod)785 static inline void ftrace_module_init(struct module *mod) { }
ftrace_module_enable(struct module * mod)786 static inline void ftrace_module_enable(struct module *mod) { }
ftrace_release_mod(struct module * mod)787 static inline void ftrace_release_mod(struct module *mod) { }
ftrace_text_reserved(const void * start,const void * end)788 static inline int ftrace_text_reserved(const void *start, const void *end)
789 {
790 return 0;
791 }
ftrace_location(unsigned long ip)792 static inline unsigned long ftrace_location(unsigned long ip)
793 {
794 return 0;
795 }
796
797 /*
798 * Again users of functions that have ftrace_ops may not
799 * have them defined when ftrace is not enabled, but these
800 * functions may still be called. Use a macro instead of inline.
801 */
802 #define ftrace_regex_open(ops, flag, inod, file) ({ -ENODEV; })
803 #define ftrace_set_early_filter(ops, buf, enable) do { } while (0)
804 #define ftrace_set_filter_ip(ops, ip, remove, reset) ({ -ENODEV; })
805 #define ftrace_set_filter(ops, buf, len, reset) ({ -ENODEV; })
806 #define ftrace_set_notrace(ops, buf, len, reset) ({ -ENODEV; })
807 #define ftrace_free_filter(ops) do { } while (0)
808 #define ftrace_ops_set_global_filter(ops) do { } while (0)
809
ftrace_filter_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)810 static inline ssize_t ftrace_filter_write(struct file *file, const char __user *ubuf,
811 size_t cnt, loff_t *ppos) { return -ENODEV; }
ftrace_notrace_write(struct file * file,const char __user * ubuf,size_t cnt,loff_t * ppos)812 static inline ssize_t ftrace_notrace_write(struct file *file, const char __user *ubuf,
813 size_t cnt, loff_t *ppos) { return -ENODEV; }
814 static inline int
ftrace_regex_release(struct inode * inode,struct file * file)815 ftrace_regex_release(struct inode *inode, struct file *file) { return -ENODEV; }
816
is_ftrace_trampoline(unsigned long addr)817 static inline bool is_ftrace_trampoline(unsigned long addr)
818 {
819 return false;
820 }
821 #endif /* CONFIG_DYNAMIC_FTRACE */
822
823 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
824 #ifndef ftrace_graph_func
825 #define ftrace_graph_func ftrace_stub
826 #define FTRACE_OPS_GRAPH_STUB FTRACE_OPS_FL_STUB
827 #else
828 #define FTRACE_OPS_GRAPH_STUB 0
829 #endif
830 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
831
832 /* totally disable ftrace - can not re-enable after this */
833 void ftrace_kill(void);
834
tracer_disable(void)835 static inline void tracer_disable(void)
836 {
837 #ifdef CONFIG_FUNCTION_TRACER
838 ftrace_enabled = 0;
839 #endif
840 }
841
842 /*
843 * Ftrace disable/restore without lock. Some synchronization mechanism
844 * must be used to prevent ftrace_enabled to be changed between
845 * disable/restore.
846 */
__ftrace_enabled_save(void)847 static inline int __ftrace_enabled_save(void)
848 {
849 #ifdef CONFIG_FUNCTION_TRACER
850 int saved_ftrace_enabled = ftrace_enabled;
851 ftrace_enabled = 0;
852 return saved_ftrace_enabled;
853 #else
854 return 0;
855 #endif
856 }
857
__ftrace_enabled_restore(int enabled)858 static inline void __ftrace_enabled_restore(int enabled)
859 {
860 #ifdef CONFIG_FUNCTION_TRACER
861 ftrace_enabled = enabled;
862 #endif
863 }
864
865 /* All archs should have this, but we define it for consistency */
866 #ifndef ftrace_return_address0
867 # define ftrace_return_address0 __builtin_return_address(0)
868 #endif
869
870 /* Archs may use other ways for ADDR1 and beyond */
871 #ifndef ftrace_return_address
872 # ifdef CONFIG_FRAME_POINTER
873 # define ftrace_return_address(n) __builtin_return_address(n)
874 # else
875 # define ftrace_return_address(n) 0UL
876 # endif
877 #endif
878
879 #define CALLER_ADDR0 ((unsigned long)ftrace_return_address0)
880 #define CALLER_ADDR1 ((unsigned long)ftrace_return_address(1))
881 #define CALLER_ADDR2 ((unsigned long)ftrace_return_address(2))
882 #define CALLER_ADDR3 ((unsigned long)ftrace_return_address(3))
883 #define CALLER_ADDR4 ((unsigned long)ftrace_return_address(4))
884 #define CALLER_ADDR5 ((unsigned long)ftrace_return_address(5))
885 #define CALLER_ADDR6 ((unsigned long)ftrace_return_address(6))
886
get_lock_parent_ip(void)887 static inline unsigned long get_lock_parent_ip(void)
888 {
889 unsigned long addr = CALLER_ADDR0;
890
891 if (!in_lock_functions(addr))
892 return addr;
893 addr = CALLER_ADDR1;
894 if (!in_lock_functions(addr))
895 return addr;
896 return CALLER_ADDR2;
897 }
898
899 #ifdef CONFIG_TRACE_PREEMPT_TOGGLE
900 extern void trace_preempt_on(unsigned long a0, unsigned long a1);
901 extern void trace_preempt_off(unsigned long a0, unsigned long a1);
902 #else
903 /*
904 * Use defines instead of static inlines because some arches will make code out
905 * of the CALLER_ADDR, when we really want these to be a real nop.
906 */
907 # define trace_preempt_on(a0, a1) do { } while (0)
908 # define trace_preempt_off(a0, a1) do { } while (0)
909 #endif
910
911 #ifdef CONFIG_FTRACE_MCOUNT_RECORD
912 extern void ftrace_init(void);
913 #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY
914 #define FTRACE_CALLSITE_SECTION "__patchable_function_entries"
915 #else
916 #define FTRACE_CALLSITE_SECTION "__mcount_loc"
917 #endif
918 #else
ftrace_init(void)919 static inline void ftrace_init(void) { }
920 #endif
921
922 /*
923 * Structure that defines an entry function trace.
924 * It's already packed but the attribute "packed" is needed
925 * to remove extra padding at the end.
926 */
927 struct ftrace_graph_ent {
928 unsigned long func; /* Current function */
929 int depth;
930 } __packed;
931
932 /*
933 * Structure that defines a return function trace.
934 * It's already packed but the attribute "packed" is needed
935 * to remove extra padding at the end.
936 */
937 struct ftrace_graph_ret {
938 unsigned long func; /* Current function */
939 int depth;
940 /* Number of functions that overran the depth limit for current task */
941 unsigned int overrun;
942 unsigned long long calltime;
943 unsigned long long rettime;
944 } __packed;
945
946 /* Type of the callback handlers for tracing function graph*/
947 typedef void (*trace_func_graph_ret_t)(struct ftrace_graph_ret *); /* return */
948 typedef int (*trace_func_graph_ent_t)(struct ftrace_graph_ent *); /* entry */
949
950 extern int ftrace_graph_entry_stub(struct ftrace_graph_ent *trace);
951
952 #ifdef CONFIG_FUNCTION_GRAPH_TRACER
953
954 struct fgraph_ops {
955 trace_func_graph_ent_t entryfunc;
956 trace_func_graph_ret_t retfunc;
957 };
958
959 /*
960 * Stack of return addresses for functions
961 * of a thread.
962 * Used in struct thread_info
963 */
964 struct ftrace_ret_stack {
965 unsigned long ret;
966 unsigned long func;
967 unsigned long long calltime;
968 #ifdef CONFIG_FUNCTION_PROFILER
969 unsigned long long subtime;
970 #endif
971 #ifdef HAVE_FUNCTION_GRAPH_FP_TEST
972 unsigned long fp;
973 #endif
974 #ifdef HAVE_FUNCTION_GRAPH_RET_ADDR_PTR
975 unsigned long *retp;
976 #endif
977 };
978
979 /*
980 * Primary handler of a function return.
981 * It relays on ftrace_return_to_handler.
982 * Defined in entry_32/64.S
983 */
984 extern void return_to_handler(void);
985
986 extern int
987 function_graph_enter(unsigned long ret, unsigned long func,
988 unsigned long frame_pointer, unsigned long *retp);
989
990 struct ftrace_ret_stack *
991 ftrace_graph_get_ret_stack(struct task_struct *task, int idx);
992
993 unsigned long ftrace_graph_ret_addr(struct task_struct *task, int *idx,
994 unsigned long ret, unsigned long *retp);
995
996 /*
997 * Sometimes we don't want to trace a function with the function
998 * graph tracer but we want them to keep traced by the usual function
999 * tracer if the function graph tracer is not configured.
1000 */
1001 #define __notrace_funcgraph notrace
1002
1003 #define FTRACE_RETFUNC_DEPTH 50
1004 #define FTRACE_RETSTACK_ALLOC_SIZE 32
1005
1006 extern int register_ftrace_graph(struct fgraph_ops *ops);
1007 extern void unregister_ftrace_graph(struct fgraph_ops *ops);
1008
1009 extern bool ftrace_graph_is_dead(void);
1010 extern void ftrace_graph_stop(void);
1011
1012 /* The current handlers in use */
1013 extern trace_func_graph_ret_t ftrace_graph_return;
1014 extern trace_func_graph_ent_t ftrace_graph_entry;
1015
1016 extern void ftrace_graph_init_task(struct task_struct *t);
1017 extern void ftrace_graph_exit_task(struct task_struct *t);
1018 extern void ftrace_graph_init_idle_task(struct task_struct *t, int cpu);
1019
pause_graph_tracing(void)1020 static inline void pause_graph_tracing(void)
1021 {
1022 atomic_inc(¤t->tracing_graph_pause);
1023 }
1024
unpause_graph_tracing(void)1025 static inline void unpause_graph_tracing(void)
1026 {
1027 atomic_dec(¤t->tracing_graph_pause);
1028 }
1029 #else /* !CONFIG_FUNCTION_GRAPH_TRACER */
1030
1031 #define __notrace_funcgraph
1032
ftrace_graph_init_task(struct task_struct * t)1033 static inline void ftrace_graph_init_task(struct task_struct *t) { }
ftrace_graph_exit_task(struct task_struct * t)1034 static inline void ftrace_graph_exit_task(struct task_struct *t) { }
ftrace_graph_init_idle_task(struct task_struct * t,int cpu)1035 static inline void ftrace_graph_init_idle_task(struct task_struct *t, int cpu) { }
1036
1037 /* Define as macros as fgraph_ops may not be defined */
1038 #define register_ftrace_graph(ops) ({ -1; })
1039 #define unregister_ftrace_graph(ops) do { } while (0)
1040
1041 static inline unsigned long
ftrace_graph_ret_addr(struct task_struct * task,int * idx,unsigned long ret,unsigned long * retp)1042 ftrace_graph_ret_addr(struct task_struct *task, int *idx, unsigned long ret,
1043 unsigned long *retp)
1044 {
1045 return ret;
1046 }
1047
pause_graph_tracing(void)1048 static inline void pause_graph_tracing(void) { }
unpause_graph_tracing(void)1049 static inline void unpause_graph_tracing(void) { }
1050 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */
1051
1052 #ifdef CONFIG_TRACING
1053
1054 /* flags for current->trace */
1055 enum {
1056 TSK_TRACE_FL_TRACE_BIT = 0,
1057 TSK_TRACE_FL_GRAPH_BIT = 1,
1058 };
1059 enum {
1060 TSK_TRACE_FL_TRACE = 1 << TSK_TRACE_FL_TRACE_BIT,
1061 TSK_TRACE_FL_GRAPH = 1 << TSK_TRACE_FL_GRAPH_BIT,
1062 };
1063
set_tsk_trace_trace(struct task_struct * tsk)1064 static inline void set_tsk_trace_trace(struct task_struct *tsk)
1065 {
1066 set_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1067 }
1068
clear_tsk_trace_trace(struct task_struct * tsk)1069 static inline void clear_tsk_trace_trace(struct task_struct *tsk)
1070 {
1071 clear_bit(TSK_TRACE_FL_TRACE_BIT, &tsk->trace);
1072 }
1073
test_tsk_trace_trace(struct task_struct * tsk)1074 static inline int test_tsk_trace_trace(struct task_struct *tsk)
1075 {
1076 return tsk->trace & TSK_TRACE_FL_TRACE;
1077 }
1078
set_tsk_trace_graph(struct task_struct * tsk)1079 static inline void set_tsk_trace_graph(struct task_struct *tsk)
1080 {
1081 set_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1082 }
1083
clear_tsk_trace_graph(struct task_struct * tsk)1084 static inline void clear_tsk_trace_graph(struct task_struct *tsk)
1085 {
1086 clear_bit(TSK_TRACE_FL_GRAPH_BIT, &tsk->trace);
1087 }
1088
test_tsk_trace_graph(struct task_struct * tsk)1089 static inline int test_tsk_trace_graph(struct task_struct *tsk)
1090 {
1091 return tsk->trace & TSK_TRACE_FL_GRAPH;
1092 }
1093
1094 enum ftrace_dump_mode;
1095
1096 extern enum ftrace_dump_mode ftrace_dump_on_oops;
1097 extern int tracepoint_printk;
1098
1099 extern void disable_trace_on_warning(void);
1100 extern int __disable_trace_on_warning;
1101
1102 int tracepoint_printk_sysctl(struct ctl_table *table, int write,
1103 void *buffer, size_t *lenp, loff_t *ppos);
1104
1105 #else /* CONFIG_TRACING */
disable_trace_on_warning(void)1106 static inline void disable_trace_on_warning(void) { }
1107 #endif /* CONFIG_TRACING */
1108
1109 #ifdef CONFIG_FTRACE_SYSCALLS
1110
1111 unsigned long arch_syscall_addr(int nr);
1112
1113 #endif /* CONFIG_FTRACE_SYSCALLS */
1114
1115 #endif /* _LINUX_FTRACE_H */
1116