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