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