ftrace_event.h 13 KB

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