1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * trace_export.c - export basic ftrace utilities to user space
4  *
5  * Copyright (C) 2009 Steven Rostedt <srostedt@redhat.com>
6  */
7 #include <linux/stringify.h>
8 #include <linux/kallsyms.h>
9 #include <linux/seq_file.h>
10 #include <linux/uaccess.h>
11 #include <linux/ftrace.h>
12 #include <linux/module.h>
13 #include <linux/init.h>
14 
15 #include "trace_output.h"
16 
17 /* Stub function for events with triggers */
ftrace_event_register(struct trace_event_call * call,enum trace_reg type,void * data)18 static int ftrace_event_register(struct trace_event_call *call,
19 				 enum trace_reg type, void *data)
20 {
21 	return 0;
22 }
23 
24 #undef TRACE_SYSTEM
25 #define TRACE_SYSTEM	ftrace
26 
27 /*
28  * The FTRACE_ENTRY_REG macro allows ftrace entry to define register
29  * function and thus become accessible via perf.
30  */
31 #undef FTRACE_ENTRY_REG
32 #define FTRACE_ENTRY_REG(name, struct_name, id, tstruct, print, regfn) \
33 	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print))
34 
35 /* not needed for this file */
36 #undef __field_struct
37 #define __field_struct(type, item)
38 
39 #undef __field
40 #define __field(type, item)				type item;
41 
42 #undef __field_fn
43 #define __field_fn(type, item)				type item;
44 
45 #undef __field_desc
46 #define __field_desc(type, container, item)		type item;
47 
48 #undef __field_packed
49 #define __field_packed(type, container, item)		type item;
50 
51 #undef __array
52 #define __array(type, item, size)			type item[size];
53 
54 #undef __array_desc
55 #define __array_desc(type, container, item, size)	type item[size];
56 
57 #undef __dynamic_array
58 #define __dynamic_array(type, item)			type item[];
59 
60 #undef F_STRUCT
61 #define F_STRUCT(args...)				args
62 
63 #undef F_printk
64 #define F_printk(fmt, args...) fmt, args
65 
66 #undef FTRACE_ENTRY
67 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print)		\
68 struct ____ftrace_##name {						\
69 	tstruct								\
70 };									\
71 static void __always_unused ____ftrace_check_##name(void)		\
72 {									\
73 	struct ____ftrace_##name *__entry = NULL;			\
74 									\
75 	/* force compile-time check on F_printk() */			\
76 	printk(print);							\
77 }
78 
79 #undef FTRACE_ENTRY_DUP
80 #define FTRACE_ENTRY_DUP(name, struct_name, id, tstruct, print)		\
81 	FTRACE_ENTRY(name, struct_name, id, PARAMS(tstruct), PARAMS(print))
82 
83 #include "trace_entries.h"
84 
85 #undef __field_ext
86 #define __field_ext(_type, _item, _filter_type) {			\
87 	.type = #_type, .name = #_item,					\
88 	.size = sizeof(_type), .align = __alignof__(_type),		\
89 	is_signed_type(_type), .filter_type = _filter_type },
90 
91 
92 #undef __field_ext_packed
93 #define __field_ext_packed(_type, _item, _filter_type) {	\
94 	.type = #_type, .name = #_item,				\
95 	.size = sizeof(_type), .align = 1,			\
96 	is_signed_type(_type), .filter_type = _filter_type },
97 
98 #undef __field
99 #define __field(_type, _item) __field_ext(_type, _item, FILTER_OTHER)
100 
101 #undef __field_fn
102 #define __field_fn(_type, _item) __field_ext(_type, _item, FILTER_TRACE_FN)
103 
104 #undef __field_desc
105 #define __field_desc(_type, _container, _item) __field_ext(_type, _item, FILTER_OTHER)
106 
107 #undef __field_packed
108 #define __field_packed(_type, _container, _item) __field_ext_packed(_type, _item, FILTER_OTHER)
109 
110 #undef __array
111 #define __array(_type, _item, _len) {					\
112 	.type = #_type"["__stringify(_len)"]", .name = #_item,		\
113 	.size = sizeof(_type[_len]), .align = __alignof__(_type),	\
114 	is_signed_type(_type), .filter_type = FILTER_OTHER },
115 
116 #undef __array_desc
117 #define __array_desc(_type, _container, _item, _len) __array(_type, _item, _len)
118 
119 #undef __dynamic_array
120 #define __dynamic_array(_type, _item) {					\
121 	.type = #_type "[]", .name = #_item,				\
122 	.size = 0, .align = __alignof__(_type),				\
123 	is_signed_type(_type), .filter_type = FILTER_OTHER },
124 
125 #undef FTRACE_ENTRY
126 #define FTRACE_ENTRY(name, struct_name, id, tstruct, print)		\
127 static struct trace_event_fields ftrace_event_fields_##name[] = {	\
128 	tstruct								\
129 	{} };
130 
131 #include "trace_entries.h"
132 
133 #undef __entry
134 #define __entry REC
135 
136 #undef __field
137 #define __field(type, item)
138 
139 #undef __field_fn
140 #define __field_fn(type, item)
141 
142 #undef __field_desc
143 #define __field_desc(type, container, item)
144 
145 #undef __field_packed
146 #define __field_packed(type, container, item)
147 
148 #undef __array
149 #define __array(type, item, len)
150 
151 #undef __array_desc
152 #define __array_desc(type, container, item, len)
153 
154 #undef __dynamic_array
155 #define __dynamic_array(type, item)
156 
157 #undef F_printk
158 #define F_printk(fmt, args...) __stringify(fmt) ", "  __stringify(args)
159 
160 #undef FTRACE_ENTRY_REG
161 #define FTRACE_ENTRY_REG(call, struct_name, etype, tstruct, print, regfn) \
162 static struct trace_event_class __refdata event_class_ftrace_##call = {	\
163 	.system			= __stringify(TRACE_SYSTEM),		\
164 	.fields_array		= ftrace_event_fields_##call,		\
165 	.fields			= LIST_HEAD_INIT(event_class_ftrace_##call.fields),\
166 	.reg			= regfn,				\
167 };									\
168 									\
169 struct trace_event_call __used event_##call = {				\
170 	.class			= &event_class_ftrace_##call,		\
171 	{								\
172 		.name			= #call,			\
173 	},								\
174 	.event.type		= etype,				\
175 	.print_fmt		= print,				\
176 	.flags			= TRACE_EVENT_FL_IGNORE_ENABLE,		\
177 };									\
178 static struct trace_event_call __used						\
179 __section("_ftrace_events") *__event_##call = &event_##call;
180 
181 #undef FTRACE_ENTRY
182 #define FTRACE_ENTRY(call, struct_name, etype, tstruct, print)		\
183 	FTRACE_ENTRY_REG(call, struct_name, etype,			\
184 			 PARAMS(tstruct), PARAMS(print), NULL)
185 
ftrace_event_is_function(struct trace_event_call * call)186 bool ftrace_event_is_function(struct trace_event_call *call)
187 {
188 	return call == &event_function;
189 }
190 
191 #include "trace_entries.h"
192