ftrace.h 22.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * Stage 1 of the trace events.
 *
 * Override the macros in <trace/trace_events.h> to include the following:
 *
 * struct ftrace_raw_<call> {
 *	struct trace_entry		ent;
 *	<type>				<item>;
 *	<type2>				<item2>[<len>];
 *	[...]
 * };
 *
 * The <type> <item> is created by the __field(type, item) macro or
 * the __array(type2, item2, len) macro.
 * We simply do "type item;", and that will create the fields
 * in the structure.
 */

#include <linux/ftrace_event.h>

21
/*
22
 * DECLARE_EVENT_CLASS can be used to add a generic function
23
24
25
 * handlers for events. That is, if all events have the same
 * parameters and just have distinct trace points.
 * Each tracepoint can be defined with DEFINE_EVENT and that
26
 * will map the DECLARE_EVENT_CLASS to the tracepoint.
27
28
29
30
31
 *
 * TRACE_EVENT is a one to one mapping between tracepoint and template.
 */
#undef TRACE_EVENT
#define TRACE_EVENT(name, proto, args, tstruct, assign, print) \
32
	DECLARE_EVENT_CLASS(name,			       \
33
34
35
36
37
38
39
40
			     PARAMS(proto),		       \
			     PARAMS(args),		       \
			     PARAMS(tstruct),		       \
			     PARAMS(assign),		       \
			     PARAMS(print));		       \
	DEFINE_EVENT(name, name, PARAMS(proto), PARAMS(args));


41
42
43
#undef __field
#define __field(type, item)		type	item;

44
45
46
#undef __field_ext
#define __field_ext(type, item, filter_type)	type	item;

47
48
49
#undef __array
#define __array(type, item, len)	type	item[len];

50
#undef __dynamic_array
51
#define __dynamic_array(type, item, len) u32 __data_loc_##item;
52

53
#undef __string
54
#define __string(item, src) __dynamic_array(char, item, -1)
55

56
57
58
#undef TP_STRUCT__entry
#define TP_STRUCT__entry(args...) args

59
60
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(name, proto, args, tstruct, assign, print)	\
61
62
63
64
65
66
67
	struct ftrace_raw_##name {					\
		struct trace_entry	ent;				\
		tstruct							\
		char			__data[0];			\
	};
#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)	\
68
69
	static struct ftrace_event_call			\
	__attribute__((__aligned__(4))) event_##name
70

71
72
73
74
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

75
76
77
#undef __cpparg
#define __cpparg(arg...) arg

78
79
/* Callbacks are meaningless to ftrace. */
#undef TRACE_EVENT_FN
80
81
82
83
#define TRACE_EVENT_FN(name, proto, args, tstruct,			\
		assign, print, reg, unreg)				\
	TRACE_EVENT(name, __cpparg(proto), __cpparg(args),		\
		__cpparg(tstruct), __cpparg(assign), __cpparg(print))	\
84

85
86
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

87

88
89
90
/*
 * Stage 2 of the trace events.
 *
91
92
 * Include the following:
 *
93
 * struct ftrace_data_offsets_<call> {
94
95
 *	u32				<item1>;
 *	u32				<item2>;
96
97
98
 *	[...]
 * };
 *
99
 * The __dynamic_array() macro will create each u32 <item>, this is
100
 * to keep the offset of each array from the beginning of the event.
101
 * The size of an array is also encoded, in the higher 16 bits of <item>.
102
103
 */

104
#undef __field
105
106
107
108
#define __field(type, item)

#undef __field_ext
#define __field_ext(type, item, filter_type)
109

110
111
112
#undef __array
#define __array(type, item, len)

113
#undef __dynamic_array
114
#define __dynamic_array(type, item, len)	u32 item;
115
116

#undef __string
117
#define __string(item, src) __dynamic_array(char, item, -1)
118

119
120
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
121
	struct ftrace_data_offsets_##call {				\
122
123
124
		tstruct;						\
	};

125
126
127
#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)

128
129
130
131
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

132
133
134
135
136
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

