4850417946f38fa07ad86433c2329253ba5f7162
[ust.git] / include / ust / marker.h
1 /*
2 * Code markup for dynamic and static tracing.
3 *
4 * See Documentation/marker.txt.
5 *
6 * (C) Copyright 2006 Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
7 * (C) Copyright 2009 Pierre-Marc Fournier <pierre-marc dot fournier at polymtl dot ca>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation;
12 * version 2.1 of the License.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #ifndef _UST_MARKER_H
25 #define _UST_MARKER_H
26
27 #include <stdarg.h>
28 #include <ust/core.h>
29 #include <urcu/list.h>
30 #include <ust/processor.h>
31 #include <ust/kcompat/kcompat.h>
32 #include <ust/kcompat/stringify.h>
33
34 #include <bits/wordsize.h>
35
36 struct ust_marker;
37
38 /**
39 * ust_marker_probe_func - Type of a marker probe function
40 * @mdata: marker data
41 * @probe_private: probe private data
42 * @call_private: call site private data
43 * @fmt: format string
44 * @args: variable argument list pointer. Use a pointer to overcome C's
45 * inability to pass this around as a pointer in a portable manner in
46 * the callee otherwise.
47 *
48 * Type of marker probe functions. They receive the mdata and need to parse the
49 * format string to recover the variable argument list.
50 */
51 typedef void ust_marker_probe_func(const struct ust_marker *mdata,
52 void *probe_private, struct registers *regs, void *call_private,
53 const char *fmt, va_list *args);
54
55 struct ust_marker_probe_closure {
56 ust_marker_probe_func *func; /* Callback */
57 void *probe_private; /* Private probe data */
58 };
59
60 struct ust_marker {
61 const char *channel; /* Name of channel where to send data */
62 const char *name; /* Marker name */
63 const char *format; /* Marker format string, describing the
64 * variable argument list.
65 */
66 char state; /* State. */
67 char ptype; /* probe type : 0 : single, 1 : multi */
68 /* Probe wrapper */
69 u16 channel_id; /* Numeric channel identifier, dynamic */
70 u16 event_id; /* Numeric event identifier, dynamic */
71 void (*call)(const struct ust_marker *mdata, void *call_private, struct registers *regs, ...);
72 struct ust_marker_probe_closure single;
73 struct ust_marker_probe_closure *multi;
74 const char *tp_name; /* Optional tracepoint name */
75 void *tp_cb; /* Optional tracepoint callback */
76 void *location; /* Address of marker in code */
77 };
78
79 #define GET_UST_MARKER(name) (__ust_marker_ust_##name)
80
81 #define _DEFINE_UST_MARKER(channel, name, tp_name_str, tp_cb, format, unique, m) \
82 struct registers __ust_marker_regs; \
83 \
84 /* This next asm has to be a basic inline asm (no input/output/clobber), \
85 * because it must not require %-sign escaping, as we most certainly \
86 * have some %-signs in the format string. \
87 */ \
88 asm volatile ( \
89 /* We only define these symbols if they have not yet been defined. Indeed, \
90 * if two markers with the same channel/name are on the same line, they \
91 * will try to create the same symbols, resulting in a conflict. This \
92 * is not unusual as it can be the result of function inlining. \
93 */ \
94 ".ifndef __mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) "\n\t" \
95 /*".section __ust_marker_strings\n\t"*/ \
96 ".section __ust_marker_strings,\"aw\"\n\t" \
97 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) ":\n\t" \
98 ".string \"" __stringify(channel) "\"\n\t" \
99 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_name_" __stringify(unique) ":\n\t" \
100 ".string \"" __stringify(name) "\"\n\t" \
101 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_format_" __stringify(unique) ":\n\t" \
102 ".string " "\"" format "\"" "\n\t" \
103 ".previous\n\t" \
104 ".endif\n\t" \
105 ); \
106 asm volatile ( \
107 /*".section __ust_marker\n\t"*/ \
108 ".section __ust_marker,\"aw\"\n\t" \
109 "2:\n\t" \
110 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) ")\n\t" /* channel string */ \
111 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_name_" __stringify(unique) ")\n\t" /* name string */ \
112 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_format_" __stringify(unique) ")\n\t" /* format string */ \
113 ".byte 0\n\t" /* state imv */ \
114 ".byte 0\n\t" /* ptype */ \
115 ".hword 0\n\t" /* channel_id */ \
116 ".hword 0\n\t" /* event_id */ \
117 ".balign " __stringify(__WORDSIZE) " / 8\n\t" /* alignment */ \
118 _ASM_PTR "(ust_marker_probe_cb)\n\t" /* call */ \
119 _ASM_PTR "(__ust_marker_empty_function)\n\t" /* ust_marker_probe_closure single.field1 */ \
120 _ASM_PTR "0\n\t" /* ust_marker_probe_closure single.field2 */ \
121 _ASM_PTR "0\n\t" /* ust_marker_probe_closure *multi */ \
122 _ASM_PTR "0\n\t" /* tp_name */ \
123 _ASM_PTR "0\n\t" /* tp_cb */ \
124 _ASM_PTR "(1f)\n\t" /* location */ \
125 ".previous\n\t" \
126 /*".section __ust_marker_ptrs\n\t"*/ \
127 ".section __ust_marker_ptrs,\"a\"\n\t" \
128 _ASM_PTR "(2b)\n\t" \
129 ".previous\n\t" \
130 "1:\n\t" \
131 ARCH_COPY_ADDR("%[outptr]") \
132 : [outptr] "=r" (m) ); \
133 \
134 save_registers(&__ust_marker_regs)
135
136
137 #define DEFINE_UST_MARKER(name, format, unique, m) \
138 _DEFINE_UST_MARKER(ust, name, NULL, NULL, format, unique, m)
139
140 #define DEFINE_UST_MARKER_TP(name, tp_name, tp_cb, format) \
141 _DEFINE_UST_MARKER_TP(ust, name, #tp_name, tp_cb, format)
142
143 #define _DEFINE_UST_MARKER_TP(channel, name, tp_name_str, tp_cb, format)\
144 static const char __mstrtab_##channel##_##name[] \
145 __attribute__((section("__ust_marker_strings"))) \
146 = #channel "\0" #name "\0" format; \
147 static struct ust_marker __ust_marker_##channel##_##name\
148 __attribute__((section("__ust_marker"))) = \
149 { __mstrtab_##channel##_##name, \
150 &__mstrtab_##channel##_##name[sizeof(#channel)],\
151 &__mstrtab_##channel##_##name[sizeof(#channel) + sizeof(#name)], \
152 0, 0, 0, 0, ust_marker_probe_cb, \
153 { __ust_marker_empty_function, NULL}, \
154 NULL, tp_name_str, tp_cb }; \
155 static struct ust_marker * const __ust_marker_ptr_##channel##_##name \
156 __attribute__((used, section("__ust_marker_ptrs"))) = \
157 &__ust_marker_##channel##_##name;
158
159 /*
160 * Make sure the alignment of the structure in the __ust_marker section will
161 * not add unwanted padding between the beginning of the section and the
162 * structure. Force alignment to the same alignment as the section start.
163 */
164
165 #define __ust_marker(channel, name, call_private, format, args...) \
166 __ust_marker_counter(channel, name, __LINE__, call_private, format, ## args)
167
168 #define __ust_marker_counter(channel, name, unique, call_private, format, args...) \
169 do { \
170 struct ust_marker *__ust_marker_counter_ptr; \
171 _DEFINE_UST_MARKER(channel, name, NULL, NULL, format, unique, __ust_marker_counter_ptr); \
172 __ust_marker_check_format(format, ## args); \
173 if (unlikely(__ust_marker_counter_ptr->state)) \
174 (__ust_marker_counter_ptr->call)(__ust_marker_counter_ptr, call_private, &__ust_marker_regs, ## args); \
175 } while (0)
176
177 #define __ust_marker_tp(channel, name, call_private, tp_name, tp_cb, format, args...) \
178 __ust_marker_tp_counter(channel, name, __LINE__, call_private, tp_name, tp_cb, format, ## args)
179
180 #define __ust_marker_tp_counter(channel, name, unique, call_private, tp_name, tp_cb, format, args...) \
181 do { \
182 struct registers __ust_marker_regs; \
183 void __check_tp_type(void) \
184 { \
185 register_trace_##tp_name(tp_cb, call_private); \
186 } \
187 _DEFINE_UST_MARKER_TP(channel, name, #tp_name, tp_cb, format); \
188 __ust_marker_check_format(format, ## args); \
189 (*__ust_marker_##channel##_##name.call)(&__ust_marker_##channel##_##name, \
190 call_private, &__ust_marker_regs, ## args); \
191 } while (0)
192
193 extern void ust_marker_update_probe_range(struct ust_marker * const *begin,
194 struct ust_marker * const *end);
195
196 /**
197 * ust_marker - Marker using code patching
198 * @name: marker name, not quoted.
199 * @format: format string
200 * @args...: variable argument list
201 *
202 * Places a marker at caller site.
203 */
204 #define ust_marker(name, format, args...) \
205 __ust_marker(ust, name, NULL, format, ## args)
206
207 /**
208 * ust_marker_tp - Marker in a tracepoint callback
209 * @name: marker name, not quoted.
210 * @tp_name: tracepoint name, not quoted.
211 * @tp_cb: tracepoint callback. Should have an associated global symbol so it
212 * is not optimized away by the compiler (should not be static).
213 * @format: format string
214 * @args...: variable argument list
215 *
216 * Places a marker in a tracepoint callback.
217 */
218 #define ust_marker_tp(name, tp_name, tp_cb, format, args...) \
219 __ust_marker_tp(ust, name, NULL, tp_name, tp_cb, format, ## args)
220
221 /**
222 * UST_MARKER_NOARGS - Format string for a marker with no argument.
223 */
224 #define UST_MARKER_NOARGS " "
225
226 extern void lock_ust_marker(void);
227 extern void unlock_ust_marker(void);
228
229 extern void ust_marker_compact_event_ids(void);
230
231 /* To be used for string format validity checking with gcc */
232 static inline void __printf(1, 2) ___ust_marker_check_format(const char *fmt, ...)
233 {
234 }
235
236 #define __ust_marker_check_format(format, args...) \
237 do { \
238 if (0) \
239 ___ust_marker_check_format(format, ## args); \
240 } while (0)
241
242 extern ust_marker_probe_func __ust_marker_empty_function;
243
244 extern void ust_marker_probe_cb(const struct ust_marker *mdata,
245 void *call_private, struct registers *regs, ...);
246
247 /*
248 * Connect a probe to a marker.
249 * private data pointer must be a valid allocated memory address, or NULL.
250 */
251 extern int ust_marker_probe_register(const char *channel, const char *name,
252 const char *format, ust_marker_probe_func *probe, void *probe_private);
253
254 /*
255 * Returns the private data given to ust_marker_probe_register.
256 */
257 extern int ust_marker_probe_unregister(const char *channel, const char *name,
258 ust_marker_probe_func *probe, void *probe_private);
259 /*
260 * Unregister a marker by providing the registered private data.
261 */
262 extern int ust_marker_probe_unregister_private_data(ust_marker_probe_func *probe,
263 void *probe_private);
264
265 extern void *ust_marker_get_private_data(const char *channel, const char *name,
266 ust_marker_probe_func *probe, int num);
267
268 /*
269 * ust_marker_synchronize_unregister must be called between the last marker probe
270 * unregistration and the first one of
271 * - the end of module exit function
272 * - the free of any resource used by the probes
273 * to ensure the code and data are valid for any possibly running probes.
274 */
275 #define ust_marker_synchronize_unregister() synchronize_sched()
276
277 struct ust_marker_iter {
278 //ust// struct module *module;
279 struct ust_marker_lib *lib;
280 struct ust_marker * const *ust_marker;
281 };
282
283 extern void ust_marker_iter_start(struct ust_marker_iter *iter);
284 extern void ust_marker_iter_next(struct ust_marker_iter *iter);
285 extern void ust_marker_iter_stop(struct ust_marker_iter *iter);
286 extern void ust_marker_iter_reset(struct ust_marker_iter *iter);
287 extern int ust_marker_get_iter_range(struct ust_marker * const **marker, struct ust_marker * const *begin,
288 struct ust_marker * const *end);
289
290 extern void ust_marker_update_process(void);
291 extern int is_ust_marker_enabled(const char *channel, const char *name);
292
293 //ust// #ifdef CONFIG_UST_MARKER_USERSPACE
294 //ust// extern void exit_user_ust_marker(struct task_struct *p);
295 //ust// #else
296 //ust// static inline void exit_user_ust_marker(struct task_struct *p)
297 //ust// {
298 //ust// }
299 //ust// #endif
300
301 struct ust_marker_addr {
302 struct ust_marker *marker;
303 void *addr;
304 };
305
306 struct ust_marker_lib {
307 struct ust_marker * const *ust_marker_start;
308 #ifdef CONFIG_UST_GDB_INTEGRATION
309 struct ust_marker_addr *ust_marker_addr_start;
310 #endif
311 int ust_marker_count;
312 struct cds_list_head list;
313 };
314
315 extern int ust_marker_register_lib(struct ust_marker * const *ust_marker_start, int ust_marker_count);
316 extern int ust_marker_unregister_lib(struct ust_marker * const *ust_marker_start);
317
318 #define UST_MARKER_LIB \
319 extern struct ust_marker * const __start___ust_marker_ptrs[] __attribute__((weak, visibility("hidden"))); \
320 extern struct ust_marker * const __stop___ust_marker_ptrs[] __attribute__((weak, visibility("hidden"))); \
321 static struct ust_marker * const __ust_marker_ptr_dummy \
322 __attribute__((used, section("__ust_marker_ptrs"))) = NULL;\
323 \
324 static void __attribute__((constructor)) __ust_marker__init(void) \
325 { \
326 ust_marker_register_lib(__start___ust_marker_ptrs, \
327 __stop___ust_marker_ptrs \
328 - __start___ust_marker_ptrs); \
329 } \
330 \
331 static void __attribute__((destructor)) __ust_marker__destroy(void)\
332 { \
333 ust_marker_unregister_lib(__start___ust_marker_ptrs); \
334 }
335
336 extern void ust_marker_set_new_ust_marker_cb(void (*cb)(struct ust_marker *));
337 extern void init_ust_marker(void);
338
339 #endif /* _UST_MARKER_H */
This page took 0.036361 seconds and 3 git commands to generate.