Commit | Line | Data |
---|---|---|
a72ca31a PMF |
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> | |
59b161cd | 7 | * (C) Copyright 2009 Pierre-Marc Fournier <pierre-marc dot fournier at polymtl dot ca> |
a72ca31a | 8 | * |
8fc2d8db PMF |
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 | |
a72ca31a PMF |
22 | */ |
23 | ||
2288bf6d PMF |
24 | #ifndef _UST_MARKER_H |
25 | #define _UST_MARKER_H | |
e4621c7f | 26 | |
a72ca31a | 27 | #include <stdarg.h> |
93d0f2ea | 28 | #include <ust/immediate.h> |
518d7abb | 29 | #include <ust/core.h> |
22d9080d | 30 | #include <urcu/list.h> |
2782fc4b | 31 | #include <ust/processor.h> |
518d7abb | 32 | #include <ust/kcompat/kcompat.h> |
59b161cd | 33 | |
9389f19d PMF |
34 | #include <bits/wordsize.h> |
35 | ||
a72ca31a PMF |
36 | struct marker; |
37 | ||
a4db742a PMF |
38 | /* To stringify the expansion of a define */ |
39 | #define XSTR(d) STR(d) | |
40 | #define STR(s) #s | |
41 | ||
a72ca31a PMF |
42 | /** |
43 | * marker_probe_func - Type of a marker probe function | |
44 | * @mdata: marker data | |
45 | * @probe_private: probe private data | |
46 | * @call_private: call site private data | |
47 | * @fmt: format string | |
48 | * @args: variable argument list pointer. Use a pointer to overcome C's | |
49 | * inability to pass this around as a pointer in a portable manner in | |
50 | * the callee otherwise. | |
51 | * | |
52 | * Type of marker probe functions. They receive the mdata and need to parse the | |
53 | * format string to recover the variable argument list. | |
54 | */ | |
55 | typedef void marker_probe_func(const struct marker *mdata, | |
75667d04 | 56 | void *probe_private, struct registers *regs, void *call_private, |
a72ca31a PMF |
57 | const char *fmt, va_list *args); |
58 | ||
59 | struct marker_probe_closure { | |
60 | marker_probe_func *func; /* Callback */ | |
61 | void *probe_private; /* Private probe data */ | |
62 | }; | |
63 | ||
64 | struct marker { | |
65 | const char *channel; /* Name of channel where to send data */ | |
66 | const char *name; /* Marker name */ | |
67 | const char *format; /* Marker format string, describing the | |
68 | * variable argument list. | |
69 | */ | |
59b161cd | 70 | DEFINE_IMV(char, state);/* Immediate value state. */ |
a72ca31a PMF |
71 | char ptype; /* probe type : 0 : single, 1 : multi */ |
72 | /* Probe wrapper */ | |
73 | u16 channel_id; /* Numeric channel identifier, dynamic */ | |
74 | u16 event_id; /* Numeric event identifier, dynamic */ | |
75667d04 | 75 | void (*call)(const struct marker *mdata, void *call_private, struct registers *regs, ...); |
a72ca31a PMF |
76 | struct marker_probe_closure single; |
77 | struct marker_probe_closure *multi; | |
78 | const char *tp_name; /* Optional tracepoint name */ | |
79 | void *tp_cb; /* Optional tracepoint callback */ | |
3ea1e2fc | 80 | void *location; /* Address of marker in code */ |
a72ca31a PMF |
81 | } __attribute__((aligned(8))); |
82 | ||
872037bb PMF |
83 | #define CONFIG_MARKERS |
84 | #ifdef CONFIG_MARKERS | |
a72ca31a | 85 | |
9692fa18 | 86 | #define _DEFINE_MARKER(channel, name, tp_name_str, tp_cb, format, unique, m) \ |
bb9ade29 | 87 | struct registers regs; \ |
bb9ade29 | 88 | \ |
a4db742a | 89 | /* This next asm has to be a basic inline asm (no input/output/clobber), \ |
9692fa18 PMF |
90 | * because it must not require %-sign escaping, as we most certainly \ |
91 | * have some %-signs in the format string. \ | |
92 | */ \ | |
a4db742a | 93 | asm volatile ( \ |
9692fa18 PMF |
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_" XSTR(channel) "_" XSTR(name) "_channel_" XSTR(unique) "\n\t" \ | |
7166e240 | 100 | /*".section __markers_strings\n\t"*/ \ |
a4db742a | 101 | ".section __markers_strings,\"aw\",@progbits\n\t" \ |
a4db742a PMF |
102 | "__mstrtab_" XSTR(channel) "_" XSTR(name) "_channel_" XSTR(unique) ":\n\t" \ |
103 | ".string \"" XSTR(channel) "\"\n\t" \ | |
104 | "__mstrtab_" XSTR(channel) "_" XSTR(name) "_name_" XSTR(unique) ":\n\t" \ | |
105 | ".string \"" XSTR(name) "\"\n\t" \ | |
106 | "__mstrtab_" XSTR(channel) "_" XSTR(name) "_format_" XSTR(unique) ":\n\t" \ | |
c6794136 | 107 | ".string " "\"" format "\"" "\n\t" \ |
a4db742a | 108 | ".previous\n\t" \ |
9692fa18 PMF |
109 | ".endif\n\t" \ |
110 | ); \ | |
111 | asm volatile ( \ | |
7166e240 | 112 | /*".section __markers\n\t"*/ \ |
a4db742a | 113 | ".section __markers,\"aw\",@progbits\n\t" \ |
6fa0d97f | 114 | ".balign 8\n\t" \ |
9692fa18 | 115 | "2:\n\t" \ |
6b68d79d PMF |
116 | _ASM_PTR "(__mstrtab_" XSTR(channel) "_" XSTR(name) "_channel_" XSTR(unique) ")\n\t" /* channel string */ \ |
117 | _ASM_PTR "(__mstrtab_" XSTR(channel) "_" XSTR(name) "_name_" XSTR(unique) ")\n\t" /* name string */ \ | |
118 | _ASM_PTR "(__mstrtab_" XSTR(channel) "_" XSTR(name) "_format_" XSTR(unique) ")\n\t" /* format string */ \ | |
6793a8bf PMF |
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 */ \ | |
6fa0d97f | 123 | ".balign " XSTR(__WORDSIZE) " / 8\n\t" /* alignment */ \ |
6793a8bf | 124 | _ASM_PTR "(marker_probe_cb)\n\t" /* call */ \ |
6b68d79d | 125 | _ASM_PTR "(__mark_empty_function)\n\t" /* marker_probe_closure single.field1 */ \ |
6793a8bf PMF |
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 */ \ | |
6793a8bf PMF |
130 | _ASM_PTR "(1f)\n\t" /* location */ \ |
131 | ".previous\n\t" \ | |
6fa0d97f | 132 | ARCH_COPY_ADDR("%[outptr]") \ |
6793a8bf | 133 | "1:\n\t" \ |
9692fa18 | 134 | : [outptr] "=r" (m) ); \ |
bb9ade29 | 135 | \ |
3ea1e2fc PMF |
136 | save_registers(®s) |
137 | ||
138 | ||
9692fa18 PMF |
139 | #define DEFINE_MARKER(channel, name, format, unique, m) \ |
140 | _DEFINE_MARKER(channel, name, NULL, NULL, format, unique, m) | |
a72ca31a | 141 | |
7166e240 PMF |
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 __mark_##channel##_##name \ | |
150 | __attribute__((section("__markers"), aligned(8))) = \ | |
151 | { __mstrtab_##channel##_##name, \ | |
152 | &__mstrtab_##channel##_##name[sizeof(#channel)], \ | |
153 | &__mstrtab_##channel##_##name[sizeof(#channel) + \ | |
154 | sizeof(#name)], \ | |
155 | 0, 0, 0, 0, marker_probe_cb, \ | |
156 | { __mark_empty_function, NULL}, \ | |
157 | NULL, tp_name_str, tp_cb } | |
a72ca31a PMF |
158 | |
159 | /* | |
160 | * Make sure the alignment of the structure in the __markers 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 | * The "generic" argument controls which marker enabling mechanism must be used. | |
165 | * If generic is true, a variable read is used. | |
166 | * If generic is false, immediate values are used. | |
167 | */ | |
bb9ade29 | 168 | |
a72ca31a | 169 | #define __trace_mark(generic, channel, name, call_private, format, args...) \ |
e05cba90 | 170 | __trace_mark_counter(generic, channel, name, __LINE__, call_private, format, ## args) |
bb9ade29 PMF |
171 | |
172 | #define __trace_mark_counter(generic, channel, name, unique, call_private, format, args...) \ | |
a72ca31a | 173 | do { \ |
9692fa18 PMF |
174 | struct marker *m; \ |
175 | DEFINE_MARKER(channel, name, format, unique, m); \ | |
a72ca31a PMF |
176 | __mark_check_format(format, ## args); \ |
177 | if (!generic) { \ | |
9692fa18 PMF |
178 | if (unlikely(imv_read(m->state))) \ |
179 | (m->call)(m, call_private, ®s, ## args); \ | |
a72ca31a | 180 | } else { \ |
9692fa18 PMF |
181 | if (unlikely(_imv_read(m->state))) \ |
182 | (m->call)(m, call_private, ®s, ## args); \ | |
a72ca31a PMF |
183 | } \ |
184 | } while (0) | |
185 | ||
bb9ade29 | 186 | #define __trace_mark_tp(channel, name, call_private, tp_name, tp_cb, format, args...) \ |
e05cba90 | 187 | __trace_mark_tp_counter(channel, name, __LINE__, call_private, tp_name, tp_cb, format, ## args) |
bb9ade29 PMF |
188 | |
189 | #define __trace_mark_tp_counter(channel, name, unique, call_private, tp_name, tp_cb, format, args...) \ | |
a72ca31a | 190 | do { \ |
9692fa18 | 191 | struct marker m; \ |
a72ca31a PMF |
192 | void __check_tp_type(void) \ |
193 | { \ | |
194 | register_trace_##tp_name(tp_cb); \ | |
195 | } \ | |
9692fa18 | 196 | DEFINE_MARKER_TP(channel, name, tp_name, tp_cb, format, unique, m);\ |
a72ca31a PMF |
197 | __mark_check_format(format, ## args); \ |
198 | (*__mark_##channel##_##name.call)(&__mark_##channel##_##name, \ | |
75667d04 | 199 | call_private, ®s, ## args); \ |
a72ca31a PMF |
200 | } while (0) |
201 | ||
202 | extern void marker_update_probe_range(struct marker *begin, | |
203 | struct marker *end); | |
204 | ||
205 | #define GET_MARKER(channel, name) (__mark_##channel##_##name) | |
206 | ||
872037bb | 207 | #else /* !CONFIG_MARKERS */ |
9692fa18 | 208 | #define DEFINE_MARKER(channel, name, tp_name, tp_cb, format, m) |
872037bb PMF |
209 | #define __trace_mark(generic, channel, name, call_private, format, args...) \ |
210 | __mark_check_format(format, ## args) | |
211 | #define __trace_mark_tp(channel, name, call_private, tp_name, tp_cb, \ | |
212 | format, args...) \ | |
213 | do { \ | |
214 | void __check_tp_type(void) \ | |
215 | { \ | |
216 | register_trace_##tp_name(tp_cb); \ | |
217 | } \ | |
218 | __mark_check_format(format, ## args); \ | |
219 | } while (0) | |
220 | static inline void marker_update_probe_range(struct marker *begin, | |
221 | struct marker *end) | |
222 | { } | |
223 | #define GET_MARKER(channel, name) | |
224 | #endif /* CONFIG_MARKERS */ | |
a72ca31a PMF |
225 | |
226 | /** | |
227 | * trace_mark - Marker using code patching | |
228 | * @channel: marker channel (where to send the data), not quoted. | |
229 | * @name: marker name, not quoted. | |
230 | * @format: format string | |
231 | * @args...: variable argument list | |
232 | * | |
233 | * Places a marker using optimized code patching technique (imv_read()) | |
234 | * to be enabled when immediate values are present. | |
235 | */ | |
236 | #define trace_mark(channel, name, format, args...) \ | |
237 | __trace_mark(0, channel, name, NULL, format, ## args) | |
238 | ||
239 | /** | |
240 | * _trace_mark - Marker using variable read | |
241 | * @channel: marker channel (where to send the data), not quoted. | |
242 | * @name: marker name, not quoted. | |
243 | * @format: format string | |
244 | * @args...: variable argument list | |
245 | * | |
246 | * Places a marker using a standard memory read (_imv_read()) to be | |
247 | * enabled. Should be used for markers in code paths where instruction | |
c1f20530 | 248 | * modification based enabling is not welcome. |
a72ca31a PMF |
249 | */ |
250 | #define _trace_mark(channel, name, format, args...) \ | |
251 | __trace_mark(1, channel, name, NULL, format, ## args) | |
252 | ||
253 | /** | |
254 | * trace_mark_tp - Marker in a tracepoint callback | |
255 | * @channel: marker channel (where to send the data), not quoted. | |
256 | * @name: marker name, not quoted. | |
257 | * @tp_name: tracepoint name, not quoted. | |
258 | * @tp_cb: tracepoint callback. Should have an associated global symbol so it | |
259 | * is not optimized away by the compiler (should not be static). | |
260 | * @format: format string | |
261 | * @args...: variable argument list | |
262 | * | |
263 | * Places a marker in a tracepoint callback. | |
264 | */ | |
265 | #define trace_mark_tp(channel, name, tp_name, tp_cb, format, args...) \ | |
266 | __trace_mark_tp(channel, name, NULL, tp_name, tp_cb, format, ## args) | |
267 | ||
268 | /** | |
269 | * MARK_NOARGS - Format string for a marker with no argument. | |
270 | */ | |
271 | #define MARK_NOARGS " " | |
272 | ||
273 | extern void lock_markers(void); | |
274 | extern void unlock_markers(void); | |
275 | ||
276 | extern void markers_compact_event_ids(void); | |
277 | ||
278 | /* To be used for string format validity checking with gcc */ | |
279 | static inline void __printf(1, 2) ___mark_check_format(const char *fmt, ...) | |
280 | { | |
281 | } | |
282 | ||
283 | #define __mark_check_format(format, args...) \ | |
284 | do { \ | |
285 | if (0) \ | |
286 | ___mark_check_format(format, ## args); \ | |
287 | } while (0) | |
288 | ||
289 | extern marker_probe_func __mark_empty_function; | |
290 | ||
291 | extern void marker_probe_cb(const struct marker *mdata, | |
75667d04 | 292 | void *call_private, struct registers *regs, ...); |
a72ca31a PMF |
293 | |
294 | /* | |
295 | * Connect a probe to a marker. | |
296 | * private data pointer must be a valid allocated memory address, or NULL. | |
297 | */ | |
298 | extern int marker_probe_register(const char *channel, const char *name, | |
299 | const char *format, marker_probe_func *probe, void *probe_private); | |
300 | ||
301 | /* | |
302 | * Returns the private data given to marker_probe_register. | |
303 | */ | |
304 | extern int marker_probe_unregister(const char *channel, const char *name, | |
305 | marker_probe_func *probe, void *probe_private); | |
306 | /* | |
307 | * Unregister a marker by providing the registered private data. | |
308 | */ | |
309 | extern int marker_probe_unregister_private_data(marker_probe_func *probe, | |
310 | void *probe_private); | |
311 | ||
312 | extern void *marker_get_private_data(const char *channel, const char *name, | |
313 | marker_probe_func *probe, int num); | |
314 | ||
315 | /* | |
316 | * marker_synchronize_unregister must be called between the last marker probe | |
317 | * unregistration and the first one of | |
318 | * - the end of module exit function | |
319 | * - the free of any resource used by the probes | |
320 | * to ensure the code and data are valid for any possibly running probes. | |
321 | */ | |
322 | #define marker_synchronize_unregister() synchronize_sched() | |
323 | ||
324 | struct marker_iter { | |
98963de4 PMF |
325 | //ust// struct module *module; |
326 | struct lib *lib; | |
a72ca31a PMF |
327 | struct marker *marker; |
328 | }; | |
329 | ||
330 | extern void marker_iter_start(struct marker_iter *iter); | |
331 | extern void marker_iter_next(struct marker_iter *iter); | |
332 | extern void marker_iter_stop(struct marker_iter *iter); | |
333 | extern void marker_iter_reset(struct marker_iter *iter); | |
334 | extern int marker_get_iter_range(struct marker **marker, struct marker *begin, | |
335 | struct marker *end); | |
336 | ||
337 | extern void marker_update_process(void); | |
338 | extern int is_marker_enabled(const char *channel, const char *name); | |
339 | ||
59b161cd PMF |
340 | //ust// #ifdef CONFIG_MARKERS_USERSPACE |
341 | //ust// extern void exit_user_markers(struct task_struct *p); | |
342 | //ust// #else | |
343 | //ust// static inline void exit_user_markers(struct task_struct *p) | |
344 | //ust// { | |
345 | //ust// } | |
346 | //ust// #endif | |
a72ca31a | 347 | |
3ea1e2fc PMF |
348 | struct marker_addr { |
349 | struct marker *marker; | |
350 | void *addr; | |
351 | }; | |
98963de4 PMF |
352 | |
353 | struct lib { | |
354 | struct marker *markers_start; | |
defa46a7 | 355 | #ifdef CONFIG_UST_GDB_INTEGRATION |
3ea1e2fc | 356 | struct marker_addr *markers_addr_start; |
defa46a7 | 357 | #endif |
98963de4 PMF |
358 | int markers_count; |
359 | struct list_head list; | |
360 | }; | |
361 | ||
bf961c7e | 362 | extern int marker_register_lib(struct marker *markers_start, int markers_count); |
24b6668c | 363 | extern int marker_unregister_lib(struct marker *markers_start); |
defa46a7 | 364 | |
55994a67 JB |
365 | #define MARKER_LIB \ |
366 | extern struct marker __start___markers[] __attribute__((weak, visibility("hidden"))); \ | |
367 | extern struct marker __stop___markers[] __attribute__((weak, visibility("hidden"))); \ | |
55994a67 JB |
368 | \ |
369 | static void __attribute__((constructor)) __markers__init(void) \ | |
370 | { \ | |
bf961c7e | 371 | marker_register_lib(__start___markers, (((long)__stop___markers)-((long)__start___markers))/sizeof(struct marker)); \ |
24b6668c PMF |
372 | } \ |
373 | \ | |
374 | static void __attribute__((destructor)) __markers__destroy(void) \ | |
375 | { \ | |
376 | marker_unregister_lib(__start___markers); \ | |
55994a67 | 377 | } |
98963de4 | 378 | |
064842c6 PMF |
379 | extern void marker_set_new_marker_cb(void (*cb)(struct marker *)); |
380 | extern void init_markers(void); | |
9160b4e4 | 381 | |
defa46a7 | 382 | #endif /* _UST_MARKER_H */ |