/*
 * Stage 3 of the trace events.
 *
137
138
139
140
141
142
143
144
 * Override the macros in <trace/trace_events.h> to include the following:
 *
 * enum print_line_t
 * ftrace_raw_output_<call>(struct trace_iterator *iter, int flags)
 * {
 *	struct trace_seq *s = &iter->seq;
 *	struct ftrace_raw_<call> *field; <-- defined in stage 1
 *	struct trace_entry *entry;
145
 *	struct trace_seq *p;
146
147
148
149
150
151
152
153
154
155
156
 *	int ret;
 *
 *	entry = iter->ent;
 *
 *	if (entry->type != event_<call>.id) {
 *		WARN_ON_ONCE(1);
 *		return TRACE_TYPE_UNHANDLED;
 *	}
 *
 *	field = (typeof(field))entry;
 *
Li Zefan's avatar
Li Zefan committed
157
 *	p = &get_cpu_var(ftrace_event_seq);
158
 *	trace_seq_init(p);
Li Zefan's avatar
Li Zefan committed
159
160
161
 *	ret = trace_seq_printf(s, "%s: ", <call>);
 *	if (ret)
 *		ret = trace_seq_printf(s, <TP_printk> "\n");
162
 *	put_cpu();
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
 *	if (!ret)
 *		return TRACE_TYPE_PARTIAL_LINE;
 *
 *	return TRACE_TYPE_HANDLED;
 * }
 *
 * This is the method used to print the raw event to the trace
 * output format. Note, this is not needed if the data is read
 * in binary.
 */

#undef __entry
#define __entry field

#undef TP_printk
#define TP_printk(fmt, args...) fmt "\n", args

