2 #ifndef _LINUX_FTRACE_EVENT_H
3 #define _LINUX_FTRACE_EVENT_H
5 #include <linux/ring_buffer.h>
6 #include <linux/trace_seq.h>
7 #include <linux/percpu.h>
8 #include <linux/hardirq.h>
9 #include <linux/perf_event.h>
10 #include <linux/tracepoint.h>
18 struct trace_print_flags {
23 struct trace_print_flags_u64 {
24 unsigned long long mask;
28 const char *ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
30 const struct trace_print_flags *flag_array);
32 const char *ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
33 const struct trace_print_flags *symbol_array);
35 #if BITS_PER_LONG == 32
36 const char *ftrace_print_symbols_seq_u64(struct trace_seq *p,
37 unsigned long long val,
38 const struct trace_print_flags_u64
42 const char *ftrace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
43 unsigned int bitmask_size);
45 const char *ftrace_print_hex_seq(struct trace_seq *p,
46 const unsigned char *buf, int len);
48 const char *ftrace_print_array_seq(struct trace_seq *p,
49 const void *buf, int count,
52 struct trace_iterator;
55 int ftrace_raw_output_prep(struct trace_iterator *iter,
56 struct trace_event *event);
59 * The trace entry - the most basic unit of tracing. This is what
60 * is printed in the end as a single line in the trace output, such as:
62 * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
67 unsigned char preempt_count;
69 unsigned short migrate_disable;
70 unsigned short padding;
71 unsigned char preempt_lazy_count;
74 #define FTRACE_MAX_EVENT \
75 ((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)
78 * Trace iterator - used by printout routines who present trace
79 * results to users and which routines might sleep, etc:
81 struct trace_iterator {
82 struct trace_array *tr;
84 struct trace_buffer *trace_buffer;
88 struct ring_buffer_iter **buffer_iter;
89 unsigned long iter_flags;
91 /* trace_seq for __print_flags() and __print_symbolic() etc. */
92 struct trace_seq tmp_seq;
94 cpumask_var_t started;
96 /* it's true when current open file is snapshot */
99 /* The below is zeroed out in pipe_read */
100 struct trace_seq seq;
101 struct trace_entry *ent;
102 unsigned long lost_events;
111 /* All new field here will be zeroed out in pipe_read */
114 enum trace_iter_flags {
115 TRACE_FILE_LAT_FMT = 1,
116 TRACE_FILE_ANNOTATE = 2,
117 TRACE_FILE_TIME_IN_NS = 4,
121 typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
122 int flags, struct trace_event *event);
124 struct trace_event_functions {
125 trace_print_func trace;
126 trace_print_func raw;
127 trace_print_func hex;
128 trace_print_func binary;
132 struct hlist_node node;
133 struct list_head list;
135 struct trace_event_functions *funcs;
138 extern int register_ftrace_event(struct trace_event *event);
139 extern int unregister_ftrace_event(struct trace_event *event);
141 /* Return values for print_line callback */
143 TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
144 TRACE_TYPE_HANDLED = 1,
145 TRACE_TYPE_UNHANDLED = 2, /* Relay to other output functions */
146 TRACE_TYPE_NO_CONSUME = 3 /* Handled but ask to not consume */
150 * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
151 * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
152 * simplifies those functions and keeps them in sync.
154 static inline enum print_line_t trace_handle_return(struct trace_seq *s)
156 return trace_seq_has_overflowed(s) ?
157 TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
160 void tracing_generic_entry_update(struct trace_entry *entry,
163 struct ftrace_event_file;
165 struct ring_buffer_event *
166 trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
167 struct ftrace_event_file *ftrace_file,
168 int type, unsigned long len,
169 unsigned long flags, int pc);
170 struct ring_buffer_event *
171 trace_current_buffer_lock_reserve(struct ring_buffer **current_buffer,
172 int type, unsigned long len,
173 unsigned long flags, int pc);
174 void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
175 struct ring_buffer_event *event,
176 unsigned long flags, int pc);
177 void trace_buffer_unlock_commit(struct ring_buffer *buffer,
178 struct ring_buffer_event *event,
179 unsigned long flags, int pc);
180 void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
181 struct ring_buffer_event *event,
182 unsigned long flags, int pc,
183 struct pt_regs *regs);
184 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
185 struct ring_buffer_event *event);
187 void tracing_record_cmdline(struct task_struct *tsk);
189 int ftrace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
195 TRACE_REG_UNREGISTER,
196 #ifdef CONFIG_PERF_EVENTS
197 TRACE_REG_PERF_REGISTER,
198 TRACE_REG_PERF_UNREGISTER,
200 TRACE_REG_PERF_CLOSE,
206 struct ftrace_event_call;
208 struct ftrace_event_class {
211 #ifdef CONFIG_PERF_EVENTS
214 int (*reg)(struct ftrace_event_call *event,
215 enum trace_reg type, void *data);
216 int (*define_fields)(struct ftrace_event_call *);
217 struct list_head *(*get_fields)(struct ftrace_event_call *);
218 struct list_head fields;
219 int (*raw_init)(struct ftrace_event_call *);
222 extern int ftrace_event_reg(struct ftrace_event_call *event,
223 enum trace_reg type, void *data);
225 int ftrace_output_event(struct trace_iterator *iter, struct ftrace_event_call *event,
228 int ftrace_event_define_field(struct ftrace_event_call *call,
229 char *type, int len, char *item, int offset,
230 int field_size, int sign, int filter);
232 struct ftrace_event_buffer {
233 struct ring_buffer *buffer;
234 struct ring_buffer_event *event;
235 struct ftrace_event_file *ftrace_file;
241 void *ftrace_event_buffer_reserve(struct ftrace_event_buffer *fbuffer,
242 struct ftrace_event_file *ftrace_file,
245 void ftrace_event_buffer_commit(struct ftrace_event_buffer *fbuffer);
247 int ftrace_event_define_field(struct ftrace_event_call *call,
248 char *type, int len, char *item, int offset,
249 int field_size, int sign, int filter);
252 TRACE_EVENT_FL_FILTERED_BIT,
253 TRACE_EVENT_FL_CAP_ANY_BIT,
254 TRACE_EVENT_FL_NO_SET_FILTER_BIT,
255 TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
256 TRACE_EVENT_FL_WAS_ENABLED_BIT,
257 TRACE_EVENT_FL_USE_CALL_FILTER_BIT,
258 TRACE_EVENT_FL_TRACEPOINT_BIT,
259 TRACE_EVENT_FL_KPROBE_BIT,
264 * FILTERED - The event has a filter attached
265 * CAP_ANY - Any user can enable for perf
266 * NO_SET_FILTER - Set when filter has error and is to be ignored
267 * IGNORE_ENABLE - For ftrace internal events, do not enable with debugfs file
268 * WAS_ENABLED - Set and stays set when an event was ever enabled
269 * (used for module unloading, if a module event is enabled,
270 * it is best to clear the buffers that used it).
271 * USE_CALL_FILTER - For ftrace internal events, don't use file filter
272 * TRACEPOINT - Event is a tracepoint
273 * KPROBE - Event is a kprobe
276 TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
277 TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
278 TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
279 TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
280 TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
281 TRACE_EVENT_FL_USE_CALL_FILTER = (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT),
282 TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
283 TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT),
286 struct ftrace_event_call {
287 struct list_head list;
288 struct ftrace_event_class *class;
291 /* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
292 struct tracepoint *tp;
294 struct trace_event event;
296 struct event_filter *filter;
300 * bit 0: filter_active
301 * bit 1: allow trace by non root (cap any)
302 * bit 2: failed to apply filter
303 * bit 3: ftrace internal event (do not enable)
304 * bit 4: Event was enabled by module
305 * bit 5: use call filter rather than file filter
306 * bit 6: Event is a tracepoint
308 int flags; /* static flags of different events */
310 #ifdef CONFIG_PERF_EVENTS
312 struct hlist_head __percpu *perf_events;
313 struct bpf_prog *prog;
315 int (*perf_perm)(struct ftrace_event_call *,
316 struct perf_event *);
320 static inline const char *
321 ftrace_event_name(struct ftrace_event_call *call)
323 if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
324 return call->tp ? call->tp->name : NULL;
330 struct ftrace_subsystem_dir;
333 FTRACE_EVENT_FL_ENABLED_BIT,
334 FTRACE_EVENT_FL_RECORDED_CMD_BIT,
335 FTRACE_EVENT_FL_FILTERED_BIT,
336 FTRACE_EVENT_FL_NO_SET_FILTER_BIT,
337 FTRACE_EVENT_FL_SOFT_MODE_BIT,
338 FTRACE_EVENT_FL_SOFT_DISABLED_BIT,
339 FTRACE_EVENT_FL_TRIGGER_MODE_BIT,
340 FTRACE_EVENT_FL_TRIGGER_COND_BIT,
344 * Ftrace event file flags:
345 * ENABLED - The event is enabled
346 * RECORDED_CMD - The comms should be recorded at sched_switch
347 * FILTERED - The event has a filter attached
348 * NO_SET_FILTER - Set when filter has error and is to be ignored
349 * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
350 * SOFT_DISABLED - When set, do not trace the event (even though its
351 * tracepoint may be enabled)
352 * TRIGGER_MODE - When set, invoke the triggers associated with the event
353 * TRIGGER_COND - When set, one or more triggers has an associated filter
356 FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT),
357 FTRACE_EVENT_FL_RECORDED_CMD = (1 << FTRACE_EVENT_FL_RECORDED_CMD_BIT),
358 FTRACE_EVENT_FL_FILTERED = (1 << FTRACE_EVENT_FL_FILTERED_BIT),
359 FTRACE_EVENT_FL_NO_SET_FILTER = (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT),
360 FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
361 FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
362 FTRACE_EVENT_FL_TRIGGER_MODE = (1 << FTRACE_EVENT_FL_TRIGGER_MODE_BIT),
363 FTRACE_EVENT_FL_TRIGGER_COND = (1 << FTRACE_EVENT_FL_TRIGGER_COND_BIT),
366 struct ftrace_event_file {
367 struct list_head list;
368 struct ftrace_event_call *event_call;
369 struct event_filter *filter;
371 struct trace_array *tr;
372 struct ftrace_subsystem_dir *system;
373 struct list_head triggers;
378 * bit 1: enabled cmd record
379 * bit 2: enable/disable with the soft disable bit
380 * bit 3: soft disabled
381 * bit 4: trigger enabled
383 * Note: The bits must be set atomically to prevent races
384 * from other writers. Reads of flags do not need to be in
385 * sync as they occur in critical sections. But the way flags
386 * is currently used, these changes do not affect the code
387 * except that when a change is made, it may have a slight
388 * delay in propagating the changes to other CPUs due to
389 * caching and such. Which is mostly OK ;-)
392 atomic_t sm_ref; /* soft-mode reference counter */
393 atomic_t tm_ref; /* trigger-mode reference counter */
396 #define __TRACE_EVENT_FLAGS(name, value) \
397 static int __init trace_init_flags_##name(void) \
399 event_##name.flags |= value; \
402 early_initcall(trace_init_flags_##name);
404 #define __TRACE_EVENT_PERF_PERM(name, expr...) \
405 static int perf_perm_##name(struct ftrace_event_call *tp_event, \
406 struct perf_event *p_event) \
408 return ({ expr; }); \
410 static int __init trace_init_perf_perm_##name(void) \
412 event_##name.perf_perm = &perf_perm_##name; \
415 early_initcall(trace_init_perf_perm_##name);
417 #define PERF_MAX_TRACE_SIZE 2048
419 #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
421 enum event_trigger_type {
423 ETT_TRACE_ONOFF = (1 << 0),
424 ETT_SNAPSHOT = (1 << 1),
425 ETT_STACKTRACE = (1 << 2),
426 ETT_EVENT_ENABLE = (1 << 3),
429 extern int filter_match_preds(struct event_filter *filter, void *rec);
431 extern int filter_check_discard(struct ftrace_event_file *file, void *rec,
432 struct ring_buffer *buffer,
433 struct ring_buffer_event *event);
434 extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
435 struct ring_buffer *buffer,
436 struct ring_buffer_event *event);
437 extern enum event_trigger_type event_triggers_call(struct ftrace_event_file *file,
439 extern void event_triggers_post_call(struct ftrace_event_file *file,
440 enum event_trigger_type tt);
443 * ftrace_trigger_soft_disabled - do triggers and test if soft disabled
444 * @file: The file pointer of the event to test
446 * If any triggers without filters are attached to this event, they
447 * will be called here. If the event is soft disabled and has no
448 * triggers that require testing the fields, it will return true,
452 ftrace_trigger_soft_disabled(struct ftrace_event_file *file)
454 unsigned long eflags = file->flags;
456 if (!(eflags & FTRACE_EVENT_FL_TRIGGER_COND)) {
457 if (eflags & FTRACE_EVENT_FL_TRIGGER_MODE)
458 event_triggers_call(file, NULL);
459 if (eflags & FTRACE_EVENT_FL_SOFT_DISABLED)
466 * Helper function for event_trigger_unlock_commit{_regs}().
467 * If there are event triggers attached to this event that requires
468 * filtering against its fields, then they wil be called as the
469 * entry already holds the field information of the current event.
471 * It also checks if the event should be discarded or not.
472 * It is to be discarded if the event is soft disabled and the
473 * event was only recorded to process triggers, or if the event
474 * filter is active and this event did not match the filters.
476 * Returns true if the event is discarded, false otherwise.
479 __event_trigger_test_discard(struct ftrace_event_file *file,
480 struct ring_buffer *buffer,
481 struct ring_buffer_event *event,
483 enum event_trigger_type *tt)
485 unsigned long eflags = file->flags;
487 if (eflags & FTRACE_EVENT_FL_TRIGGER_COND)
488 *tt = event_triggers_call(file, entry);
490 if (test_bit(FTRACE_EVENT_FL_SOFT_DISABLED_BIT, &file->flags))
491 ring_buffer_discard_commit(buffer, event);
492 else if (!filter_check_discard(file, entry, buffer, event))
499 * event_trigger_unlock_commit - handle triggers and finish event commit
500 * @file: The file pointer assoctiated to the event
501 * @buffer: The ring buffer that the event is being written to
502 * @event: The event meta data in the ring buffer
503 * @entry: The event itself
504 * @irq_flags: The state of the interrupts at the start of the event
505 * @pc: The state of the preempt count at the start of the event.
507 * This is a helper function to handle triggers that require data
508 * from the event itself. It also tests the event against filters and
509 * if the event is soft disabled and should be discarded.
512 event_trigger_unlock_commit(struct ftrace_event_file *file,
513 struct ring_buffer *buffer,
514 struct ring_buffer_event *event,
515 void *entry, unsigned long irq_flags, int pc)
517 enum event_trigger_type tt = ETT_NONE;
519 if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
520 trace_buffer_unlock_commit(buffer, event, irq_flags, pc);
523 event_triggers_post_call(file, tt);
527 * event_trigger_unlock_commit_regs - handle triggers and finish event commit
528 * @file: The file pointer assoctiated to the event
529 * @buffer: The ring buffer that the event is being written to
530 * @event: The event meta data in the ring buffer
531 * @entry: The event itself
532 * @irq_flags: The state of the interrupts at the start of the event
533 * @pc: The state of the preempt count at the start of the event.
535 * This is a helper function to handle triggers that require data
536 * from the event itself. It also tests the event against filters and
537 * if the event is soft disabled and should be discarded.
539 * Same as event_trigger_unlock_commit() but calls
540 * trace_buffer_unlock_commit_regs() instead of trace_buffer_unlock_commit().
543 event_trigger_unlock_commit_regs(struct ftrace_event_file *file,
544 struct ring_buffer *buffer,
545 struct ring_buffer_event *event,
546 void *entry, unsigned long irq_flags, int pc,
547 struct pt_regs *regs)
549 enum event_trigger_type tt = ETT_NONE;
551 if (!__event_trigger_test_discard(file, buffer, event, entry, &tt))
552 trace_buffer_unlock_commit_regs(buffer, event,
553 irq_flags, pc, regs);
556 event_triggers_post_call(file, tt);
559 #ifdef CONFIG_BPF_SYSCALL
560 unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx);
562 static inline unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
570 FILTER_STATIC_STRING,
576 extern int trace_event_raw_init(struct ftrace_event_call *call);
577 extern int trace_define_field(struct ftrace_event_call *call, const char *type,
578 const char *name, int offset, int size,
579 int is_signed, int filter_type);
580 extern int trace_add_event_call(struct ftrace_event_call *call);
581 extern int trace_remove_event_call(struct ftrace_event_call *call);
583 #define is_signed_type(type) (((type)(-1)) < (type)1)
585 int trace_set_clr_event(const char *system, const char *event, int set);
588 * The double __builtin_constant_p is because gcc will give us an error
589 * if we try to allocate the static variable to fmt if it is not a
590 * constant. Even with the outer if statement optimizing out.
592 #define event_trace_printk(ip, fmt, args...) \
594 __trace_printk_check_format(fmt, ##args); \
595 tracing_record_cmdline(current); \
596 if (__builtin_constant_p(fmt)) { \
597 static const char *trace_printk_fmt \
598 __attribute__((section("__trace_printk_fmt"))) = \
599 __builtin_constant_p(fmt) ? fmt : NULL; \
601 __trace_bprintk(ip, trace_printk_fmt, ##args); \
603 __trace_printk(ip, fmt, ##args); \
606 #ifdef CONFIG_PERF_EVENTS
609 DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
611 extern int perf_trace_init(struct perf_event *event);
612 extern void perf_trace_destroy(struct perf_event *event);
613 extern int perf_trace_add(struct perf_event *event, int flags);
614 extern void perf_trace_del(struct perf_event *event, int flags);
615 extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
617 extern void ftrace_profile_free_filter(struct perf_event *event);
618 extern void *perf_trace_buf_prepare(int size, unsigned short type,
619 struct pt_regs **regs, int *rctxp);
622 perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
623 u64 count, struct pt_regs *regs, void *head,
624 struct task_struct *task)
626 perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
630 #endif /* _LINUX_FTRACE_EVENT_H */