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