180
181
#undef __get_dynamic_array
#define __get_dynamic_array(field)	\
182
		((void *)__entry + (__entry->__data_loc_##field & 0xffff))
183

184
#undef __get_str
185
#define __get_str(field) (char *)__get_dynamic_array(field)
186

187
188
189
#undef __print_flags
#define __print_flags(flag, delim, flag_array...)			\
	({								\
190
		static const struct trace_print_flags __flags[] =	\
191
			{ flag_array, { -1, NULL }};			\
192
		ftrace_print_flags_seq(p, delim, flag, __flags);	\
193
194
	})

195
196
197
198
199
200
201
202
#undef __print_symbolic
#define __print_symbolic(value, symbol_array...)			\
	({								\
		static const struct trace_print_flags symbols[] =	\
			{ symbol_array, { -1, NULL }};			\
		ftrace_print_symbols_seq(p, value, symbols);		\
	})

203
204
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
205
static notrace enum print_line_t					\
206
207
ftrace_raw_output_id_##call(int event_id, const char *name,		\
			    struct trace_iterator *iter, int flags)	\
208
209
210
211
{									\
	struct trace_seq *s = &iter->seq;				\
	struct ftrace_raw_##call *field;				\
	struct trace_entry *entry;					\
212
	struct trace_seq *p;						\
213
214
215
216
	int ret;							\
									\
	entry = iter->ent;						\
									\
217
	if (entry->type != event_id) {					\
218
219
220
221
222
223
		WARN_ON_ONCE(1);					\
		return TRACE_TYPE_UNHANDLED;				\
	}								\
									\
	field = (typeof(field))entry;					\
									\
224
	p = &get_cpu_var(ftrace_event_seq);				\
225
	trace_seq_init(p);						\
226
227
228
	ret = trace_seq_printf(s, "%s: ", name);			\
	if (ret)							\
		ret = trace_seq_printf(s, print);			\
229
	put_cpu();							\
230
231
232
233
234
	if (!ret)							\
		return TRACE_TYPE_PARTIAL_LINE;				\
									\
	return TRACE_TYPE_HANDLED;					\
}
235
236
237

#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)			\
238
static notrace enum print_line_t					\
239
240
241
242
243
244
ftrace_raw_output_##name(struct trace_iterator *iter, int flags)	\
{									\
	return ftrace_raw_output_id_##template(event_##name.id,		\
					       #name, iter, flags);	\
}

245
246
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, call, proto, args, print)		\
247
static notrace enum print_line_t					\
248
249
250
251
252
253
ftrace_raw_output_##call(struct trace_iterator *iter, int flags)	\
{									\
	struct trace_seq *s = &iter->seq;				\
	struct ftrace_raw_##template *field;				\
	struct trace_entry *entry;					\
	struct trace_seq *p;						\
254
255
256
257
258
259
260
261
262
263
264
	int ret;							\
									\
	entry = iter->ent;						\
									\
	if (entry->type != event_##call.id) {				\
		WARN_ON_ONCE(1);					\
		return TRACE_TYPE_UNHANDLED;				\
	}								\
									\
	field = (typeof(field))entry;					\
									\
265
	p = &get_cpu_var(ftrace_event_seq);				\
266
	trace_seq_init(p);						\
267
268
269
	ret = trace_seq_printf(s, "%s: ", #call);			\
	if (ret)							\
		ret = trace_seq_printf(s, print);			\
270
	put_cpu();							\
271
272
273
274
275
	if (!ret)							\
		return TRACE_TYPE_PARTIAL_LINE;				\
									\
	return TRACE_TYPE_HANDLED;					\
}
276

277
278
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

279
280
#undef __field_ext
#define __field_ext(type, item, filter_type)				\
281
282
	ret = trace_define_field(event_call, #type, #item,		\
				 offsetof(typeof(field), item),		\
283
284
				 sizeof(field.item),			\
				 is_signed_type(type), filter_type);	\
285
286
287
	if (ret)							\
		return ret;

288
289
290
#undef __field
#define __field(type, item)	__field_ext(type, item, FILTER_OTHER)

291
292
293
294
295
#undef __array
#define __array(type, item, len)					\
	BUILD_BUG_ON(len > MAX_FILTER_STR_VAL);				\
	ret = trace_define_field(event_call, #type "[" #len "]", #item,	\
				 offsetof(typeof(field), item),		\
296
297
				 sizeof(field.item),			\
				 is_signed_type(type), FILTER_OTHER);	\
298
299
300
	if (ret)							\
		return ret;

301
302
#undef __dynamic_array
#define __dynamic_array(type, item, len)				       \
303
	ret = trace_define_field(event_call, "__data_loc " #type "[]", #item,  \
304
				 offsetof(typeof(field), __data_loc_##item),   \
305
306
				 sizeof(field.__data_loc_##item),	       \
				 is_signed_type(type), FILTER_OTHER);
307

308
#undef __string
309
#define __string(item, src) __dynamic_array(char, item, -1)
310

311
312
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, func, print)	\
313
static int notrace							\
314
ftrace_define_fields_##call(struct ftrace_event_call *event_call)	\
315
316
317
318
319
320
321
322
323
{									\
	struct ftrace_raw_##call field;					\
	int ret;							\
									\
	tstruct;							\
									\
	return ret;							\
}

324
325
326
#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)

327
328
329
330
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

331
332
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

333
334
335
336
337
338
339
340
341
342
/*
 * remember the offset of each array from the beginning of the event.
 */

#undef __entry
#define __entry entry

#undef __field
#define __field(type, item)

343
344
345
#undef __field_ext
#define __field_ext(type, item, filter_type)

346
347
348
349
350
351
352
#undef __array
#define __array(type, item, len)

#undef __dynamic_array
#define __dynamic_array(type, item, len)				\
	__data_offsets->item = __data_size +				\
			       offsetof(typeof(*entry), __data);	\
353
	__data_offsets->item |= (len * sizeof(type)) << 16;		\
354
355
356
	__data_size += (len) * sizeof(type);

#undef __string
357
#define __string(item, src) __dynamic_array(char, item, strlen(src) + 1)
358

359
360
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
361
static inline notrace int ftrace_get_offsets_##call(			\
362
363
364
365
366
367
368
369
370
371
	struct ftrace_data_offsets_##call *__data_offsets, proto)       \
{									\
	int __data_size = 0;						\
	struct ftrace_raw_##call __maybe_unused *entry;			\
									\
	tstruct;							\
									\
	return __data_size;						\
}

372
373
374
#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)

375
376
377
378
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

379
380
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

381
#ifdef CONFIG_PERF_EVENTS
382
383

/*
384
 * Generate the functions needed for tracepoint perf_event support.
385
 *
386
 * NOTE: The insertion profile callback (ftrace_profile_<call>) is defined later
387
 *
388
 * static int ftrace_profile_enable_<call>(void)
389
 * {
390
 * 	return register_trace_<call>(ftrace_profile_<call>);
391
392
 * }
 *
393
 * static void ftrace_profile_disable_<call>(void)
394
 * {
395
 * 	unregister_trace_<call>(ftrace_profile_<call>);
396
397
398
399
 * }
 *
 */

400
401
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)
402
403
404

#undef DEFINE_EVENT
#define DEFINE_EVENT(template, name, proto, args)			\
405
									\
406
static void perf_trace_##name(proto);					\
407
									\
408
static notrace int							\
409
perf_trace_enable_##name(struct ftrace_event_call *unused)		\
410
{									\
411
	return register_trace_##name(perf_trace_##name);		\
412
413
}									\
									\
414
static notrace void							\
415
perf_trace_disable_##name(struct ftrace_event_call *unused)		\
416
{									\
417
	unregister_trace_##name(perf_trace_##name);			\
418
419
}

420
421
422
423
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

424
425
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

426
#endif /* CONFIG_PERF_EVENTS */
427

428
/*
429
 * Stage 4 of the trace events.
430
 *
431
 * Override the macros in <trace/trace_events.h> to include the following:
432
433
434
 *
 * static void ftrace_event_<call>(proto)
 * {
435
 *	event_trace_printk(_RET_IP_, "<call>: " <fmt>);
436
437
 * }
 *
438
 * static int ftrace_reg_event_<call>(struct ftrace_event_call *unused)
439
 * {
440
 *	return register_trace_<call>(ftrace_event_<call>);
441
442
 * }
 *
443
 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
444
 * {
445
 *	unregister_trace_<call>(ftrace_event_<call>);
446
447
448
 * }
 *
 *
449
 * For those macros defined with TRACE_EVENT:
450
451
452
453
454
 *
 * static struct ftrace_event_call event_<call>;
 *
 * static void ftrace_raw_event_<call>(proto)
 * {
Li Zefan's avatar
Li Zefan committed
455
 *	struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
456
457
 *	struct ring_buffer_event *event;
 *	struct ftrace_raw_<call> *entry; <-- defined in stage 1
458
 *	struct ring_buffer *buffer;
459
 *	unsigned long irq_flags;
Li Zefan's avatar
Li Zefan committed
460
 *	int __data_size;
461
462
463
464
465
 *	int pc;
 *
 *	local_save_flags(irq_flags);
 *	pc = preempt_count();
 *
Li Zefan's avatar
Li Zefan committed
466
467
 *	__data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
 *
468
469
 *	event = trace_current_buffer_lock_reserve(&buffer,
 *				  event_<call>.id,
Li Zefan's avatar
Li Zefan committed
470
 *				  sizeof(*entry) + __data_size,
471
472
473
474
475
 *				  irq_flags, pc);
 *	if (!event)
 *		return;
 *	entry	= ring_buffer_event_data(event);
 *
Li Zefan's avatar
Li Zefan committed
476
477
 *	{ <assign>; }  <-- Here we assign the entries by the __field and
 *			   __array macros.
478
 *
Li Zefan's avatar
Li Zefan committed
479
480
481
 *	if (!filter_current_check_discard(buffer, event_call, entry, event))
 *		trace_current_buffer_unlock_commit(buffer,
 *						   event, irq_flags, pc);
482
483
 * }
 *
484
 * static int ftrace_raw_reg_event_<call>(struct ftrace_event_call *unused)
485
 * {
Li Zefan's avatar
Li Zefan committed
486
 *	return register_trace_<call>(ftrace_raw_event_<call>);
487
488
 * }
 *
489
 * static void ftrace_unreg_event_<call>(struct ftrace_event_call *unused)
490
 * {
491
 *	unregister_trace_<call>(ftrace_raw_event_<call>);
492
493
494
 * }
 *
 * static struct trace_event ftrace_event_type_<call> = {
495
 *	.trace			= ftrace_raw_output_<call>, <-- stage 2
496
497
 * };
 *
Li Zefan's avatar
Li Zefan committed
498
499
 * static const char print_fmt_<call>[] = <TP_printk>;
 *
500
501
502
 * static struct ftrace_event_call __used
 * __attribute__((__aligned__(4)))
 * __attribute__((section("_ftrace_events"))) event_<call> = {
503
 *	.name			= "<call>",
504
 *	.system			= "<system>",
505
 *	.raw_init		= trace_event_raw_init,
506
507
 *	.regfunc		= ftrace_reg_event_<call>,
 *	.unregfunc		= ftrace_unreg_event_<call>,
Li Zefan's avatar
Li Zefan committed
508
509
 *	.print_fmt		= print_fmt_<call>,
 *	.define_fields		= ftrace_define_fields_<call>,
510
511
512
513
 * }
 *
 */

514
#ifdef CONFIG_PERF_EVENTS
Peter Zijlstra's avatar
Peter Zijlstra committed
515

516
517
518
#define _TRACE_PERF_INIT(call)						\
	.perf_event_enable = perf_trace_enable_##call,			\
	.perf_event_disable = perf_trace_disable_##call,
Peter Zijlstra's avatar
Peter Zijlstra committed
519
520

#else
521
#define _TRACE_PERF_INIT(call)
522
#endif /* CONFIG_PERF_EVENTS */
Peter Zijlstra's avatar
Peter Zijlstra committed
523

524
525
#undef __entry
#define __entry entry
526

527
528
529
530
531
532
#undef __field
#define __field(type, item)

#undef __array
#define __array(type, item, len)

533
534
535
536
#undef __dynamic_array
#define __dynamic_array(type, item, len)				\
	__entry->__data_loc_##item = __data_offsets.item;

537
#undef __string
538
#define __string(item, src) __dynamic_array(char, item, -1)       	\
539
540
541
542
543

#undef __assign_str
#define __assign_str(dst, src)						\
	strcpy(__get_str(dst), src);

544
545
546
547
548
549
#undef TP_fast_assign
#define TP_fast_assign(args...) args

#undef TP_perf_assign
#define TP_perf_assign(args...)

550
551
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
552
									\
553
554
static notrace void							\
ftrace_raw_event_id_##call(struct ftrace_event_call *event_call,	\
555
				       proto)				\
