481bc79b0f42e7295ef6c284498f6123db76b8bd
[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,\"aw\"\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 /*
209 * trace_mark() -- TO BE DEPRECATED
210 * @channel: name prefix, not quoted. Ignored.
211 * @name: marker name, not quoted.
212 * @format: format string
213 * @args...: variable argument list
214 *
215 * Kept as a compatibility API and will be *DEPRECATED* in favor of
216 * ust_marker().
217 */
218 #define trace_mark(channel, name, format, args...) \
219 ust_marker(ust, name, format, args)
220
221 /**
222 * ust_marker_tp - Marker in a tracepoint callback
223 * @name: marker name, not quoted.
224 * @tp_name: tracepoint name, not quoted.
225 * @tp_cb: tracepoint callback. Should have an associated global symbol so it
226 * is not optimized away by the compiler (should not be static).
227 * @format: format string
228 * @args...: variable argument list
229 *
230 * Places a marker in a tracepoint callback.
231 */
232 #define ust_marker_tp(name, tp_name, tp_cb, format, args...) \
233 __ust_marker_tp(ust, name, NULL, tp_name, tp_cb, format, ## args)
234
235 /**
236 * UST_MARKER_NOARGS - Format string for a marker with no argument.
237 */
238 #define UST_MARKER_NOARGS " "
239
240 extern void lock_ust_marker(void);
241 extern void unlock_ust_marker(void);
242
243 extern void ust_marker_compact_event_ids(void);
244
245 /* To be used for string format validity checking with gcc */
246 static inline void __printf(1, 2) ___ust_marker_check_format(const char *fmt, ...)
247 {
248 }
249
250 #define __ust_marker_check_format(format, args...) \
251 do { \
252 if (0) \
253 ___ust_marker_check_format(format, ## args); \
254 } while (0)
255
256 extern ust_marker_probe_func __ust_marker_empty_function;
257
258 extern void ust_marker_probe_cb(const struct ust_marker *mdata,
259 void *call_private, struct registers *regs, ...);
260
261 /*
262 * Connect a probe to a marker.
263 * private data pointer must be a valid allocated memory address, or NULL.
264 */
265 extern int ust_marker_probe_register(const char *channel, const char *name,
266 const char *format, ust_marker_probe_func *probe, void *probe_private);
267
268 /*
269 * Returns the private data given to ust_marker_probe_register.
270 */
271 extern int ust_marker_probe_unregister(const char *channel, const char *name,
272 ust_marker_probe_func *probe, void *probe_private);
273 /*
274 * Unregister a marker by providing the registered private data.
275 */
276 extern int ust_marker_probe_unregister_private_data(ust_marker_probe_func *probe,
277 void *probe_private);
278
279 extern void *ust_marker_get_private_data(const char *channel, const char *name,
280 ust_marker_probe_func *probe, int num);
281
282 /*
283 * ust_marker_synchronize_unregister must be called between the last marker probe
284 * unregistration and the first one of
285 * - the end of module exit function
286 * - the free of any resource used by the probes
287 * to ensure the code and data are valid for any possibly running probes.
288 */
289 #define ust_marker_synchronize_unregister() synchronize_sched()
290
291 struct ust_marker_iter {
292 //ust// struct module *module;
293 struct ust_marker_lib *lib;
294 struct ust_marker * const *ust_marker;
295 };
296
297 extern void ust_marker_iter_start(struct ust_marker_iter *iter);
298 extern void ust_marker_iter_next(struct ust_marker_iter *iter);
299 extern void ust_marker_iter_stop(struct ust_marker_iter *iter);
300 extern void ust_marker_iter_reset(struct ust_marker_iter *iter);
301 extern int ust_marker_get_iter_range(struct ust_marker * const **marker, struct ust_marker * const *begin,
302 struct ust_marker * const *end);
303
304 extern void ust_marker_update_process(void);
305 extern int is_ust_marker_enabled(const char *channel, const char *name);
306
307 //ust// #ifdef CONFIG_UST_MARKER_USERSPACE
308 //ust// extern void exit_user_ust_marker(struct task_struct *p);
309 //ust// #else
310 //ust// static inline void exit_user_ust_marker(struct task_struct *p)
311 //ust// {
312 //ust// }
313 //ust// #endif
314
315 struct ust_marker_addr {
316 struct ust_marker *marker;
317 void *addr;
318 };
319
320 struct ust_marker_lib {
321 struct ust_marker * const *ust_marker_start;
322 #ifdef CONFIG_UST_GDB_INTEGRATION
323 struct ust_marker_addr *ust_marker_addr_start;
324 #endif
325 int ust_marker_count;
326 struct cds_list_head list;
327 };
328
329 extern int ust_marker_register_lib(struct ust_marker * const *ust_marker_start, int ust_marker_count);
330 extern int ust_marker_unregister_lib(struct ust_marker * const *ust_marker_start);
331
332 #define UST_MARKER_LIB \
333 extern struct ust_marker * const __start___ust_marker_ptrs[] __attribute__((weak, visibility("hidden"))); \
334 extern struct ust_marker * const __stop___ust_marker_ptrs[] __attribute__((weak, visibility("hidden"))); \
335 static struct ust_marker * const __ust_marker_ptr_dummy \
336 __attribute__((used, section("__ust_marker_ptrs"))) = NULL;\
337 \
338 static void __attribute__((constructor)) __ust_marker__init(void) \
339 { \
340 ust_marker_register_lib(__start___ust_marker_ptrs, \
341 __stop___ust_marker_ptrs \
342 - __start___ust_marker_ptrs); \
343 } \
344 \
345 static void __attribute__((destructor)) __ust_marker__destroy(void)\
346 { \
347 ust_marker_unregister_lib(__start___ust_marker_ptrs); \
348 }
349
350 extern void ust_marker_set_new_ust_marker_cb(void (*cb)(struct ust_marker *));
351 extern void init_ust_marker(void);
352
353 #endif /* _UST_MARKER_H */
This page took 0.03667 seconds and 3 git commands to generate.