ftrace_event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. #ifndef _LINUX_FTRACE_EVENT_H
  2. #define _LINUX_FTRACE_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. struct trace_array;
  9. struct trace_buffer;
  10. struct tracer;
  11. struct dentry;
  12. struct trace_print_flags {
  13. unsigned long mask;
  14. const char *name;
  15. };
  16. struct trace_print_flags_u64 {
  17. unsigned long long mask;
  18. const char *name;
  19. };
  20. const char *ftrace_print_flags_seq(struct trace_seq *p, const char *delim,
  21. unsigned long flags,
  22. const struct trace_print_flags *flag_array);
  23. const char *ftrace_print_symbols_seq(struct trace_seq *p, unsigned long val,
  24. const struct trace_print_flags *symbol_array);
  25. #if BITS_PER_LONG == 32
  26. const char *ftrace_print_symbols_seq_u64(struct trace_seq *p,
  27. unsigned long long val,
  28. const struct trace_print_flags_u64
  29. *symbol_array);
  30. #endif
  31. const char *ftrace_print_hex_seq(struct trace_seq *p,
  32. const unsigned char *buf, int len);
  33. struct trace_iterator;
  34. struct trace_event;
  35. int ftrace_raw_output_prep(struct trace_iterator *iter,
  36. struct trace_event *event);
  37. /*
  38. * The trace entry - the most basic unit of tracing. This is what
  39. * is printed in the end as a single line in the trace output, such as:
  40. *
  41. * bash-15816 [01] 235.197585: idle_cpu <- irq_enter
  42. */
  43. struct trace_entry {
  44. unsigned short type;
  45. unsigned char flags;
  46. unsigned char preempt_count;
  47. int pid;
  48. };
  49. #define FTRACE_MAX_EVENT \
  50. ((1 << (sizeof(((struct trace_entry *)0)->type) * 8)) - 1)
  51. /*
  52. * Trace iterator - used by printout routines who present trace
  53. * results to users and which routines might sleep, etc:
  54. */
  55. struct trace_iterator {
  56. struct trace_array *tr;
  57. struct tracer *trace;
  58. struct trace_buffer *trace_buffer;
  59. void *private;
  60. int cpu_file;
  61. struct mutex mutex;
  62. struct ring_buffer_iter **buffer_iter;
  63. unsigned long iter_flags;
  64. /* trace_seq for __print_flags() and __print_symbolic() etc. */
  65. struct trace_seq tmp_seq;
  66. cpumask_var_t started;
  67. /* it's true when current open file is snapshot */
  68. bool snapshot;
  69. /* The below is zeroed out in pipe_read */
  70. struct trace_seq seq;
  71. struct trace_entry *ent;
  72. unsigned long lost_events;
  73. int leftover;
  74. int ent_size;
  75. int cpu;
  76. u64 ts;
  77. loff_t pos;
  78. long idx;
  79. /* All new field here will be zeroed out in pipe_read */
  80. };
  81. enum trace_iter_flags {
  82. TRACE_FILE_LAT_FMT = 1,
  83. TRACE_FILE_ANNOTATE = 2,
  84. TRACE_FILE_TIME_IN_NS = 4,
  85. };
  86. typedef enum print_line_t (*trace_print_func)(struct trace_iterator *iter,
  87. int flags, struct trace_event *event);
  88. struct trace_event_functions {
  89. trace_print_func trace;
  90. trace_print_func raw;
  91. trace_print_func hex;
  92. trace_print_func binary;
  93. };
  94. struct trace_event {
  95. struct hlist_node node;
  96. struct list_head list;
  97. int type;
  98. struct trace_event_functions *funcs;
  99. };
  100. extern int register_ftrace_event(struct trace_event *event);
  101. extern int unregister_ftrace_event(struct trace_event *event);
  102. /* Return values for print_line callback */
  103. enum print_line_t {
  104. TRACE_TYPE_PARTIAL_LINE = 0, /* Retry after flushing the seq */
  105. TRACE_TYPE_HANDLED = 1,
  106. TRACE_TYPE_UNHANDLED = 2, /* Relay to other output functions */
  107. TRACE_TYPE_NO_CONSUME = 3 /* Handled but ask to not consume */
  108. };
  109. void tracing_generic_entry_update(struct trace_entry *entry,
  110. unsigned long flags,
  111. int pc);
  112. struct ftrace_event_file;
  113. struct ring_buffer_event *
  114. trace_event_buffer_lock_reserve(struct ring_buffer **current_buffer,
  115. struct ftrace_event_file *ftrace_file,
  116. int type, unsigned long len,
  117. unsigned long flags, int pc);
  118. struct ring_buffer_event *
  119. trace_current_buffer_lock_reserve(struct ring_buffer **current_buffer,
  120. int type, unsigned long len,
  121. unsigned long flags, int pc);
  122. void trace_current_buffer_unlock_commit(struct ring_buffer *buffer,
  123. struct ring_buffer_event *event,
  124. unsigned long flags, int pc);
  125. void trace_buffer_unlock_commit(struct ring_buffer *buffer,
  126. struct ring_buffer_event *event,
  127. unsigned long flags, int pc);
  128. void trace_buffer_unlock_commit_regs(struct ring_buffer *buffer,
  129. struct ring_buffer_event *event,
  130. unsigned long flags, int pc,
  131. struct pt_regs *regs);
  132. void trace_current_buffer_discard_commit(struct ring_buffer *buffer,
  133. struct ring_buffer_event *event);
  134. void tracing_record_cmdline(struct task_struct *tsk);
  135. struct event_filter;
  136. enum trace_reg {
  137. TRACE_REG_REGISTER,
  138. TRACE_REG_UNREGISTER,
  139. #ifdef CONFIG_PERF_EVENTS
  140. TRACE_REG_PERF_REGISTER,
  141. TRACE_REG_PERF_UNREGISTER,
  142. TRACE_REG_PERF_OPEN,
  143. TRACE_REG_PERF_CLOSE,
  144. TRACE_REG_PERF_ADD,
  145. TRACE_REG_PERF_DEL,
  146. #endif
  147. };
  148. struct ftrace_event_call;
  149. struct ftrace_event_class {
  150. char *system;
  151. void *probe;
  152. #ifdef CONFIG_PERF_EVENTS
  153. void *perf_probe;
  154. #endif
  155. int (*reg)(struct ftrace_event_call *event,
  156. enum trace_reg type, void *data);
  157. int (*define_fields)(struct ftrace_event_call *);
  158. struct list_head *(*get_fields)(struct ftrace_event_call *);
  159. struct list_head fields;
  160. int (*raw_init)(struct ftrace_event_call *);
  161. };
  162. extern int ftrace_event_reg(struct ftrace_event_call *event,
  163. enum trace_reg type, void *data);
  164. enum {
  165. TRACE_EVENT_FL_FILTERED_BIT,
  166. TRACE_EVENT_FL_CAP_ANY_BIT,
  167. TRACE_EVENT_FL_NO_SET_FILTER_BIT,
  168. TRACE_EVENT_FL_IGNORE_ENABLE_BIT,
  169. TRACE_EVENT_FL_WAS_ENABLED_BIT,
  170. TRACE_EVENT_FL_USE_CALL_FILTER_BIT,
  171. };
  172. /*
  173. * Event flags:
  174. * FILTERED - The event has a filter attached
  175. * CAP_ANY - Any user can enable for perf
  176. * NO_SET_FILTER - Set when filter has error and is to be ignored
  177. * IGNORE_ENABLE - For ftrace internal events, do not enable with debugfs file
  178. * WAS_ENABLED - Set and stays set when an event was ever enabled
  179. * (used for module unloading, if a module event is enabled,
  180. * it is best to clear the buffers that used it).
  181. * USE_CALL_FILTER - For ftrace internal events, don't use file filter
  182. */
  183. enum {
  184. TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
  185. TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
  186. TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
  187. TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
  188. TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
  189. TRACE_EVENT_FL_USE_CALL_FILTER = (1 << TRACE_EVENT_FL_USE_CALL_FILTER_BIT),
  190. };
  191. struct ftrace_event_call {
  192. struct list_head list;
  193. struct ftrace_event_class *class;
  194. char *name;
  195. struct trace_event event;
  196. const char *print_fmt;
  197. struct event_filter *filter;
  198. struct list_head *files;
  199. void *mod;
  200. void *data;
  201. /*
  202. * bit 0: filter_active
  203. * bit 1: allow trace by non root (cap any)
  204. * bit 2: failed to apply filter
  205. * bit 3: ftrace internal event (do not enable)
  206. * bit 4: Event was enabled by module
  207. * bit 5: use call filter rather than file filter
  208. */
  209. int flags; /* static flags of different events */
  210. #ifdef CONFIG_PERF_EVENTS
  211. int perf_refcount;
  212. struct hlist_head __percpu *perf_events;
  213. int (*perf_perm)(struct ftrace_event_call *,
  214. struct perf_event *);
  215. #endif
  216. };
  217. struct trace_array;
  218. struct ftrace_subsystem_dir;
  219. enum {
  220. FTRACE_EVENT_FL_ENABLED_BIT,
  221. FTRACE_EVENT_FL_RECORDED_CMD_BIT,
  222. FTRACE_EVENT_FL_FILTERED_BIT,
  223. FTRACE_EVENT_FL_NO_SET_FILTER_BIT,
  224. FTRACE_EVENT_FL_SOFT_MODE_BIT,
  225. FTRACE_EVENT_FL_SOFT_DISABLED_BIT,
  226. FTRACE_EVENT_FL_TRIGGER_MODE_BIT,
  227. };
  228. /*
  229. * Ftrace event file flags:
  230. * ENABLED - The event is enabled
  231. * RECORDED_CMD - The comms should be recorded at sched_switch
  232. * FILTERED - The event has a filter attached
  233. * NO_SET_FILTER - Set when filter has error and is to be ignored
  234. * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
  235. * SOFT_DISABLED - When set, do not trace the event (even though its
  236. * tracepoint may be enabled)
  237. * TRIGGER_MODE - When set, invoke the triggers associated with the event
  238. */
  239. enum {
  240. FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT),
  241. FTRACE_EVENT_FL_RECORDED_CMD = (1 << FTRACE_EVENT_FL_RECORDED_CMD_BIT),
  242. FTRACE_EVENT_FL_FILTERED = (1 << FTRACE_EVENT_FL_FILTERED_BIT),
  243. FTRACE_EVENT_FL_NO_SET_FILTER = (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT),
  244. FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
  245. FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
  246. FTRACE_EVENT_FL_TRIGGER_MODE = (1 << FTRACE_EVENT_FL_TRIGGER_MODE_BIT),
  247. };
  248. struct ftrace_event_file {
  249. struct list_head list;
  250. struct ftrace_event_call *event_call;
  251. struct event_filter *filter;
  252. struct dentry *dir;
  253. struct trace_array *tr;
  254. struct ftrace_subsystem_dir *system;
  255. struct list_head triggers;
  256. /*
  257. * 32 bit flags:
  258. * bit 0: enabled
  259. * bit 1: enabled cmd record
  260. * bit 2: enable/disable with the soft disable bit
  261. * bit 3: soft disabled
  262. * bit 4: trigger enabled
  263. *
  264. * Note: The bits must be set atomically to prevent races
  265. * from other writers. Reads of flags do not need to be in
  266. * sync as they occur in critical sections. But the way flags
  267. * is currently used, these changes do not affect the code
  268. * except that when a change is made, it may have a slight
  269. * delay in propagating the changes to other CPUs due to
  270. * caching and such. Which is mostly OK ;-)
  271. */
  272. unsigned long flags;
  273. atomic_t sm_ref; /* soft-mode reference counter */
  274. atomic_t tm_ref; /* trigger-mode reference counter */
  275. };
  276. #define __TRACE_EVENT_FLAGS(name, value) \
  277. static int __init trace_init_flags_##name(void) \
  278. { \
  279. event_##name.flags = value; \
  280. return 0; \
  281. } \
  282. early_initcall(trace_init_flags_##name);
  283. #define __TRACE_EVENT_PERF_PERM(name, expr...) \
  284. static int perf_perm_##name(struct ftrace_event_call *tp_event, \
  285. struct perf_event *p_event) \
  286. { \
  287. return ({ expr; }); \
  288. } \
  289. static int __init trace_init_perf_perm_##name(void) \
  290. { \
  291. event_##name.perf_perm = &perf_perm_##name; \
  292. return 0; \
  293. } \
  294. early_initcall(trace_init_perf_perm_##name);
  295. #define PERF_MAX_TRACE_SIZE 2048
  296. #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
  297. enum event_trigger_type {
  298. ETT_NONE = (0),
  299. ETT_TRACE_ONOFF = (1 << 0),
  300. ETT_SNAPSHOT = (1 << 1),
  301. ETT_STACKTRACE = (1 << 2),
  302. };
  303. extern void destroy_preds(struct ftrace_event_file *file);
  304. extern void destroy_call_preds(struct ftrace_event_call *call);
  305. extern int filter_match_preds(struct event_filter *filter, void *rec);
  306. extern int filter_check_discard(struct ftrace_event_file *file, void *rec,
  307. struct ring_buffer *buffer,
  308. struct ring_buffer_event *event);
  309. extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
  310. struct ring_buffer *buffer,
  311. struct ring_buffer_event *event);
  312. extern void event_triggers_call(struct ftrace_event_file *file);
  313. enum {
  314. FILTER_OTHER = 0,
  315. FILTER_STATIC_STRING,
  316. FILTER_DYN_STRING,
  317. FILTER_PTR_STRING,
  318. FILTER_TRACE_FN,
  319. };
  320. #define EVENT_STORAGE_SIZE 128
  321. extern struct mutex event_storage_mutex;
  322. extern char event_storage[EVENT_STORAGE_SIZE];
  323. extern int trace_event_raw_init(struct ftrace_event_call *call);
  324. extern int trace_define_field(struct ftrace_event_call *call, const char *type,
  325. const char *name, int offset, int size,
  326. int is_signed, int filter_type);
  327. extern int trace_add_event_call(struct ftrace_event_call *call);
  328. extern int trace_remove_event_call(struct ftrace_event_call *call);
  329. #define is_signed_type(type) (((type)(-1)) < (type)1)
  330. int trace_set_clr_event(const char *system, const char *event, int set);
  331. /*
  332. * The double __builtin_constant_p is because gcc will give us an error
  333. * if we try to allocate the static variable to fmt if it is not a
  334. * constant. Even with the outer if statement optimizing out.
  335. */
  336. #define event_trace_printk(ip, fmt, args...) \
  337. do { \
  338. __trace_printk_check_format(fmt, ##args); \
  339. tracing_record_cmdline(current); \
  340. if (__builtin_constant_p(fmt)) { \
  341. static const char *trace_printk_fmt \
  342. __attribute__((section("__trace_printk_fmt"))) = \
  343. __builtin_constant_p(fmt) ? fmt : NULL; \
  344. \
  345. __trace_bprintk(ip, trace_printk_fmt, ##args); \
  346. } else \
  347. __trace_printk(ip, fmt, ##args); \
  348. } while (0)
  349. /**
  350. * tracepoint_string - register constant persistent string to trace system
  351. * @str - a constant persistent string that will be referenced in tracepoints
  352. *
  353. * If constant strings are being used in tracepoints, it is faster and
  354. * more efficient to just save the pointer to the string and reference
  355. * that with a printf "%s" instead of saving the string in the ring buffer
  356. * and wasting space and time.
  357. *
  358. * The problem with the above approach is that userspace tools that read
  359. * the binary output of the trace buffers do not have access to the string.
  360. * Instead they just show the address of the string which is not very
  361. * useful to users.
  362. *
  363. * With tracepoint_string(), the string will be registered to the tracing
  364. * system and exported to userspace via the debugfs/tracing/printk_formats
  365. * file that maps the string address to the string text. This way userspace
  366. * tools that read the binary buffers have a way to map the pointers to
  367. * the ASCII strings they represent.
  368. *
  369. * The @str used must be a constant string and persistent as it would not
  370. * make sense to show a string that no longer exists. But it is still fine
  371. * to be used with modules, because when modules are unloaded, if they
  372. * had tracepoints, the ring buffers are cleared too. As long as the string
  373. * does not change during the life of the module, it is fine to use
  374. * tracepoint_string() within a module.
  375. */
  376. #define tracepoint_string(str) \
  377. ({ \
  378. static const char *___tp_str __tracepoint_string = str; \
  379. ___tp_str; \
  380. })
  381. #define __tracepoint_string __attribute__((section("__tracepoint_str")))
  382. #ifdef CONFIG_PERF_EVENTS
  383. struct perf_event;
  384. DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
  385. extern int perf_trace_init(struct perf_event *event);
  386. extern void perf_trace_destroy(struct perf_event *event);
  387. extern int perf_trace_add(struct perf_event *event, int flags);
  388. extern void perf_trace_del(struct perf_event *event, int flags);
  389. extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  390. char *filter_str);
  391. extern void ftrace_profile_free_filter(struct perf_event *event);
  392. extern void *perf_trace_buf_prepare(int size, unsigned short type,
  393. struct pt_regs *regs, int *rctxp);
  394. static inline void
  395. perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
  396. u64 count, struct pt_regs *regs, void *head,
  397. struct task_struct *task)
  398. {
  399. perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
  400. }
  401. #endif
  402. #endif /* _LINUX_FTRACE_EVENT_H */