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