ef3c1bd8acfd7fb30f1eeccccf4016d2364d6063
[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; either
12 * version 2.1 of the License, or (at your option) any later version.
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/immediate.h>
29 #include <ust/core.h>
30 #include <urcu/list.h>
31 #include <ust/processor.h>
32 #include <ust/kcompat/kcompat.h>
33 #include <ust/kcompat/stringify.h>
34
35 #include <bits/wordsize.h>
36
37 struct marker;
38
39 /**
40 * marker_probe_func - Type of a marker probe function
41 * @mdata: marker data
42 * @probe_private: probe private data
43 * @call_private: call site private data
44 * @fmt: format string
45 * @args: variable argument list pointer. Use a pointer to overcome C's
46 * inability to pass this around as a pointer in a portable manner in
47 * the callee otherwise.
48 *
49 * Type of marker probe functions. They receive the mdata and need to parse the
50 * format string to recover the variable argument list.
51 */
52 typedef void marker_probe_func(const struct marker *mdata,
53 void *probe_private, struct registers *regs, void *call_private,
54 const char *fmt, va_list *args);
55
56 struct marker_probe_closure {
57 marker_probe_func *func; /* Callback */
58 void *probe_private; /* Private probe data */
59 };
60
61 struct marker {
62 const char *channel; /* Name of channel where to send data */
63 const char *name; /* Marker name */
64 const char *format; /* Marker format string, describing the
65 * variable argument list.
66 */
67 DEFINE_IMV(char, state);/* Immediate value state. */
68 char ptype; /* probe type : 0 : single, 1 : multi */
69 /* Probe wrapper */
70 u16 channel_id; /* Numeric channel identifier, dynamic */
71 u16 event_id; /* Numeric event identifier, dynamic */
72 void (*call)(const struct marker *mdata, void *call_private, struct registers *regs, ...);
73 struct marker_probe_closure single;
74 struct marker_probe_closure *multi;
75 const char *tp_name; /* Optional tracepoint name */
76 void *tp_cb; /* Optional tracepoint callback */
77 void *location; /* Address of marker in code */
78 } __attribute__((aligned(128)));/*
79 * Aligned on 128 bytes because it is
80 * globally visible and gcc happily
81 * align these on the structure size.
82 */
83
84 #define GET_MARKER(channel, name) (__mark_##channel##_##name)
85
86 #define _DEFINE_MARKER(channel, name, tp_name_str, tp_cb, format, unique, m) \
87 struct registers regs; \
88 \
89 /* This next asm has to be a basic inline asm (no input/output/clobber), \
90 * because it must not require %-sign escaping, as we most certainly \
91 * have some %-signs in the format string. \
92 */ \
93 asm volatile ( \
94 /* We only define these symbols if they have not yet been defined. Indeed, \
95 * if two markers with the same channel/name are on the same line, they \
96 * will try to create the same symbols, resulting in a conflict. This \
97 * is not unusual as it can be the result of function inlining. \
98 */ \
99 ".ifndef __mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) "\n\t" \
100 /*".section __markers_strings\n\t"*/ \
101 ".section __markers_strings,\"aw\",@progbits\n\t" \
102 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) ":\n\t" \
103 ".string \"" __stringify(channel) "\"\n\t" \
104 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_name_" __stringify(unique) ":\n\t" \
105 ".string \"" __stringify(name) "\"\n\t" \
106 "__mstrtab_" __stringify(channel) "_" __stringify(name) "_format_" __stringify(unique) ":\n\t" \
107 ".string " "\"" format "\"" "\n\t" \
108 ".previous\n\t" \
109 ".endif\n\t" \
110 ); \
111 asm volatile ( \
112 /*".section __markers\n\t"*/ \
113 ".section __markers,\"aw\",@progbits\n\t" \
114 ".balign 128\n\t" \
115 "2:\n\t" \
116 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_channel_" __stringify(unique) ")\n\t" /* channel string */ \
117 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_name_" __stringify(unique) ")\n\t" /* name string */ \
118 _ASM_PTR "(__mstrtab_" __stringify(channel) "_" __stringify(name) "_format_" __stringify(unique) ")\n\t" /* format string */ \
119 ".byte 0\n\t" /* state imv */ \
120 ".byte 0\n\t" /* ptype */ \
121 ".word 0\n\t" /* channel_id */ \
122 ".word 0\n\t" /* event_id */ \
123 ".balign " __stringify(__WORDSIZE) " / 8\n\t" /* alignment */ \
124 _ASM_PTR "(marker_probe_cb)\n\t" /* call */ \
125 _ASM_PTR "(__mark_empty_function)\n\t" /* marker_probe_closure single.field1 */ \
126 _ASM_PTR "0\n\t" /* marker_probe_closure single.field2 */ \
127 _ASM_PTR "0\n\t" /* marker_probe_closure *multi */ \
128 _ASM_PTR "0\n\t" /* tp_name */ \
129 _ASM_PTR "0\n\t" /* tp_cb */ \
130 _ASM_PTR "(1f)\n\t" /* location */ \
131 ".previous\n\t" \
132 "1:\n\t" \
133 ARCH_COPY_ADDR("%[outptr]") \
134 : [outptr] "=r" (m) ); \
135 \
136 save_registers(&regs)
137
138
139 #define DEFINE_MARKER(channel, name, format, unique, m) \
140 _DEFINE_MARKER(channel, name, NULL, NULL, format, unique, m)
141
142 #define DEFINE_MARKER_TP(channel, name, tp_name, tp_cb, format) \
143 _DEFINE_MARKER_TP(channel, name, #tp_name, tp_cb, format)
144
145 #define _DEFINE_MARKER_TP(channel, name, tp_name_str, tp_cb, format) \
146 static const char __mstrtab_##channel##_##name[] \
147 __attribute__((section("__markers_strings"))) \
148 = #channel "\0" #name "\0" format; \
149 static struct marker GET_MARKER(channel, name) \
150 __attribute__((section("__markers"), aligned(128))) = \
151 { __mstrtab_##channel##_##name, \
152 &__mstrtab_##channel##_##name[sizeof(#channel)], \
153 &__mstrtab_##channel##_##name[sizeof(#channel) + sizeof(#name)], \
154 0, 0, 0, 0, marker_probe_cb, \
155 { __mark_empty_function, NULL}, \
156 NULL, tp_name_str, tp_cb }
157
158 /*
159 * Make sure the alignment of the structure in the __markers section will
160 * not add unwanted padding between the beginning of the section and the
161 * structure. Force alignment to the same alignment as the section start.
162 *
163 * The "generic" argument controls which marker enabling mechanism must be used.
164 * If generic is true, a variable read is used.
165 * If generic is false, immediate values are used.
166 */
167
168 #define __trace_mark(generic, channel, name, call_private, format, args...) \
169 __trace_mark_counter(generic, channel, name, __LINE__, call_private, format, ## args)
170
171 #define __trace_mark_counter(generic, channel, name, unique, call_private, format, args...) \
172 do { \
173 struct marker *m; \
174 DEFINE_MARKER(channel, name, format, unique, m); \
175 __mark_check_format(format, ## args); \
176 if (!generic) { \
177 if (unlikely(imv_read(m->state))) \
178 (m->call)(m, call_private, &regs, ## args); \
179 } else { \
180 if (unlikely(_imv_read(m->state))) \
181 (m->call)(m, call_private, &regs, ## args); \
182 } \
183 } while (0)
184
185 #define __trace_mark_tp(channel, name, call_private, tp_name, tp_cb, format, args...) \
186 __trace_mark_tp_counter(channel, name, __LINE__, call_private, tp_name, tp_cb, format, ## args)
187
188 #define __trace_mark_tp_counter(channel, name, unique, call_private, tp_name, tp_cb, format, args...) \
189 do { \
190 struct registers regs; \
191 void __check_tp_type(void) \
192 { \
193 register_trace_##tp_name(tp_cb, call_private); \
194 } \
195 DEFINE_MARKER_TP(channel, name, tp_name, tp_cb, format);\
196 __mark_check_format(format, ## args); \
197 (*GET_MARKER(channel, name).call)(&GET_MARKER(channel, name), \
198 call_private, &regs, ## args); \
199 } while (0)
200
201 extern void marker_update_probe_range(struct marker *begin,
202 struct marker *end);
203
204 /**
205 * trace_mark - Marker using code patching
206 * @channel: marker channel (where to send the data), not quoted.
207 * @name: marker name, not quoted.
208 * @format: format string
209 * @args...: variable argument list
210 *
211 * Places a marker using optimized code patching technique (imv_read())
212 * to be enabled when immediate values are present.
213 */
214 #define trace_mark(channel, name, format, args...) \
215 __trace_mark(0, channel, name, NULL, format, ## args)
216
217 /**
218 * _trace_mark - Marker using variable read
219 * @channel: marker channel (where to send the data), not quoted.
220 * @name: marker name, not quoted.
221 * @format: format string
222 * @args...: variable argument list
223 *
224 * Places a marker using a standard memory read (_imv_read()) to be
225 * enabled. Should be used for markers in code paths where instruction
226 * modification based enabling is not welcome.
227 */
228 #define _trace_mark(channel, name, format, args...) \
229 __trace_mark(1, channel, name, NULL, format, ## args)
230
231 /**
232 * trace_mark_tp - Marker in a tracepoint callback
233 * @channel: marker channel (where to send the data), not quoted.
234 * @name: marker name, not quoted.
235 * @tp_name: tracepoint name, not quoted.
236 * @tp_cb: tracepoint callback. Should have an associated global symbol so it
237 * is not optimized away by the compiler (should not be static).
238 * @format: format string
239 * @args...: variable argument list
240 *
241 * Places a marker in a tracepoint callback.
242 */
243 #define trace_mark_tp(channel, name, tp_name, tp_cb, format, args...) \
244 __trace_mark_tp(channel, name, NULL, tp_name, tp_cb, format, ## args)
245
246 /**
247 * MARK_NOARGS - Format string for a marker with no argument.
248 */
249 #define MARK_NOARGS " "
250
251 extern void lock_markers(void);
252 extern void unlock_markers(void);
253
254 extern void markers_compact_event_ids(void);
255
256 /* To be used for string format validity checking with gcc */
257 static inline void __printf(1, 2) ___mark_check_format(const char *fmt, ...)
258 {
259 }
260
261 #define __mark_check_format(format, args...) \
262 do { \
263 if (0) \
264 ___mark_check_format(format, ## args); \
265 } while (0)
266
267 extern marker_probe_func __mark_empty_function;
268
269 extern void marker_probe_cb(const struct marker *mdata,
270 void *call_private, struct registers *regs, ...);
271
272 /*
273 * Connect a probe to a marker.
274 * private data pointer must be a valid allocated memory address, or NULL.
275 */
276 extern int marker_probe_register(const char *channel, const char *name,
277 const char *format, marker_probe_func *probe, void *probe_private);
278
279 /*
280 * Returns the private data given to marker_probe_register.
281 */
282 extern int marker_probe_unregister(const char *channel, const char *name,
283 marker_probe_func *probe, void *probe_private);
284 /*
285 * Unregister a marker by providing the registered private data.
286 */
287 extern int marker_probe_unregister_private_data(marker_probe_func *probe,
288 void *probe_private);
289
290 extern void *marker_get_private_data(const char *channel, const char *name,
291 marker_probe_func *probe, int num);
292
293 /*
294 * marker_synchronize_unregister must be called between the last marker probe
295 * unregistration and the first one of
296 * - the end of module exit function
297 * - the free of any resource used by the probes
298 * to ensure the code and data are valid for any possibly running probes.
299 */
300 #define marker_synchronize_unregister() synchronize_sched()
301
302 struct marker_iter {
303 //ust// struct module *module;
304 struct lib *lib;
305 struct marker *marker;
306 };
307
308 extern void marker_iter_start(struct marker_iter *iter);
309 extern void marker_iter_next(struct marker_iter *iter);
310 extern void marker_iter_stop(struct marker_iter *iter);
311 extern void marker_iter_reset(struct marker_iter *iter);
312 extern int marker_get_iter_range(struct marker **marker, struct marker *begin,
313 struct marker *end);
314
315 extern void marker_update_process(void);
316 extern int is_marker_enabled(const char *channel, const char *name);
317
318 //ust// #ifdef CONFIG_MARKERS_USERSPACE
319 //ust// extern void exit_user_markers(struct task_struct *p);
320 //ust// #else
321 //ust// static inline void exit_user_markers(struct task_struct *p)
322 //ust// {
323 //ust// }
324 //ust// #endif
325
326 struct marker_addr {
327 struct marker *marker;
328 void *addr;
329 };
330
331 struct lib {
332 struct marker *markers_start;
333 #ifdef CONFIG_UST_GDB_INTEGRATION
334 struct marker_addr *markers_addr_start;
335 #endif
336 int markers_count;
337 struct cds_list_head list;
338 };
339
340 extern int marker_register_lib(struct marker *markers_start, int markers_count);
341 extern int marker_unregister_lib(struct marker *markers_start);
342
343 #define MARKER_LIB \
344 extern struct marker __start___markers[] __attribute__((weak, visibility("hidden"))); \
345 extern struct marker __stop___markers[] __attribute__((weak, visibility("hidden"))); \
346 \
347 static void __attribute__((constructor)) __markers__init(void) \
348 { \
349 marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker)); \
350 } \
351 \
352 static void __attribute__((destructor)) __markers__destroy(void) \
353 { \
354 marker_unregister_lib(__start___markers); \
355 }
356
357 extern void marker_set_new_marker_cb(void (*cb)(struct marker *));
358 extern void init_markers(void);
359
360 #endif /* _UST_MARKER_H */
This page took 0.037381 seconds and 3 git commands to generate.