perf_counter, ftrace: Fix perf_counter integration
[deliverable/linux.git] / include / trace / ftrace.h
CommitLineData
f42c85e7
SR
1/*
2 * Stage 1 of the trace events.
3 *
4 * Override the macros in <trace/trace_events.h> to include the following:
5 *
6 * struct ftrace_raw_<call> {
7 * struct trace_entry ent;
8 * <type> <item>;
9 * <type2> <item2>[<len>];
10 * [...]
11 * };
12 *
13 * The <type> <item> is created by the __field(type, item) macro or
14 * the __array(type2, item2, len) macro.
15 * We simply do "type item;", and that will create the fields
16 * in the structure.
17 */
18
19#include <linux/ftrace_event.h>
20
7fcb7c47
LZ
21#undef __field
22#define __field(type, item) type item;
23
f42c85e7
SR
24#undef __array
25#define __array(type, item, len) type item[len];
26
7fcb7c47
LZ
27#undef __dynamic_array
28#define __dynamic_array(type, item, len) unsigned short __data_loc_##item;
f42c85e7 29
9cbf1176 30#undef __string
7fcb7c47 31#define __string(item, src) __dynamic_array(char, item, -1)
9cbf1176 32
f42c85e7
SR
33#undef TP_STRUCT__entry
34#define TP_STRUCT__entry(args...) args
35
36#undef TRACE_EVENT
37#define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
38 struct ftrace_raw_##name { \
39 struct trace_entry ent; \
40 tstruct \
7fcb7c47 41 char __data[0]; \
f42c85e7
SR
42 }; \
43 static struct ftrace_event_call event_##name
44
45#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
46
9cbf1176 47
f42c85e7
SR
48/*
49 * Stage 2 of the trace events.
50 *
9cbf1176
FW
51 * Include the following:
52 *
7fcb7c47
LZ
53 * struct ftrace_data_offsets_<call> {
54 * int <item1>;
55 * int <item2>;
9cbf1176
FW
56 * [...]
57 * };
58 *
7fcb7c47
LZ
59 * The __dynamic_array() macro will create each int <item>, this is
60 * to keep the offset of each array from the beginning of the event.
9cbf1176
FW
61 */
62
7fcb7c47
LZ
63#undef __field
64#define __field(type, item);
65
9cbf1176
FW
66#undef __array
67#define __array(type, item, len)
68
7fcb7c47
LZ
69#undef __dynamic_array
70#define __dynamic_array(type, item, len) int item;
9cbf1176
FW
71
72#undef __string
7fcb7c47 73#define __string(item, src) __dynamic_array(char, item, -1)
9cbf1176
FW
74
75#undef TRACE_EVENT
76#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
7fcb7c47 77 struct ftrace_data_offsets_##call { \
9cbf1176
FW
78 tstruct; \
79 };
80
81#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
82
6ff9a64d
SR
83/*
84 * Setup the showing format of trace point.
85 *
86 * int
87 * ftrace_format_##call(struct trace_seq *s)
88 * {
89 * struct ftrace_raw_##call field;
90 * int ret;
91 *
92 * ret = trace_seq_printf(s, #type " " #item ";"
93 * " offset:%u; size:%u;\n",
94 * offsetof(struct ftrace_raw_##call, item),
95 * sizeof(field.type));
96 *
97 * }
98 */
99
100#undef TP_STRUCT__entry
101#define TP_STRUCT__entry(args...) args
102
103#undef __field
104#define __field(type, item) \
105 ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \
106 "offset:%u;\tsize:%u;\n", \
107 (unsigned int)offsetof(typeof(field), item), \
108 (unsigned int)sizeof(field.item)); \
109 if (!ret) \
110 return 0;
111
112#undef __array
113#define __array(type, item, len) \
114 ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \
115 "offset:%u;\tsize:%u;\n", \
116 (unsigned int)offsetof(typeof(field), item), \
117 (unsigned int)sizeof(field.item)); \
118 if (!ret) \
119 return 0;
120
121#undef __dynamic_array
122#define __dynamic_array(type, item, len) \
123 ret = trace_seq_printf(s, "\tfield:__data_loc " #item ";\t" \
124 "offset:%u;\tsize:%u;\n", \
125 (unsigned int)offsetof(typeof(field), \
126 __data_loc_##item), \
127 (unsigned int)sizeof(field.__data_loc_##item)); \
128 if (!ret) \
129 return 0;
130
131#undef __string
132#define __string(item, src) __dynamic_array(char, item, -1)
133
134#undef __entry
135#define __entry REC
136
137#undef __print_symbolic
138#undef __get_dynamic_array
139#undef __get_str
140
141#undef TP_printk
142#define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args)
143
144#undef TP_fast_assign
145#define TP_fast_assign(args...) args
146
3a659305
PZ
147#undef TP_perf_assign
148#define TP_perf_assign(args...)
149
6ff9a64d
SR
150#undef TRACE_EVENT
151#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
152static int \
153ftrace_format_##call(struct trace_seq *s) \
154{ \
155 struct ftrace_raw_##call field __attribute__((unused)); \
156 int ret = 0; \
157 \
158 tstruct; \
159 \
160 trace_seq_printf(s, "\nprint fmt: " print); \
161 \
162 return ret; \
163}
164
165#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
166
9cbf1176
FW
167/*
168 * Stage 3 of the trace events.
169 *
f42c85e7
SR
170 * Override the macros in <trace/trace_events.h> to include the following:
171 *
172 * enum print_line_t
173 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
174 * {
175 * struct trace_seq *s = &iter->seq;
176 * struct ftrace_raw_<call> *field; <-- defined in stage 1
177 * struct trace_entry *entry;
be74b73a 178 * struct trace_seq *p;
f42c85e7
SR
179 * int ret;
180 *
181 * entry = iter->ent;
182 *
183 * if (entry->type != event_<call>.id) {
184 * WARN_ON_ONCE(1);
185 * return TRACE_TYPE_UNHANDLED;
186 * }
187 *
188 * field = (typeof(field))entry;
189 *
be74b73a 190 * p = get_cpu_var(ftrace_event_seq);
56d8bd3f 191 * trace_seq_init(p);
f42c85e7 192 * ret = trace_seq_printf(s, <TP_printk> "\n");
be74b73a 193 * put_cpu();
f42c85e7
SR
194 * if (!ret)
195 * return TRACE_TYPE_PARTIAL_LINE;
196 *
197 * return TRACE_TYPE_HANDLED;
198 * }
199 *
200 * This is the method used to print the raw event to the trace
201 * output format. Note, this is not needed if the data is read
202 * in binary.
203 */
204
205#undef __entry
206#define __entry field
207
208#undef TP_printk
209#define TP_printk(fmt, args...) fmt "\n", args
210
7fcb7c47
LZ
211#undef __get_dynamic_array
212#define __get_dynamic_array(field) \
213 ((void *)__entry + __entry->__data_loc_##field)
214
9cbf1176 215#undef __get_str
7fcb7c47 216#define __get_str(field) (char *)__get_dynamic_array(field)
9cbf1176 217
be74b73a
SR
218#undef __print_flags
219#define __print_flags(flag, delim, flag_array...) \
220 ({ \
221 static const struct trace_print_flags flags[] = \
222 { flag_array, { -1, NULL }}; \
223 ftrace_print_flags_seq(p, delim, flag, flags); \
224 })
225
0f4fc29d
SR
226#undef __print_symbolic
227#define __print_symbolic(value, symbol_array...) \
228 ({ \
229 static const struct trace_print_flags symbols[] = \
230 { symbol_array, { -1, NULL }}; \
231 ftrace_print_symbols_seq(p, value, symbols); \
232 })
233
f42c85e7
SR
234#undef TRACE_EVENT
235#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
236enum print_line_t \
237ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \
238{ \
239 struct trace_seq *s = &iter->seq; \
240 struct ftrace_raw_##call *field; \
241 struct trace_entry *entry; \
be74b73a 242 struct trace_seq *p; \
f42c85e7
SR
243 int ret; \
244 \
245 entry = iter->ent; \
246 \
247 if (entry->type != event_##call.id) { \
248 WARN_ON_ONCE(1); \
249 return TRACE_TYPE_UNHANDLED; \
250 } \
251 \
252 field = (typeof(field))entry; \
253 \
be74b73a 254 p = &get_cpu_var(ftrace_event_seq); \
56d8bd3f 255 trace_seq_init(p); \
f42c85e7 256 ret = trace_seq_printf(s, #call ": " print); \
be74b73a 257 put_cpu(); \
f42c85e7
SR
258 if (!ret) \
259 return TRACE_TYPE_PARTIAL_LINE; \
260 \
261 return TRACE_TYPE_HANDLED; \
262}
263
264#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
265
f42c85e7
SR
266#undef __field
267#define __field(type, item) \
268 ret = trace_define_field(event_call, #type, #item, \
269 offsetof(typeof(field), item), \
a118e4d1 270 sizeof(field.item), is_signed_type(type)); \
f42c85e7
SR
271 if (ret) \
272 return ret;
273
274#undef __array
275#define __array(type, item, len) \
276 BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \
277 ret = trace_define_field(event_call, #type "[" #len "]", #item, \
278 offsetof(typeof(field), item), \
a118e4d1 279 sizeof(field.item), 0); \
f42c85e7
SR
280 if (ret) \
281 return ret;
282
7fcb7c47
LZ
283#undef __dynamic_array
284#define __dynamic_array(type, item, len) \
285 ret = trace_define_field(event_call, "__data_loc" "[" #type "]", #item,\
286 offsetof(typeof(field), __data_loc_##item), \
287 sizeof(field.__data_loc_##item), 0);
288
9cbf1176 289#undef __string
7fcb7c47 290#define __string(item, src) __dynamic_array(char, item, -1)
9cbf1176 291
f42c85e7
SR
292#undef TRACE_EVENT
293#define TRACE_EVENT(call, proto, args, tstruct, func, print) \
294int \
295ftrace_define_fields_##call(void) \
296{ \
297 struct ftrace_raw_##call field; \
298 struct ftrace_event_call *event_call = &event_##call; \
299 int ret; \
300 \
a118e4d1
TZ
301 __common_field(int, type, 1); \
302 __common_field(unsigned char, flags, 0); \
303 __common_field(unsigned char, preempt_count, 0); \
304 __common_field(int, pid, 1); \
305 __common_field(int, tgid, 1); \
f42c85e7
SR
306 \
307 tstruct; \
308 \
309 return ret; \
310}
311
312#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
313
7fcb7c47
LZ
314/*
315 * remember the offset of each array from the beginning of the event.
316 */
317
318#undef __entry
319#define __entry entry
320
321#undef __field
322#define __field(type, item)
323
324#undef __array
325#define __array(type, item, len)
326
327#undef __dynamic_array
328#define __dynamic_array(type, item, len) \
329 __data_offsets->item = __data_size + \
330 offsetof(typeof(*entry), __data); \
331 __data_size += (len) * sizeof(type);
332
333#undef __string
334#define __string(item, src) __dynamic_array(char, item, strlen(src) + 1) \
335
336#undef TRACE_EVENT
337#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
338static inline int ftrace_get_offsets_##call( \
339 struct ftrace_data_offsets_##call *__data_offsets, proto) \
340{ \
341 int __data_size = 0; \
342 struct ftrace_raw_##call __maybe_unused *entry; \
343 \
344 tstruct; \
345 \
346 return __data_size; \
347}
348
349#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
350
3a659305
PZ
351#ifdef CONFIG_EVENT_PROFILE
352
353/*
354 * Generate the functions needed for tracepoint perf_counter support.
355 *
356 * static void ftrace_profile_<call>(proto)
357 * {
358 * extern void perf_tpcounter_event(int, u64, u64);
359 * u64 __addr = 0, __count = 1;
360 *
361 * <assign> <-- here we expand the TP_perf_assign() macro
362 *
363 * perf_tpcounter_event(event_<call>.id, __addr, __count);
364 * }
365 *
366 * static int ftrace_profile_enable_<call>(struct ftrace_event_call *event_call)
367 * {
368 * int ret = 0;
369 *
370 * if (!atomic_inc_return(&event_call->profile_count))
371 * ret = register_trace_<call>(ftrace_profile_<call>);
372 *
373 * return ret;
374 * }
375 *
376 * static void ftrace_profile_disable_<call>(struct ftrace_event_call *event_call)
377 * {
378 * if (atomic_add_negative(-1, &event->call->profile_count))
379 * unregister_trace_<call>(ftrace_profile_<call>);
380 * }
381 *
382 */
383
384#undef TP_fast_assign
385#define TP_fast_assign(args...)
386
387#undef TP_perf_assign
388#define TP_perf_assign(args...) args
389
390#undef __perf_addr
391#define __perf_addr(a) __addr = (a)
392
393#undef __perf_count
394#define __perf_count(c) __count = (c)
395
396#undef TRACE_EVENT
397#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
398 \
399static void ftrace_profile_##call(proto) \
400{ \
401 extern void perf_tpcounter_event(int, u64, u64); \
402 u64 __addr = 0, __count = 1; \
403 { assign; } \
404 perf_tpcounter_event(event_##call.id, __addr, __count); \
405} \
406 \
407static int ftrace_profile_enable_##call(struct ftrace_event_call *event_call) \
408{ \
409 int ret = 0; \
410 \
411 if (!atomic_inc_return(&event_call->profile_count)) \
412 ret = register_trace_##call(ftrace_profile_##call); \
413 \
414 return ret; \
415} \
416 \
417static void ftrace_profile_disable_##call(struct ftrace_event_call *event_call)\
418{ \
419 if (atomic_add_negative(-1, &event_call->profile_count)) \
420 unregister_trace_##call(ftrace_profile_##call); \
421}
422
423#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
424
425#undef TP_fast_assign
426#define TP_fast_assign(args...) args
427
428#undef TP_perf_assign
429#define TP_perf_assign(args...)
430
431#endif
432
c32e827b 433/*
9cbf1176 434 * Stage 4 of the trace events.
c32e827b 435 *
ea20d929 436 * Override the macros in <trace/trace_events.h> to include the following:
c32e827b
SR
437 *
438 * static void ftrace_event_<call>(proto)
439 * {
ef18012b 440 * event_trace_printk(_RET_IP_, "<call>: " <fmt>);
c32e827b
SR
441 * }
442 *
443 * static int ftrace_reg_event_<call>(void)
444 * {
ef18012b 445 * int ret;
c32e827b 446 *
ef18012b
SR
447 * ret = register_trace_<call>(ftrace_event_<call>);
448 * if (!ret)
449 * pr_info("event trace: Could not activate trace point "
450 * "probe to <call>");
451 * return ret;
c32e827b
SR
452 * }
453 *
454 * static void ftrace_unreg_event_<call>(void)
455 * {
ef18012b 456 * unregister_trace_<call>(ftrace_event_<call>);
c32e827b
SR
457 * }
458 *
c32e827b 459 *
157587d7 460 * For those macros defined with TRACE_EVENT:
c32e827b
SR
461 *
462 * static struct ftrace_event_call event_<call>;
463 *
464 * static void ftrace_raw_event_<call>(proto)
465 * {
ef18012b
SR
466 * struct ring_buffer_event *event;
467 * struct ftrace_raw_<call> *entry; <-- defined in stage 1
468 * unsigned long irq_flags;
469 * int pc;
470 *
471 * local_save_flags(irq_flags);
472 * pc = preempt_count();
473 *
474 * event = trace_current_buffer_lock_reserve(event_<call>.id,
475 * sizeof(struct ftrace_raw_<call>),
476 * irq_flags, pc);
477 * if (!event)
478 * return;
479 * entry = ring_buffer_event_data(event);
480 *
481 * <assign>; <-- Here we assign the entries by the __field and
0e3d0f05 482 * __array macros.
c32e827b 483 *
ef18012b 484 * trace_current_buffer_unlock_commit(event, irq_flags, pc);
c32e827b
SR
485 * }
486 *
487 * static int ftrace_raw_reg_event_<call>(void)
488 * {
ef18012b 489 * int ret;
c32e827b 490 *
ef18012b
SR
491 * ret = register_trace_<call>(ftrace_raw_event_<call>);
492 * if (!ret)
493 * pr_info("event trace: Could not activate trace point "
494 * "probe to <call>");
495 * return ret;
c32e827b
SR
496 * }
497 *
498 * static void ftrace_unreg_event_<call>(void)
499 * {
ef18012b 500 * unregister_trace_<call>(ftrace_raw_event_<call>);
c32e827b
SR
501 * }
502 *
503 * static struct trace_event ftrace_event_type_<call> = {
ef18012b 504 * .trace = ftrace_raw_output_<call>, <-- stage 2
c32e827b
SR
505 * };
506 *
507 * static int ftrace_raw_init_event_<call>(void)
508 * {
ef18012b 509 * int id;
c32e827b 510 *
ef18012b
SR
511 * id = register_ftrace_event(&ftrace_event_type_<call>);
512 * if (!id)
513 * return -ENODEV;
514 * event_<call>.id = id;
515 * return 0;
c32e827b
SR
516 * }
517 *
518 * static struct ftrace_event_call __used
519 * __attribute__((__aligned__(4)))
520 * __attribute__((section("_ftrace_events"))) event_<call> = {
ef18012b 521 * .name = "<call>",
0e3d0f05 522 * .system = "<system>",
ef18012b
SR
523 * .raw_init = ftrace_raw_init_event_<call>,
524 * .regfunc = ftrace_reg_event_<call>,
525 * .unregfunc = ftrace_unreg_event_<call>,
981d081e 526 * .show_format = ftrace_format_<call>,
c32e827b
SR
527 * }
528 *
529 */
530
2939b046
SR
531#undef TP_FMT
532#define TP_FMT(fmt, args...) fmt "\n", ##args
c32e827b 533
ac199db0 534#ifdef CONFIG_EVENT_PROFILE
ac199db0
PZ
535
536#define _TRACE_PROFILE_INIT(call) \
537 .profile_count = ATOMIC_INIT(-1), \
538 .profile_enable = ftrace_profile_enable_##call, \
539 .profile_disable = ftrace_profile_disable_##call,
540
541#else
ac199db0
PZ
542#define _TRACE_PROFILE_INIT(call)
543#endif
544
da4d0302
SR
545#undef __entry
546#define __entry entry
d20e3b03 547
9cbf1176
FW
548#undef __field
549#define __field(type, item)
550
551#undef __array
552#define __array(type, item, len)
553
7fcb7c47
LZ
554#undef __dynamic_array
555#define __dynamic_array(type, item, len) \
556 __entry->__data_loc_##item = __data_offsets.item;
557
9cbf1176 558#undef __string
7fcb7c47 559#define __string(item, src) __dynamic_array(char, item, -1) \
9cbf1176
FW
560
561#undef __assign_str
562#define __assign_str(dst, src) \
9cbf1176
FW
563 strcpy(__get_str(dst), src);
564
da4d0302 565#undef TRACE_EVENT
30a8fecc 566#define TRACE_EVENT(call, proto, args, tstruct, assign, print) \
c32e827b
SR
567 \
568static struct ftrace_event_call event_##call; \
569 \
570static void ftrace_raw_event_##call(proto) \
571{ \
7fcb7c47 572 struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
f2aebaee 573 struct ftrace_event_call *event_call = &event_##call; \
c32e827b
SR
574 struct ring_buffer_event *event; \
575 struct ftrace_raw_##call *entry; \
576 unsigned long irq_flags; \
7fcb7c47 577 int __data_size; \
c32e827b
SR
578 int pc; \
579 \
580 local_save_flags(irq_flags); \
581 pc = preempt_count(); \
582 \
7fcb7c47 583 __data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
9cbf1176 584 \
c32e827b 585 event = trace_current_buffer_lock_reserve(event_##call.id, \
7fcb7c47 586 sizeof(*entry) + __data_size, \
9cbf1176 587 irq_flags, pc); \
c32e827b
SR
588 if (!event) \
589 return; \
590 entry = ring_buffer_event_data(event); \
591 \
7fcb7c47
LZ
592 \
593 tstruct \
594 \
a9c1c3ab 595 { assign; } \
c32e827b 596 \
f2aebaee 597 if (!filter_current_check_discard(event_call, entry, event)) \
77d9f465 598 trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \
c32e827b
SR
599} \
600 \
601static int ftrace_raw_reg_event_##call(void) \
602{ \
603 int ret; \
604 \
605 ret = register_trace_##call(ftrace_raw_event_##call); \
633ddaa7 606 if (ret) \
c32e827b 607 pr_info("event trace: Could not activate trace point " \
633ddaa7 608 "probe to " #call "\n"); \
c32e827b
SR
609 return ret; \
610} \
611 \
612static void ftrace_raw_unreg_event_##call(void) \
613{ \
614 unregister_trace_##call(ftrace_raw_event_##call); \
615} \
616 \
617static struct trace_event ftrace_event_type_##call = { \
618 .trace = ftrace_raw_output_##call, \
619}; \
620 \
621static int ftrace_raw_init_event_##call(void) \
622{ \
623 int id; \
624 \
625 id = register_ftrace_event(&ftrace_event_type_##call); \
626 if (!id) \
627 return -ENODEV; \
628 event_##call.id = id; \
cf027f64 629 INIT_LIST_HEAD(&event_##call.fields); \
0a19e53c 630 init_preds(&event_##call); \
c32e827b
SR
631 return 0; \
632} \
633 \
634static struct ftrace_event_call __used \
635__attribute__((__aligned__(4))) \
636__attribute__((section("_ftrace_events"))) event_##call = { \
ef18012b 637 .name = #call, \
9cc26a26 638 .system = __stringify(TRACE_SYSTEM), \
6d723736 639 .event = &ftrace_event_type_##call, \
c32e827b 640 .raw_init = ftrace_raw_init_event_##call, \
da4d0302
SR
641 .regfunc = ftrace_raw_reg_event_##call, \
642 .unregfunc = ftrace_raw_unreg_event_##call, \
981d081e 643 .show_format = ftrace_format_##call, \
cf027f64 644 .define_fields = ftrace_define_fields_##call, \
ac199db0 645 _TRACE_PROFILE_INIT(call) \
c32e827b 646}
ac199db0 647
f42c85e7 648#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
ac199db0 649
ac199db0
PZ
650#undef _TRACE_PROFILE_INIT
651
This page took 0.074398 seconds and 5 git commands to generate.