2 #ifndef _LINUX_TRACE_EVENT_H
3 #define _LINUX_TRACE_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 *trace_print_flags_seq(struct trace_seq *p, const char *delim,
30 const struct trace_print_flags *flag_array);
32 const char *trace_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 *trace_print_symbols_seq_u64(struct trace_seq *p,
37 unsigned long long val,
38 const struct trace_print_flags_u64
42 const char *trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
43 unsigned int bitmask_size);
45 const char *trace_print_hex_seq(struct trace_seq *p,
46 const unsigned char *buf, int len);
48 const char *trace_print_array_seq(struct trace_seq *p,
49 const void *buf, int count,
52 struct trace_iterator;
55 int trace_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 TRACE_EVENT_TYPE_MAX \
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_trace_event(struct trace_event *event);
139 extern int unregister_trace_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 trace_event_file;
165 struct ring_buffer_event *
166 trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
167 struct trace_event_file *trace_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_buffer_unlock_commit(struct trace_array *tr,
175 struct ring_buffer *buffer,
176 struct ring_buffer_event *event,
177 unsigned long flags, int pc);
178 void trace_buffer_unlock_commit_regs(struct trace_array *tr,
179 struct ring_buffer *buffer,
180 struct ring_buffer_event *event,
181 unsigned long flags, int pc,
182 struct pt_regs *regs);
183 void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
184 struct ring_buffer_event *event);
186 void tracing_record_cmdline(struct task_struct *tsk);
188 int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
194 TRACE_REG_UNREGISTER,
195 #ifdef CONFIG_PERF_EVENTS
196 TRACE_REG_PERF_REGISTER,
197 TRACE_REG_PERF_UNREGISTER,
199 TRACE_REG_PERF_CLOSE,
205 struct trace_event_call;
207 struct trace_event_class {
210 #ifdef CONFIG_PERF_EVENTS
213 int (*reg)(struct trace_event_call *event,
214 enum trace_reg type, void *data);
215 int (*define_fields)(struct trace_event_call *);
216 struct list_head *(*get_fields)(struct trace_event_call *);
217 struct list_head fields;
218 int (*raw_init)(struct trace_event_call *);
221 extern int trace_event_reg(struct trace_event_call *event,
222 enum trace_reg type, void *data);
224 struct trace_event_buffer {
225 struct ring_buffer *buffer;
226 struct ring_buffer_event *event;
227 struct trace_event_file *trace_file;
233 void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
234 struct trace_event_file *trace_file,
237 void trace_event_buffer_commit(struct trace_event_buffer *fbuffer);
240 TRACE_EVENT_FL_FILTERED_BIT,
241 TRACE_EVENT_FL_CAP_ANY_BIT,
242 TRACE_EVENT_FL_NO_SET_FILTER_BIT,
243 TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
244 TRACE_EVENT_FL_WAS_ENABLED_BIT,
245 TRACE_EVENT_FL_USE_CALL_FILTER_BIT,
246 TRACE_EVENT_FL_TRACEPOINT_BIT,
247 TRACE_EVENT_FL_KPROBE_BIT,
248 TRACE_EVENT_FL_UPROBE_BIT,
253 * FILTERED - The event has a filter attached
254 * CAP_ANY - Any user can enable for perf
255 * NO_SET_FILTER - Set when filter has error and is to be ignored
256 * IGNORE_ENABLE - For trace internal events, do not enable with debugfs file
257 * WAS_ENABLED - Set and stays set when an event was ever enabled
258 * (used for module unloading, if a module event is enabled,
259 * it is best to clear the buffers that used it).
260 * USE_CALL_FILTER - For trace internal events, don't use file filter
261 * TRACEPOINT - Event is a tracepoint
262 * KPROBE - Event is a kprobe
263 * UPROBE - Event is a uprobe
266 TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
267 TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
268 TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
269 TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
270 TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
271 TRACE_EVENT_FL_USE_CALL_FILTER = (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT),
272 TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
273 TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT),
274 TRACE_EVENT_FL_UPROBE = (1 << TRACE_EVENT_FL_UPROBE_BIT),
277 #define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
279 struct trace_event_call {
280 struct list_head list;
281 struct trace_event_class *class;
284 /* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
285 struct tracepoint *tp;
287 struct trace_event event;
289 struct event_filter *filter;
293 * bit 0: filter_active
294 * bit 1: allow trace by non root (cap any)
295 * bit 2: failed to apply filter
296 * bit 3: trace internal event (do not enable)
297 * bit 4: Event was enabled by module
298 * bit 5: use call filter rather than file filter
299 * bit 6: Event is a tracepoint
301 int flags; /* static flags of different events */
303 #ifdef CONFIG_PERF_EVENTS
305 struct hlist_head __percpu *perf_events;
306 struct bpf_prog *prog;
308 int (*perf_perm)(struct trace_event_call *,
309 struct perf_event *);
313 static inline const char *
314 trace_event_name(struct trace_event_call *call)
316 if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
317 return call->tp ? call->tp->name : NULL;
323 struct trace_subsystem_dir;
326 EVENT_FILE_FL_ENABLED_BIT,
327 EVENT_FILE_FL_RECORDED_CMD_BIT,
328 EVENT_FILE_FL_FILTERED_BIT,
329 EVENT_FILE_FL_NO_SET_FILTER_BIT,
330 EVENT_FILE_FL_SOFT_MODE_BIT,
331 EVENT_FILE_FL_SOFT_DISABLED_BIT,
332 EVENT_FILE_FL_TRIGGER_MODE_BIT,
333 EVENT_FILE_FL_TRIGGER_COND_BIT,
334 EVENT_FILE_FL_PID_FILTER_BIT,
339 * ENABLED - The event is enabled
340 * RECORDED_CMD - The comms should be recorded at sched_switch
341 * FILTERED - The event has a filter attached
342 * NO_SET_FILTER - Set when filter has error and is to be ignored
343 * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
344 * SOFT_DISABLED - When set, do not trace the event (even though its
345 * tracepoint may be enabled)
346 * TRIGGER_MODE - When set, invoke the triggers associated with the event
347 * TRIGGER_COND - When set, one or more triggers has an associated filter
348 * PID_FILTER - When set, the event is filtered based on pid
351 EVENT_FILE_FL_ENABLED = (1 << EVENT_FILE_FL_ENABLED_BIT),
352 EVENT_FILE_FL_RECORDED_CMD = (1 << EVENT_FILE_FL_RECORDED_CMD_BIT),
353 EVENT_FILE_FL_FILTERED = (1 << EVENT_FILE_FL_FILTERED_BIT),
354 EVENT_FILE_FL_NO_SET_FILTER = (1 << EVENT_FILE_FL_NO_SET_FILTER_BIT),
355 EVENT_FILE_FL_SOFT_MODE = (1 << EVENT_FILE_FL_SOFT_MODE_BIT),
356 EVENT_FILE_FL_SOFT_DISABLED = (1 << EVENT_FILE_FL_SOFT_DISABLED_BIT),
357 EVENT_FILE_FL_TRIGGER_MODE = (1 << EVENT_FILE_FL_TRIGGER_MODE_BIT),
358 EVENT_FILE_FL_TRIGGER_COND = (1 << EVENT_FILE_FL_TRIGGER_COND_BIT),
359 EVENT_FILE_FL_PID_FILTER = (1 << EVENT_FILE_FL_PID_FILTER_BIT),
362 struct trace_event_file {
363 struct list_head list;
364 struct trace_event_call *event_call;
365 struct event_filter *filter;
367 struct trace_array *tr;
368 struct trace_subsystem_dir *system;
369 struct list_head triggers;
374 * bit 1: enabled cmd record
375 * bit 2: enable/disable with the soft disable bit
376 * bit 3: soft disabled
377 * bit 4: trigger enabled
379 * Note: The bits must be set atomically to prevent races
380 * from other writers. Reads of flags do not need to be in
381 * sync as they occur in critical sections. But the way flags
382 * is currently used, these changes do not affect the code
383 * except that when a change is made, it may have a slight
384 * delay in propagating the changes to other CPUs due to
385 * caching and such. Which is mostly OK ;-)
388 atomic_t sm_ref; /* soft-mode reference counter */
389 atomic_t tm_ref; /* trigger-mode reference counter */
392 #define __TRACE_EVENT_FLAGS(name, value) \
393 static int __init trace_init_flags_##name(void) \
395 event_##name.flags |= value; \
398 early_initcall(trace_init_flags_##name);
400 #define __TRACE_EVENT_PERF_PERM(name, expr...) \
401 static int perf_perm_##name(struct trace_event_call *tp_event, \
402 struct perf_event *p_event) \
404 return ({ expr; }); \
406 static int __init trace_init_perf_perm_##name(void) \
408 event_##name.perf_perm = &perf_perm_##name; \
411 early_initcall(trace_init_perf_perm_##name);
413 #define PERF_MAX_TRACE_SIZE 2048
415 #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
417 enum event_trigger_type {
419 ETT_TRACE_ONOFF = (1 << 0),
420 ETT_SNAPSHOT = (1 << 1),
421 ETT_STACKTRACE = (1 << 2),
422 ETT_EVENT_ENABLE = (1 << 3),
425 extern int filter_match_preds(struct event_filter *filter, void *rec);
427 extern int filter_check_discard(struct trace_event_file *file, void *rec,
428 struct ring_buffer *buffer,
429 struct ring_buffer_event *event);
430 extern int call_filter_check_discard(struct trace_event_call *call, void *rec,
431 struct ring_buffer *buffer,
432 struct ring_buffer_event *event);
433 extern enum event_trigger_type event_triggers_call(struct trace_event_file *file,
435 extern void event_triggers_post_call(struct trace_event_file *file,
436 enum event_trigger_type tt);
438 bool trace_event_ignore_this_pid(struct trace_event_file *trace_file);
441 * trace_trigger_soft_disabled - do triggers and test if soft disabled
442 * @file: The file pointer of the event to test
444 * If any triggers without filters are attached to this event, they
445 * will be called here. If the event is soft disabled and has no
446 * triggers that require testing the fields, it will return true,
450 trace_trigger_soft_disabled(struct trace_event_file *file)
452 unsigned long eflags = file->flags;
454 if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
455 if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
456 event_triggers_call(file, NULL);
457 if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
459 if (eflags & EVENT_FILE_FL_PID_FILTER)
460 return trace_event_ignore_this_pid(file);
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 trace_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 & EVENT_FILE_FL_TRIGGER_COND)
488 *tt = event_triggers_call(file, entry);
490 if (test_bit(EVENT_FILE_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 trace_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(file->tr, 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 trace_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(file->tr, buffer, event,
553 irq_flags, pc, regs);
556 event_triggers_post_call(file, tt);
559 #ifdef CONFIG_BPF_EVENTS
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,
578 extern int trace_event_raw_init(struct trace_event_call *call);
579 extern int trace_define_field(struct trace_event_call *call, const char *type,
580 const char *name, int offset, int size,
581 int is_signed, int filter_type);
582 extern int trace_add_event_call(struct trace_event_call *call);
583 extern int trace_remove_event_call(struct trace_event_call *call);
585 #define is_signed_type(type) (((type)(-1)) < (type)1)
587 int trace_set_clr_event(const char *system, const char *event, int set);
590 * The double __builtin_constant_p is because gcc will give us an error
591 * if we try to allocate the static variable to fmt if it is not a
592 * constant. Even with the outer if statement optimizing out.
594 #define event_trace_printk(ip, fmt, args...) \
596 __trace_printk_check_format(fmt, ##args); \
597 tracing_record_cmdline(current); \
598 if (__builtin_constant_p(fmt)) { \
599 static const char *trace_printk_fmt \
600 __attribute__((section("__trace_printk_fmt"))) = \
601 __builtin_constant_p(fmt) ? fmt : NULL; \
603 __trace_bprintk(ip, trace_printk_fmt, ##args); \
605 __trace_printk(ip, fmt, ##args); \
608 #ifdef CONFIG_PERF_EVENTS
611 DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
613 extern int perf_trace_init(struct perf_event *event);
614 extern void perf_trace_destroy(struct perf_event *event);
615 extern int perf_trace_add(struct perf_event *event, int flags);
616 extern void perf_trace_del(struct perf_event *event, int flags);
617 extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
619 extern void ftrace_profile_free_filter(struct perf_event *event);
620 extern void *perf_trace_buf_prepare(int size, unsigned short type,
621 struct pt_regs **regs, int *rctxp);
624 perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
625 u64 count, struct pt_regs *regs, void *head,
626 struct task_struct *task)
628 perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
632 #endif /* _LINUX_TRACE_EVENT_H */