556
{									\
557
	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
558
559
	struct ring_buffer_event *event;				\
	struct ftrace_raw_##call *entry;				\
560
	struct ring_buffer *buffer;					\
561
	unsigned long irq_flags;					\
562
	int __data_size;						\
563
564
565
566
567
	int pc;								\
									\
	local_save_flags(irq_flags);					\
	pc = preempt_count();						\
									\
568
	__data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
569
									\
570
	event = trace_current_buffer_lock_reserve(&buffer,		\
571
				 event_call->id,			\
572
				 sizeof(*entry) + __data_size,		\
573
				 irq_flags, pc);			\
574
575
576
577
	if (!event)							\
		return;							\
	entry	= ring_buffer_event_data(event);			\
									\
578
579
	tstruct								\
									\
580
	{ assign; }							\
581
									\
582
583
584
	if (!filter_current_check_discard(buffer, event_call, entry, event)) \
		trace_nowake_buffer_unlock_commit(buffer,		\
						  event, irq_flags, pc); \
585
586
587
588
589
}

#undef DEFINE_EVENT
#define DEFINE_EVENT(template, call, proto, args)			\
									\
590
static notrace void ftrace_raw_event_##call(proto)			\
591
592
{									\
	ftrace_raw_event_id_##template(&event_##call, args);		\
593
594
}									\
									\
