Commit | Line | Data |
---|---|---|
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 | ||
f42c85e7 SR |
21 | #undef __array |
22 | #define __array(type, item, len) type item[len]; | |
23 | ||
24 | #undef __field | |
25 | #define __field(type, item) type item; | |
26 | ||
9cbf1176 | 27 | #undef __string |
b0aae68c | 28 | #define __string(item, src) unsigned short __str_loc_##item; |
9cbf1176 | 29 | |
f42c85e7 SR |
30 | #undef TP_STRUCT__entry |
31 | #define TP_STRUCT__entry(args...) args | |
32 | ||
33 | #undef TRACE_EVENT | |
34 | #define TRACE_EVENT(name, proto, args, tstruct, assign, print) \ | |
35 | struct ftrace_raw_##name { \ | |
36 | struct trace_entry ent; \ | |
37 | tstruct \ | |
9cbf1176 | 38 | char __str_data[0]; \ |
f42c85e7 SR |
39 | }; \ |
40 | static struct ftrace_event_call event_##name | |
41 | ||
42 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
43 | ||
9cbf1176 | 44 | |
f42c85e7 SR |
45 | /* |
46 | * Stage 2 of the trace events. | |
47 | * | |
9cbf1176 FW |
48 | * Include the following: |
49 | * | |
50 | * struct ftrace_str_offsets_<call> { | |
51 | * int <str1>; | |
52 | * int <str2>; | |
53 | * [...] | |
54 | * }; | |
55 | * | |
56 | * The __string() macro will create each int <str>, this is to | |
57 | * keep the offset of each string from the beggining of the event | |
58 | * once we perform the strlen() of the src strings. | |
59 | * | |
60 | */ | |
61 | ||
9cbf1176 FW |
62 | #undef __array |
63 | #define __array(type, item, len) | |
64 | ||
65 | #undef __field | |
66 | #define __field(type, item); | |
67 | ||
68 | #undef __string | |
69 | #define __string(item, src) int item; | |
70 | ||
71 | #undef TRACE_EVENT | |
72 | #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \ | |
73 | struct ftrace_str_offsets_##call { \ | |
74 | tstruct; \ | |
75 | }; | |
76 | ||
77 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
78 | ||
79 | /* | |
80 | * Stage 3 of the trace events. | |
81 | * | |
f42c85e7 SR |
82 | * Override the macros in <trace/trace_events.h> to include the following: |
83 | * | |
84 | * enum print_line_t | |
85 | * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags) | |
86 | * { | |
87 | * struct trace_seq *s = &iter->seq; | |
88 | * struct ftrace_raw_<call> *field; <-- defined in stage 1 | |
89 | * struct trace_entry *entry; | |
be74b73a | 90 | * struct trace_seq *p; |
f42c85e7 SR |
91 | * int ret; |
92 | * | |
93 | * entry = iter->ent; | |
94 | * | |
95 | * if (entry->type != event_<call>.id) { | |
96 | * WARN_ON_ONCE(1); | |
97 | * return TRACE_TYPE_UNHANDLED; | |
98 | * } | |
99 | * | |
100 | * field = (typeof(field))entry; | |
101 | * | |
be74b73a | 102 | * p = get_cpu_var(ftrace_event_seq); |
f42c85e7 | 103 | * ret = trace_seq_printf(s, <TP_printk> "\n"); |
be74b73a | 104 | * put_cpu(); |
f42c85e7 SR |
105 | * if (!ret) |
106 | * return TRACE_TYPE_PARTIAL_LINE; | |
107 | * | |
108 | * return TRACE_TYPE_HANDLED; | |
109 | * } | |
110 | * | |
111 | * This is the method used to print the raw event to the trace | |
112 | * output format. Note, this is not needed if the data is read | |
113 | * in binary. | |
114 | */ | |
115 | ||
116 | #undef __entry | |
117 | #define __entry field | |
118 | ||
119 | #undef TP_printk | |
120 | #define TP_printk(fmt, args...) fmt "\n", args | |
121 | ||
9cbf1176 | 122 | #undef __get_str |
6a74aa40 | 123 | #define __get_str(field) ((char *)__entry + __entry->__str_loc_##field) |
9cbf1176 | 124 | |
be74b73a SR |
125 | #undef __print_flags |
126 | #define __print_flags(flag, delim, flag_array...) \ | |
127 | ({ \ | |
128 | static const struct trace_print_flags flags[] = \ | |
129 | { flag_array, { -1, NULL }}; \ | |
130 | ftrace_print_flags_seq(p, delim, flag, flags); \ | |
131 | }) | |
132 | ||
f42c85e7 SR |
133 | #undef TRACE_EVENT |
134 | #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \ | |
135 | enum print_line_t \ | |
136 | ftrace_raw_output_##call(struct trace_iterator *iter, int flags) \ | |
137 | { \ | |
138 | struct trace_seq *s = &iter->seq; \ | |
139 | struct ftrace_raw_##call *field; \ | |
140 | struct trace_entry *entry; \ | |
be74b73a | 141 | struct trace_seq *p; \ |
f42c85e7 SR |
142 | int ret; \ |
143 | \ | |
144 | entry = iter->ent; \ | |
145 | \ | |
146 | if (entry->type != event_##call.id) { \ | |
147 | WARN_ON_ONCE(1); \ | |
148 | return TRACE_TYPE_UNHANDLED; \ | |
149 | } \ | |
150 | \ | |
151 | field = (typeof(field))entry; \ | |
152 | \ | |
be74b73a | 153 | p = &get_cpu_var(ftrace_event_seq); \ |
f42c85e7 | 154 | ret = trace_seq_printf(s, #call ": " print); \ |
be74b73a | 155 | put_cpu(); \ |
f42c85e7 SR |
156 | if (!ret) \ |
157 | return TRACE_TYPE_PARTIAL_LINE; \ | |
158 | \ | |
159 | return TRACE_TYPE_HANDLED; \ | |
160 | } | |
161 | ||
162 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
163 | ||
164 | /* | |
165 | * Setup the showing format of trace point. | |
166 | * | |
167 | * int | |
168 | * ftrace_format_##call(struct trace_seq *s) | |
169 | * { | |
170 | * struct ftrace_raw_##call field; | |
171 | * int ret; | |
172 | * | |
173 | * ret = trace_seq_printf(s, #type " " #item ";" | |
174 | * " offset:%u; size:%u;\n", | |
175 | * offsetof(struct ftrace_raw_##call, item), | |
176 | * sizeof(field.type)); | |
177 | * | |
178 | * } | |
179 | */ | |
180 | ||
181 | #undef TP_STRUCT__entry | |
182 | #define TP_STRUCT__entry(args...) args | |
183 | ||
184 | #undef __field | |
185 | #define __field(type, item) \ | |
186 | ret = trace_seq_printf(s, "\tfield:" #type " " #item ";\t" \ | |
187 | "offset:%u;\tsize:%u;\n", \ | |
188 | (unsigned int)offsetof(typeof(field), item), \ | |
189 | (unsigned int)sizeof(field.item)); \ | |
190 | if (!ret) \ | |
191 | return 0; | |
192 | ||
193 | #undef __array | |
194 | #define __array(type, item, len) \ | |
195 | ret = trace_seq_printf(s, "\tfield:" #type " " #item "[" #len "];\t" \ | |
196 | "offset:%u;\tsize:%u;\n", \ | |
197 | (unsigned int)offsetof(typeof(field), item), \ | |
198 | (unsigned int)sizeof(field.item)); \ | |
199 | if (!ret) \ | |
200 | return 0; | |
201 | ||
9cbf1176 FW |
202 | #undef __string |
203 | #define __string(item, src) \ | |
204 | ret = trace_seq_printf(s, "\tfield: __str_loc " #item ";\t" \ | |
205 | "offset:%u;tsize:%u;\n", \ | |
206 | (unsigned int)offsetof(typeof(field), \ | |
207 | __str_loc_##item), \ | |
208 | (unsigned int)sizeof(field.__str_loc_##item)); \ | |
209 | if (!ret) \ | |
210 | return 0; | |
211 | ||
f42c85e7 SR |
212 | #undef __entry |
213 | #define __entry REC | |
214 | ||
215 | #undef TP_printk | |
216 | #define TP_printk(fmt, args...) "%s, %s\n", #fmt, __stringify(args) | |
217 | ||
218 | #undef TP_fast_assign | |
219 | #define TP_fast_assign(args...) args | |
220 | ||
221 | #undef TRACE_EVENT | |
222 | #define TRACE_EVENT(call, proto, args, tstruct, func, print) \ | |
223 | static int \ | |
224 | ftrace_format_##call(struct trace_seq *s) \ | |
225 | { \ | |
76aa8111 JF |
226 | struct ftrace_raw_##call field __attribute__((unused)); \ |
227 | int ret = 0; \ | |
f42c85e7 SR |
228 | \ |
229 | tstruct; \ | |
230 | \ | |
231 | trace_seq_printf(s, "\nprint fmt: " print); \ | |
232 | \ | |
233 | return ret; \ | |
234 | } | |
235 | ||
236 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
237 | ||
238 | #undef __field | |
239 | #define __field(type, item) \ | |
240 | ret = trace_define_field(event_call, #type, #item, \ | |
241 | offsetof(typeof(field), item), \ | |
a118e4d1 | 242 | sizeof(field.item), is_signed_type(type)); \ |
f42c85e7 SR |
243 | if (ret) \ |
244 | return ret; | |
245 | ||
246 | #undef __array | |
247 | #define __array(type, item, len) \ | |
248 | BUILD_BUG_ON(len > MAX_FILTER_STR_VAL); \ | |
249 | ret = trace_define_field(event_call, #type "[" #len "]", #item, \ | |
250 | offsetof(typeof(field), item), \ | |
a118e4d1 | 251 | sizeof(field.item), 0); \ |
f42c85e7 SR |
252 | if (ret) \ |
253 | return ret; | |
254 | ||
9cbf1176 FW |
255 | #undef __string |
256 | #define __string(item, src) \ | |
257 | ret = trace_define_field(event_call, "__str_loc", #item, \ | |
258 | offsetof(typeof(field), __str_loc_##item), \ | |
a118e4d1 | 259 | sizeof(field.__str_loc_##item), 0); |
9cbf1176 | 260 | |
f42c85e7 SR |
261 | #undef TRACE_EVENT |
262 | #define TRACE_EVENT(call, proto, args, tstruct, func, print) \ | |
263 | int \ | |
264 | ftrace_define_fields_##call(void) \ | |
265 | { \ | |
266 | struct ftrace_raw_##call field; \ | |
267 | struct ftrace_event_call *event_call = &event_##call; \ | |
268 | int ret; \ | |
269 | \ | |
a118e4d1 TZ |
270 | __common_field(int, type, 1); \ |
271 | __common_field(unsigned char, flags, 0); \ | |
272 | __common_field(unsigned char, preempt_count, 0); \ | |
273 | __common_field(int, pid, 1); \ | |
274 | __common_field(int, tgid, 1); \ | |
f42c85e7 SR |
275 | \ |
276 | tstruct; \ | |
277 | \ | |
278 | return ret; \ | |
279 | } | |
280 | ||
281 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) | |
282 | ||
c32e827b | 283 | /* |
9cbf1176 | 284 | * Stage 4 of the trace events. |
c32e827b | 285 | * |
ea20d929 | 286 | * Override the macros in <trace/trace_events.h> to include the following: |
c32e827b SR |
287 | * |
288 | * static void ftrace_event_<call>(proto) | |
289 | * { | |
ef18012b | 290 | * event_trace_printk(_RET_IP_, "<call>: " <fmt>); |
c32e827b SR |
291 | * } |
292 | * | |
293 | * static int ftrace_reg_event_<call>(void) | |
294 | * { | |
ef18012b | 295 | * int ret; |
c32e827b | 296 | * |
ef18012b SR |
297 | * ret = register_trace_<call>(ftrace_event_<call>); |
298 | * if (!ret) | |
299 | * pr_info("event trace: Could not activate trace point " | |
300 | * "probe to <call>"); | |
301 | * return ret; | |
c32e827b SR |
302 | * } |
303 | * | |
304 | * static void ftrace_unreg_event_<call>(void) | |
305 | * { | |
ef18012b | 306 | * unregister_trace_<call>(ftrace_event_<call>); |
c32e827b SR |
307 | * } |
308 | * | |
c32e827b | 309 | * |
157587d7 | 310 | * For those macros defined with TRACE_EVENT: |
c32e827b SR |
311 | * |
312 | * static struct ftrace_event_call event_<call>; | |
313 | * | |
314 | * static void ftrace_raw_event_<call>(proto) | |
315 | * { | |
ef18012b SR |
316 | * struct ring_buffer_event *event; |
317 | * struct ftrace_raw_<call> *entry; <-- defined in stage 1 | |
318 | * unsigned long irq_flags; | |
319 | * int pc; | |
320 | * | |
321 | * local_save_flags(irq_flags); | |
322 | * pc = preempt_count(); | |
323 | * | |
324 | * event = trace_current_buffer_lock_reserve(event_<call>.id, | |
325 | * sizeof(struct ftrace_raw_<call>), | |
326 | * irq_flags, pc); | |
327 | * if (!event) | |
328 | * return; | |
329 | * entry = ring_buffer_event_data(event); | |
330 | * | |
331 | * <assign>; <-- Here we assign the entries by the __field and | |
0e3d0f05 | 332 | * __array macros. |
c32e827b | 333 | * |
ef18012b | 334 | * trace_current_buffer_unlock_commit(event, irq_flags, pc); |
c32e827b SR |
335 | * } |
336 | * | |
337 | * static int ftrace_raw_reg_event_<call>(void) | |
338 | * { | |
ef18012b | 339 | * int ret; |
c32e827b | 340 | * |
ef18012b SR |
341 | * ret = register_trace_<call>(ftrace_raw_event_<call>); |
342 | * if (!ret) | |
343 | * pr_info("event trace: Could not activate trace point " | |
344 | * "probe to <call>"); | |
345 | * return ret; | |
c32e827b SR |
346 | * } |
347 | * | |
348 | * static void ftrace_unreg_event_<call>(void) | |
349 | * { | |
ef18012b | 350 | * unregister_trace_<call>(ftrace_raw_event_<call>); |
c32e827b SR |
351 | * } |
352 | * | |
353 | * static struct trace_event ftrace_event_type_<call> = { | |
ef18012b | 354 | * .trace = ftrace_raw_output_<call>, <-- stage 2 |
c32e827b SR |
355 | * }; |
356 | * | |
357 | * static int ftrace_raw_init_event_<call>(void) | |
358 | * { | |
ef18012b | 359 | * int id; |
c32e827b | 360 | * |
ef18012b SR |
361 | * id = register_ftrace_event(&ftrace_event_type_<call>); |
362 | * if (!id) | |
363 | * return -ENODEV; | |
364 | * event_<call>.id = id; | |
365 | * return 0; | |
c32e827b SR |
366 | * } |
367 | * | |
368 | * static struct ftrace_event_call __used | |
369 | * __attribute__((__aligned__(4))) | |
370 | * __attribute__((section("_ftrace_events"))) event_<call> = { | |
ef18012b | 371 | * .name = "<call>", |
0e3d0f05 | 372 | * .system = "<system>", |
ef18012b SR |
373 | * .raw_init = ftrace_raw_init_event_<call>, |
374 | * .regfunc = ftrace_reg_event_<call>, | |
375 | * .unregfunc = ftrace_unreg_event_<call>, | |
981d081e | 376 | * .show_format = ftrace_format_<call>, |
c32e827b SR |
377 | * } |
378 | * | |
379 | */ | |
380 | ||
2939b046 SR |
381 | #undef TP_FMT |
382 | #define TP_FMT(fmt, args...) fmt "\n", ##args | |
c32e827b | 383 | |
ac199db0 PZ |
384 | #ifdef CONFIG_EVENT_PROFILE |
385 | #define _TRACE_PROFILE(call, proto, args) \ | |
386 | static void ftrace_profile_##call(proto) \ | |
387 | { \ | |
388 | extern void perf_tpcounter_event(int); \ | |
389 | perf_tpcounter_event(event_##call.id); \ | |
390 | } \ | |
391 | \ | |
392 | static int ftrace_profile_enable_##call(struct ftrace_event_call *call) \ | |
393 | { \ | |
394 | int ret = 0; \ | |
395 | \ | |
396 | if (!atomic_inc_return(&call->profile_count)) \ | |
397 | ret = register_trace_##call(ftrace_profile_##call); \ | |
398 | \ | |
399 | return ret; \ | |
400 | } \ | |
401 | \ | |
402 | static void ftrace_profile_disable_##call(struct ftrace_event_call *call) \ | |
403 | { \ | |
404 | if (atomic_add_negative(-1, &call->profile_count)) \ | |
405 | unregister_trace_##call(ftrace_profile_##call); \ | |
406 | } | |
407 | ||
408 | #define _TRACE_PROFILE_INIT(call) \ | |
409 | .profile_count = ATOMIC_INIT(-1), \ | |
410 | .profile_enable = ftrace_profile_enable_##call, \ | |
411 | .profile_disable = ftrace_profile_disable_##call, | |
412 | ||
413 | #else | |
414 | #define _TRACE_PROFILE(call, proto, args) | |
415 | #define _TRACE_PROFILE_INIT(call) | |
416 | #endif | |
417 | ||
da4d0302 SR |
418 | #undef __entry |
419 | #define __entry entry | |
d20e3b03 | 420 | |
9cbf1176 FW |
421 | #undef __field |
422 | #define __field(type, item) | |
423 | ||
424 | #undef __array | |
425 | #define __array(type, item, len) | |
426 | ||
427 | #undef __string | |
428 | #define __string(item, src) \ | |
429 | __str_offsets.item = __str_size + \ | |
430 | offsetof(typeof(*entry), __str_data); \ | |
431 | __str_size += strlen(src) + 1; | |
432 | ||
433 | #undef __assign_str | |
434 | #define __assign_str(dst, src) \ | |
435 | __entry->__str_loc_##dst = __str_offsets.dst; \ | |
436 | strcpy(__get_str(dst), src); | |
437 | ||
da4d0302 | 438 | #undef TRACE_EVENT |
30a8fecc | 439 | #define TRACE_EVENT(call, proto, args, tstruct, assign, print) \ |
ac199db0 | 440 | _TRACE_PROFILE(call, PARAMS(proto), PARAMS(args)) \ |
c32e827b SR |
441 | \ |
442 | static struct ftrace_event_call event_##call; \ | |
443 | \ | |
444 | static void ftrace_raw_event_##call(proto) \ | |
445 | { \ | |
9cbf1176 | 446 | struct ftrace_str_offsets_##call __maybe_unused __str_offsets; \ |
7ce7e424 | 447 | struct ftrace_event_call *call = &event_##call; \ |
c32e827b SR |
448 | struct ring_buffer_event *event; \ |
449 | struct ftrace_raw_##call *entry; \ | |
450 | unsigned long irq_flags; \ | |
9cbf1176 | 451 | int __str_size = 0; \ |
c32e827b SR |
452 | int pc; \ |
453 | \ | |
454 | local_save_flags(irq_flags); \ | |
455 | pc = preempt_count(); \ | |
456 | \ | |
9cbf1176 FW |
457 | tstruct; \ |
458 | \ | |
c32e827b | 459 | event = trace_current_buffer_lock_reserve(event_##call.id, \ |
9cbf1176 FW |
460 | sizeof(struct ftrace_raw_##call) + __str_size,\ |
461 | irq_flags, pc); \ | |
c32e827b SR |
462 | if (!event) \ |
463 | return; \ | |
464 | entry = ring_buffer_event_data(event); \ | |
465 | \ | |
da4d0302 | 466 | assign; \ |
c32e827b | 467 | \ |
eb02ce01 | 468 | if (!filter_current_check_discard(call, entry, event)) \ |
77d9f465 | 469 | trace_nowake_buffer_unlock_commit(event, irq_flags, pc); \ |
c32e827b SR |
470 | } \ |
471 | \ | |
472 | static int ftrace_raw_reg_event_##call(void) \ | |
473 | { \ | |
474 | int ret; \ | |
475 | \ | |
476 | ret = register_trace_##call(ftrace_raw_event_##call); \ | |
633ddaa7 | 477 | if (ret) \ |
c32e827b | 478 | pr_info("event trace: Could not activate trace point " \ |
633ddaa7 | 479 | "probe to " #call "\n"); \ |
c32e827b SR |
480 | return ret; \ |
481 | } \ | |
482 | \ | |
483 | static void ftrace_raw_unreg_event_##call(void) \ | |
484 | { \ | |
485 | unregister_trace_##call(ftrace_raw_event_##call); \ | |
486 | } \ | |
487 | \ | |
488 | static struct trace_event ftrace_event_type_##call = { \ | |
489 | .trace = ftrace_raw_output_##call, \ | |
490 | }; \ | |
491 | \ | |
492 | static int ftrace_raw_init_event_##call(void) \ | |
493 | { \ | |
494 | int id; \ | |
495 | \ | |
496 | id = register_ftrace_event(&ftrace_event_type_##call); \ | |
497 | if (!id) \ | |
498 | return -ENODEV; \ | |
499 | event_##call.id = id; \ | |
cf027f64 | 500 | INIT_LIST_HEAD(&event_##call.fields); \ |
0a19e53c | 501 | init_preds(&event_##call); \ |
c32e827b SR |
502 | return 0; \ |
503 | } \ | |
504 | \ | |
505 | static struct ftrace_event_call __used \ | |
506 | __attribute__((__aligned__(4))) \ | |
507 | __attribute__((section("_ftrace_events"))) event_##call = { \ | |
ef18012b | 508 | .name = #call, \ |
9cc26a26 | 509 | .system = __stringify(TRACE_SYSTEM), \ |
6d723736 | 510 | .event = &ftrace_event_type_##call, \ |
c32e827b | 511 | .raw_init = ftrace_raw_init_event_##call, \ |
da4d0302 SR |
512 | .regfunc = ftrace_raw_reg_event_##call, \ |
513 | .unregfunc = ftrace_raw_unreg_event_##call, \ | |
981d081e | 514 | .show_format = ftrace_format_##call, \ |
cf027f64 | 515 | .define_fields = ftrace_define_fields_##call, \ |
ac199db0 | 516 | _TRACE_PROFILE_INIT(call) \ |
c32e827b | 517 | } |
ac199db0 | 518 | |
f42c85e7 | 519 | #include TRACE_INCLUDE(TRACE_INCLUDE_FILE) |
ac199db0 PZ |
520 | |
521 | #undef _TRACE_PROFILE | |
522 | #undef _TRACE_PROFILE_INIT | |
523 |