ftrace_event.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. };
  227. /*
  228. * Ftrace event file flags:
  229. * ENABLED - The event is enabled
  230. * RECORDED_CMD - The comms should be recorded at sched_switch
  231. * FILTERED - The event has a filter attached
  232. * NO_SET_FILTER - Set when filter has error and is to be ignored
  233. * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
  234. * SOFT_DISABLED - When set, do not trace the event (even though its
  235. * tracepoint may be enabled)
  236. */
  237. enum {
  238. FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT),
  239. FTRACE_EVENT_FL_RECORDED_CMD = (1 << FTRACE_EVENT_FL_RECORDED_CMD_BIT),
  240. FTRACE_EVENT_FL_FILTERED = (1 << FTRACE_EVENT_FL_FILTERED_BIT),
  241. FTRACE_EVENT_FL_NO_SET_FILTER = (1 << FTRACE_EVENT_FL_NO_SET_FILTER_BIT),
  242. FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
  243. FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
  244. };
  245. struct ftrace_event_file {
  246. struct list_head list;
  247. struct ftrace_event_call *event_call;
  248. struct event_filter *filter;
  249. struct dentry *dir;
  250. struct trace_array *tr;
  251. struct ftrace_subsystem_dir *system;
  252. /*
  253. * 32 bit flags:
  254. * bit 0: enabled
  255. * bit 1: enabled cmd record
  256. * bit 2: enable/disable with the soft disable bit
  257. * bit 3: soft disabled
  258. *
  259. * Note: The bits must be set atomically to prevent races
  260. * from other writers. Reads of flags do not need to be in
  261. * sync as they occur in critical sections. But the way flags
  262. * is currently used, these changes do not affect the code
  263. * except that when a change is made, it may have a slight
  264. * delay in propagating the changes to other CPUs due to
  265. * caching and such. Which is mostly OK ;-)
  266. */
  267. unsigned long flags;
  268. atomic_t sm_ref; /* soft-mode reference counter */
  269. };
  270. #define __TRACE_EVENT_FLAGS(name, value) \
  271. static int __init trace_init_flags_##name(void) \
  272. { \
  273. event_##name.flags = value; \
  274. return 0; \
  275. } \
  276. early_initcall(trace_init_flags_##name);
  277. #define __TRACE_EVENT_PERF_PERM(name, expr...) \
  278. static int perf_perm_##name(struct ftrace_event_call *tp_event, \
  279. struct perf_event *p_event) \
  280. { \
  281. return ({ expr; }); \
  282. } \
  283. static int __init trace_init_perf_perm_##name(void) \
  284. { \
  285. event_##name.perf_perm = &perf_perm_##name; \
  286. return 0; \
  287. } \
  288. early_initcall(trace_init_perf_perm_##name);
  289. #define PERF_MAX_TRACE_SIZE 2048
  290. #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
  291. extern void destroy_preds(struct ftrace_event_file *file);
  292. extern void destroy_call_preds(struct ftrace_event_call *call);
  293. extern int filter_match_preds(struct event_filter *filter, void *rec);
  294. extern int filter_check_discard(struct ftrace_event_file *file, void *rec,
  295. struct ring_buffer *buffer,
  296. struct ring_buffer_event *event);
  297. extern int call_filter_check_discard(struct ftrace_event_call *call, void *rec,
  298. struct ring_buffer *buffer,
  299. struct ring_buffer_event *event);
  300. enum {
  301. FILTER_OTHER = 0,
  302. FILTER_STATIC_STRING,
  303. FILTER_DYN_STRING,
  304. FILTER_PTR_STRING,
  305. FILTER_TRACE_FN,
  306. };
  307. #define EVENT_STORAGE_SIZE 128
  308. extern struct mutex event_storage_mutex;
  309. extern char event_storage[EVENT_STORAGE_SIZE];
  310. extern int trace_event_raw_init(struct ftrace_event_call *call);
  311. extern int trace_define_field(struct ftrace_event_call *call, const char *type,
  312. const char *name, int offset, int size,
  313. int is_signed, int filter_type);
  314. extern int trace_add_event_call(struct ftrace_event_call *call);
  315. extern int trace_remove_event_call(struct ftrace_event_call *call);
  316. #define is_signed_type(type) (((type)(-1)) < (type)1)
  317. int trace_set_clr_event(const char *system, const char *event, int set);
  318. /*
  319. * The double __builtin_constant_p is because gcc will give us an error
  320. * if we try to allocate the static variable to fmt if it is not a
  321. * constant. Even with the outer if statement optimizing out.
  322. */
  323. #define event_trace_printk(ip, fmt, args...) \
  324. do { \
  325. __trace_printk_check_format(fmt, ##args); \
  326. tracing_record_cmdline(current); \
  327. if (__builtin_constant_p(fmt)) { \
  328. static const char *trace_printk_fmt \
  329. __attribute__((section("__trace_printk_fmt"))) = \
  330. __builtin_constant_p(fmt) ? fmt : NULL; \
  331. \
  332. __trace_bprintk(ip, trace_printk_fmt, ##args); \
  333. } else \
  334. __trace_printk(ip, fmt, ##args); \
  335. } while (0)
  336. /**
  337. * tracepoint_string - register constant persistent string to trace system
  338. * @str - a constant persistent string that will be referenced in tracepoints
  339. *
  340. * If constant strings are being used in tracepoints, it is faster and
  341. * more efficient to just save the pointer to the string and reference
  342. * that with a printf "%s" instead of saving the string in the ring buffer
  343. * and wasting space and time.
  344. *
  345. * The problem with the above approach is that userspace tools that read
  346. * the binary output of the trace buffers do not have access to the string.
  347. * Instead they just show the address of the string which is not very
  348. * useful to users.
  349. *
  350. * With tracepoint_string(), the string will be registered to the tracing
  351. * system and exported to userspace via the debugfs/tracing/printk_formats
  352. * file that maps the string address to the string text. This way userspace
  353. * tools that read the binary buffers have a way to map the pointers to
  354. * the ASCII strings they represent.
  355. *
  356. * The @str used must be a constant string and persistent as it would not
  357. * make sense to show a string that no longer exists. But it is still fine
  358. * to be used with modules, because when modules are unloaded, if they
  359. * had tracepoints, the ring buffers are cleared too. As long as the string
  360. * does not change during the life of the module, it is fine to use
  361. * tracepoint_string() within a module.
  362. */
  363. #define tracepoint_string(str) \
  364. ({ \
  365. static const char *___tp_str __tracepoint_string = str; \
  366. ___tp_str; \
  367. })
  368. #define __tracepoint_string __attribute__((section("__tracepoint_str")))
  369. #ifdef CONFIG_PERF_EVENTS
  370. struct perf_event;
  371. DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
  372. extern int perf_trace_init(struct perf_event *event);
  373. extern void perf_trace_destroy(struct perf_event *event);
  374. extern int perf_trace_add(struct perf_event *event, int flags);
  375. extern void perf_trace_del(struct perf_event *event, int flags);
  376. extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  377. char *filter_str);
  378. extern void ftrace_profile_free_filter(struct perf_event *event);
  379. extern void *perf_trace_buf_prepare(int size, unsigned short type,
  380. struct pt_regs *regs, int *rctxp);
  381. static inline void
  382. perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
  383. u64 count, struct pt_regs *regs, void *head,
  384. struct task_struct *task)
  385. {
  386. perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
  387. }
  388. #endif
  389. #endif /* _LINUX_FTRACE_EVENT_H */