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