595
596
static notrace int							\
ftrace_raw_reg_event_##call(struct ftrace_event_call *unused)		\
597
{									\
598
	return register_trace_##call(ftrace_raw_event_##call);		\
599
600
}									\
									\
601
602
static notrace void							\
ftrace_raw_unreg_event_##call(struct ftrace_event_call *unused)		\
603
604
605
606
607
608
{									\
	unregister_trace_##call(ftrace_raw_event_##call);		\
}									\
									\
static struct trace_event ftrace_event_type_##call = {			\
	.trace			= ftrace_raw_output_##call,		\
609
};
610
611
612
613
614
615
616

#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)

Lai Jiangshan's avatar
Lai Jiangshan committed
617
618
619
620
621
622
623
624
625
626
627
#undef __entry
#define __entry REC

#undef __print_flags
#undef __print_symbolic
#undef __get_dynamic_array
#undef __get_str

#undef TP_printk
#define TP_printk(fmt, args...) "\"" fmt "\", "  __stringify(args)

628
#undef DECLARE_EVENT_CLASS
Lai Jiangshan's avatar
Lai Jiangshan committed
629
630
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
static const char print_fmt_##call[] = print;
631
632
633

#undef DEFINE_EVENT
#define DEFINE_EVENT(template, call, proto, args)			\
634
635
636
637
									\
