1 // SPDX-License-Identifier: GPL-2.0 2 3 #ifndef _LINUX_KERNEL_TRACE_H 4 #define _LINUX_KERNEL_TRACE_H 5 6 #include <linux/fs.h> 7 #include <linux/atomic.h> 8 #include <linux/sched.h> 9 #include <linux/clocksource.h> 10 #include <linux/ring_buffer.h> 11 #include <linux/mmiotrace.h> 12 #include <linux/tracepoint.h> 13 #include <linux/ftrace.h> 14 #include <linux/trace.h> 15 #include <linux/hw_breakpoint.h> 16 #include <linux/trace_seq.h> 17 #include <linux/trace_events.h> 18 #include <linux/compiler.h> 19 #include <linux/glob.h> 20 #include <linux/irq_work.h> 21 #include <linux/workqueue.h> 22 #include <linux/ctype.h> 23 #include <linux/once_lite.h> 24 #include <linux/ftrace_regs.h> 25 #include <linux/llist.h> 26 27 #include "pid_list.h" 28 29 #ifdef CONFIG_FTRACE_SYSCALLS 30 #include <asm/unistd.h> /* For NR_syscalls */ 31 #include <asm/syscall.h> /* some archs define it here */ 32 #endif 33 34 #define TRACE_MODE_WRITE 0640 35 #define TRACE_MODE_READ 0440 36 37 enum trace_type { 38 __TRACE_FIRST_TYPE = 0, 39 40 TRACE_FN, 41 TRACE_CTX, 42 TRACE_WAKE, 43 TRACE_STACK, 44 TRACE_PRINT, 45 TRACE_BPRINT, 46 TRACE_MMIO_RW, 47 TRACE_MMIO_MAP, 48 TRACE_BRANCH, 49 TRACE_GRAPH_RET, 50 TRACE_GRAPH_ENT, 51 TRACE_GRAPH_RETADDR_ENT, 52 TRACE_USER_STACK, 53 TRACE_BLK, 54 TRACE_BPUTS, 55 TRACE_HWLAT, 56 TRACE_OSNOISE, 57 TRACE_TIMERLAT, 58 TRACE_RAW_DATA, 59 TRACE_FUNC_REPEATS, 60 61 __TRACE_LAST_TYPE, 62 }; 63 64 65 #undef __field 66 #define __field(type, item) type item; 67 68 #undef __field_fn 69 #define __field_fn(type, item) type item; 70 71 #undef __field_packed 72 #define __field_packed(type, item) type item; 73 74 #undef __field_struct 75 #define __field_struct(type, item) __field(type, item) 76 77 #undef __field_desc 78 #define __field_desc(type, container, item) 79 80 #undef __field_desc_packed 81 #define __field_desc_packed(type, container, item) 82 83 #undef __array 84 #define __array(type, item, size) type item[size]; 85 86 /* 87 * For backward compatibility, older user space expects to see the 88 * kernel_stack event with a fixed size caller field. But today the fix 89 * size is ignored by the kernel, and the real structure is dynamic. 90 * Expose to user space: "unsigned long caller[8];" but the real structure 91 * will be "unsigned long caller[] __counted_by(size)" 92 */ 93 #undef __stack_array 94 #define __stack_array(type, item, size, field) type item[] __counted_by(field); 95 96 #undef __array_desc 97 #define __array_desc(type, container, item, size) 98 99 #undef __dynamic_array 100 #define __dynamic_array(type, item) type item[]; 101 102 #undef __rel_dynamic_array 103 #define __rel_dynamic_array(type, item) type item[]; 104 105 #undef F_STRUCT 106 #define F_STRUCT(args...) args 107 108 #undef FTRACE_ENTRY 109 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print) \ 110 struct struct_name { \ 111 struct trace_entry ent; \ 112 tstruct \ 113 } 114 115 #undef FTRACE_ENTRY_DUP 116 #define FTRACE_ENTRY_DUP(name, name_struct, id, tstruct, printk) 117 118 #undef FTRACE_ENTRY_REG 119 #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print, regfn) \ 120 FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print)) 121 122 #undef FTRACE_ENTRY_PACKED 123 #define FTRACE_ENTRY_PACKED(name, struct_name, id, tstruct, print) \ 124 FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print)) __packed 125 126 #include "trace_entries.h" 127 128 /* Use this for memory failure errors */ 129 #define MEM_FAIL(condition, fmt, ...) \ 130 DO_ONCE_LITE_IF(condition, pr_err, "ERROR: " fmt, ##__VA_ARGS__) 131 132 #define FAULT_STRING "(fault)" 133 134 #define HIST_STACKTRACE_DEPTH 31 135 #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long)) 136 #define HIST_STACKTRACE_SKIP 5 137 138 #define SYSCALL_FAULT_USER_MAX 165 139 140 /* 141 * syscalls are special, and need special handling, this is why 142 * they are not included in trace_entries.h 143 */ 144 struct syscall_trace_enter { 145 struct trace_entry ent; 146 int nr; 147 unsigned long args[]; 148 }; 149 150 struct syscall_trace_exit { 151 struct trace_entry ent; 152 int nr; 153 long ret; 154 }; 155 156 struct kprobe_trace_entry_head { 157 struct trace_entry ent; 158 unsigned long ip; 159 }; 160 161 struct eprobe_trace_entry_head { 162 struct trace_entry ent; 163 }; 164 165 struct kretprobe_trace_entry_head { 166 struct trace_entry ent; 167 unsigned long func; 168 unsigned long ret_ip; 169 }; 170 171 struct fentry_trace_entry_head { 172 struct trace_entry ent; 173 unsigned long ip; 174 }; 175 176 struct fexit_trace_entry_head { 177 struct trace_entry ent; 178 unsigned long func; 179 unsigned long ret_ip; 180 }; 181 182 #define TRACE_BUF_SIZE 1024 183 184 struct trace_array; 185 186 /* 187 * The CPU trace array - it consists of thousands of trace entries 188 * plus some other descriptor data: (for example which task started 189 * the trace, etc.) 190 */ 191 struct trace_array_cpu { 192 local_t disabled; 193 194 unsigned long entries; 195 unsigned long saved_latency; 196 unsigned long critical_start; 197 unsigned long critical_end; 198 unsigned long critical_sequence; 199 unsigned long nice; 200 unsigned long policy; 201 unsigned long rt_priority; 202 unsigned long skipped_entries; 203 u64 preempt_timestamp; 204 pid_t pid; 205 kuid_t uid; 206 char comm[TASK_COMM_LEN]; 207 208 #ifdef CONFIG_FUNCTION_TRACER 209 int ftrace_ignore_pid; 210 #endif 211 bool ignore_pid; 212 }; 213 214 struct tracer; 215 struct trace_option_dentry; 216 217 struct array_buffer { 218 struct trace_array *tr; 219 struct trace_buffer *buffer; 220 struct trace_array_cpu __percpu *data; 221 u64 time_start; 222 int cpu; 223 }; 224 225 #define TRACE_FLAGS_MAX_SIZE 64 226 227 struct trace_options { 228 struct tracer *tracer; 229 struct trace_option_dentry *topts; 230 }; 231 232 struct trace_pid_list *trace_pid_list_alloc(void); 233 void trace_pid_list_free(struct trace_pid_list *pid_list); 234 bool trace_pid_list_is_set(struct trace_pid_list *pid_list, unsigned int pid); 235 int trace_pid_list_set(struct trace_pid_list *pid_list, unsigned int pid); 236 int trace_pid_list_clear(struct trace_pid_list *pid_list, unsigned int pid); 237 int trace_pid_list_first(struct trace_pid_list *pid_list, unsigned int *pid); 238 int trace_pid_list_next(struct trace_pid_list *pid_list, unsigned int pid, 239 unsigned int *next); 240 241 enum { 242 TRACE_PIDS = BIT(0), 243 TRACE_NO_PIDS = BIT(1), 244 }; 245 246 static inline bool pid_type_enabled(int type, struct trace_pid_list *pid_list, 247 struct trace_pid_list *no_pid_list) 248 { 249 /* Return true if the pid list in type has pids */ 250 return ((type & TRACE_PIDS) && pid_list) || 251 ((type & TRACE_NO_PIDS) && no_pid_list); 252 } 253 254 static inline bool still_need_pid_events(int type, struct trace_pid_list *pid_list, 255 struct trace_pid_list *no_pid_list) 256 { 257 /* 258 * Turning off what is in @type, return true if the "other" 259 * pid list, still has pids in it. 260 */ 261 return (!(type & TRACE_PIDS) && pid_list) || 262 (!(type & TRACE_NO_PIDS) && no_pid_list); 263 } 264 265 typedef bool (*cond_update_fn_t)(struct trace_array *tr, void *cond_data); 266 267 /** 268 * struct cond_snapshot - conditional snapshot data and callback 269 * 270 * The cond_snapshot structure encapsulates a callback function and 271 * data associated with the snapshot for a given tracing instance. 272 * 273 * When a snapshot is taken conditionally, by invoking 274 * tracing_snapshot_cond(tr, cond_data), the cond_data passed in is 275 * passed in turn to the cond_snapshot.update() function. That data 276 * can be compared by the update() implementation with the cond_data 277 * contained within the struct cond_snapshot instance associated with 278 * the trace_array. Because the tr->max_lock is held throughout the 279 * update() call, the update() function can directly retrieve the 280 * cond_snapshot and cond_data associated with the per-instance 281 * snapshot associated with the trace_array. 282 * 283 * The cond_snapshot.update() implementation can save data to be 284 * associated with the snapshot if it decides to, and returns 'true' 285 * in that case, or it returns 'false' if the conditional snapshot 286 * shouldn't be taken. 287 * 288 * The cond_snapshot instance is created and associated with the 289 * user-defined cond_data by tracing_cond_snapshot_enable(). 290 * Likewise, the cond_snapshot instance is destroyed and is no longer 291 * associated with the trace instance by 292 * tracing_cond_snapshot_disable(). 293 * 294 * The method below is required. 295 * 296 * @update: When a conditional snapshot is invoked, the update() 297 * callback function is invoked with the tr->max_lock held. The 298 * update() implementation signals whether or not to actually 299 * take the snapshot, by returning 'true' if so, 'false' if no 300 * snapshot should be taken. Because the max_lock is held for 301 * the duration of update(), the implementation is safe to 302 * directly retrieved and save any implementation data it needs 303 * to in association with the snapshot. 304 */ 305 struct cond_snapshot { 306 void *cond_data; 307 cond_update_fn_t update; 308 }; 309 310 /* 311 * struct trace_func_repeats - used to keep track of the consecutive 312 * (on the same CPU) calls of a single function. 313 */ 314 struct trace_func_repeats { 315 unsigned long ip; 316 unsigned long parent_ip; 317 unsigned long count; 318 u64 ts_last_call; 319 }; 320 321 struct trace_module_delta { 322 struct rcu_head rcu; 323 long delta[]; 324 }; 325 326 /* 327 * The trace array - an array of per-CPU trace arrays. This is the 328 * highest level data structure that individual tracers deal with. 329 * They have on/off state as well: 330 */ 331 struct trace_array { 332 struct list_head list; 333 char *name; 334 struct array_buffer array_buffer; 335 #ifdef CONFIG_TRACER_SNAPSHOT 336 /* 337 * The snapshot_buffer is used to snapshot the trace when a maximum 338 * latency is reached, or when the user initiates a snapshot. 339 * Some tracers will use this to store a maximum trace while 340 * it continues examining live traces. 341 * 342 * The buffers for the snapshot_buffer are set up the same as the 343 * array_buffer. When a snapshot is taken, the buffer of the 344 * snapshot_buffer is swapped with the buffer of the array_buffer 345 * and the buffers are reset for the array_buffer so the tracing can 346 * continue. 347 */ 348 struct array_buffer snapshot_buffer; 349 bool allocated_snapshot; 350 spinlock_t snapshot_trigger_lock; 351 unsigned int snapshot; 352 #ifdef CONFIG_TRACER_MAX_TRACE 353 unsigned long max_latency; 354 struct dentry *d_max_latency; 355 #ifdef CONFIG_FSNOTIFY 356 struct work_struct fsnotify_work; 357 struct irq_work fsnotify_irqwork; 358 #endif /* CONFIG_FSNOTIFY */ 359 #endif /* CONFIG_TRACER_MAX_TRACE */ 360 #endif /* CONFIG_TRACER_SNAPSHOT */ 361 362 /* The below is for memory mapped ring buffer */ 363 unsigned int mapped; 364 unsigned long range_addr_start; 365 unsigned long range_addr_size; 366 char *range_name; 367 long text_delta; 368 struct trace_module_delta *module_delta; 369 void *scratch; /* pointer in persistent memory */ 370 int scratch_size; 371 372 int buffer_disabled; 373 374 struct trace_pid_list __rcu *filtered_pids; 375 struct trace_pid_list __rcu *filtered_no_pids; 376 /* 377 * max_lock is used to protect the swapping of buffers 378 * when taking a max snapshot. The buffers themselves are 379 * protected by per_cpu spinlocks. But the action of the swap 380 * needs its own lock. 381 * 382 * This is defined as a arch_spinlock_t in order to help 383 * with performance when lockdep debugging is enabled. 384 * 385 * It is also used in other places outside the update_max_tr 386 * so it needs to be defined outside of the 387 * CONFIG_TRACER_SNAPSHOT. 388 */ 389 arch_spinlock_t max_lock; 390 #ifdef CONFIG_FTRACE_SYSCALLS 391 int sys_refcount_enter; 392 int sys_refcount_exit; 393 struct trace_event_file *enter_syscall_files[NR_syscalls]; 394 struct trace_event_file *exit_syscall_files[NR_syscalls]; 395 #endif 396 int stop_count; 397 int clock_id; 398 int nr_topts; 399 bool clear_trace; 400 int buffer_percent; 401 unsigned int n_err_log_entries; 402 struct tracer *current_trace; 403 struct tracer_flags *current_trace_flags; 404 u64 trace_flags; 405 unsigned char trace_flags_index[TRACE_FLAGS_MAX_SIZE]; 406 unsigned int flags; 407 raw_spinlock_t start_lock; 408 union { 409 const char *system_names; 410 char *boot_events; 411 }; 412 struct list_head err_log; 413 struct dentry *dir; 414 struct dentry *options; 415 struct dentry *percpu_dir; 416 struct eventfs_inode *event_dir; 417 struct trace_options *topts; 418 struct list_head systems; 419 struct list_head events; 420 struct list_head marker_list; 421 struct list_head tracers; 422 struct trace_event_file *trace_marker_file; 423 cpumask_var_t tracing_cpumask; /* only trace on set CPUs */ 424 /* one per_cpu trace_pipe can be opened by only one user */ 425 cpumask_var_t pipe_cpumask; 426 int ref; 427 int trace_ref; 428 #ifdef CONFIG_MODULES 429 struct list_head mod_events; 430 #endif 431 #ifdef CONFIG_FUNCTION_TRACER 432 struct ftrace_ops *ops; 433 struct trace_pid_list __rcu *function_pids; 434 struct trace_pid_list __rcu *function_no_pids; 435 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 436 struct fgraph_ops *gops; 437 #endif 438 #ifdef CONFIG_DYNAMIC_FTRACE 439 /* All of these are protected by the ftrace_lock */ 440 struct list_head func_probes; 441 struct list_head mod_trace; 442 struct list_head mod_notrace; 443 #endif 444 /* function tracing enabled */ 445 int function_enabled; 446 #endif 447 int no_filter_buffering_ref; 448 unsigned int syscall_buf_sz; 449 struct list_head hist_vars; 450 #ifdef CONFIG_TRACER_SNAPSHOT 451 struct cond_snapshot *cond_snapshot; 452 #endif 453 struct trace_func_repeats __percpu *last_func_repeats; 454 /* 455 * On boot up, the ring buffer is set to the minimum size, so that 456 * we do not waste memory on systems that are not using tracing. 457 */ 458 bool ring_buffer_expanded; 459 /* 460 * If the ring buffer is a read only backup instance, it will be 461 * removed after dumping all data via pipe, because no readable data. 462 */ 463 bool free_on_close; 464 struct work_struct autoremove_work; 465 }; 466 467 enum { 468 TRACE_ARRAY_FL_GLOBAL = BIT(0), 469 TRACE_ARRAY_FL_BOOT = BIT(1), 470 TRACE_ARRAY_FL_LAST_BOOT = BIT(2), 471 TRACE_ARRAY_FL_MOD_INIT = BIT(3), 472 TRACE_ARRAY_FL_MEMMAP = BIT(4), 473 TRACE_ARRAY_FL_VMALLOC = BIT(5), 474 TRACE_ARRAY_FL_RDONLY = BIT(6), 475 }; 476 477 #ifdef CONFIG_MODULES 478 bool module_exists(const char *module); 479 #else 480 static inline bool module_exists(const char *module) 481 { 482 return false; 483 } 484 #endif 485 486 extern struct list_head ftrace_trace_arrays; 487 488 extern struct mutex trace_types_lock; 489 490 extern int trace_array_get(struct trace_array *tr); 491 extern int tracing_check_open_get_tr(struct trace_array *tr); 492 extern struct trace_array *trace_array_find(const char *instance); 493 extern struct trace_array *trace_array_find_get(const char *instance); 494 495 extern u64 tracing_event_time_stamp(struct trace_buffer *buffer, struct ring_buffer_event *rbe); 496 extern int tracing_set_clock(struct trace_array *tr, const char *clockstr); 497 498 extern bool trace_clock_in_ns(struct trace_array *tr); 499 500 extern unsigned long trace_adjust_address(struct trace_array *tr, unsigned long addr); 501 502 extern struct trace_array *printk_trace; 503 504 static inline bool trace_array_is_readonly(struct trace_array *tr) 505 { 506 /* backup instance is read only. */ 507 return tr->flags & TRACE_ARRAY_FL_RDONLY; 508 } 509 510 /* 511 * The global tracer (top) should be the first trace array added, 512 * but we check the flag anyway. 513 */ 514 static inline struct trace_array *top_trace_array(void) 515 { 516 struct trace_array *tr; 517 518 if (list_empty(&ftrace_trace_arrays)) 519 return NULL; 520 521 tr = list_entry(ftrace_trace_arrays.prev, 522 typeof(*tr), list); 523 WARN_ON(!(tr->flags & TRACE_ARRAY_FL_GLOBAL)); 524 return tr; 525 } 526 527 #define FTRACE_CMP_TYPE(var, type) \ 528 __builtin_types_compatible_p(typeof(var), type *) 529 530 #undef IF_ASSIGN 531 #define IF_ASSIGN(var, entry, etype, id) \ 532 if (FTRACE_CMP_TYPE(var, etype)) { \ 533 var = (typeof(var))(entry); \ 534 WARN_ON(id != 0 && (entry)->type != id); \ 535 break; \ 536 } 537 538 /* Will cause compile errors if type is not found. */ 539 extern void __ftrace_bad_type(void); 540 541 /* 542 * The trace_assign_type is a verifier that the entry type is 543 * the same as the type being assigned. To add new types simply 544 * add a line with the following format: 545 * 546 * IF_ASSIGN(var, ent, type, id); 547 * 548 * Where "type" is the trace type that includes the trace_entry 549 * as the "ent" item. And "id" is the trace identifier that is 550 * used in the trace_type enum. 551 * 552 * If the type can have more than one id, then use zero. 553 */ 554 #define trace_assign_type(var, ent) \ 555 do { \ 556 IF_ASSIGN(var, ent, struct ftrace_entry, TRACE_FN); \ 557 IF_ASSIGN(var, ent, struct ctx_switch_entry, 0); \ 558 IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \ 559 IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\ 560 IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \ 561 IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \ 562 IF_ASSIGN(var, ent, struct bputs_entry, TRACE_BPUTS); \ 563 IF_ASSIGN(var, ent, struct hwlat_entry, TRACE_HWLAT); \ 564 IF_ASSIGN(var, ent, struct osnoise_entry, TRACE_OSNOISE);\ 565 IF_ASSIGN(var, ent, struct timerlat_entry, TRACE_TIMERLAT);\ 566 IF_ASSIGN(var, ent, struct raw_data_entry, TRACE_RAW_DATA);\ 567 IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \ 568 TRACE_MMIO_RW); \ 569 IF_ASSIGN(var, ent, struct trace_mmiotrace_map, \ 570 TRACE_MMIO_MAP); \ 571 IF_ASSIGN(var, ent, struct trace_branch, TRACE_BRANCH); \ 572 IF_ASSIGN(var, ent, struct ftrace_graph_ent_entry, \ 573 TRACE_GRAPH_ENT); \ 574 IF_ASSIGN(var, ent, struct fgraph_retaddr_ent_entry,\ 575 TRACE_GRAPH_RETADDR_ENT); \ 576 IF_ASSIGN(var, ent, struct ftrace_graph_ret_entry, \ 577 TRACE_GRAPH_RET); \ 578 IF_ASSIGN(var, ent, struct func_repeats_entry, \ 579 TRACE_FUNC_REPEATS); \ 580 __ftrace_bad_type(); \ 581 } while (0) 582 583 /* 584 * An option specific to a tracer. This is a boolean value. 585 * The bit is the bit index that sets its value on the 586 * flags value in struct tracer_flags. 587 */ 588 struct tracer_opt { 589 const char *name; /* Will appear on the trace_options file */ 590 u32 bit; /* Mask assigned in val field in tracer_flags */ 591 }; 592 593 /* 594 * The set of specific options for a tracer. Your tracer 595 * have to set the initial value of the flags val. 596 */ 597 struct tracer_flags { 598 u32 val; 599 struct tracer_opt *opts; 600 struct tracer *trace; 601 }; 602 603 /* Makes more easy to define a tracer opt */ 604 #define TRACER_OPT(s, b) .name = #s, .bit = b 605 606 607 struct trace_option_dentry { 608 struct tracer_opt *opt; 609 struct tracer_flags *flags; 610 struct trace_array *tr; 611 struct dentry *entry; 612 }; 613 614 /** 615 * struct tracer - a specific tracer and its callbacks to interact with tracefs 616 * @name: the name chosen to select it on the available_tracers file 617 * @init: called when one switches to this tracer (echo name > current_tracer) 618 * @reset: called when one switches to another tracer 619 * @start: called when tracing is unpaused (echo 1 > tracing_on) 620 * @stop: called when tracing is paused (echo 0 > tracing_on) 621 * @update_thresh: called when tracing_thresh is updated 622 * @open: called when the trace file is opened 623 * @pipe_open: called when the trace_pipe file is opened 624 * @close: called when the trace file is released 625 * @pipe_close: called when the trace_pipe file is released 626 * @read: override the default read callback on trace_pipe 627 * @splice_read: override the default splice_read callback on trace_pipe 628 * @selftest: selftest to run on boot (see trace_selftest.c) 629 * @print_headers: override the first lines that describe your columns 630 * @print_line: callback that prints a trace 631 * @set_flag: signals one of your private flags changed (trace_options file) 632 * @flags: your private flags 633 */ 634 struct tracer { 635 const char *name; 636 int (*init)(struct trace_array *tr); 637 void (*reset)(struct trace_array *tr); 638 void (*start)(struct trace_array *tr); 639 void (*stop)(struct trace_array *tr); 640 int (*update_thresh)(struct trace_array *tr); 641 void (*open)(struct trace_iterator *iter); 642 void (*pipe_open)(struct trace_iterator *iter); 643 void (*close)(struct trace_iterator *iter); 644 void (*pipe_close)(struct trace_iterator *iter); 645 ssize_t (*read)(struct trace_iterator *iter, 646 struct file *filp, char __user *ubuf, 647 size_t cnt, loff_t *ppos); 648 ssize_t (*splice_read)(struct trace_iterator *iter, 649 struct file *filp, 650 loff_t *ppos, 651 struct pipe_inode_info *pipe, 652 size_t len, 653 unsigned int flags); 654 #ifdef CONFIG_FTRACE_STARTUP_TEST 655 int (*selftest)(struct tracer *trace, 656 struct trace_array *tr); 657 #endif 658 void (*print_header)(struct seq_file *m); 659 enum print_line_t (*print_line)(struct trace_iterator *iter); 660 /* If you handled the flag setting, return 0 */ 661 int (*set_flag)(struct trace_array *tr, 662 u32 old_flags, u32 bit, int set); 663 /* Return 0 if OK with change, else return non-zero */ 664 int (*flag_changed)(struct trace_array *tr, 665 u64 mask, int set); 666 struct tracer *next; 667 struct tracer_flags *flags; 668 struct tracer_flags *default_flags; 669 int enabled; 670 bool print_max; 671 bool allow_instances; 672 #ifdef CONFIG_TRACER_MAX_TRACE 673 bool use_max_tr; 674 #endif 675 /* True if tracer cannot be enabled in kernel param */ 676 bool noboot; 677 }; 678 679 static inline struct ring_buffer_iter * 680 trace_buffer_iter(struct trace_iterator *iter, int cpu) 681 { 682 return iter->buffer_iter ? iter->buffer_iter[cpu] : NULL; 683 } 684 685 extern int tracing_disabled; 686 687 int tracer_init(struct tracer *t, struct trace_array *tr); 688 int tracing_is_enabled(void); 689 void tracing_reset_online_cpus(struct array_buffer *buf); 690 void tracing_reset_all_online_cpus(void); 691 void tracing_reset_all_online_cpus_unlocked(void); 692 int tracing_open_generic(struct inode *inode, struct file *filp); 693 int tracing_open_generic_tr(struct inode *inode, struct file *filp); 694 int tracing_release_generic_tr(struct inode *inode, struct file *file); 695 int tracing_open_file_tr(struct inode *inode, struct file *filp); 696 int tracing_release_file_tr(struct inode *inode, struct file *filp); 697 int tracing_single_release_file_tr(struct inode *inode, struct file *filp); 698 bool tracer_tracing_is_on(struct trace_array *tr); 699 void tracer_tracing_on(struct trace_array *tr); 700 void tracer_tracing_off(struct trace_array *tr); 701 void tracer_tracing_disable(struct trace_array *tr); 702 void tracer_tracing_enable(struct trace_array *tr); 703 struct dentry *trace_create_file(const char *name, 704 umode_t mode, 705 struct dentry *parent, 706 void *data, 707 const struct file_operations *fops); 708 struct dentry *trace_create_cpu_file(const char *name, 709 umode_t mode, 710 struct dentry *parent, 711 void *data, 712 long cpu, 713 const struct file_operations *fops); 714 int tracing_get_cpu(struct inode *inode); 715 716 717 /** 718 * tracer_tracing_is_on_cpu - show real state of ring buffer enabled on for a cpu 719 * @tr : the trace array to know if ring buffer is enabled 720 * @cpu: The cpu buffer to check if enabled 721 * 722 * Shows real state of the per CPU buffer if it is enabled or not. 723 */ 724 static inline bool tracer_tracing_is_on_cpu(struct trace_array *tr, int cpu) 725 { 726 if (tr->array_buffer.buffer) 727 return ring_buffer_record_is_on_cpu(tr->array_buffer.buffer, cpu); 728 return false; 729 } 730 731 int tracing_init_dentry(void); 732 733 struct ring_buffer_event; 734 735 struct ring_buffer_event * 736 trace_buffer_lock_reserve(struct trace_buffer *buffer, 737 int type, 738 unsigned long len, 739 unsigned int trace_ctx); 740 741 int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, int cpu); 742 743 struct trace_entry *tracing_get_trace_entry(struct trace_array *tr, 744 struct trace_array_cpu *data); 745 746 struct trace_entry *trace_find_next_entry(struct trace_iterator *iter, 747 int *ent_cpu, u64 *ent_ts); 748 749 void trace_buffer_unlock_commit_nostack(struct trace_buffer *buffer, 750 struct ring_buffer_event *event); 751 752 bool trace_is_tracepoint_string(const char *str); 753 const char *trace_event_format(struct trace_iterator *iter, const char *fmt); 754 char *trace_iter_expand_format(struct trace_iterator *iter); 755 bool ignore_event(struct trace_iterator *iter); 756 757 int trace_empty(struct trace_iterator *iter); 758 759 void *trace_find_next_entry_inc(struct trace_iterator *iter); 760 761 void trace_init_global_iter(struct trace_iterator *iter); 762 763 void tracing_iter_reset(struct trace_iterator *iter, int cpu); 764 765 unsigned long trace_total_entries_cpu(struct trace_array *tr, int cpu); 766 unsigned long trace_total_entries(struct trace_array *tr); 767 768 void trace_function(struct trace_array *tr, 769 unsigned long ip, 770 unsigned long parent_ip, 771 unsigned int trace_ctx, 772 struct ftrace_regs *regs); 773 void trace_graph_function(struct trace_array *tr, 774 unsigned long ip, 775 unsigned long parent_ip, 776 unsigned int trace_ctx); 777 void trace_latency_header(struct seq_file *m); 778 void trace_default_header(struct seq_file *m); 779 void print_trace_header(struct seq_file *m, struct trace_iterator *iter); 780 781 void trace_graph_return(struct ftrace_graph_ret *trace, struct fgraph_ops *gops, 782 struct ftrace_regs *fregs); 783 int trace_graph_entry(struct ftrace_graph_ent *trace, struct fgraph_ops *gops, 784 struct ftrace_regs *fregs); 785 786 void tracing_start_cmdline_record(void); 787 void tracing_stop_cmdline_record(void); 788 void tracing_start_tgid_record(void); 789 void tracing_stop_tgid_record(void); 790 791 int register_tracer(struct tracer *type); 792 int is_tracing_stopped(void); 793 794 loff_t tracing_lseek(struct file *file, loff_t offset, int whence); 795 796 extern cpumask_var_t __read_mostly tracing_buffer_mask; 797 798 #define for_each_tracing_cpu(cpu) \ 799 for_each_cpu(cpu, tracing_buffer_mask) 800 801 extern unsigned long nsecs_to_usecs(unsigned long nsecs); 802 803 extern unsigned long tracing_thresh; 804 extern struct workqueue_struct *trace_init_wq __initdata; 805 806 /* PID filtering */ 807 808 bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids, 809 pid_t search_pid); 810 bool trace_ignore_this_task(struct trace_pid_list *filtered_pids, 811 struct trace_pid_list *filtered_no_pids, 812 struct task_struct *task); 813 void trace_filter_add_remove_task(struct trace_pid_list *pid_list, 814 struct task_struct *self, 815 struct task_struct *task); 816 void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos); 817 void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos); 818 int trace_pid_show(struct seq_file *m, void *v); 819 int trace_pid_write(struct trace_pid_list *filtered_pids, 820 struct trace_pid_list **new_pid_list, 821 const char __user *ubuf, size_t cnt); 822 823 #ifdef CONFIG_TRACER_SNAPSHOT 824 void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu, 825 void *cond_data); 826 void update_max_tr_single(struct trace_array *tr, 827 struct task_struct *tsk, int cpu); 828 829 #if defined(CONFIG_TRACER_MAX_TRACE) && defined(CONFIG_FSNOTIFY) 830 # define LATENCY_FS_NOTIFY 831 #endif 832 833 #ifdef LATENCY_FS_NOTIFY 834 void latency_fsnotify(struct trace_array *tr); 835 #else 836 static inline void latency_fsnotify(struct trace_array *tr) { } 837 #endif 838 #endif /* CONFIG_TRACER_SNAPSHOT */ 839 840 #ifdef CONFIG_STACKTRACE 841 void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, int skip); 842 #else 843 static inline void __trace_stack(struct trace_array *tr, unsigned int trace_ctx, 844 int skip) 845 { 846 } 847 #endif /* CONFIG_STACKTRACE */ 848 849 #ifdef CONFIG_TRACER_MAX_TRACE 850 static inline bool tracer_uses_snapshot(struct tracer *tracer) 851 { 852 return tracer->use_max_tr; 853 } 854 #else 855 static inline bool tracer_uses_snapshot(struct tracer *tracer) 856 { 857 return false; 858 } 859 #endif 860 861 void trace_last_func_repeats(struct trace_array *tr, 862 struct trace_func_repeats *last_info, 863 unsigned int trace_ctx); 864 865 extern u64 ftrace_now(int cpu); 866 867 extern void trace_find_cmdline(int pid, char comm[]); 868 extern int trace_find_tgid(int pid); 869 extern void trace_event_follow_fork(struct trace_array *tr, bool enable); 870 871 extern int trace_events_enabled(struct trace_array *tr, const char *system); 872 873 #ifdef CONFIG_DYNAMIC_FTRACE 874 extern unsigned long ftrace_update_tot_cnt; 875 extern unsigned long ftrace_number_of_pages; 876 extern unsigned long ftrace_number_of_groups; 877 extern u64 ftrace_update_time; 878 extern u64 ftrace_total_mod_time; 879 void ftrace_init_trace_array(struct trace_array *tr); 880 #else 881 static inline void ftrace_init_trace_array(struct trace_array *tr) { } 882 #endif 883 #define DYN_FTRACE_TEST_NAME trace_selftest_dynamic_test_func 884 extern int DYN_FTRACE_TEST_NAME(void); 885 #define DYN_FTRACE_TEST_NAME2 trace_selftest_dynamic_test_func2 886 extern int DYN_FTRACE_TEST_NAME2(void); 887 888 extern void trace_set_ring_buffer_expanded(struct trace_array *tr); 889 extern bool tracing_selftest_disabled; 890 891 #ifdef CONFIG_FTRACE_STARTUP_TEST 892 extern void __init disable_tracing_selftest(const char *reason); 893 894 extern int trace_selftest_startup_function(struct tracer *trace, 895 struct trace_array *tr); 896 extern int trace_selftest_startup_function_graph(struct tracer *trace, 897 struct trace_array *tr); 898 extern int trace_selftest_startup_irqsoff(struct tracer *trace, 899 struct trace_array *tr); 900 extern int trace_selftest_startup_preemptoff(struct tracer *trace, 901 struct trace_array *tr); 902 extern int trace_selftest_startup_preemptirqsoff(struct tracer *trace, 903 struct trace_array *tr); 904 extern int trace_selftest_startup_wakeup(struct tracer *trace, 905 struct trace_array *tr); 906 extern int trace_selftest_startup_nop(struct tracer *trace, 907 struct trace_array *tr); 908 extern int trace_selftest_startup_branch(struct tracer *trace, 909 struct trace_array *tr); 910 extern bool __read_mostly tracing_selftest_running; 911 /* 912 * Tracer data references selftest functions that only occur 913 * on boot up. These can be __init functions. Thus, when selftests 914 * are enabled, then the tracers need to reference __init functions. 915 */ 916 #define __tracer_data __refdata 917 #else 918 static inline void __init disable_tracing_selftest(const char *reason) 919 { 920 } 921 /* Tracers are seldom changed. Optimize when selftests are disabled. */ 922 #define __tracer_data __read_mostly 923 #define tracing_selftest_running 0 924 #endif /* CONFIG_FTRACE_STARTUP_TEST */ 925 926 extern void *head_page(struct trace_array_cpu *data); 927 extern unsigned long long ns2usecs(u64 nsec); 928 929 __printf(2, 0) 930 int trace_vbprintk(unsigned long ip, const char *fmt, va_list args); 931 __printf(2, 0) 932 int trace_vprintk(unsigned long ip, const char *fmt, va_list args); 933 __printf(3, 0) 934 int trace_array_vprintk(struct trace_array *tr, 935 unsigned long ip, const char *fmt, va_list args); 936 __printf(3, 4) 937 int trace_array_printk_buf(struct trace_buffer *buffer, 938 unsigned long ip, const char *fmt, ...); 939 void trace_printk_seq(struct trace_seq *s); 940 enum print_line_t print_trace_line(struct trace_iterator *iter); 941 942 extern char trace_find_mark(unsigned long long duration); 943 944 struct ftrace_hash; 945 946 struct ftrace_mod_load { 947 struct list_head list; 948 char *func; 949 char *module; 950 int enable; 951 }; 952 953 enum { 954 FTRACE_HASH_FL_MOD = (1 << 0), 955 }; 956 957 struct ftrace_hash { 958 unsigned long size_bits; 959 struct hlist_head *buckets; 960 unsigned long count; 961 unsigned long flags; 962 struct rcu_head rcu; 963 }; 964 965 struct ftrace_func_entry * 966 ftrace_lookup_ip(struct ftrace_hash *hash, unsigned long ip); 967 968 static __always_inline bool ftrace_hash_empty(struct ftrace_hash *hash) 969 { 970 return !hash || !(hash->count || (hash->flags & FTRACE_HASH_FL_MOD)); 971 } 972 973 /* Standard output formatting function used for function return traces */ 974 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 975 976 /* Flag options */ 977 #define TRACE_GRAPH_PRINT_OVERRUN 0x1 978 #define TRACE_GRAPH_PRINT_CPU 0x2 979 #define TRACE_GRAPH_PRINT_OVERHEAD 0x4 980 #define TRACE_GRAPH_PRINT_PROC 0x8 981 #define TRACE_GRAPH_PRINT_DURATION 0x10 982 #define TRACE_GRAPH_PRINT_ABS_TIME 0x20 983 #define TRACE_GRAPH_PRINT_REL_TIME 0x40 984 #define TRACE_GRAPH_PRINT_IRQS 0x80 985 #define TRACE_GRAPH_PRINT_TAIL 0x100 986 #define TRACE_GRAPH_SLEEP_TIME 0x200 987 #define TRACE_GRAPH_GRAPH_TIME 0x400 988 #define TRACE_GRAPH_PRINT_RETVAL 0x800 989 #define TRACE_GRAPH_PRINT_RETVAL_HEX 0x1000 990 #define TRACE_GRAPH_PRINT_RETADDR 0x2000 991 #define TRACE_GRAPH_ARGS 0x4000 992 #define TRACE_GRAPH_PRINT_FILL_SHIFT 28 993 #define TRACE_GRAPH_PRINT_FILL_MASK (0x3 << TRACE_GRAPH_PRINT_FILL_SHIFT) 994 995 #ifdef CONFIG_FUNCTION_PROFILER 996 extern void ftrace_graph_graph_time_control(bool enable); 997 #else 998 static inline void ftrace_graph_graph_time_control(bool enable) { } 999 #endif 1000 1001 extern enum print_line_t 1002 print_graph_function_flags(struct trace_iterator *iter, u32 flags); 1003 extern void print_graph_headers_flags(struct seq_file *s, u32 flags); 1004 extern void 1005 trace_print_graph_duration(unsigned long long duration, struct trace_seq *s); 1006 extern void graph_trace_open(struct trace_iterator *iter); 1007 extern void graph_trace_close(struct trace_iterator *iter); 1008 extern int __trace_graph_entry(struct trace_array *tr, 1009 struct ftrace_graph_ent *trace, 1010 unsigned int trace_ctx); 1011 extern int __trace_graph_retaddr_entry(struct trace_array *tr, 1012 struct ftrace_graph_ent *trace, 1013 unsigned int trace_ctx, 1014 unsigned long retaddr, 1015 struct ftrace_regs *fregs); 1016 extern void __trace_graph_return(struct trace_array *tr, 1017 struct ftrace_graph_ret *trace, 1018 unsigned int trace_ctx, 1019 u64 calltime, u64 rettime); 1020 1021 extern void init_array_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops); 1022 extern int allocate_fgraph_ops(struct trace_array *tr, struct ftrace_ops *ops); 1023 extern void free_fgraph_ops(struct trace_array *tr); 1024 1025 enum { 1026 TRACE_GRAPH_FL = 1, 1027 1028 /* 1029 * In the very unlikely case that an interrupt came in 1030 * at a start of graph tracing, and we want to trace 1031 * the function in that interrupt, the depth can be greater 1032 * than zero, because of the preempted start of a previous 1033 * trace. In an even more unlikely case, depth could be 2 1034 * if a softirq interrupted the start of graph tracing, 1035 * followed by an interrupt preempting a start of graph 1036 * tracing in the softirq, and depth can even be 3 1037 * if an NMI came in at the start of an interrupt function 1038 * that preempted a softirq start of a function that 1039 * preempted normal context!!!! Luckily, it can't be 1040 * greater than 3, so the next two bits are a mask 1041 * of what the depth is when we set TRACE_GRAPH_FL 1042 */ 1043 1044 TRACE_GRAPH_DEPTH_START_BIT, 1045 TRACE_GRAPH_DEPTH_END_BIT, 1046 1047 /* 1048 * To implement set_graph_notrace, if this bit is set, we ignore 1049 * function graph tracing of called functions, until the return 1050 * function is called to clear it. 1051 */ 1052 TRACE_GRAPH_NOTRACE_BIT, 1053 }; 1054 1055 #define TRACE_GRAPH_NOTRACE (1 << TRACE_GRAPH_NOTRACE_BIT) 1056 1057 static inline unsigned long ftrace_graph_depth(unsigned long *task_var) 1058 { 1059 return (*task_var >> TRACE_GRAPH_DEPTH_START_BIT) & 3; 1060 } 1061 1062 static inline void ftrace_graph_set_depth(unsigned long *task_var, int depth) 1063 { 1064 *task_var &= ~(3 << TRACE_GRAPH_DEPTH_START_BIT); 1065 *task_var |= (depth & 3) << TRACE_GRAPH_DEPTH_START_BIT; 1066 } 1067 1068 #ifdef CONFIG_DYNAMIC_FTRACE 1069 extern struct ftrace_hash __rcu *ftrace_graph_hash; 1070 extern struct ftrace_hash __rcu *ftrace_graph_notrace_hash; 1071 1072 static inline int 1073 ftrace_graph_addr(unsigned long *task_var, struct ftrace_graph_ent *trace) 1074 { 1075 unsigned long addr = trace->func; 1076 int ret = 0; 1077 struct ftrace_hash *hash; 1078 1079 preempt_disable_notrace(); 1080 1081 /* 1082 * Have to open code "rcu_dereference_sched()" because the 1083 * function graph tracer can be called when RCU is not 1084 * "watching". 1085 * Protected with schedule_on_each_cpu(ftrace_sync) 1086 */ 1087 hash = rcu_dereference_protected(ftrace_graph_hash, !preemptible()); 1088 1089 if (ftrace_hash_empty(hash)) { 1090 ret = 1; 1091 goto out; 1092 } 1093 1094 if (ftrace_lookup_ip(hash, addr)) { 1095 /* 1096 * This needs to be cleared on the return functions 1097 * when the depth is zero. 1098 */ 1099 *task_var |= TRACE_GRAPH_FL; 1100 ftrace_graph_set_depth(task_var, trace->depth); 1101 1102 /* 1103 * If no irqs are to be traced, but a set_graph_function 1104 * is set, and called by an interrupt handler, we still 1105 * want to trace it. 1106 */ 1107 if (in_hardirq()) 1108 trace_recursion_set(TRACE_IRQ_BIT); 1109 else 1110 trace_recursion_clear(TRACE_IRQ_BIT); 1111 ret = 1; 1112 } 1113 1114 out: 1115 preempt_enable_notrace(); 1116 return ret; 1117 } 1118 1119 static inline void 1120 ftrace_graph_addr_finish(struct fgraph_ops *gops, struct ftrace_graph_ret *trace) 1121 { 1122 unsigned long *task_var = fgraph_get_task_var(gops); 1123 1124 if ((*task_var & TRACE_GRAPH_FL) && 1125 trace->depth == ftrace_graph_depth(task_var)) 1126 *task_var &= ~TRACE_GRAPH_FL; 1127 } 1128 1129 static inline int ftrace_graph_notrace_addr(unsigned long addr) 1130 { 1131 int ret = 0; 1132 struct ftrace_hash *notrace_hash; 1133 1134 preempt_disable_notrace(); 1135 1136 /* 1137 * Have to open code "rcu_dereference_sched()" because the 1138 * function graph tracer can be called when RCU is not 1139 * "watching". 1140 * Protected with schedule_on_each_cpu(ftrace_sync) 1141 */ 1142 notrace_hash = rcu_dereference_protected(ftrace_graph_notrace_hash, 1143 !preemptible()); 1144 1145 if (ftrace_lookup_ip(notrace_hash, addr)) 1146 ret = 1; 1147 1148 preempt_enable_notrace(); 1149 return ret; 1150 } 1151 #else 1152 static inline int ftrace_graph_addr(unsigned long *task_var, struct ftrace_graph_ent *trace) 1153 { 1154 return 1; 1155 } 1156 1157 static inline int ftrace_graph_notrace_addr(unsigned long addr) 1158 { 1159 return 0; 1160 } 1161 static inline void ftrace_graph_addr_finish(struct fgraph_ops *gops, struct ftrace_graph_ret *trace) 1162 { } 1163 #endif /* CONFIG_DYNAMIC_FTRACE */ 1164 1165 extern unsigned int fgraph_max_depth; 1166 extern int fgraph_no_sleep_time; 1167 extern bool fprofile_no_sleep_time; 1168 1169 static inline bool 1170 ftrace_graph_ignore_func(struct fgraph_ops *gops, struct ftrace_graph_ent *trace) 1171 { 1172 unsigned long *task_var = fgraph_get_task_var(gops); 1173 1174 /* trace it when it is-nested-in or is a function enabled. */ 1175 return !((*task_var & TRACE_GRAPH_FL) || 1176 ftrace_graph_addr(task_var, trace)) || 1177 (trace->depth < 0) || 1178 (fgraph_max_depth && trace->depth >= fgraph_max_depth); 1179 } 1180 1181 void fgraph_init_ops(struct ftrace_ops *dst_ops, 1182 struct ftrace_ops *src_ops); 1183 1184 #else /* CONFIG_FUNCTION_GRAPH_TRACER */ 1185 static inline enum print_line_t 1186 print_graph_function_flags(struct trace_iterator *iter, u32 flags) 1187 { 1188 return TRACE_TYPE_UNHANDLED; 1189 } 1190 static inline void free_fgraph_ops(struct trace_array *tr) { } 1191 /* ftrace_ops may not be defined */ 1192 #define init_array_fgraph_ops(tr, ops) do { } while (0) 1193 #define allocate_fgraph_ops(tr, ops) ({ 0; }) 1194 #endif /* CONFIG_FUNCTION_GRAPH_TRACER */ 1195 1196 extern struct list_head ftrace_pids; 1197 1198 #ifdef CONFIG_FUNCTION_TRACER 1199 1200 #define FTRACE_PID_IGNORE -1 1201 #define FTRACE_PID_TRACE -2 1202 1203 struct ftrace_func_command { 1204 struct list_head list; 1205 char *name; 1206 int (*func)(struct trace_array *tr, 1207 struct ftrace_hash *hash, 1208 char *func, char *cmd, 1209 char *params, int enable); 1210 }; 1211 extern bool ftrace_filter_param __initdata; 1212 extern int ftrace_is_dead(void); 1213 int ftrace_create_function_files(struct trace_array *tr, 1214 struct dentry *parent); 1215 void ftrace_destroy_function_files(struct trace_array *tr); 1216 int ftrace_allocate_ftrace_ops(struct trace_array *tr); 1217 void ftrace_free_ftrace_ops(struct trace_array *tr); 1218 void ftrace_init_global_array_ops(struct trace_array *tr); 1219 struct trace_array *trace_get_global_array(void); 1220 void ftrace_init_array_ops(struct trace_array *tr, ftrace_func_t func); 1221 void ftrace_reset_array_ops(struct trace_array *tr); 1222 void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d_tracer); 1223 void ftrace_init_tracefs_toplevel(struct trace_array *tr, 1224 struct dentry *d_tracer); 1225 void ftrace_clear_pids(struct trace_array *tr); 1226 int init_function_trace(void); 1227 void ftrace_pid_follow_fork(struct trace_array *tr, bool enable); 1228 #else 1229 static inline int ftrace_is_dead(void) { return 0; } 1230 static inline int 1231 ftrace_create_function_files(struct trace_array *tr, 1232 struct dentry *parent) 1233 { 1234 return 0; 1235 } 1236 static inline int ftrace_allocate_ftrace_ops(struct trace_array *tr) 1237 { 1238 return 0; 1239 } 1240 static inline void ftrace_free_ftrace_ops(struct trace_array *tr) { } 1241 static inline void ftrace_destroy_function_files(struct trace_array *tr) { } 1242 static inline __init void 1243 ftrace_init_global_array_ops(struct trace_array *tr) { } 1244 static inline void ftrace_reset_array_ops(struct trace_array *tr) { } 1245 static inline void ftrace_init_tracefs(struct trace_array *tr, struct dentry *d) { } 1246 static inline void ftrace_init_tracefs_toplevel(struct trace_array *tr, struct dentry *d) { } 1247 static inline void ftrace_clear_pids(struct trace_array *tr) { } 1248 static inline int init_function_trace(void) { return 0; } 1249 static inline void ftrace_pid_follow_fork(struct trace_array *tr, bool enable) { } 1250 /* ftace_func_t type is not defined, use macro instead of static inline */ 1251 #define ftrace_init_array_ops(tr, func) do { } while (0) 1252 #endif /* CONFIG_FUNCTION_TRACER */ 1253 1254 #if defined(CONFIG_FUNCTION_TRACER) && defined(CONFIG_DYNAMIC_FTRACE) 1255 1256 struct ftrace_probe_ops { 1257 void (*func)(unsigned long ip, 1258 unsigned long parent_ip, 1259 struct trace_array *tr, 1260 struct ftrace_probe_ops *ops, 1261 void *data); 1262 int (*init)(struct ftrace_probe_ops *ops, 1263 struct trace_array *tr, 1264 unsigned long ip, void *init_data, 1265 void **data); 1266 void (*free)(struct ftrace_probe_ops *ops, 1267 struct trace_array *tr, 1268 unsigned long ip, void *data); 1269 int (*print)(struct seq_file *m, 1270 unsigned long ip, 1271 struct ftrace_probe_ops *ops, 1272 void *data); 1273 }; 1274 1275 struct ftrace_func_mapper; 1276 typedef int (*ftrace_mapper_func)(void *data); 1277 1278 struct ftrace_func_mapper *allocate_ftrace_func_mapper(void); 1279 void **ftrace_func_mapper_find_ip(struct ftrace_func_mapper *mapper, 1280 unsigned long ip); 1281 int ftrace_func_mapper_add_ip(struct ftrace_func_mapper *mapper, 1282 unsigned long ip, void *data); 1283 void *ftrace_func_mapper_remove_ip(struct ftrace_func_mapper *mapper, 1284 unsigned long ip); 1285 void free_ftrace_func_mapper(struct ftrace_func_mapper *mapper, 1286 ftrace_mapper_func free_func); 1287 1288 extern int 1289 register_ftrace_function_probe(char *glob, struct trace_array *tr, 1290 struct ftrace_probe_ops *ops, void *data); 1291 extern int 1292 unregister_ftrace_function_probe_func(char *glob, struct trace_array *tr, 1293 struct ftrace_probe_ops *ops); 1294 extern void clear_ftrace_function_probes(struct trace_array *tr); 1295 1296 int register_ftrace_command(struct ftrace_func_command *cmd); 1297 int unregister_ftrace_command(struct ftrace_func_command *cmd); 1298 1299 void ftrace_create_filter_files(struct ftrace_ops *ops, 1300 struct dentry *parent); 1301 void ftrace_destroy_filter_files(struct ftrace_ops *ops); 1302 1303 extern int ftrace_set_filter(struct ftrace_ops *ops, unsigned char *buf, 1304 int len, int reset); 1305 extern int ftrace_set_notrace(struct ftrace_ops *ops, unsigned char *buf, 1306 int len, int reset); 1307 #else 1308 struct ftrace_func_command; 1309 1310 static inline __init int register_ftrace_command(struct ftrace_func_command *cmd) 1311 { 1312 return -EINVAL; 1313 } 1314 static inline __init int unregister_ftrace_command(char *cmd_name) 1315 { 1316 return -EINVAL; 1317 } 1318 static inline void clear_ftrace_function_probes(struct trace_array *tr) 1319 { 1320 } 1321 1322 /* 1323 * The ops parameter passed in is usually undefined. 1324 * This must be a macro. 1325 */ 1326 #define ftrace_create_filter_files(ops, parent) do { } while (0) 1327 #define ftrace_destroy_filter_files(ops) do { } while (0) 1328 #endif /* CONFIG_FUNCTION_TRACER && CONFIG_DYNAMIC_FTRACE */ 1329 1330 bool ftrace_event_is_function(struct trace_event_call *call); 1331 1332 /* 1333 * struct trace_parser - servers for reading the user input separated by spaces 1334 * @cont: set if the input is not complete - no final space char was found 1335 * @buffer: holds the parsed user input 1336 * @idx: user input length 1337 * @size: buffer size 1338 */ 1339 struct trace_parser { 1340 bool cont; 1341 bool fail; 1342 char *buffer; 1343 unsigned idx; 1344 unsigned size; 1345 }; 1346 1347 static inline bool trace_parser_loaded(struct trace_parser *parser) 1348 { 1349 return !parser->fail && parser->idx != 0; 1350 } 1351 1352 static inline bool trace_parser_cont(struct trace_parser *parser) 1353 { 1354 return parser->cont; 1355 } 1356 1357 static inline void trace_parser_clear(struct trace_parser *parser) 1358 { 1359 parser->cont = false; 1360 parser->idx = 0; 1361 } 1362 1363 static inline void trace_parser_fail(struct trace_parser *parser) 1364 { 1365 parser->fail = true; 1366 } 1367 1368 extern int trace_parser_get_init(struct trace_parser *parser, int size); 1369 extern void trace_parser_put(struct trace_parser *parser); 1370 extern int trace_get_user(struct trace_parser *parser, const char __user *ubuf, 1371 size_t cnt, loff_t *ppos); 1372 1373 /* 1374 * Only create function graph options if function graph is configured. 1375 */ 1376 #ifdef CONFIG_FUNCTION_GRAPH_TRACER 1377 # define FGRAPH_FLAGS \ 1378 C(DISPLAY_GRAPH, "display-graph"), 1379 #else 1380 # define FGRAPH_FLAGS 1381 #endif 1382 1383 #ifdef CONFIG_BRANCH_TRACER 1384 # define BRANCH_FLAGS \ 1385 C(BRANCH, "branch"), 1386 #else 1387 # define BRANCH_FLAGS 1388 #endif 1389 1390 #ifdef CONFIG_FUNCTION_TRACER 1391 # define FUNCTION_FLAGS \ 1392 C(FUNCTION, "function-trace"), \ 1393 C(FUNC_FORK, "function-fork"), 1394 # define FUNCTION_DEFAULT_FLAGS TRACE_ITER(FUNCTION) 1395 #else 1396 # define FUNCTION_FLAGS 1397 # define FUNCTION_DEFAULT_FLAGS 0UL 1398 # define TRACE_ITER_FUNC_FORK_BIT -1 1399 #endif 1400 1401 #ifdef CONFIG_STACKTRACE 1402 # define STACK_FLAGS \ 1403 C(STACKTRACE, "stacktrace"), 1404 #else 1405 # define STACK_FLAGS 1406 #endif 1407 1408 #ifdef CONFIG_FUNCTION_PROFILER 1409 # define PROFILER_FLAGS \ 1410 C(PROF_TEXT_OFFSET, "prof-text-offset"), 1411 # ifdef CONFIG_FUNCTION_GRAPH_TRACER 1412 # define FPROFILE_FLAGS \ 1413 C(GRAPH_TIME, "graph-time"), 1414 # define FPROFILE_DEFAULT_FLAGS TRACE_ITER(GRAPH_TIME) 1415 # else 1416 # define FPROFILE_FLAGS 1417 # define FPROFILE_DEFAULT_FLAGS 0UL 1418 # endif 1419 #else 1420 # define PROFILER_FLAGS 1421 # define FPROFILE_FLAGS 1422 # define FPROFILE_DEFAULT_FLAGS 0UL 1423 # define TRACE_ITER_PROF_TEXT_OFFSET_BIT -1 1424 #endif 1425 1426 /* 1427 * trace_iterator_flags is an enumeration that defines bit 1428 * positions into trace_flags that controls the output. 1429 * 1430 * NOTE: These bits must match the trace_options array in 1431 * trace.c (this macro guarantees it). 1432 */ 1433 #define TRACE_FLAGS \ 1434 C(PRINT_PARENT, "print-parent"), \ 1435 C(SYM_OFFSET, "sym-offset"), \ 1436 C(SYM_ADDR, "sym-addr"), \ 1437 C(VERBOSE, "verbose"), \ 1438 C(RAW, "raw"), \ 1439 C(HEX, "hex"), \ 1440 C(BIN, "bin"), \ 1441 C(BLOCK, "block"), \ 1442 C(FIELDS, "fields"), \ 1443 C(PRINTK, "trace_printk"), \ 1444 C(ANNOTATE, "annotate"), \ 1445 C(USERSTACKTRACE, "userstacktrace"), \ 1446 C(SYM_USEROBJ, "sym-userobj"), \ 1447 C(PRINTK_MSGONLY, "printk-msg-only"), \ 1448 C(CONTEXT_INFO, "context-info"), /* Print pid/cpu/time */ \ 1449 C(LATENCY_FMT, "latency-format"), \ 1450 C(RECORD_CMD, "record-cmd"), \ 1451 C(RECORD_TGID, "record-tgid"), \ 1452 C(OVERWRITE, "overwrite"), \ 1453 C(STOP_ON_FREE, "disable_on_free"), \ 1454 C(IRQ_INFO, "irq-info"), \ 1455 C(MARKERS, "markers"), \ 1456 C(EVENT_FORK, "event-fork"), \ 1457 C(TRACE_PRINTK, "trace_printk_dest"), \ 1458 C(COPY_MARKER, "copy_trace_marker"), \ 1459 C(PAUSE_ON_TRACE, "pause-on-trace"), \ 1460 C(HASH_PTR, "hash-ptr"), /* Print hashed pointer */ \ 1461 C(BITMASK_LIST, "bitmask-list"), \ 1462 FUNCTION_FLAGS \ 1463 FGRAPH_FLAGS \ 1464 STACK_FLAGS \ 1465 BRANCH_FLAGS \ 1466 PROFILER_FLAGS \ 1467 FPROFILE_FLAGS 1468 1469 /* 1470 * By defining C, we can make TRACE_FLAGS a list of bit names 1471 * that will define the bits for the flag masks. 1472 */ 1473 #undef C 1474 #define C(a, b) TRACE_ITER_##a##_BIT 1475 1476 enum trace_iterator_bits { 1477 TRACE_FLAGS 1478 /* Make sure we don't go more than we have bits for */ 1479 TRACE_ITER_LAST_BIT 1480 }; 1481 1482 /* 1483 * And use TRACE_ITER(flag) to define the bit masks. 1484 */ 1485 #define TRACE_ITER(flag) \ 1486 (TRACE_ITER_##flag##_BIT < 0 ? 0 : 1ULL << (TRACE_ITER_##flag##_BIT)) 1487 1488 /* 1489 * TRACE_ITER_SYM_MASK masks the options in trace_flags that 1490 * control the output of kernel symbols. 1491 */ 1492 #define TRACE_ITER_SYM_MASK \ 1493 (TRACE_ITER(PRINT_PARENT)|TRACE_ITER(SYM_OFFSET)|TRACE_ITER(SYM_ADDR)) 1494 1495 extern struct tracer nop_trace; 1496 1497 #ifdef CONFIG_BRANCH_TRACER 1498 extern int enable_branch_tracing(struct trace_array *tr); 1499 extern void disable_branch_tracing(void); 1500 static inline int trace_branch_enable(struct trace_array *tr) 1501 { 1502 if (tr->trace_flags & TRACE_ITER(BRANCH)) 1503 return enable_branch_tracing(tr); 1504 return 0; 1505 } 1506 static inline void trace_branch_disable(void) 1507 { 1508 /* due to races, always disable */ 1509 disable_branch_tracing(); 1510 } 1511 #else 1512 static inline int trace_branch_enable(struct trace_array *tr) 1513 { 1514 return 0; 1515 } 1516 static inline void trace_branch_disable(void) 1517 { 1518 } 1519 #endif /* CONFIG_BRANCH_TRACER */ 1520 1521 /* set ring buffers to default size if not already done so */ 1522 int tracing_update_buffers(struct trace_array *tr); 1523 1524 union trace_synth_field { 1525 u8 as_u8; 1526 u16 as_u16; 1527 u32 as_u32; 1528 u64 as_u64; 1529 struct trace_dynamic_info as_dynamic; 1530 }; 1531 1532 struct ftrace_event_field { 1533 struct list_head link; 1534 const char *name; 1535 const char *type; 1536 int filter_type; 1537 int offset; 1538 int size; 1539 unsigned int is_signed:1; 1540 unsigned int needs_test:1; 1541 int len; 1542 }; 1543 1544 struct prog_entry; 1545 1546 struct event_filter { 1547 struct prog_entry __rcu *prog; 1548 char *filter_string; 1549 }; 1550 1551 struct event_subsystem { 1552 struct list_head list; 1553 const char *name; 1554 struct event_filter *filter; 1555 int ref_count; 1556 }; 1557 1558 struct trace_subsystem_dir { 1559 struct list_head list; 1560 struct event_subsystem *subsystem; 1561 struct trace_array *tr; 1562 struct eventfs_inode *ei; 1563 int ref_count; 1564 int nr_events; 1565 }; 1566 1567 void trace_buffer_unlock_commit_regs(struct trace_array *tr, 1568 struct trace_buffer *buffer, 1569 struct ring_buffer_event *event, 1570 unsigned int trcace_ctx, 1571 struct pt_regs *regs); 1572 1573 static inline void trace_buffer_unlock_commit(struct trace_array *tr, 1574 struct trace_buffer *buffer, 1575 struct ring_buffer_event *event, 1576 unsigned int trace_ctx) 1577 { 1578 trace_buffer_unlock_commit_regs(tr, buffer, event, trace_ctx, NULL); 1579 } 1580 1581 DECLARE_PER_CPU(bool, trace_taskinfo_save); 1582 int trace_save_cmdline(struct task_struct *tsk); 1583 int trace_create_savedcmd(void); 1584 int trace_alloc_tgid_map(void); 1585 void trace_free_saved_cmdlines_buffer(void); 1586 1587 extern const struct file_operations tracing_saved_cmdlines_fops; 1588 extern const struct file_operations tracing_saved_tgids_fops; 1589 extern const struct file_operations tracing_saved_cmdlines_size_fops; 1590 1591 DECLARE_PER_CPU(struct ring_buffer_event *, trace_buffered_event); 1592 DECLARE_PER_CPU(int, trace_buffered_event_cnt); 1593 void trace_buffered_event_disable(void); 1594 void trace_buffered_event_enable(void); 1595 1596 void early_enable_events(struct trace_array *tr, char *buf, bool disable_first); 1597 1598 struct trace_user_buf; 1599 struct trace_user_buf_info { 1600 struct trace_user_buf __percpu *tbuf; 1601 size_t size; 1602 int ref; 1603 }; 1604 1605 typedef int (*trace_user_buf_copy)(char *dst, const char __user *src, 1606 size_t size, void *data); 1607 int trace_user_fault_init(struct trace_user_buf_info *tinfo, size_t size); 1608 int trace_user_fault_get(struct trace_user_buf_info *tinfo); 1609 int trace_user_fault_put(struct trace_user_buf_info *tinfo); 1610 void trace_user_fault_destroy(struct trace_user_buf_info *tinfo); 1611 char *trace_user_fault_read(struct trace_user_buf_info *tinfo, 1612 const char __user *ptr, size_t size, 1613 trace_user_buf_copy copy_func, void *data); 1614 1615 static __always_inline void 1616 trace_event_setup(struct ring_buffer_event *event, 1617 int type, unsigned int trace_ctx) 1618 { 1619 struct trace_entry *ent = ring_buffer_event_data(event); 1620 1621 tracing_generic_entry_update(ent, type, trace_ctx); 1622 } 1623 1624 static __always_inline struct ring_buffer_event * 1625 __trace_buffer_lock_reserve(struct trace_buffer *buffer, 1626 int type, 1627 unsigned long len, 1628 unsigned int trace_ctx) 1629 { 1630 struct ring_buffer_event *event; 1631 1632 event = ring_buffer_lock_reserve(buffer, len); 1633 if (event != NULL) 1634 trace_event_setup(event, type, trace_ctx); 1635 1636 return event; 1637 } 1638 1639 static __always_inline void 1640 __buffer_unlock_commit(struct trace_buffer *buffer, struct ring_buffer_event *event) 1641 { 1642 __this_cpu_write(trace_taskinfo_save, true); 1643 1644 /* If this is the temp buffer, we need to commit fully */ 1645 if (this_cpu_read(trace_buffered_event) == event) { 1646 /* Length is in event->array[0] */ 1647 ring_buffer_write(buffer, event->array[0], &event->array[1]); 1648 /* Release the temp buffer */ 1649 this_cpu_dec(trace_buffered_event_cnt); 1650 /* ring_buffer_unlock_commit() enables preemption */ 1651 preempt_enable_notrace(); 1652 } else 1653 ring_buffer_unlock_commit(buffer); 1654 } 1655 1656 static inline void 1657 __trace_event_discard_commit(struct trace_buffer *buffer, 1658 struct ring_buffer_event *event) 1659 { 1660 if (this_cpu_read(trace_buffered_event) == event) { 1661 /* Simply release the temp buffer and enable preemption */ 1662 this_cpu_dec(trace_buffered_event_cnt); 1663 preempt_enable_notrace(); 1664 return; 1665 } 1666 /* ring_buffer_discard_commit() enables preemption */ 1667 ring_buffer_discard_commit(buffer, event); 1668 } 1669 1670 /* 1671 * Helper function for event_trigger_unlock_commit{_regs}(). 1672 * If there are event triggers attached to this event that requires 1673 * filtering against its fields, then they will be called as the 1674 * entry already holds the field information of the current event. 1675 * 1676 * It also checks if the event should be discarded or not. 1677 * It is to be discarded if the event is soft disabled and the 1678 * event was only recorded to process triggers, or if the event 1679 * filter is active and this event did not match the filters. 1680 * 1681 * Returns true if the event is discarded, false otherwise. 1682 */ 1683 static inline bool 1684 __event_trigger_test_discard(struct trace_event_file *file, 1685 struct trace_buffer *buffer, 1686 struct ring_buffer_event *event, 1687 void *entry, 1688 enum event_trigger_type *tt) 1689 { 1690 unsigned long eflags = file->flags; 1691 1692 if (eflags & EVENT_FILE_FL_TRIGGER_COND) 1693 *tt = event_triggers_call(file, buffer, entry, event); 1694 1695 if (likely(!(file->flags & (EVENT_FILE_FL_SOFT_DISABLED | 1696 EVENT_FILE_FL_FILTERED | 1697 EVENT_FILE_FL_PID_FILTER)))) 1698 return false; 1699 1700 if (file->flags & EVENT_FILE_FL_SOFT_DISABLED) 1701 goto discard; 1702 1703 if (file->flags & EVENT_FILE_FL_FILTERED && 1704 !filter_match_preds(file->filter, entry)) 1705 goto discard; 1706 1707 if ((file->flags & EVENT_FILE_FL_PID_FILTER) && 1708 trace_event_ignore_this_pid(file)) 1709 goto discard; 1710 1711 return false; 1712 discard: 1713 __trace_event_discard_commit(buffer, event); 1714 return true; 1715 } 1716 1717 /** 1718 * event_trigger_unlock_commit - handle triggers and finish event commit 1719 * @file: The file pointer associated with the event 1720 * @buffer: The ring buffer that the event is being written to 1721 * @event: The event meta data in the ring buffer 1722 * @entry: The event itself 1723 * @trace_ctx: The tracing context flags. 1724 * 1725 * This is a helper function to handle triggers that require data 1726 * from the event itself. It also tests the event against filters and 1727 * if the event is soft disabled and should be discarded. 1728 */ 1729 static inline void 1730 event_trigger_unlock_commit(struct trace_event_file *file, 1731 struct trace_buffer *buffer, 1732 struct ring_buffer_event *event, 1733 void *entry, unsigned int trace_ctx) 1734 { 1735 enum event_trigger_type tt = ETT_NONE; 1736 1737 if (!__event_trigger_test_discard(file, buffer, event, entry, &tt)) 1738 trace_buffer_unlock_commit(file->tr, buffer, event, trace_ctx); 1739 1740 if (tt) 1741 event_triggers_post_call(file, tt); 1742 } 1743 1744 #define FILTER_PRED_INVALID ((unsigned short)-1) 1745 #define FILTER_PRED_IS_RIGHT (1 << 15) 1746 #define FILTER_PRED_FOLD (1 << 15) 1747 1748 /* 1749 * The max preds is the size of unsigned short with 1750 * two flags at the MSBs. One bit is used for both the IS_RIGHT 1751 * and FOLD flags. The other is reserved. 1752 * 1753 * 2^14 preds is way more than enough. 1754 */ 1755 #define MAX_FILTER_PRED 16384 1756 1757 struct filter_pred; 1758 struct regex; 1759 1760 typedef int (*regex_match_func)(char *str, struct regex *r, int len); 1761 1762 enum regex_type { 1763 MATCH_FULL = 0, 1764 MATCH_FRONT_ONLY, 1765 MATCH_MIDDLE_ONLY, 1766 MATCH_END_ONLY, 1767 MATCH_GLOB, 1768 MATCH_INDEX, 1769 }; 1770 1771 struct regex { 1772 char pattern[MAX_FILTER_STR_VAL]; 1773 int len; 1774 int field_len; 1775 regex_match_func match; 1776 }; 1777 1778 static inline bool is_string_field(struct ftrace_event_field *field) 1779 { 1780 return field->filter_type == FILTER_DYN_STRING || 1781 field->filter_type == FILTER_RDYN_STRING || 1782 field->filter_type == FILTER_STATIC_STRING || 1783 field->filter_type == FILTER_PTR_STRING || 1784 field->filter_type == FILTER_COMM; 1785 } 1786 1787 static inline bool is_function_field(struct ftrace_event_field *field) 1788 { 1789 return field->filter_type == FILTER_TRACE_FN; 1790 } 1791 1792 extern enum regex_type 1793 filter_parse_regex(char *buff, int len, char **search, int *not); 1794 extern void print_event_filter(struct trace_event_file *file, 1795 struct trace_seq *s); 1796 extern int apply_event_filter(struct trace_event_file *file, 1797 char *filter_string); 1798 extern int apply_subsystem_event_filter(struct trace_subsystem_dir *dir, 1799 char *filter_string); 1800 extern void print_subsystem_event_filter(struct event_subsystem *system, 1801 struct trace_seq *s); 1802 extern int filter_assign_type(const char *type); 1803 extern int create_event_filter(struct trace_array *tr, 1804 struct trace_event_call *call, 1805 char *filter_str, bool set_str, 1806 struct event_filter **filterp); 1807 extern void free_event_filter(struct event_filter *filter); 1808 1809 struct ftrace_event_field * 1810 trace_find_event_field(struct trace_event_call *call, char *name); 1811 1812 extern void trace_event_enable_cmd_record(bool enable); 1813 extern void trace_event_enable_tgid_record(bool enable); 1814 1815 extern int event_trace_init(void); 1816 extern int init_events(void); 1817 extern int event_trace_add_tracer(struct dentry *parent, struct trace_array *tr); 1818 extern int event_trace_del_tracer(struct trace_array *tr); 1819 extern void __trace_early_add_events(struct trace_array *tr); 1820 1821 extern struct trace_event_file *__find_event_file(struct trace_array *tr, 1822 const char *system, 1823 const char *event); 1824 extern struct trace_event_file *find_event_file(struct trace_array *tr, 1825 const char *system, 1826 const char *event); 1827 1828 static inline void *event_file_data(struct file *filp) 1829 { 1830 return READ_ONCE(file_inode(filp)->i_private); 1831 } 1832 1833 extern struct mutex event_mutex; 1834 extern struct list_head ftrace_events; 1835 1836 /* 1837 * When the trace_event_file is the filp->i_private pointer, 1838 * it must be taken under the event_mutex lock, and then checked 1839 * if the EVENT_FILE_FL_FREED flag is set. If it is, then the 1840 * data pointed to by the trace_event_file can not be trusted. 1841 * 1842 * Use the event_file_file() to access the trace_event_file from 1843 * the filp the first time under the event_mutex and check for 1844 * NULL. If it is needed to be retrieved again and the event_mutex 1845 * is still held, then the event_file_data() can be used and it 1846 * is guaranteed to be valid. 1847 */ 1848 static inline struct trace_event_file *event_file_file(struct file *filp) 1849 { 1850 struct trace_event_file *file; 1851 1852 lockdep_assert_held(&event_mutex); 1853 file = READ_ONCE(file_inode(filp)->i_private); 1854 if (!file || file->flags & EVENT_FILE_FL_FREED) 1855 return NULL; 1856 return file; 1857 } 1858 1859 extern const struct file_operations event_trigger_fops; 1860 extern const struct file_operations event_hist_fops; 1861 extern const struct file_operations event_hist_debug_fops; 1862 extern const struct file_operations event_inject_fops; 1863 1864 #ifdef CONFIG_HIST_TRIGGERS 1865 extern int register_trigger_hist_cmd(void); 1866 extern int register_trigger_hist_enable_disable_cmds(void); 1867 #else 1868 static inline int register_trigger_hist_cmd(void) { return 0; } 1869 static inline int register_trigger_hist_enable_disable_cmds(void) { return 0; } 1870 #endif 1871 1872 extern int register_trigger_cmds(void); 1873 extern void clear_event_triggers(struct trace_array *tr); 1874 1875 enum { 1876 EVENT_TRIGGER_FL_PROBE = BIT(0), 1877 EVENT_TRIGGER_FL_COUNT = BIT(1), 1878 }; 1879 1880 struct event_trigger_data { 1881 unsigned long count; 1882 int ref; 1883 int flags; 1884 struct event_command *cmd_ops; 1885 struct event_filter __rcu *filter; 1886 char *filter_str; 1887 void *private_data; 1888 bool paused; 1889 bool paused_tmp; 1890 struct list_head list; 1891 char *name; 1892 struct list_head named_list; 1893 struct event_trigger_data *named_data; 1894 struct llist_node llist; 1895 }; 1896 1897 /* Avoid typos */ 1898 #define ENABLE_EVENT_STR "enable_event" 1899 #define DISABLE_EVENT_STR "disable_event" 1900 #define ENABLE_HIST_STR "enable_hist" 1901 #define DISABLE_HIST_STR "disable_hist" 1902 1903 struct enable_trigger_data { 1904 struct trace_event_file *file; 1905 bool enable; 1906 bool hist; 1907 }; 1908 1909 bool event_trigger_count(struct event_trigger_data *data, 1910 struct trace_buffer *buffer, void *rec, 1911 struct ring_buffer_event *event); 1912 1913 extern int event_enable_trigger_print(struct seq_file *m, 1914 struct event_trigger_data *data); 1915 extern void event_enable_trigger_free(struct event_trigger_data *data); 1916 extern int event_enable_trigger_parse(struct event_command *cmd_ops, 1917 struct trace_event_file *file, 1918 char *glob, char *cmd, 1919 char *param_and_filter); 1920 extern int event_enable_register_trigger(char *glob, 1921 struct event_trigger_data *data, 1922 struct trace_event_file *file); 1923 extern void event_enable_unregister_trigger(char *glob, 1924 struct event_trigger_data *test, 1925 struct trace_event_file *file); 1926 extern struct event_trigger_data * 1927 trigger_data_alloc(struct event_command *cmd_ops, char *cmd, char *param, 1928 void *private_data); 1929 extern void trigger_data_free(struct event_trigger_data *data); 1930 extern int event_trigger_init(struct event_trigger_data *data); 1931 extern int trace_event_trigger_enable_disable(struct trace_event_file *file, 1932 int trigger_enable); 1933 extern void update_cond_flag(struct trace_event_file *file); 1934 extern int set_trigger_filter(char *filter_str, 1935 struct event_trigger_data *trigger_data, 1936 struct trace_event_file *file); 1937 extern struct event_trigger_data *find_named_trigger(const char *name); 1938 extern bool is_named_trigger(struct event_trigger_data *test); 1939 extern int save_named_trigger(const char *name, 1940 struct event_trigger_data *data); 1941 extern void del_named_trigger(struct event_trigger_data *data); 1942 extern void pause_named_trigger(struct event_trigger_data *data); 1943 extern void unpause_named_trigger(struct event_trigger_data *data); 1944 extern void set_named_trigger_data(struct event_trigger_data *data, 1945 struct event_trigger_data *named_data); 1946 extern struct event_trigger_data * 1947 get_named_trigger_data(struct event_trigger_data *data); 1948 extern int register_event_command(struct event_command *cmd); 1949 extern int unregister_event_command(struct event_command *cmd); 1950 extern int register_trigger_hist_enable_disable_cmds(void); 1951 extern bool event_trigger_check_remove(const char *glob); 1952 extern bool event_trigger_empty_param(const char *param); 1953 extern int event_trigger_separate_filter(char *param_and_filter, char **param, 1954 char **filter, bool param_required); 1955 extern int event_trigger_parse_num(char *trigger, 1956 struct event_trigger_data *trigger_data); 1957 extern int event_trigger_set_filter(struct event_command *cmd_ops, 1958 struct trace_event_file *file, 1959 char *param, 1960 struct event_trigger_data *trigger_data); 1961 extern void event_trigger_reset_filter(struct event_command *cmd_ops, 1962 struct event_trigger_data *trigger_data); 1963 extern int event_trigger_register(struct event_command *cmd_ops, 1964 struct trace_event_file *file, 1965 char *glob, 1966 struct event_trigger_data *trigger_data); 1967 extern void event_trigger_unregister(struct event_command *cmd_ops, 1968 struct trace_event_file *file, 1969 char *glob, 1970 struct event_trigger_data *trigger_data); 1971 1972 extern void event_file_get(struct trace_event_file *file); 1973 extern void event_file_put(struct trace_event_file *file); 1974 1975 /** 1976 * struct event_command - callbacks and data members for event commands 1977 * 1978 * Event commands are invoked by users by writing the command name 1979 * into the 'trigger' file associated with a trace event. The 1980 * parameters associated with a specific invocation of an event 1981 * command are used to create an event trigger instance, which is 1982 * added to the list of trigger instances associated with that trace 1983 * event. When the event is hit, the set of triggers associated with 1984 * that event is invoked. 1985 * 1986 * The data members in this structure provide per-event command data 1987 * for various event commands. 1988 * 1989 * All the data members below, except for @post_trigger, must be set 1990 * for each event command. 1991 * 1992 * @name: The unique name that identifies the event command. This is 1993 * the name used when setting triggers via trigger files. 1994 * 1995 * @trigger_type: A unique id that identifies the event command 1996 * 'type'. This value has two purposes, the first to ensure that 1997 * only one trigger of the same type can be set at a given time 1998 * for a particular event e.g. it doesn't make sense to have both 1999 * a traceon and traceoff trigger attached to a single event at 2000 * the same time, so traceon and traceoff have the same type 2001 * though they have different names. The @trigger_type value is 2002 * also used as a bit value for deferring the actual trigger 2003 * action until after the current event is finished. Some 2004 * commands need to do this if they themselves log to the trace 2005 * buffer (see the @post_trigger() member below). @trigger_type 2006 * values are defined by adding new values to the trigger_type 2007 * enum in include/linux/trace_events.h. 2008 * 2009 * @flags: See the enum event_command_flags below. 2010 * 2011 * All the methods below, except for @set_filter() and @unreg_all(), 2012 * must be implemented. 2013 * 2014 * @parse: The callback function responsible for parsing and 2015 * registering the trigger written to the 'trigger' file by the 2016 * user. It allocates the trigger instance and registers it with 2017 * the appropriate trace event. It makes use of the other 2018 * event_command callback functions to orchestrate this, and is 2019 * usually implemented by the generic utility function 2020 * @event_trigger_callback() (see trace_event_triggers.c). 2021 * 2022 * @reg: Adds the trigger to the list of triggers associated with the 2023 * event, and enables the event trigger itself, after 2024 * initializing it (via the event_command @init() function). 2025 * This is also where commands can use the @trigger_type value to 2026 * make the decision as to whether or not multiple instances of 2027 * the trigger should be allowed. This is usually implemented by 2028 * the generic utility function @register_trigger() (see 2029 * trace_event_triggers.c). 2030 * 2031 * @unreg: Removes the trigger from the list of triggers associated 2032 * with the event, and disables the event trigger itself, after 2033 * initializing it (via the event_command @free() function). 2034 * This is usually implemented by the generic utility function 2035 * @unregister_trigger() (see trace_event_triggers.c). 2036 * 2037 * @unreg_all: An optional function called to remove all the triggers 2038 * from the list of triggers associated with the event. Called 2039 * when a trigger file is opened in truncate mode. 2040 * 2041 * @set_filter: An optional function called to parse and set a filter 2042 * for the trigger. If no @set_filter() method is set for the 2043 * event command, filters set by the user for the command will be 2044 * ignored. This is usually implemented by the generic utility 2045 * function @set_trigger_filter() (see trace_event_triggers.c). 2046 * 2047 * All the methods below, except for @init() and @free(), must be 2048 * implemented. 2049 * 2050 * @trigger: The trigger 'probe' function called when the triggering 2051 * event occurs. The data passed into this callback is the data 2052 * that was supplied to the event_command @reg() function that 2053 * registered the trigger (see struct event_command) along with 2054 * the trace record, rec. 2055 * 2056 * @count_func: If defined and a numeric parameter is passed to the 2057 * trigger, then this function will be called before @trigger 2058 * is called. If this function returns false, then @trigger is not 2059 * executed. 2060 * 2061 * @init: An optional initialization function called for the trigger 2062 * when the trigger is registered (via the event_command reg() 2063 * function). This can be used to perform per-trigger 2064 * initialization such as incrementing a per-trigger reference 2065 * count, for instance. This is usually implemented by the 2066 * generic utility function @event_trigger_init() (see 2067 * trace_event_triggers.c). 2068 * 2069 * @free: An optional de-initialization function called for the 2070 * trigger when the trigger is unregistered (via the 2071 * event_command @reg() function). This can be used to perform 2072 * per-trigger de-initialization such as decrementing a 2073 * per-trigger reference count and freeing corresponding trigger 2074 * data, for instance. This is usually implemented by the 2075 * generic utility function @event_trigger_free() (see 2076 * trace_event_triggers.c). 2077 * 2078 * @print: The callback function invoked to have the trigger print 2079 * itself. This is usually implemented by a wrapper function 2080 * that calls the generic utility function @event_trigger_print() 2081 * (see trace_event_triggers.c). 2082 */ 2083 struct event_command { 2084 struct list_head list; 2085 char *name; 2086 enum event_trigger_type trigger_type; 2087 int flags; 2088 int (*parse)(struct event_command *cmd_ops, 2089 struct trace_event_file *file, 2090 char *glob, char *cmd, 2091 char *param_and_filter); 2092 int (*reg)(char *glob, 2093 struct event_trigger_data *data, 2094 struct trace_event_file *file); 2095 void (*unreg)(char *glob, 2096 struct event_trigger_data *data, 2097 struct trace_event_file *file); 2098 void (*unreg_all)(struct trace_event_file *file); 2099 int (*set_filter)(char *filter_str, 2100 struct event_trigger_data *data, 2101 struct trace_event_file *file); 2102 void (*trigger)(struct event_trigger_data *data, 2103 struct trace_buffer *buffer, 2104 void *rec, 2105 struct ring_buffer_event *rbe); 2106 bool (*count_func)(struct event_trigger_data *data, 2107 struct trace_buffer *buffer, 2108 void *rec, 2109 struct ring_buffer_event *rbe); 2110 int (*init)(struct event_trigger_data *data); 2111 void (*free)(struct event_trigger_data *data); 2112 int (*print)(struct seq_file *m, 2113 struct event_trigger_data *data); 2114 }; 2115 2116 /** 2117 * enum event_command_flags - flags for struct event_command 2118 * 2119 * @POST_TRIGGER: A flag that says whether or not this command needs 2120 * to have its action delayed until after the current event has 2121 * been closed. Some triggers need to avoid being invoked while 2122 * an event is currently in the process of being logged, since 2123 * the trigger may itself log data into the trace buffer. Thus 2124 * we make sure the current event is committed before invoking 2125 * those triggers. To do that, the trigger invocation is split 2126 * in two - the first part checks the filter using the current 2127 * trace record; if a command has the @post_trigger flag set, it 2128 * sets a bit for itself in the return value, otherwise it 2129 * directly invokes the trigger. Once all commands have been 2130 * either invoked or set their return flag, the current record is 2131 * either committed or discarded. At that point, if any commands 2132 * have deferred their triggers, those commands are finally 2133 * invoked following the close of the current event. In other 2134 * words, if the event_command @func() probe implementation 2135 * itself logs to the trace buffer, this flag should be set, 2136 * otherwise it can be left unspecified. 2137 * 2138 * @NEEDS_REC: A flag that says whether or not this command needs 2139 * access to the trace record in order to perform its function, 2140 * regardless of whether or not it has a filter associated with 2141 * it (filters make a trigger require access to the trace record 2142 * but are not always present). 2143 */ 2144 enum event_command_flags { 2145 EVENT_CMD_FL_POST_TRIGGER = 1, 2146 EVENT_CMD_FL_NEEDS_REC = 2, 2147 }; 2148 2149 static inline bool event_command_post_trigger(struct event_command *cmd_ops) 2150 { 2151 return cmd_ops->flags & EVENT_CMD_FL_POST_TRIGGER; 2152 } 2153 2154 static inline bool event_command_needs_rec(struct event_command *cmd_ops) 2155 { 2156 return cmd_ops->flags & EVENT_CMD_FL_NEEDS_REC; 2157 } 2158 2159 extern int trace_event_enable_disable(struct trace_event_file *file, 2160 int enable, int soft_disable); 2161 extern int tracing_alloc_snapshot(void); 2162 extern void tracing_snapshot_cond(struct trace_array *tr, void *cond_data); 2163 extern int tracing_snapshot_cond_enable(struct trace_array *tr, void *cond_data, cond_update_fn_t update); 2164 2165 extern int tracing_snapshot_cond_disable(struct trace_array *tr); 2166 extern void *tracing_cond_snapshot_data(struct trace_array *tr); 2167 2168 extern const char *__start___trace_bprintk_fmt[]; 2169 extern const char *__stop___trace_bprintk_fmt[]; 2170 2171 extern const char *__start___tracepoint_str[]; 2172 extern const char *__stop___tracepoint_str[]; 2173 2174 void trace_printk_control(bool enabled); 2175 void trace_printk_start_comm(void); 2176 void trace_printk_start_stop_comm(int enabled); 2177 int trace_keep_overwrite(struct tracer *tracer, u64 mask, int set); 2178 int set_tracer_flag(struct trace_array *tr, u64 mask, int enabled); 2179 2180 /* Used from boot time tracer */ 2181 extern int trace_set_options(struct trace_array *tr, char *option); 2182 extern int tracing_set_tracer(struct trace_array *tr, const char *buf); 2183 extern ssize_t tracing_resize_ring_buffer(struct trace_array *tr, 2184 unsigned long size, int cpu_id); 2185 extern int tracing_set_cpumask(struct trace_array *tr, 2186 cpumask_var_t tracing_cpumask_new); 2187 2188 2189 #define MAX_EVENT_NAME_LEN 64 2190 2191 extern ssize_t trace_parse_run_command(struct file *file, 2192 const char __user *buffer, size_t count, loff_t *ppos, 2193 int (*createfn)(const char *)); 2194 2195 extern unsigned int err_pos(char *cmd, const char *str); 2196 extern void tracing_log_err(struct trace_array *tr, 2197 const char *loc, const char *cmd, 2198 const char **errs, u8 type, u16 pos); 2199 2200 /* 2201 * Normal trace_printk() and friends allocates special buffers 2202 * to do the manipulation, as well as saves the print formats 2203 * into sections to display. But the trace infrastructure wants 2204 * to use these without the added overhead at the price of being 2205 * a bit slower (used mainly for warnings, where we don't care 2206 * about performance). The internal_trace_puts() is for such 2207 * a purpose. 2208 */ 2209 #define internal_trace_puts(str) __trace_puts(_THIS_IP_, str) 2210 2211 #undef FTRACE_ENTRY 2212 #define FTRACE_ENTRY(call, struct_name, id, tstruct, print) \ 2213 extern struct trace_event_call \ 2214 __aligned(4) event_##call; 2215 #undef FTRACE_ENTRY_DUP 2216 #define FTRACE_ENTRY_DUP(call, struct_name, id, tstruct, print) \ 2217 FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print)) 2218 #undef FTRACE_ENTRY_PACKED 2219 #define FTRACE_ENTRY_PACKED(call, struct_name, id, tstruct, print) \ 2220 FTRACE_ENTRY(call, struct_name, id, PARAMS(tstruct), PARAMS(print)) 2221 2222 #include "trace_entries.h" 2223 2224 #if defined(CONFIG_PERF_EVENTS) && defined(CONFIG_FUNCTION_TRACER) 2225 int perf_ftrace_event_register(struct trace_event_call *call, 2226 enum trace_reg type, void *data); 2227 #else 2228 #define perf_ftrace_event_register NULL 2229 #endif 2230 2231 #ifdef CONFIG_FTRACE_SYSCALLS 2232 void init_ftrace_syscalls(void); 2233 const char *get_syscall_name(int syscall); 2234 #else 2235 static inline void init_ftrace_syscalls(void) { } 2236 static inline const char *get_syscall_name(int syscall) 2237 { 2238 return NULL; 2239 } 2240 #endif 2241 2242 #ifdef CONFIG_EVENT_TRACING 2243 void trace_event_init(void); 2244 void trace_event_update_all(struct trace_eval_map **map, int len); 2245 /* Used from boot time tracer */ 2246 extern int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set); 2247 extern int trigger_process_regex(struct trace_event_file *file, char *buff); 2248 #else 2249 static inline void __init trace_event_init(void) { } 2250 static inline void trace_event_update_all(struct trace_eval_map **map, int len) { } 2251 #endif 2252 2253 #ifdef CONFIG_TRACER_SNAPSHOT 2254 void tracing_snapshot_instance(struct trace_array *tr); 2255 int tracing_alloc_snapshot_instance(struct trace_array *tr); 2256 int tracing_arm_snapshot(struct trace_array *tr); 2257 void tracing_disarm_snapshot(struct trace_array *tr); 2258 #else 2259 static inline void tracing_snapshot_instance(struct trace_array *tr) { } 2260 static inline int tracing_alloc_snapshot_instance(struct trace_array *tr) 2261 { 2262 return 0; 2263 } 2264 static inline int tracing_arm_snapshot(struct trace_array *tr) { return 0; } 2265 static inline void tracing_disarm_snapshot(struct trace_array *tr) { } 2266 #endif 2267 2268 #ifdef CONFIG_PREEMPT_TRACER 2269 void tracer_preempt_on(unsigned long a0, unsigned long a1); 2270 void tracer_preempt_off(unsigned long a0, unsigned long a1); 2271 #else 2272 static inline void tracer_preempt_on(unsigned long a0, unsigned long a1) { } 2273 static inline void tracer_preempt_off(unsigned long a0, unsigned long a1) { } 2274 #endif 2275 #ifdef CONFIG_IRQSOFF_TRACER 2276 void tracer_hardirqs_on(unsigned long a0, unsigned long a1); 2277 void tracer_hardirqs_off(unsigned long a0, unsigned long a1); 2278 #else 2279 static inline void tracer_hardirqs_on(unsigned long a0, unsigned long a1) { } 2280 static inline void tracer_hardirqs_off(unsigned long a0, unsigned long a1) { } 2281 #endif 2282 2283 /* 2284 * Reset the state of the trace_iterator so that it can read consumed data. 2285 * Normally, the trace_iterator is used for reading the data when it is not 2286 * consumed, and must retain state. 2287 */ 2288 static __always_inline void trace_iterator_reset(struct trace_iterator *iter) 2289 { 2290 memset_startat(iter, 0, seq); 2291 iter->pos = -1; 2292 } 2293 2294 /* Check the name is good for event/group/fields */ 2295 static inline bool __is_good_name(const char *name, bool hash_ok) 2296 { 2297 if (!isalpha(*name) && *name != '_' && (!hash_ok || *name != '-')) 2298 return false; 2299 while (*++name != '\0') { 2300 if (!isalpha(*name) && !isdigit(*name) && *name != '_' && 2301 (!hash_ok || *name != '-')) 2302 return false; 2303 } 2304 return true; 2305 } 2306 2307 /* Check the name is good for event/group/fields */ 2308 static inline bool is_good_name(const char *name) 2309 { 2310 return __is_good_name(name, false); 2311 } 2312 2313 /* Check the name is good for system */ 2314 static inline bool is_good_system_name(const char *name) 2315 { 2316 return __is_good_name(name, true); 2317 } 2318 2319 /* Convert certain expected symbols into '_' when generating event names */ 2320 static inline void sanitize_event_name(char *name) 2321 { 2322 while (*name++ != '\0') 2323 if (*name == ':' || *name == '.' || *name == '*') 2324 *name = '_'; 2325 } 2326 2327 #ifdef CONFIG_STACKTRACE 2328 void __ftrace_trace_stack(struct trace_array *tr, 2329 struct trace_buffer *buffer, 2330 unsigned int trace_ctx, 2331 int skip, struct pt_regs *regs); 2332 2333 static __always_inline void ftrace_trace_stack(struct trace_array *tr, 2334 struct trace_buffer *buffer, 2335 unsigned int trace_ctx, 2336 int skip, struct pt_regs *regs) 2337 { 2338 if (!(tr->trace_flags & TRACE_ITER(STACKTRACE))) 2339 return; 2340 2341 __ftrace_trace_stack(tr, buffer, trace_ctx, skip, regs); 2342 } 2343 #else 2344 static inline void __ftrace_trace_stack(struct trace_array *tr, 2345 struct trace_buffer *buffer, 2346 unsigned int trace_ctx, 2347 int skip, struct pt_regs *regs) 2348 { 2349 } 2350 static inline void ftrace_trace_stack(struct trace_array *tr, 2351 struct trace_buffer *buffer, 2352 unsigned long trace_ctx, 2353 int skip, struct pt_regs *regs) 2354 { 2355 } 2356 #endif 2357 2358 /* 2359 * This is a generic way to read and write a u64 value from a file in tracefs. 2360 * 2361 * The value is stored on the variable pointed by *val. The value needs 2362 * to be at least *min and at most *max. The write is protected by an 2363 * existing *lock. 2364 */ 2365 struct trace_min_max_param { 2366 struct mutex *lock; 2367 u64 *val; 2368 u64 *min; 2369 u64 *max; 2370 }; 2371 2372 #define U64_STR_SIZE 24 /* 20 digits max */ 2373 2374 extern const struct file_operations trace_min_max_fops; 2375 2376 #ifdef CONFIG_RV 2377 extern int rv_init_interface(void); 2378 #else 2379 static inline int rv_init_interface(void) 2380 { 2381 return 0; 2382 } 2383 #endif 2384 2385 /* 2386 * This is used only to distinguish 2387 * function address from trampoline code. 2388 * So this value has no meaning. 2389 */ 2390 #define FTRACE_TRAMPOLINE_MARKER ((unsigned long) INT_MAX) 2391 2392 /* 2393 * This is used to get the address of the args array based on 2394 * the type of the entry. 2395 */ 2396 #define FGRAPH_ENTRY_ARGS(e) \ 2397 ({ \ 2398 unsigned long *_args; \ 2399 struct ftrace_graph_ent_entry *_e = e; \ 2400 \ 2401 if (IS_ENABLED(CONFIG_FUNCTION_GRAPH_RETADDR) && \ 2402 e->ent.type == TRACE_GRAPH_RETADDR_ENT) { \ 2403 struct fgraph_retaddr_ent_entry *_re; \ 2404 \ 2405 _re = (typeof(_re))_e; \ 2406 _args = _re->args; \ 2407 } else { \ 2408 _args = _e->args; \ 2409 } \ 2410 _args; \ 2411 }) 2412 2413 #endif /* _LINUX_KERNEL_TRACE_H */ 2414