trace_events.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TRACE_EVENT_H
  3. #define _LINUX_TRACE_EVENT_H
  4. #include <linux/ring_buffer.h>
  5. #include <linux/trace_seq.h>
  6. #include <linux/percpu.h>
  7. #include <linux/hardirq.h>
  8. #include <linux/perf_event.h>
  9. #include <linux/tracepoint.h>
  10. struct trace_array;
  11. struct array_buffer;
  12. struct tracer;
  13. struct dentry;
  14. struct bpf_prog;
  15. union bpf_attr;
  16. const char *trace_print_flags_seq(struct trace_seq *p, const char *delim,
  17. unsigned long flags,
  18. const struct trace_print_flags *flag_array);
  19. const char *trace_print_symbols_seq(struct trace_seq *p, unsigned long val,
  20. const struct trace_print_flags *symbol_array);
  21. #if BITS_PER_LONG == 32
  22. const char *trace_print_flags_seq_u64(struct trace_seq *p, const char *delim,
  23. unsigned long long flags,
  24. const struct trace_print_flags_u64 *flag_array);
  25. const char *trace_print_symbols_seq_u64(struct trace_seq *p,
  26. unsigned long long val,
  27. const struct trace_print_flags_u64
  28. *symbol_array);
  29. #endif
  30. const char *trace_print_bitmask_seq(struct trace_seq *p, void *bitmask_ptr,
  31. unsigned int bitmask_size);
  32. const char *trace_print_hex_seq(struct trace_seq *p,
  33. const unsigned char *buf, int len,
  34. bool concatenate);
  35. const char *trace_print_array_seq(struct trace_seq *p,
  36. const void *buf, int count,
  37. size_t el_size);
  38. const char *
  39. trace_print_hex_dump_seq(struct trace_seq *p, const char *prefix_str,
  40. int prefix_type, int rowsize, int groupsize,
  41. const void *buf, size_t len, bool ascii);
  42. struct trace_iterator;
  43. struct trace_event;
  44. int trace_raw_output_prep(struct trace_iterator *iter,
  45. struct trace_event *event);
  46. extern __printf(2, 3)
  47. void trace_event_printf(struct trace_iterator *iter, const char *fmt, ...);
  48. /*
  49. * The trace entry - the most basic unit of tracing. This is what
  50. * is printed in the end as a single line in the trace output, such as:
  51. *
  52. * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
  53. */
  54. struct trace_entry {
  55. unsigned short type;
  56. unsigned char flags;
  57. unsigned char preempt_count;
  58. int pid;
  59. };
  60. #define TRACE_EVENT_TYPE_MAX \
  61. ((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)
  62. /*
  63. * Trace iterator - used by printout routines who present trace
  64. * results to users and which routines might sleep, etc:
  65. */
  66. struct trace_iterator {
  67. struct trace_array *tr;
  68. struct tracer *trace;
  69. struct array_buffer *array_buffer;
  70. void *private;
  71. int cpu_file;
  72. struct mutex mutex;
  73. struct ring_buffer_iter **buffer_iter;
  74. unsigned long iter_flags;
  75. void *temp; /* temp holder */
  76. unsigned int temp_size;
  77. char *fmt; /* modified format holder */
  78. unsigned int fmt_size;
  79. long wait_index;
  80. /* trace_seq for __print_flags() and __print_symbolic() etc. */
  81. struct trace_seq tmp_seq;
  82. cpumask_var_t started;
  83. /* it's true when current open file is snapshot */
  84. bool snapshot;
  85. /* The below is zeroed out in pipe_read */
  86. struct trace_seq seq;
  87. struct trace_entry *ent;
  88. unsigned long lost_events;
  89. int leftover;
  90. int ent_size;
  91. int cpu;
  92. u64 ts;
  93. loff_t pos;
  94. long idx;
  95. /* All new field here will be zeroed out in pipe_read */
  96. };
  97. enum trace_iter_flags {
  98. TRACE_FILE_LAT_FMT = 1,
  99. TRACE_FILE_ANNOTATE = 2,
  100. TRACE_FILE_TIME_IN_NS = 4,
  101. };
  102. typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
  103. int flags, struct trace_event *event);
  104. struct trace_event_functions {
  105. trace_print_func trace;
  106. trace_print_func raw;
  107. trace_print_func hex;
  108. trace_print_func binary;
  109. };
  110. struct trace_event {
  111. struct hlist_node node;
  112. struct list_head list;
  113. int type;
  114. struct trace_event_functions *funcs;
  115. };
  116. extern int register_trace_event(struct trace_event *event);
  117. extern int unregister_trace_event(struct trace_event *event);
  118. /* Return values for print_line callback */
  119. enum print_line_t {
  120. TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
  121. TRACE_TYPE_HANDLED = 1,
  122. TRACE_TYPE_UNHANDLED = 2, /* Relay to other output functions */
  123. TRACE_TYPE_NO_CONSUME = 3 /* Handled but ask to not consume */
  124. };
  125. enum print_line_t trace_handle_return(struct trace_seq *s);
  126. static inline void tracing_generic_entry_update(struct trace_entry *entry,
  127. unsigned short type,
  128. unsigned int trace_ctx)
  129. {
  130. entry->preempt_count = trace_ctx & 0xff;
  131. entry->pid = current->pid;
  132. entry->type = type;
  133. entry->flags = trace_ctx >> 16;
  134. }
  135. unsigned int tracing_gen_ctx_irq_test(unsigned int irqs_status);
  136. enum trace_flag_type {
  137. TRACE_FLAG_IRQS_OFF = 0x01,
  138. TRACE_FLAG_IRQS_NOSUPPORT = 0x02,
  139. TRACE_FLAG_NEED_RESCHED = 0x04,
  140. TRACE_FLAG_HARDIRQ = 0x08,
  141. TRACE_FLAG_SOFTIRQ = 0x10,
  142. TRACE_FLAG_PREEMPT_RESCHED = 0x20,
  143. TRACE_FLAG_NMI = 0x40,
  144. TRACE_FLAG_BH_OFF = 0x80,
  145. };
  146. #ifdef CONFIG_TRACE_IRQFLAGS_SUPPORT
  147. static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
  148. {
  149. unsigned int irq_status = irqs_disabled_flags(irqflags) ?
  150. TRACE_FLAG_IRQS_OFF : 0;
  151. return tracing_gen_ctx_irq_test(irq_status);
  152. }
  153. static inline unsigned int tracing_gen_ctx(void)
  154. {
  155. unsigned long irqflags;
  156. local_save_flags(irqflags);
  157. return tracing_gen_ctx_flags(irqflags);
  158. }
  159. #else
  160. static inline unsigned int tracing_gen_ctx_flags(unsigned long irqflags)
  161. {
  162. return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
  163. }
  164. static inline unsigned int tracing_gen_ctx(void)
  165. {
  166. return tracing_gen_ctx_irq_test(TRACE_FLAG_IRQS_NOSUPPORT);
  167. }
  168. #endif
  169. static inline unsigned int tracing_gen_ctx_dec(void)
  170. {
  171. unsigned int trace_ctx;
  172. trace_ctx = tracing_gen_ctx();
  173. /*
  174. * Subtract one from the preemption counter if preemption is enabled,
  175. * see trace_event_buffer_reserve()for details.
  176. */
  177. if (IS_ENABLED(CONFIG_PREEMPTION))
  178. trace_ctx--;
  179. return trace_ctx;
  180. }
  181. struct trace_event_file;
  182. struct ring_buffer_event *
  183. trace_event_buffer_lock_reserve(struct trace_buffer **current_buffer,
  184. struct trace_event_file *trace_file,
  185. int type, unsigned long len,
  186. unsigned int trace_ctx);
  187. #define TRACE_RECORD_CMDLINE BIT(0)
  188. #define TRACE_RECORD_TGID BIT(1)
  189. void tracing_record_taskinfo(struct task_struct *task, int flags);
  190. void tracing_record_taskinfo_sched_switch(struct task_struct *prev,
  191. struct task_struct *next, int flags);
  192. void tracing_record_cmdline(struct task_struct *task);
  193. void tracing_record_tgid(struct task_struct *task);
  194. int trace_output_call(struct trace_iterator *iter, char *name, char *fmt, ...);
  195. struct event_filter;
  196. enum trace_reg {
  197. TRACE_REG_REGISTER,
  198. TRACE_REG_UNREGISTER,
  199. #ifdef CONFIG_PERF_EVENTS
  200. TRACE_REG_PERF_REGISTER,
  201. TRACE_REG_PERF_UNREGISTER,
  202. TRACE_REG_PERF_OPEN,
  203. TRACE_REG_PERF_CLOSE,
  204. /*
  205. * These (ADD/DEL) use a 'boolean' return value, where 1 (true) means a
  206. * custom action was taken and the default action is not to be
  207. * performed.
  208. */
  209. TRACE_REG_PERF_ADD,
  210. TRACE_REG_PERF_DEL,
  211. #endif
  212. };
  213. struct trace_event_call;
  214. #define TRACE_FUNCTION_TYPE ((const char *)~0UL)
  215. struct trace_event_fields {
  216. const char *type;
  217. union {
  218. struct {
  219. const char *name;
  220. const int size;
  221. const int align;
  222. const int is_signed;
  223. const int filter_type;
  224. const int len;
  225. };
  226. int (*define_fields)(struct trace_event_call *);
  227. };
  228. };
  229. struct trace_event_class {
  230. const char *system;
  231. void *probe;
  232. #ifdef CONFIG_PERF_EVENTS
  233. void *perf_probe;
  234. #endif
  235. int (*reg)(struct trace_event_call *event,
  236. enum trace_reg type, void *data);
  237. struct trace_event_fields *fields_array;
  238. struct list_head *(*get_fields)(struct trace_event_call *);
  239. struct list_head fields;
  240. int (*raw_init)(struct trace_event_call *);
  241. };
  242. extern int trace_event_reg(struct trace_event_call *event,
  243. enum trace_reg type, void *data);
  244. struct trace_event_buffer {
  245. struct trace_buffer *buffer;
  246. struct ring_buffer_event *event;
  247. struct trace_event_file *trace_file;
  248. void *entry;
  249. unsigned int trace_ctx;
  250. struct pt_regs *regs;
  251. };
  252. void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
  253. struct trace_event_file *trace_file,
  254. unsigned long len);
  255. void trace_event_buffer_commit(struct trace_event_buffer *fbuffer);
  256. enum {
  257. TRACE_EVENT_FL_FILTERED_BIT,
  258. TRACE_EVENT_FL_CAP_ANY_BIT,
  259. TRACE_EVENT_FL_NO_SET_FILTER_BIT,
  260. TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
  261. TRACE_EVENT_FL_TRACEPOINT_BIT,
  262. TRACE_EVENT_FL_DYNAMIC_BIT,
  263. TRACE_EVENT_FL_KPROBE_BIT,
  264. TRACE_EVENT_FL_UPROBE_BIT,
  265. TRACE_EVENT_FL_EPROBE_BIT,
  266. TRACE_EVENT_FL_CUSTOM_BIT,
  267. };
  268. /*
  269. * Event flags:
  270. * FILTERED - The event has a filter attached
  271. * CAP_ANY - Any user can enable for perf
  272. * NO_SET_FILTER - Set when filter has error and is to be ignored
  273. * IGNORE_ENABLE - For trace internal events, do not enable with debugfs file
  274. * TRACEPOINT - Event is a tracepoint
  275. * DYNAMIC - Event is a dynamic event (created at run time)
  276. * KPROBE - Event is a kprobe
  277. * UPROBE - Event is a uprobe
  278. * EPROBE - Event is an event probe
  279. * CUSTOM - Event is a custom event (to be attached to an exsiting tracepoint)
  280. * This is set when the custom event has not been attached
  281. * to a tracepoint yet, then it is cleared when it is.
  282. */
  283. enum {
  284. TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
  285. TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
  286. TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
  287. TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
  288. TRACE_EVENT_FL_TRACEPOINT = (1 << TRACE_EVENT_FL_TRACEPOINT_BIT),
  289. TRACE_EVENT_FL_DYNAMIC = (1 << TRACE_EVENT_FL_DYNAMIC_BIT),
  290. TRACE_EVENT_FL_KPROBE = (1 << TRACE_EVENT_FL_KPROBE_BIT),
  291. TRACE_EVENT_FL_UPROBE = (1 << TRACE_EVENT_FL_UPROBE_BIT),
  292. TRACE_EVENT_FL_EPROBE = (1 << TRACE_EVENT_FL_EPROBE_BIT),
  293. TRACE_EVENT_FL_CUSTOM = (1 << TRACE_EVENT_FL_CUSTOM_BIT),
  294. };
  295. #define TRACE_EVENT_FL_UKPROBE (TRACE_EVENT_FL_KPROBE | TRACE_EVENT_FL_UPROBE)
  296. struct trace_event_call {
  297. struct list_head list;
  298. struct trace_event_class *class;
  299. union {
  300. char *name;
  301. /* Set TRACE_EVENT_FL_TRACEPOINT flag when using "tp" */
  302. struct tracepoint *tp;
  303. };
  304. struct trace_event event;
  305. char *print_fmt;
  306. struct event_filter *filter;
  307. /*
  308. * Static events can disappear with modules,
  309. * where as dynamic ones need their own ref count.
  310. */
  311. union {
  312. void *module;
  313. atomic_t refcnt;
  314. };
  315. void *data;
  316. /* See the TRACE_EVENT_FL_* flags above */
  317. int flags; /* static flags of different events */
  318. #ifdef CONFIG_PERF_EVENTS
  319. int perf_refcount;
  320. struct hlist_head __percpu *perf_events;
  321. struct bpf_prog_array __rcu *prog_array;
  322. int (*perf_perm)(struct trace_event_call *,
  323. struct perf_event *);
  324. #endif
  325. };
  326. #ifdef CONFIG_DYNAMIC_EVENTS
  327. bool trace_event_dyn_try_get_ref(struct trace_event_call *call);
  328. void trace_event_dyn_put_ref(struct trace_event_call *call);
  329. bool trace_event_dyn_busy(struct trace_event_call *call);
  330. #else
  331. static inline bool trace_event_dyn_try_get_ref(struct trace_event_call *call)
  332. {
  333. /* Without DYNAMIC_EVENTS configured, nothing should be calling this */
  334. return false;
  335. }
  336. static inline void trace_event_dyn_put_ref(struct trace_event_call *call)
  337. {
  338. }
  339. static inline bool trace_event_dyn_busy(struct trace_event_call *call)
  340. {
  341. /* Nothing should call this without DYNAIMIC_EVENTS configured. */
  342. return true;
  343. }
  344. #endif
  345. static inline bool trace_event_try_get_ref(struct trace_event_call *call)
  346. {
  347. if (call->flags & TRACE_EVENT_FL_DYNAMIC)
  348. return trace_event_dyn_try_get_ref(call);
  349. else
  350. return try_module_get(call->module);
  351. }
  352. static inline void trace_event_put_ref(struct trace_event_call *call)
  353. {
  354. if (call->flags & TRACE_EVENT_FL_DYNAMIC)
  355. trace_event_dyn_put_ref(call);
  356. else
  357. module_put(call->module);
  358. }
  359. #ifdef CONFIG_PERF_EVENTS
  360. static inline bool bpf_prog_array_valid(struct trace_event_call *call)
  361. {
  362. /*
  363. * This inline function checks whether call->prog_array
  364. * is valid or not. The function is called in various places,
  365. * outside rcu_read_lock/unlock, as a heuristic to speed up execution.
  366. *
  367. * If this function returns true, and later call->prog_array
  368. * becomes false inside rcu_read_lock/unlock region,
  369. * we bail out then. If this function return false,
  370. * there is a risk that we might miss a few events if the checking
  371. * were delayed until inside rcu_read_lock/unlock region and
  372. * call->prog_array happened to become non-NULL then.
  373. *
  374. * Here, READ_ONCE() is used instead of rcu_access_pointer().
  375. * rcu_access_pointer() requires the actual definition of
  376. * "struct bpf_prog_array" while READ_ONCE() only needs
  377. * a declaration of the same type.
  378. */
  379. return !!READ_ONCE(call->prog_array);
  380. }
  381. #endif
  382. static inline const char *
  383. trace_event_name(struct trace_event_call *call)
  384. {
  385. if (call->flags & TRACE_EVENT_FL_CUSTOM)
  386. return call->name;
  387. else if (call->flags & TRACE_EVENT_FL_TRACEPOINT)
  388. return call->tp ? call->tp->name : NULL;
  389. else
  390. return call->name;
  391. }
  392. static inline struct list_head *
  393. trace_get_fields(struct trace_event_call *event_call)
  394. {
  395. if (!event_call->class->get_fields)
  396. return &event_call->class->fields;
  397. return event_call->class->get_fields(event_call);
  398. }
  399. struct trace_subsystem_dir;
  400. enum {
  401. EVENT_FILE_FL_ENABLED_BIT,
  402. EVENT_FILE_FL_RECORDED_CMD_BIT,
  403. EVENT_FILE_FL_RECORDED_TGID_BIT,
  404. EVENT_FILE_FL_FILTERED_BIT,
  405. EVENT_FILE_FL_NO_SET_FILTER_BIT,
  406. EVENT_FILE_FL_SOFT_MODE_BIT,
  407. EVENT_FILE_FL_SOFT_DISABLED_BIT,
  408. EVENT_FILE_FL_TRIGGER_MODE_BIT,
  409. EVENT_FILE_FL_TRIGGER_COND_BIT,
  410. EVENT_FILE_FL_PID_FILTER_BIT,
  411. EVENT_FILE_FL_WAS_ENABLED_BIT,
  412. };
  413. extern struct trace_event_file *trace_get_event_file(const char *instance,
  414. const char *system,
  415. const char *event);
  416. extern void trace_put_event_file(struct trace_event_file *file);
  417. #define MAX_DYNEVENT_CMD_LEN (2048)
  418. enum dynevent_type {
  419. DYNEVENT_TYPE_SYNTH = 1,
  420. DYNEVENT_TYPE_KPROBE,
  421. DYNEVENT_TYPE_NONE,
  422. };
  423. struct dynevent_cmd;
  424. typedef int (*dynevent_create_fn_t)(struct dynevent_cmd *cmd);
  425. struct dynevent_cmd {
  426. struct seq_buf seq;
  427. const char *event_name;
  428. unsigned int n_fields;
  429. enum dynevent_type type;
  430. dynevent_create_fn_t run_command;
  431. void *private_data;
  432. };
  433. extern int dynevent_create(struct dynevent_cmd *cmd);
  434. extern int synth_event_delete(const char *name);
  435. extern void synth_event_cmd_init(struct dynevent_cmd *cmd,
  436. char *buf, int maxlen);
  437. extern int __synth_event_gen_cmd_start(struct dynevent_cmd *cmd,
  438. const char *name,
  439. struct module *mod, ...);
  440. #define synth_event_gen_cmd_start(cmd, name, mod, ...) \
  441. __synth_event_gen_cmd_start(cmd, name, mod, ## __VA_ARGS__, NULL)
  442. struct synth_field_desc {
  443. const char *type;
  444. const char *name;
  445. };
  446. extern int synth_event_gen_cmd_array_start(struct dynevent_cmd *cmd,
  447. const char *name,
  448. struct module *mod,
  449. struct synth_field_desc *fields,
  450. unsigned int n_fields);
  451. extern int synth_event_create(const char *name,
  452. struct synth_field_desc *fields,
  453. unsigned int n_fields, struct module *mod);
  454. extern int synth_event_add_field(struct dynevent_cmd *cmd,
  455. const char *type,
  456. const char *name);
  457. extern int synth_event_add_field_str(struct dynevent_cmd *cmd,
  458. const char *type_name);
  459. extern int synth_event_add_fields(struct dynevent_cmd *cmd,
  460. struct synth_field_desc *fields,
  461. unsigned int n_fields);
  462. #define synth_event_gen_cmd_end(cmd) \
  463. dynevent_create(cmd)
  464. struct synth_event;
  465. struct synth_event_trace_state {
  466. struct trace_event_buffer fbuffer;
  467. struct synth_trace_event *entry;
  468. struct trace_buffer *buffer;
  469. struct synth_event *event;
  470. unsigned int cur_field;
  471. unsigned int n_u64;
  472. bool disabled;
  473. bool add_next;
  474. bool add_name;
  475. };
  476. extern int synth_event_trace(struct trace_event_file *file,
  477. unsigned int n_vals, ...);
  478. extern int synth_event_trace_array(struct trace_event_file *file, u64 *vals,
  479. unsigned int n_vals);
  480. extern int synth_event_trace_start(struct trace_event_file *file,
  481. struct synth_event_trace_state *trace_state);
  482. extern int synth_event_add_next_val(u64 val,
  483. struct synth_event_trace_state *trace_state);
  484. extern int synth_event_add_val(const char *field_name, u64 val,
  485. struct synth_event_trace_state *trace_state);
  486. extern int synth_event_trace_end(struct synth_event_trace_state *trace_state);
  487. extern int kprobe_event_delete(const char *name);
  488. extern void kprobe_event_cmd_init(struct dynevent_cmd *cmd,
  489. char *buf, int maxlen);
  490. #define kprobe_event_gen_cmd_start(cmd, name, loc, ...) \
  491. __kprobe_event_gen_cmd_start(cmd, false, name, loc, ## __VA_ARGS__, NULL)
  492. #define kretprobe_event_gen_cmd_start(cmd, name, loc, ...) \
  493. __kprobe_event_gen_cmd_start(cmd, true, name, loc, ## __VA_ARGS__, NULL)
  494. extern int __kprobe_event_gen_cmd_start(struct dynevent_cmd *cmd,
  495. bool kretprobe,
  496. const char *name,
  497. const char *loc, ...);
  498. #define kprobe_event_add_fields(cmd, ...) \
  499. __kprobe_event_add_fields(cmd, ## __VA_ARGS__, NULL)
  500. #define kprobe_event_add_field(cmd, field) \
  501. __kprobe_event_add_fields(cmd, field, NULL)
  502. extern int __kprobe_event_add_fields(struct dynevent_cmd *cmd, ...);
  503. #define kprobe_event_gen_cmd_end(cmd) \
  504. dynevent_create(cmd)
  505. #define kretprobe_event_gen_cmd_end(cmd) \
  506. dynevent_create(cmd)
  507. /*
  508. * Event file flags:
  509. * ENABLED - The event is enabled
  510. * RECORDED_CMD - The comms should be recorded at sched_switch
  511. * RECORDED_TGID - The tgids should be recorded at sched_switch
  512. * FILTERED - The event has a filter attached
  513. * NO_SET_FILTER - Set when filter has error and is to be ignored
  514. * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
  515. * SOFT_DISABLED - When set, do not trace the event (even though its
  516. * tracepoint may be enabled)
  517. * TRIGGER_MODE - When set, invoke the triggers associated with the event
  518. * TRIGGER_COND - When set, one or more triggers has an associated filter
  519. * PID_FILTER - When set, the event is filtered based on pid
  520. * WAS_ENABLED - Set when enabled to know to clear trace on module removal
  521. */
  522. enum {
  523. EVENT_FILE_FL_ENABLED = (1 << EVENT_FILE_FL_ENABLED_BIT),
  524. EVENT_FILE_FL_RECORDED_CMD = (1 << EVENT_FILE_FL_RECORDED_CMD_BIT),
  525. EVENT_FILE_FL_RECORDED_TGID = (1 << EVENT_FILE_FL_RECORDED_TGID_BIT),
  526. EVENT_FILE_FL_FILTERED = (1 << EVENT_FILE_FL_FILTERED_BIT),
  527. EVENT_FILE_FL_NO_SET_FILTER = (1 << EVENT_FILE_FL_NO_SET_FILTER_BIT),
  528. EVENT_FILE_FL_SOFT_MODE = (1 << EVENT_FILE_FL_SOFT_MODE_BIT),
  529. EVENT_FILE_FL_SOFT_DISABLED = (1 << EVENT_FILE_FL_SOFT_DISABLED_BIT),
  530. EVENT_FILE_FL_TRIGGER_MODE = (1 << EVENT_FILE_FL_TRIGGER_MODE_BIT),
  531. EVENT_FILE_FL_TRIGGER_COND = (1 << EVENT_FILE_FL_TRIGGER_COND_BIT),
  532. EVENT_FILE_FL_PID_FILTER = (1 << EVENT_FILE_FL_PID_FILTER_BIT),
  533. EVENT_FILE_FL_WAS_ENABLED = (1 << EVENT_FILE_FL_WAS_ENABLED_BIT),
  534. };
  535. struct trace_event_file {
  536. struct list_head list;
  537. struct trace_event_call *event_call;
  538. struct event_filter __rcu *filter;
  539. struct dentry *dir;
  540. struct trace_array *tr;
  541. struct trace_subsystem_dir *system;
  542. struct list_head triggers;
  543. /*
  544. * 32 bit flags:
  545. * bit 0: enabled
  546. * bit 1: enabled cmd record
  547. * bit 2: enable/disable with the soft disable bit
  548. * bit 3: soft disabled
  549. * bit 4: trigger enabled
  550. *
  551. * Note: The bits must be set atomically to prevent races
  552. * from other writers. Reads of flags do not need to be in
  553. * sync as they occur in critical sections. But the way flags
  554. * is currently used, these changes do not affect the code
  555. * except that when a change is made, it may have a slight
  556. * delay in propagating the changes to other CPUs due to
  557. * caching and such. Which is mostly OK ;-)
  558. */
  559. unsigned long flags;
  560. atomic_t sm_ref; /* soft-mode reference counter */
  561. atomic_t tm_ref; /* trigger-mode reference counter */
  562. };
  563. #define __TRACE_EVENT_FLAGS(name, value) \
  564. static int __init trace_init_flags_##name(void) \
  565. { \
  566. event_##name.flags |= value; \
  567. return 0; \
  568. } \
  569. early_initcall(trace_init_flags_##name);
  570. #define __TRACE_EVENT_PERF_PERM(name, expr...) \
  571. static int perf_perm_##name(struct trace_event_call *tp_event, \
  572. struct perf_event *p_event) \
  573. { \
  574. return ({ expr; }); \
  575. } \
  576. static int __init trace_init_perf_perm_##name(void) \
  577. { \
  578. event_##name.perf_perm = &perf_perm_##name; \
  579. return 0; \
  580. } \
  581. early_initcall(trace_init_perf_perm_##name);
  582. #define PERF_MAX_TRACE_SIZE 8192
  583. #define MAX_FILTER_STR_VAL 256U /* Should handle KSYM_SYMBOL_LEN */
  584. enum event_trigger_type {
  585. ETT_NONE = (0),
  586. ETT_TRACE_ONOFF = (1 << 0),
  587. ETT_SNAPSHOT = (1 << 1),
  588. ETT_STACKTRACE = (1 << 2),
  589. ETT_EVENT_ENABLE = (1 << 3),
  590. ETT_EVENT_HIST = (1 << 4),
  591. ETT_HIST_ENABLE = (1 << 5),
  592. ETT_EVENT_EPROBE = (1 << 6),
  593. };
  594. extern int filter_match_preds(struct event_filter *filter, void *rec);
  595. extern enum event_trigger_type
  596. event_triggers_call(struct trace_event_file *file,
  597. struct trace_buffer *buffer, void *rec,
  598. struct ring_buffer_event *event);
  599. extern void
  600. event_triggers_post_call(struct trace_event_file *file,
  601. enum event_trigger_type tt);
  602. bool trace_event_ignore_this_pid(struct trace_event_file *trace_file);
  603. bool __trace_trigger_soft_disabled(struct trace_event_file *file);
  604. /**
  605. * trace_trigger_soft_disabled - do triggers and test if soft disabled
  606. * @file: The file pointer of the event to test
  607. *
  608. * If any triggers without filters are attached to this event, they
  609. * will be called here. If the event is soft disabled and has no
  610. * triggers that require testing the fields, it will return true,
  611. * otherwise false.
  612. */
  613. static __always_inline bool
  614. trace_trigger_soft_disabled(struct trace_event_file *file)
  615. {
  616. unsigned long eflags = file->flags;
  617. if (likely(!(eflags & (EVENT_FILE_FL_TRIGGER_MODE |
  618. EVENT_FILE_FL_SOFT_DISABLED |
  619. EVENT_FILE_FL_PID_FILTER))))
  620. return false;
  621. if (likely(eflags & EVENT_FILE_FL_TRIGGER_COND))
  622. return false;
  623. return __trace_trigger_soft_disabled(file);
  624. }
  625. #ifdef CONFIG_BPF_EVENTS
  626. unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx);
  627. int perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
  628. void perf_event_detach_bpf_prog(struct perf_event *event);
  629. int perf_event_query_prog_array(struct perf_event *event, void __user *info);
  630. int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
  631. int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *prog);
  632. struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name);
  633. void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp);
  634. int bpf_get_perf_event_info(const struct perf_event *event, u32 *prog_id,
  635. u32 *fd_type, const char **buf,
  636. u64 *probe_offset, u64 *probe_addr);
  637. int bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog);
  638. #else
  639. static inline unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
  640. {
  641. return 1;
  642. }
  643. static inline int
  644. perf_event_attach_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie)
  645. {
  646. return -EOPNOTSUPP;
  647. }
  648. static inline void perf_event_detach_bpf_prog(struct perf_event *event) { }
  649. static inline int
  650. perf_event_query_prog_array(struct perf_event *event, void __user *info)
  651. {
  652. return -EOPNOTSUPP;
  653. }
  654. static inline int bpf_probe_register(struct bpf_raw_event_map *btp, struct bpf_prog *p)
  655. {
  656. return -EOPNOTSUPP;
  657. }
  658. static inline int bpf_probe_unregister(struct bpf_raw_event_map *btp, struct bpf_prog *p)
  659. {
  660. return -EOPNOTSUPP;
  661. }
  662. static inline struct bpf_raw_event_map *bpf_get_raw_tracepoint(const char *name)
  663. {
  664. return NULL;
  665. }
  666. static inline void bpf_put_raw_tracepoint(struct bpf_raw_event_map *btp)
  667. {
  668. }
  669. static inline int bpf_get_perf_event_info(const struct perf_event *event,
  670. u32 *prog_id, u32 *fd_type,
  671. const char **buf, u64 *probe_offset,
  672. u64 *probe_addr)
  673. {
  674. return -EOPNOTSUPP;
  675. }
  676. static inline int
  677. bpf_kprobe_multi_link_attach(const union bpf_attr *attr, struct bpf_prog *prog)
  678. {
  679. return -EOPNOTSUPP;
  680. }
  681. #endif
  682. enum {
  683. FILTER_OTHER = 0,
  684. FILTER_STATIC_STRING,
  685. FILTER_DYN_STRING,
  686. FILTER_RDYN_STRING,
  687. FILTER_PTR_STRING,
  688. FILTER_TRACE_FN,
  689. FILTER_COMM,
  690. FILTER_CPU,
  691. };
  692. extern int trace_event_raw_init(struct trace_event_call *call);
  693. extern int trace_define_field(struct trace_event_call *call, const char *type,
  694. const char *name, int offset, int size,
  695. int is_signed, int filter_type);
  696. extern int trace_add_event_call(struct trace_event_call *call);
  697. extern int trace_remove_event_call(struct trace_event_call *call);
  698. extern int trace_event_get_offsets(struct trace_event_call *call);
  699. int ftrace_set_clr_event(struct trace_array *tr, char *buf, int set);
  700. int trace_set_clr_event(const char *system, const char *event, int set);
  701. int trace_array_set_clr_event(struct trace_array *tr, const char *system,
  702. const char *event, bool enable);
  703. /*
  704. * The double __builtin_constant_p is because gcc will give us an error
  705. * if we try to allocate the static variable to fmt if it is not a
  706. * constant. Even with the outer if statement optimizing out.
  707. */
  708. #define event_trace_printk(ip, fmt, args...) \
  709. do { \
  710. __trace_printk_check_format(fmt, ##args); \
  711. tracing_record_cmdline(current); \
  712. if (__builtin_constant_p(fmt)) { \
  713. static const char *trace_printk_fmt \
  714. __section("__trace_printk_fmt") = \
  715. __builtin_constant_p(fmt) ? fmt : NULL; \
  716. \
  717. __trace_bprintk(ip, trace_printk_fmt, ##args); \
  718. } else \
  719. __trace_printk(ip, fmt, ##args); \
  720. } while (0)
  721. #ifdef CONFIG_PERF_EVENTS
  722. struct perf_event;
  723. DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
  724. DECLARE_PER_CPU(int, bpf_kprobe_override);
  725. extern int perf_trace_init(struct perf_event *event);
  726. extern void perf_trace_destroy(struct perf_event *event);
  727. extern int perf_trace_add(struct perf_event *event, int flags);
  728. extern void perf_trace_del(struct perf_event *event, int flags);
  729. #ifdef CONFIG_KPROBE_EVENTS
  730. extern int perf_kprobe_init(struct perf_event *event, bool is_retprobe);
  731. extern void perf_kprobe_destroy(struct perf_event *event);
  732. extern int bpf_get_kprobe_info(const struct perf_event *event,
  733. u32 *fd_type, const char **symbol,
  734. u64 *probe_offset, u64 *probe_addr,
  735. bool perf_type_tracepoint);
  736. #endif
  737. #ifdef CONFIG_UPROBE_EVENTS
  738. extern int perf_uprobe_init(struct perf_event *event,
  739. unsigned long ref_ctr_offset, bool is_retprobe);
  740. extern void perf_uprobe_destroy(struct perf_event *event);
  741. extern int bpf_get_uprobe_info(const struct perf_event *event,
  742. u32 *fd_type, const char **filename,
  743. u64 *probe_offset, u64 *probe_addr,
  744. bool perf_type_tracepoint);
  745. #endif
  746. extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  747. char *filter_str);
  748. extern void ftrace_profile_free_filter(struct perf_event *event);
  749. void perf_trace_buf_update(void *record, u16 type);
  750. void *perf_trace_buf_alloc(int size, struct pt_regs **regs, int *rctxp);
  751. int perf_event_set_bpf_prog(struct perf_event *event, struct bpf_prog *prog, u64 bpf_cookie);
  752. void perf_event_free_bpf_prog(struct perf_event *event);
  753. void bpf_trace_run1(struct bpf_prog *prog, u64 arg1);
  754. void bpf_trace_run2(struct bpf_prog *prog, u64 arg1, u64 arg2);
  755. void bpf_trace_run3(struct bpf_prog *prog, u64 arg1, u64 arg2,
  756. u64 arg3);
  757. void bpf_trace_run4(struct bpf_prog *prog, u64 arg1, u64 arg2,
  758. u64 arg3, u64 arg4);
  759. void bpf_trace_run5(struct bpf_prog *prog, u64 arg1, u64 arg2,
  760. u64 arg3, u64 arg4, u64 arg5);
  761. void bpf_trace_run6(struct bpf_prog *prog, u64 arg1, u64 arg2,
  762. u64 arg3, u64 arg4, u64 arg5, u64 arg6);
  763. void bpf_trace_run7(struct bpf_prog *prog, u64 arg1, u64 arg2,
  764. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7);
  765. void bpf_trace_run8(struct bpf_prog *prog, u64 arg1, u64 arg2,
  766. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
  767. u64 arg8);
  768. void bpf_trace_run9(struct bpf_prog *prog, u64 arg1, u64 arg2,
  769. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
  770. u64 arg8, u64 arg9);
  771. void bpf_trace_run10(struct bpf_prog *prog, u64 arg1, u64 arg2,
  772. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
  773. u64 arg8, u64 arg9, u64 arg10);
  774. void bpf_trace_run11(struct bpf_prog *prog, u64 arg1, u64 arg2,
  775. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
  776. u64 arg8, u64 arg9, u64 arg10, u64 arg11);
  777. void bpf_trace_run12(struct bpf_prog *prog, u64 arg1, u64 arg2,
  778. u64 arg3, u64 arg4, u64 arg5, u64 arg6, u64 arg7,
  779. u64 arg8, u64 arg9, u64 arg10, u64 arg11, u64 arg12);
  780. void perf_trace_run_bpf_submit(void *raw_data, int size, int rctx,
  781. struct trace_event_call *call, u64 count,
  782. struct pt_regs *regs, struct hlist_head *head,
  783. struct task_struct *task);
  784. static inline void
  785. perf_trace_buf_submit(void *raw_data, int size, int rctx, u16 type,
  786. u64 count, struct pt_regs *regs, void *head,
  787. struct task_struct *task)
  788. {
  789. perf_tp_event(type, count, raw_data, size, regs, head, rctx, task);
  790. }
  791. #endif
  792. #define TRACE_EVENT_STR_MAX 512
  793. /*
  794. * gcc warns that you can not use a va_list in an inlined
  795. * function. But lets me make it into a macro :-/
  796. */
  797. #define __trace_event_vstr_len(fmt, va) \
  798. ({ \
  799. va_list __ap; \
  800. int __ret; \
  801. \
  802. va_copy(__ap, *(va)); \
  803. __ret = vsnprintf(NULL, 0, fmt, __ap) + 1; \
  804. va_end(__ap); \
  805. \
  806. min(__ret, TRACE_EVENT_STR_MAX); \
  807. })
  808. #endif /* _LINUX_TRACE_EVENT_H */
  809. /*
  810. * Note: we keep the TRACE_CUSTOM_EVENT outside the include file ifdef protection.
  811. * This is due to the way trace custom events work. If a file includes two
  812. * trace event headers under one "CREATE_CUSTOM_TRACE_EVENTS" the first include
  813. * will override the TRACE_CUSTOM_EVENT and break the second include.
  814. */
  815. #ifndef TRACE_CUSTOM_EVENT
  816. #define DECLARE_CUSTOM_EVENT_CLASS(name, proto, args, tstruct, assign, print)
  817. #define DEFINE_CUSTOM_EVENT(template, name, proto, args)
  818. #define TRACE_CUSTOM_EVENT(name, proto, args, struct, assign, print)
  819. #endif /* ifdef TRACE_CUSTOM_EVENT (see note above) */