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