trace_events.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. #ifndef _LINUX_TRACE_EVENT_H
  2. #define _LINUX_TRACE_EVENT_H
  3. #include <linux/ring_buffer.h>
  4. #include <linux/trace_seq.h>
  5. #include <linux/percpu.h>
  6. #include <linux/hardirq.h>
  7. #include <linux/perf_event.h>
  8. #include <linux/tracepoint.h>
  9. struct trace_array;
  10. struct trace_buffer;
  11. struct tracer;
  12. struct dentry;
  13. struct bpf_prog;
  14. const char *trace_print_flags_seq(struct trace_seq *p, const char *delim,
  15. unsigned long flags,
  16. const struct trace_print_flags *flag_array);
  17. const char *trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
  18. const struct trace_print_flags *symbol_array);
  19. #if BITS_PER_LONG == 32
  20. const char *trace_print_symbols_seq_u64(struct trace_seq *p,
  21. unsigned long long val,
  22. const struct trace_print_flags_u64
  23. *symbol_array);
  24. #endif
  25. const char *trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
  26. unsigned int bitmask_size);
  27. const char *trace_print_hex_seq(struct trace_seq *p,
  28. const unsigned char *buf, int len);
  29. const char *trace_print_array_seq(struct trace_seq *p,
  30. const void *buf, int count,
  31. size_t el_size);
  32. struct trace_iterator;
  33. struct trace_event;
  34. int trace_raw_output_prep(struct trace_iterator *iter,
  35. struct trace_event *event);
  36. /*
  37. * The trace entry - the most basic unit of tracing. This is what
  38. * is printed in the end as a single line in the trace output, such as:
  39. *
  40. * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
  41. */
  42. struct trace_entry {
  43. unsigned short type;
  44. unsigned char flags;
  45. unsigned char preempt_count;
  46. int pid;
  47. };
  48. #define TRACE_EVENT_TYPE_MAX \
  49. ((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)
  50. /*
  51. * Trace iterator - used by printout routines who present trace
  52. * results to users and which routines might sleep, etc:
  53. */
  54. struct trace_iterator {
  55. struct trace_array *tr;
  56. struct tracer *trace;
  57. struct trace_buffer *trace_buffer;
  58. void *private;
  59. int cpu_file;
  60. struct mutex mutex;
  61. struct ring_buffer_iter **buffer_iter;
  62. unsigned long iter_flags;
  63. /* trace_seq for __print_flags() and __print_symbolic() etc. */
  64. struct trace_seq tmp_seq;
  65. cpumask_var_t started;
  66. /* it's true when current open file is snapshot */
  67. bool snapshot;
  68. /* The below is zeroed out in pipe_read */
  69. struct trace_seq seq;
  70. struct trace_entry *ent;
  71. unsigned long lost_events;
  72. int leftover;
  73. int ent_size;
  74. int cpu;
  75. u64 ts;
  76. loff_t pos;
  77. long idx;
  78. /* All new field here will be zeroed out in pipe_read */
  79. };
  80. enum trace_iter_flags {
  81. TRACE_FILE_LAT_FMT = 1,
  82. TRACE_FILE_ANNOTATE = 2,
  83. TRACE_FILE_TIME_IN_NS = 4,
  84. };
  85. typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
  86. int flags, struct trace_event *event);
  87. struct trace_event_functions {
  88. trace_print_func trace;
  89. trace_print_func raw;
  90. trace_print_func hex;
  91. trace_print_func binary;
  92. };
  93. struct trace_event {
  94. struct hlist_node node;
  95. struct list_head list;
  96. int type;
  97. struct trace_event_functions *funcs;
  98. };
  99. extern int register_trace_event(struct trace_event *event);
  100. extern int unregister_trace_event(struct trace_event *event);
  101. /* Return values for print_line callback */
  102. enum print_line_t {
  103. TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
  104. TRACE_TYPE_HANDLED = 1,
  105. TRACE_TYPE_UNHANDLED = 2, /* Relay to other output functions */
  106. TRACE_TYPE_NO_CONSUME = 3 /* Handled but ask to not consume */
  107. };
  108. /*
  109. * Several functions return TRACE_TYPE_PARTIAL_LINE if the trace_seq
  110. * overflowed, and TRACE_TYPE_HANDLED otherwise. This helper function
  111. * simplifies those functions and keeps them in sync.
  112. */
  113. static inline enum print_line_t trace_handle_return(struct trace_seq *s)
  114. {
  115. return trace_seq_has_overflowed(s) ?
  116. TRACE_TYPE_PARTIAL_LINE : TRACE_TYPE_HANDLED;
  117. }
  118. void tracing_generic_entry_update(struct trace_entry *entry,
  119. unsigned long flags,
  120. int pc);
  121. struct trace_event_file;
  122. struct ring_buffer_event *
  123. trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
  124. struct trace_event_file *trace_file,
  125. int type, unsigned long len,
  126. unsigned long flags, int pc);
  127. void tracing_record_cmdline(struct task_struct *tsk);
  128. int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
  129. struct event_filter;
  130. enum trace_reg {
  131. TRACE_REG_REGISTER,
  132. TRACE_REG_UNREGISTER,
  133. #ifdef CONFIG_PERF_EVENTS
  134. TRACE_REG_PERF_REGISTER,
  135. TRACE_REG_PERF_UNREGISTER,
  136. TRACE_REG_PERF_OPEN,
  137. TRACE_REG_PERF_CLOSE,
  138. TRACE_REG_PERF_ADD,
  139. TRACE_REG_PERF_DEL,
  140. #endif
  141. };
  142. struct trace_event_call;
  143. struct trace_event_class {
  144. const char *system;
  145. void *probe;
  146. #ifdef CONFIG_PERF_EVENTS
  147. void *perf_probe;
  148. #endif
  149. int (*reg)(struct trace_event_call *event,
  150. enum trace_reg type, void *data);
  151. int (*define_fields)(struct trace_event_call *);
  152. struct list_head *(*get_fields)(struct trace_event_call *);
  153. struct list_head fields;
  154. int (*raw_init)(struct trace_event_call *);
  155. };
  156. extern int trace_event_reg(struct trace_event_call *event,
  157. enum trace_reg type, void *data);
  158. struct trace_event_buffer {
  159. struct ring_buffer *buffer;
  160. struct ring_buffer_event *event;
  161. struct trace_event_file *trace_file;
  162. void *entry;
  163. unsigned long flags;
  164. int pc;
  165. };
  166. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  167. struct trace_event_file *trace_file,
  168. unsigned long len);
  169. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer);
  170. enum {
  171. TRACE_EVENT_FL_FILTERED_BIT,
  172. TRACE_EVENT_FL_CAP_ANY_BIT,
  173. TRACE_EVENT_FL_NO_SET_FILTER_BIT,
  174. TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
  175. TRACE_EVENT_FL_WAS_ENABLED_BIT,
  176. TRACE_EVENT_FL_TRACEPOINT_BIT,
  177. TRACE_EVENT_FL_KPROBE_BIT,
  178. TRACE_EVENT_FL_UPROBE_BIT,
  179. };
  180. /*
  181. * Event flags:
  182. * FILTERED - The event has a filter attached
  183. * CAP_ANY - Any user can enable for perf
  184. * NO_SET_FILTER - Set when filter has error and is to be ignored
  185. * IGNORE_ENABLE - For trace internal events, do not enable with debugfs file
  186. * WAS_ENABLED - Set and stays set when an event was ever enabled
  187. * (used for module unloading, if a module event is enabled,
  188. * it is best to clear the buffers that used it).
  189. * TRACEPOINT - Event is a tracepoint
  190. * KPROBE - Event is a kprobe
  191. * UPROBE - Event is a uprobe
  192. */
  193. enum {
  194. TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
  195. TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
  196. TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
  197. TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
  198. TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
  199. TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
  200. TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT),
  201. TRACE_EVENT_FL_UPROBE = (1 << TRACE_EVENT_FL_UPROBE_BIT),
  202. };
  203. #define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
  204. struct trace_event_call {
  205. struct list_head list;
  206. struct trace_event_class *class;
  207. union {
  208. char *name;
  209. /* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
  210. struct tracepoint *tp;
  211. };
  212. struct trace_event event;
  213. char *print_fmt;
  214. struct event_filter *filter;
  215. void *mod;
  216. void *data;
  217. /*
  218. * bit 0: filter_active
  219. * bit 1: allow trace by non root (cap any)
  220. * bit 2: failed to apply filter
  221. * bit 3: trace internal event (do not enable)
  222. * bit 4: Event was enabled by module
  223. * bit 5: use call filter rather than file filter
  224. * bit 6: Event is a tracepoint
  225. */
  226. int flags; /* static flags of different events */
  227. #ifdef CONFIG_PERF_EVENTS
  228. int perf_refcount;
  229. struct hlist_head __percpu *perf_events;
  230. struct bpf_prog *prog;
  231. struct perf_event *bpf_prog_owner;
  232. int (*perf_perm)(struct trace_event_call *,
  233. struct perf_event *);
  234. #endif
  235. };
  236. static inline const char *
  237. trace_event_name(struct trace_event_call *call)
  238. {
  239. if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
  240. return call->tp ? call->tp->name : NULL;
  241. else
  242. return call->name;
  243. }
  244. struct trace_array;
  245. struct trace_subsystem_dir;
  246. enum {
  247. EVENT_FILE_FL_ENABLED_BIT,
  248. EVENT_FILE_FL_RECORDED_CMD_BIT,
  249. EVENT_FILE_FL_FILTERED_BIT,
  250. EVENT_FILE_FL_NO_SET_FILTER_BIT,
  251. EVENT_FILE_FL_SOFT_MODE_BIT,
  252. EVENT_FILE_FL_SOFT_DISABLED_BIT,
  253. EVENT_FILE_FL_TRIGGER_MODE_BIT,
  254. EVENT_FILE_FL_TRIGGER_COND_BIT,
  255. EVENT_FILE_FL_PID_FILTER_BIT,
  256. };
  257. /*
  258. * Event file flags:
  259. * ENABLED - The event is enabled
  260. * RECORDED_CMD - The comms should be recorded at sched_switch
  261. * FILTERED - The event has a filter attached
  262. * NO_SET_FILTER - Set when filter has error and is to be ignored
  263. * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
  264. * SOFT_DISABLED - When set, do not trace the event (even though its
  265. * tracepoint may be enabled)
  266. * TRIGGER_MODE - When set, invoke the triggers associated with the event
  267. * TRIGGER_COND - When set, one or more triggers has an associated filter
  268. * PID_FILTER - When set, the event is filtered based on pid
  269. */
  270. enum {
  271. EVENT_FILE_FL_ENABLED = (1 << EVENT_FILE_FL_ENABLED_BIT),
  272. EVENT_FILE_FL_RECORDED_CMD = (1 << EVENT_FILE_FL_RECORDED_CMD_BIT),
  273. EVENT_FILE_FL_FILTERED = (1 << EVENT_FILE_FL_FILTERED_BIT),
  274. EVENT_FILE_FL_NO_SET_FILTER = (1 << EVENT_FILE_FL_NO_SET_FILTER_BIT),
  275. EVENT_FILE_FL_SOFT_MODE = (1 << EVENT_FILE_FL_SOFT_MODE_BIT),
  276. EVENT_FILE_FL_SOFT_DISABLED = (1 << EVENT_FILE_FL_SOFT_DISABLED_BIT),
  277. EVENT_FILE_FL_TRIGGER_MODE = (1 << EVENT_FILE_FL_TRIGGER_MODE_BIT),
  278. EVENT_FILE_FL_TRIGGER_COND = (1 << EVENT_FILE_FL_TRIGGER_COND_BIT),
  279. EVENT_FILE_FL_PID_FILTER = (1 << EVENT_FILE_FL_PID_FILTER_BIT),
  280. };
  281. struct trace_event_file {
  282. struct list_head list;
  283. struct trace_event_call *event_call;
  284. struct event_filter *filter;
  285. struct dentry *dir;
  286. struct trace_array *tr;
  287. struct trace_subsystem_dir *system;
  288. struct list_head triggers;
  289. /*
  290. * 32 bit flags:
  291. * bit 0: enabled
  292. * bit 1: enabled cmd record
  293. * bit 2: enable/disable with the soft disable bit
  294. * bit 3: soft disabled
  295. * bit 4: trigger enabled
  296. *
  297. * Note: The bits must be set atomically to prevent races
  298. * from other writers. Reads of flags do not need to be in
  299. * sync as they occur in critical sections. But the way flags
  300. * is currently used, these changes do not affect the code
  301. * except that when a change is made, it may have a slight
  302. * delay in propagating the changes to other CPUs due to
  303. * caching and such. Which is mostly OK ;-)
  304. */
  305. unsigned long flags;
  306. atomic_t sm_ref; /* soft-mode reference counter */
  307. atomic_t tm_ref; /* trigger-mode reference counter */
  308. };
  309. #define __TRACE_EVENT_FLAGS(name, value) \
  310. static int __init trace_init_flags_##name(void) \
  311. { \
  312. event_##name.flags |= value; \
  313. return 0; \
  314. } \
  315. early_initcall(trace_init_flags_##name);
  316. #define __TRACE_EVENT_PERF_PERM(name, expr...) \
  317. static int perf_perm_##name(struct trace_event_call *tp_event, \
  318. struct perf_event *p_event) \
  319. { \
  320. return ({ expr; }); \
  321. } \
  322. static int __init trace_init_perf_perm_##name(void) \
  323. { \
  324. event_##name.perf_perm = &perf_perm_##name; \
  325. return 0; \
  326. } \
  327. early_initcall(trace_init_perf_perm_##name);
  328. #define PERF_MAX_TRACE_SIZE 2048
  329. #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
  330. enum event_trigger_type {
  331. ETT_NONE = (0),
  332. ETT_TRACE_ONOFF = (1 << 0),
  333. ETT_SNAPSHOT = (1 << 1),
  334. ETT_STACKTRACE = (1 << 2),
  335. ETT_EVENT_ENABLE = (1 << 3),
  336. ETT_EVENT_HIST = (1 << 4),
  337. ETT_HIST_ENABLE = (1 << 5),
  338. };
  339. extern int filter_match_preds(struct event_filter *filter, void *rec);
  340. extern enum event_trigger_type event_triggers_call(struct trace_event_file *file,
  341. void *rec);
  342. extern void event_triggers_post_call(struct trace_event_file *file,
  343. enum event_trigger_type tt,
  344. void *rec);
  345. bool trace_event_ignore_this_pid(struct trace_event_file *trace_file);
  346. /**
  347. * trace_trigger_soft_disabled - do triggers and test if soft disabled
  348. * @file: The file pointer of the event to test
  349. *
  350. * If any triggers without filters are attached to this event, they
  351. * will be called here. If the event is soft disabled and has no
  352. * triggers that require testing the fields, it will return true,
  353. * otherwise false.
  354. */
  355. static inline bool
  356. trace_trigger_soft_disabled(struct trace_event_file *file)
  357. {
  358. unsigned long eflags = file->flags;
  359. if (!(eflags & EVENT_FILE_FL_TRIGGER_COND)) {
  360. if (eflags & EVENT_FILE_FL_TRIGGER_MODE)
  361. event_triggers_call(file, NULL);
  362. if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
  363. return true;
  364. if (eflags & EVENT_FILE_FL_PID_FILTER)
  365. return trace_event_ignore_this_pid(file);
  366. }
  367. return false;
  368. }
  369. #ifdef CONFIG_BPF_EVENTS
  370. unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx);
  371. #else
  372. static inline unsigned int trace_call_bpf(struct bpf_prog *prog, void *ctx)
  373. {
  374. return 1;
  375. }
  376. #endif
  377. enum {
  378. FILTER_OTHER = 0,
  379. FILTER_STATIC_STRING,
  380. FILTER_DYN_STRING,
  381. FILTER_PTR_STRING,
  382. FILTER_TRACE_FN,
  383. FILTER_COMM,
  384. FILTER_CPU,
  385. };
  386. extern int trace_event_raw_init(struct trace_event_call *call);
  387. extern int trace_define_field(struct trace_event_call *call, const char *type,
  388. const char *name, int offset, int size,
  389. int is_signed, int filter_type);
  390. extern int trace_add_event_call(struct trace_event_call *call);
  391. extern int trace_remove_event_call(struct trace_event_call *call);
  392. extern int trace_event_get_offsets(struct trace_event_call *call);
  393. #define is_signed_type(type) (((type)(-1)) < (type)1)
  394. int trace_set_clr_event(const char *system, const char *event, int set);
  395. /*
  396. * The double __builtin_constant_p is because gcc will give us an error
  397. * if we try to allocate the static variable to fmt if it is not a
  398. * constant. Even with the outer if statement optimizing out.
  399. */
  400. #define event_trace_printk(ip, fmt, args...) \
  401. do { \
  402. __trace_printk_check_format(fmt, ##args); \
  403. tracing_record_cmdline(current); \
  404. if (__builtin_constant_p(fmt)) { \
  405. static const char *trace_printk_fmt \
  406. __attribute__((section("__trace_printk_fmt"))) = \
  407. __builtin_constant_p(fmt) ? fmt : NULL; \
  408. \
  409. __trace_bprintk(ip, trace_printk_fmt, ##args); \
  410. } else \
  411. __trace_printk(ip, fmt, ##args); \
  412. } while (0)
  413. #ifdef CONFIG_PERF_EVENTS
  414. struct perf_event;
  415. DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
  416. extern int perf_trace_init(struct perf_event *event);
  417. extern void perf_trace_destroy(struct perf_event *event);
  418. extern int perf_trace_add(struct perf_event *event, int flags);
  419. extern void perf_trace_del(struct perf_event *event, int flags);
  420. extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  421. char *filter_str);
  422. extern void ftrace_profile_free_filter(struct perf_event *event);
  423. void perf_trace_buf_update(void *record, u16 type);
  424. void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp);
  425. void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
  426. struct trace_event_call *call, u64 count,
  427. struct pt_regs *regs, struct hlist_head *head,
  428. struct task_struct *task);
  429. static inline void
  430. perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,
  431. u64 count, struct pt_regs *regs, void *head,
  432. struct task_struct *task)
  433. {
  434. perf_tp_event(type, count, raw_data, size, regs, head, rctx, task);
  435. }
  436. #endif
  437. #endif /* _LINUX_TRACE_EVENT_H */