static struct ftrace_event_call __used					\
__attribute__((__aligned__(4)))						\
__attribute__((section("_ftrace_events"))) event_##call = {		\
638
	.name			= #call,				\
639
	.system			= __stringify(TRACE_SYSTEM),		\
640
	.event			= &ftrace_event_type_##call,		\
641
	.raw_init		= trace_event_raw_init,			\
642
643
	.regfunc		= ftrace_raw_reg_event_##call,		\
	.unregfunc		= ftrace_raw_unreg_event_##call,	\
Lai Jiangshan's avatar
Lai Jiangshan committed
644
	.print_fmt		= print_fmt_##template,			\
645
	.define_fields		= ftrace_define_fields_##template,	\
646
	_TRACE_PERF_INIT(call)					\
647
}
Peter Zijlstra's avatar
Peter Zijlstra committed
648

649
650
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, call, proto, args, print)		\
651
									\
Lai Jiangshan's avatar
Lai Jiangshan committed
652
653
static const char print_fmt_##call[] = print;				\
									\
654
655
656
static struct ftrace_event_call __used					\
__attribute__((__aligned__(4)))						\
__attribute__((section("_ftrace_events"))) event_##call = {		\
657
	.name			= #call,				\
658
	.system			= __stringify(TRACE_SYSTEM),		\
659
	.event			= &ftrace_event_type_##call,		\
660
	.raw_init		= trace_event_raw_init,			\
661
662
	.regfunc		= ftrace_raw_reg_event_##call,		\
	.unregfunc		= ftrace_raw_unreg_event_##call,	\
Lai Jiangshan's avatar
Lai Jiangshan committed
663
	.print_fmt		= print_fmt_##call,			\
664
	.define_fields		= ftrace_define_fields_##template,	\
665
	_TRACE_PERF_INIT(call)					\
666
}
Peter Zijlstra's avatar
Peter Zijlstra committed
667

668
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
Peter Zijlstra's avatar
Peter Zijlstra committed
669

670
/*
671
 * Define the insertion callback to perf events
672
673
674
675
 *
 * The job is very similar to ftrace_raw_event_<call> except that we don't
 * insert in the ring buffer but in a perf counter.
 *
676
 * static void ftrace_perf_<call>(proto)
677
678
679
 * {
 *	struct ftrace_data_offsets_<call> __maybe_unused __data_offsets;
 *	struct ftrace_event_call *event_call = &event_<call>;
680
 *	extern void perf_tp_event(int, u64, u64, void *, int);
681
 *	struct ftrace_raw_##call *entry;
682
 *	struct perf_trace_buf *trace_buf;
683
684
 *	u64 __addr = 0, __count = 1;
 *	unsigned long irq_flags;
685
 *	struct trace_entry *ent;
686
687
 *	int __entry_size;
 *	int __data_size;
688
 *	int __cpu
689
690
691
692
693
 *	int pc;
 *
 *	pc = preempt_count();
 *
 *	__data_size = ftrace_get_offsets_<call>(&__data_offsets, args);
694
695
696
697
698
699
 *
 *	// Below we want to get the aligned size by taking into account
 *	// the u32 field that will later store the buffer size
 *	__entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),
 *			     sizeof(u64));
 *	__entry_size -= sizeof(u32);
700
 *
701
702
703
704
705
706
 *	// Protect the non nmi buffer
 *	// This also protects the rcu read side
 *	local_irq_save(irq_flags);
 *	__cpu = smp_processor_id();
 *
 *	if (in_nmi())
707
 *		trace_buf = rcu_dereference_sched(perf_trace_buf_nmi);
708
 *	else
709
 *		trace_buf = rcu_dereference_sched(perf_trace_buf);
710
 *
711
 *	if (!trace_buf)
712
 *		goto end;
713
 *
714
715
716
717
718
719
720
721
722
723
724
725
 *	trace_buf = per_cpu_ptr(trace_buf, __cpu);
 *
 * 	// Avoid recursion from perf that could mess up the buffer
 * 	if (trace_buf->recursion++)
 *		goto end_recursion;
 *
 * 	raw_data = trace_buf->buf;
 *
 *	// Make recursion update visible before entering perf_tp_event
 *	// so that we protect from perf recursions.
 *
 *	barrier();
726
 *
727
728
729
730
731
732
 *	//zero dead bytes from alignment to avoid stack leak to userspace:
 *	*(u64 *)(&raw_data[__entry_size - sizeof(u64)]) = 0ULL;
 *	entry = (struct ftrace_raw_<call> *)raw_data;
 *	ent = &entry->ent;
 *	tracing_generic_entry_update(ent, irq_flags, pc);
 *	ent->type = event_call->id;
733
 *
734
 *	<tstruct> <- do some jobs with dynamic arrays
735
 *
736
 *	<assign>  <- affect our values
737
 *
738
 *	perf_tp_event(event_call->id, __addr, __count, entry,
739
 *		     __entry_size);  <- submit them to perf counter
740
741
742
743
 *
 * }
 */

