ftrace_event.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. };
  171. /*
  172. * Event flags:
  173. * FILTERED - The event has a filter attached
  174. * CAP_ANY - Any user can enable for perf
  175. * NO_SET_FILTER - Set when filter has error and is to be ignored
  176. * IGNORE_ENABLE - For ftrace internal events, do not enable with debugfs file
  177. * WAS_ENABLED - Set and stays set when an event was ever enabled
  178. * (used for module unloading, if a module event is enabled,
  179. * it is best to clear the buffers that used it).
  180. */
  181. enum {
  182. TRACE_EVENT_FL_FILTERED = (1 << TRACE_EVENT_FL_FILTERED_BIT),
  183. TRACE_EVENT_FL_CAP_ANY = (1 << TRACE_EVENT_FL_CAP_ANY_BIT),
  184. TRACE_EVENT_FL_NO_SET_FILTER = (1 << TRACE_EVENT_FL_NO_SET_FILTER_BIT),
  185. TRACE_EVENT_FL_IGNORE_ENABLE = (1 << TRACE_EVENT_FL_IGNORE_ENABLE_BIT),
  186. TRACE_EVENT_FL_WAS_ENABLED = (1 << TRACE_EVENT_FL_WAS_ENABLED_BIT),
  187. };
  188. struct ftrace_event_call {
  189. struct list_head list;
  190. struct ftrace_event_class *class;
  191. char *name;
  192. struct trace_event event;
  193. const char *print_fmt;
  194. struct event_filter *filter;
  195. struct list_head *files;
  196. void *mod;
  197. void *data;
  198. /*
  199. * bit 0: filter_active
  200. * bit 1: allow trace by non root (cap any)
  201. * bit 2: failed to apply filter
  202. * bit 3: ftrace internal event (do not enable)
  203. * bit 4: Event was enabled by module
  204. */
  205. int flags; /* static flags of different events */
  206. #ifdef CONFIG_PERF_EVENTS
  207. int perf_refcount;
  208. struct hlist_head __percpu *perf_events;
  209. #endif
  210. };
  211. struct trace_array;
  212. struct ftrace_subsystem_dir;
  213. enum {
  214. FTRACE_EVENT_FL_ENABLED_BIT,
  215. FTRACE_EVENT_FL_RECORDED_CMD_BIT,
  216. FTRACE_EVENT_FL_SOFT_MODE_BIT,
  217. FTRACE_EVENT_FL_SOFT_DISABLED_BIT,
  218. };
  219. /*
  220. * Ftrace event file flags:
  221. * ENABLED - The event is enabled
  222. * RECORDED_CMD - The comms should be recorded at sched_switch
  223. * SOFT_MODE - The event is enabled/disabled by SOFT_DISABLED
  224. * SOFT_DISABLED - When set, do not trace the event (even though its
  225. * tracepoint may be enabled)
  226. */
  227. enum {
  228. FTRACE_EVENT_FL_ENABLED = (1 << FTRACE_EVENT_FL_ENABLED_BIT),
  229. FTRACE_EVENT_FL_RECORDED_CMD = (1 << FTRACE_EVENT_FL_RECORDED_CMD_BIT),
  230. FTRACE_EVENT_FL_SOFT_MODE = (1 << FTRACE_EVENT_FL_SOFT_MODE_BIT),
  231. FTRACE_EVENT_FL_SOFT_DISABLED = (1 << FTRACE_EVENT_FL_SOFT_DISABLED_BIT),
  232. };
  233. struct ftrace_event_file {
  234. struct list_head list;
  235. struct ftrace_event_call *event_call;
  236. struct dentry *dir;
  237. struct trace_array *tr;
  238. struct ftrace_subsystem_dir *system;
  239. /*
  240. * 32 bit flags:
  241. * bit 0: enabled
  242. * bit 1: enabled cmd record
  243. * bit 2: enable/disable with the soft disable bit
  244. * bit 3: soft disabled
  245. *
  246. * Note: The bits must be set atomically to prevent races
  247. * from other writers. Reads of flags do not need to be in
  248. * sync as they occur in critical sections. But the way flags
  249. * is currently used, these changes do not affect the code
  250. * except that when a change is made, it may have a slight
  251. * delay in propagating the changes to other CPUs due to
  252. * caching and such. Which is mostly OK ;-)
  253. */
  254. unsigned long flags;
  255. atomic_t sm_ref; /* soft-mode reference counter */
  256. };
  257. #define __TRACE_EVENT_FLAGS(name, value) \
  258. static int __init trace_init_flags_##name(void) \
  259. { \
  260. event_##name.flags = value; \
  261. return 0; \
  262. } \
  263. early_initcall(trace_init_flags_##name);
  264. #define PERF_MAX_TRACE_SIZE 2048
  265. #define MAX_FILTER_STR_VAL 256 /* Should handle KSYM_SYMBOL_LEN */
  266. extern void destroy_preds(struct ftrace_event_call *call);
  267. extern int filter_match_preds(struct event_filter *filter, void *rec);
  268. extern int filter_current_check_discard(struct ring_buffer *buffer,
  269. struct ftrace_event_call *call,
  270. void *rec,
  271. struct ring_buffer_event *event);
  272. enum {
  273. FILTER_OTHER = 0,
  274. FILTER_STATIC_STRING,
  275. FILTER_DYN_STRING,
  276. FILTER_PTR_STRING,
  277. FILTER_TRACE_FN,
  278. };
  279. #define EVENT_STORAGE_SIZE 128
  280. extern struct mutex event_storage_mutex;
  281. extern char event_storage[EVENT_STORAGE_SIZE];
  282. extern int trace_event_raw_init(struct ftrace_event_call *call);
  283. extern int trace_define_field(struct ftrace_event_call *call, const char *type,
  284. const char *name, int offset, int size,
  285. int is_signed, int filter_type);
  286. extern int trace_add_event_call(struct ftrace_event_call *call);
  287. extern int trace_remove_event_call(struct ftrace_event_call *call);
  288. #define is_signed_type(type) (((type)(-1)) < (type)1)
  289. int trace_set_clr_event(const char *system, const char *event, int set);
  290. /*
  291. * The double __builtin_constant_p is because gcc will give us an error
  292. * if we try to allocate the static variable to fmt if it is not a
  293. * constant. Even with the outer if statement optimizing out.
  294. */
  295. #define event_trace_printk(ip, fmt, args...) \
  296. do { \
  297. __trace_printk_check_format(fmt, ##args); \
  298. tracing_record_cmdline(current); \
  299. if (__builtin_constant_p(fmt)) { \
  300. static const char *trace_printk_fmt \
  301. __attribute__((section("__trace_printk_fmt"))) = \
  302. __builtin_constant_p(fmt) ? fmt : NULL; \
  303. \
  304. __trace_bprintk(ip, trace_printk_fmt, ##args); \
  305. } else \
  306. __trace_printk(ip, fmt, ##args); \
  307. } while (0)
  308. /**
  309. * tracepoint_string - register constant persistent string to trace system
  310. * @str - a constant persistent string that will be referenced in tracepoints
  311. *
  312. * If constant strings are being used in tracepoints, it is faster and
  313. * more efficient to just save the pointer to the string and reference
  314. * that with a printf "%s" instead of saving the string in the ring buffer
  315. * and wasting space and time.
  316. *
  317. * The problem with the above approach is that userspace tools that read
  318. * the binary output of the trace buffers do not have access to the string.
  319. * Instead they just show the address of the string which is not very
  320. * useful to users.
  321. *
  322. * With tracepoint_string(), the string will be registered to the tracing
  323. * system and exported to userspace via the debugfs/tracing/printk_formats
  324. * file that maps the string address to the string text. This way userspace
  325. * tools that read the binary buffers have a way to map the pointers to
  326. * the ASCII strings they represent.
  327. *
  328. * The @str used must be a constant string and persistent as it would not
  329. * make sense to show a string that no longer exists. But it is still fine
  330. * to be used with modules, because when modules are unloaded, if they
  331. * had tracepoints, the ring buffers are cleared too. As long as the string
  332. * does not change during the life of the module, it is fine to use
  333. * tracepoint_string() within a module.
  334. */
  335. #define tracepoint_string(str) \
  336. ({ \
  337. static const char *___tp_str __tracepoint_string = str; \
  338. ___tp_str; \
  339. })
  340. #define __tracepoint_string __attribute__((section("__tracepoint_str")))
  341. #ifdef CONFIG_PERF_EVENTS
  342. struct perf_event;
  343. DECLARE_PER_CPU(struct pt_regs, perf_trace_regs);
  344. extern int perf_trace_init(struct perf_event *event);
  345. extern void perf_trace_destroy(struct perf_event *event);
  346. extern int perf_trace_add(struct perf_event *event, int flags);
  347. extern void perf_trace_del(struct perf_event *event, int flags);
  348. extern int ftrace_profile_set_filter(struct perf_event *event, int event_id,
  349. char *filter_str);
  350. extern void ftrace_profile_free_filter(struct perf_event *event);
  351. extern void *perf_trace_buf_prepare(int size, unsigned short type,
  352. struct pt_regs *regs, int *rctxp);
  353. static inline void
  354. perf_trace_buf_submit(void *raw_data, int size, int rctx, u64 addr,
  355. u64 count, struct pt_regs *regs, void *head,
  356. struct task_struct *task)
  357. {
  358. perf_tp_event(addr, count, raw_data, size, regs, head, rctx, task);
  359. }
  360. #endif
  361. #endif /* _LINUX_FTRACE_EVENT_H */