744
#ifdef CONFIG_PERF_EVENTS
745

Lai Jiangshan's avatar
Lai Jiangshan committed
746
747
748
749
750
751
752
753
754
755
#undef __entry
#define __entry entry

#undef __get_dynamic_array
#define __get_dynamic_array(field)	\
		((void *)__entry + (__entry->__data_loc_##field & 0xffff))

#undef __get_str
#define __get_str(field) (char *)__get_dynamic_array(field)

756
757
758
759
760
761
#undef __perf_addr
#define __perf_addr(a) __addr = (a)

#undef __perf_count
#define __perf_count(c) __count = (c)

762
763
#undef DECLARE_EVENT_CLASS
#define DECLARE_EVENT_CLASS(call, proto, args, tstruct, assign, print)	\
764
static notrace void							\
765
perf_trace_templ_##call(struct ftrace_event_call *event_call,		\
766
			struct pt_regs *__regs, proto)			\
767
768
769
770
{									\
	struct ftrace_data_offsets_##call __maybe_unused __data_offsets;\
	struct ftrace_raw_##call *entry;				\
	u64 __addr = 0, __count = 1;					\
771
	struct hlist_head *head;					\
772
773
	int __entry_size;						\
	int __data_size;						\
774
	int rctx;							\
775
776
									\
	__data_size = ftrace_get_offsets_##call(&__data_offsets, args); \
777
778
	__entry_size = ALIGN(__data_size + sizeof(*entry) + sizeof(u32),\
			     sizeof(u64));				\
779
	__entry_size -= sizeof(u32);					\
780
									\
781
	if (WARN_ONCE(__entry_size > PERF_MAX_TRACE_SIZE,		\
782
783
		      "profile buffer not large enough"))		\
		return;							\
784
									\
785
	entry = (struct ftrace_raw_##call *)perf_trace_buf_prepare(	\
786
		__entry_size, event_call->id, __regs, &rctx);		\
787
788
	if (!entry)							\
		return;							\
789
									\
790
791
792
793
	tstruct								\
									\
	{ assign; }							\
									\
794
	head = per_cpu_ptr(event_call->perf_events, smp_processor_id());\
795
	perf_trace_buf_submit(entry, __entry_size, rctx, __addr,	\
796
		__count, __regs, head);					\
797
798
}

799
#undef DEFINE_EVENT
800
801
802
803
#define DEFINE_EVENT(template, call, proto, args)			\
static notrace void perf_trace_##call(proto)				\
{									\
	struct ftrace_event_call *event_call = &event_##call;		\
804
	struct pt_regs __regs;						\
805
									\
806
807
	perf_fetch_caller_regs(&__regs, 1);				\
	perf_trace_templ_##template(event_call, &__regs, args);		\
808
809
}

810
811
812
813
#undef DEFINE_EVENT_PRINT
#define DEFINE_EVENT_PRINT(template, name, proto, args, print)	\
	DEFINE_EVENT(template, name, PARAMS(proto), PARAMS(args))

814
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
815
#endif /* CONFIG_PERF_EVENTS */
816

Peter Zijlstra's avatar
Peter Zijlstra committed
817
818
#undef _TRACE_PROFILE_INIT