Commit | Line | Data |
---|---|---|
68c1021b | 1 | /* |
fe566790 | 2 | * Copyright (C) 2007-2011 Mathieu Desnoyers |
68c1021b | 3 | * |
34e4b7db PMF |
4 | * This library is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU Lesser General Public | |
f37142a4 MD |
6 | * License as published by the Free Software Foundation; |
7 | * version 2.1 of the License. | |
68c1021b | 8 | * |
34e4b7db | 9 | * This library is distributed in the hope that it will be useful, |
68c1021b | 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
34e4b7db PMF |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. | |
68c1021b | 13 | * |
34e4b7db PMF |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the Free Software | |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
68c1021b | 17 | */ |
59b161cd | 18 | |
b0c4126f | 19 | #define _LGPL_SOURCE |
909bc43f PMF |
20 | #include <stdlib.h> |
21 | #include <errno.h> | |
b27f8e75 | 22 | #include <pthread.h> |
b7ea1a1c | 23 | #include <urcu-bp.h> |
17bb07b4 | 24 | #include <urcu/rculist.h> |
10c56168 | 25 | #include <urcu/hlist.h> |
769d0157 | 26 | |
518d7abb | 27 | #include <ust/core.h> |
93d0f2ea | 28 | #include <ust/marker.h> |
535b0d0a | 29 | #include <ust/marker-internal.h> |
fbae86d6 | 30 | #include <ust/tracepoint.h> |
81614639 | 31 | #include <ust/tracepoint-internal.h> |
909bc43f | 32 | |
30ffe279 | 33 | #include "usterr_signal_safe.h" |
59b161cd PMF |
34 | #include "channels.h" |
35 | #include "tracercore.h" | |
c93858f1 | 36 | #include "tracer.h" |
68c1021b | 37 | |
b521931e MD |
38 | extern struct ust_marker * const __start___ust_marker_ptrs[] __attribute__((visibility("hidden"))); |
39 | extern struct ust_marker * const __stop___ust_marker_ptrs[] __attribute__((visibility("hidden"))); | |
defa46a7 | 40 | |
b521931e MD |
41 | /* Set to 1 to enable ust_marker debug output */ |
42 | static const int ust_marker_debug; | |
b27f8e75 MD |
43 | static int initialized; |
44 | static void (*new_ust_marker_cb)(struct ust_marker *); | |
68c1021b PMF |
45 | |
46 | /* | |
fd0a1aea MD |
47 | * ust_marker mutex protects the builtin and module ust_marker and the |
48 | * hash table, as well as the ust_marker_libs list. | |
68c1021b | 49 | */ |
b521931e | 50 | static DEFINE_MUTEX(ust_marker_mutex); |
b521931e | 51 | static CDS_LIST_HEAD(ust_marker_libs); |
772030fe | 52 | |
b27f8e75 MD |
53 | /* |
54 | * Allow nested mutex for mutex listing and nested enable. | |
55 | */ | |
56 | static __thread int nested_mutex; | |
57 | ||
b521931e | 58 | void lock_ust_marker(void) |
68c1021b | 59 | { |
b27f8e75 MD |
60 | if (!(nested_mutex++)) |
61 | pthread_mutex_lock(&ust_marker_mutex); | |
68c1021b PMF |
62 | } |
63 | ||
b521931e | 64 | void unlock_ust_marker(void) |
68c1021b | 65 | { |
b27f8e75 MD |
66 | if (!(--nested_mutex)) |
67 | pthread_mutex_unlock(&ust_marker_mutex); | |
68c1021b PMF |
68 | } |
69 | ||
70 | /* | |
b521931e | 71 | * ust_marker hash table, containing the active ust_marker. |
fd0a1aea | 72 | * Protected by ust_marker mutex. |
68c1021b | 73 | */ |
5cfbb58f MD |
74 | #define UST_MARKER_HASH_BITS 6 |
75 | #define UST_MARKER_TABLE_SIZE (1 << UST_MARKER_HASH_BITS) | |
76 | static struct cds_hlist_head ust_marker_table[UST_MARKER_TABLE_SIZE]; | |
68c1021b | 77 | |
fd0a1aea MD |
78 | struct ust_marker_probe_array { |
79 | struct rcu_head rcu; | |
80 | struct ust_marker_probe_closure c[0]; | |
81 | }; | |
82 | ||
68c1021b PMF |
83 | /* |
84 | * Note about RCU : | |
b521931e MD |
85 | * It is used to make sure every handler has finished using its private |
86 | * data between two consecutive operation (add or remove) on a given | |
87 | * ust_marker. It is also used to delay the free of multiple probes | |
88 | * array until a quiescent state is reached. ust_marker entries | |
89 | * modifications are protected by the ust_marker_mutex. | |
68c1021b | 90 | */ |
b521931e | 91 | struct ust_marker_entry { |
10c56168 | 92 | struct cds_hlist_node hlist; |
68c1021b PMF |
93 | char *format; |
94 | char *name; | |
95 | /* Probe wrapper */ | |
fe566790 | 96 | void (*call)(const struct ust_marker *mdata, void *call_private, ...); |
b521931e | 97 | struct ust_marker_probe_closure single; |
fd0a1aea | 98 | struct ust_marker_probe_array *multi; |
68c1021b | 99 | int refcount; /* Number of times armed. 0 if disarmed. */ |
68c1021b PMF |
100 | u16 channel_id; |
101 | u16 event_id; | |
102 | unsigned char ptype:1; | |
103 | unsigned char format_allocated:1; | |
104 | char channel[0]; /* Contains channel'\0'name'\0'format'\0' */ | |
105 | }; | |
106 | ||
68c1021b | 107 | /** |
b521931e MD |
108 | * __ust_marker_empty_function - Empty probe callback |
109 | * @mdata: ust_marker data | |
68c1021b PMF |
110 | * @probe_private: probe private data |
111 | * @call_private: call site private data | |
112 | * @fmt: format string | |
113 | * @...: variable argument list | |
114 | * | |
b521931e MD |
115 | * Empty callback provided as a probe to the ust_marker. By providing |
116 | * this to a disabled ust_marker, we make sure the execution flow is | |
117 | * always valid even though the function pointer change and the | |
118 | * ust_marker enabling are two distinct operations that modifies the | |
119 | * execution flow of preemptible code. | |
68c1021b | 120 | */ |
b521931e | 121 | notrace void __ust_marker_empty_function(const struct ust_marker *mdata, |
fe566790 | 122 | void *probe_private, void *call_private, const char *fmt, va_list *args) |
68c1021b PMF |
123 | { |
124 | } | |
68c1021b PMF |
125 | |
126 | /* | |
b521931e MD |
127 | * ust_marker_probe_cb Callback that prepares the variable argument list for probes. |
128 | * @mdata: pointer of type struct ust_marker | |
68c1021b PMF |
129 | * @call_private: caller site private data |
130 | * @...: Variable argument list. | |
131 | * | |
132 | * Since we do not use "typical" pointer based RCU in the 1 argument case, we | |
0222e121 | 133 | * need to put a full cmm_smp_rmb() in this branch. This is why we do not use |
68c1021b PMF |
134 | * rcu_dereference() for the pointer read. |
135 | */ | |
b521931e | 136 | notrace void ust_marker_probe_cb(const struct ust_marker *mdata, |
fe566790 | 137 | void *call_private, ...) |
68c1021b PMF |
138 | { |
139 | va_list args; | |
140 | char ptype; | |
141 | ||
142 | /* | |
143 | * rcu_read_lock_sched does two things : disabling preemption to make | |
144 | * sure the teardown of the callbacks can be done correctly when they | |
145 | * are in modules and they insure RCU read coherency. | |
146 | */ | |
fd0a1aea | 147 | rcu_read_lock(); |
68c1021b PMF |
148 | ptype = mdata->ptype; |
149 | if (likely(!ptype)) { | |
b521931e | 150 | ust_marker_probe_func *func; |
68c1021b | 151 | /* Must read the ptype before ptr. They are not data dependant, |
0222e121 MD |
152 | * so we put an explicit cmm_smp_rmb() here. */ |
153 | cmm_smp_rmb(); | |
68c1021b PMF |
154 | func = mdata->single.func; |
155 | /* Must read the ptr before private data. They are not data | |
0222e121 MD |
156 | * dependant, so we put an explicit cmm_smp_rmb() here. */ |
157 | cmm_smp_rmb(); | |
fe566790 MD |
158 | va_start(args, call_private); |
159 | func(mdata, mdata->single.probe_private, call_private, | |
68c1021b PMF |
160 | mdata->format, &args); |
161 | va_end(args); | |
162 | } else { | |
fd0a1aea | 163 | struct ust_marker_probe_array *multi; |
68c1021b PMF |
164 | int i; |
165 | /* | |
166 | * Read mdata->ptype before mdata->multi. | |
167 | */ | |
0222e121 | 168 | cmm_smp_rmb(); |
68c1021b PMF |
169 | multi = mdata->multi; |
170 | /* | |
171 | * multi points to an array, therefore accessing the array | |
172 | * depends on reading multi. However, even in this case, | |
173 | * we must insure that the pointer is read _before_ the array | |
0222e121 MD |
174 | * data. Same as rcu_dereference, but we need a full cmm_smp_rmb() |
175 | * in the fast path, so put the explicit cmm_barrier here. | |
68c1021b | 176 | */ |
0222e121 | 177 | cmm_smp_read_barrier_depends(); |
fd0a1aea | 178 | for (i = 0; multi->c[i].func; i++) { |
fe566790 | 179 | va_start(args, call_private); |
fd0a1aea | 180 | multi->c[i].func(mdata, multi->c[i].probe_private, |
fe566790 | 181 | call_private, mdata->format, &args); |
68c1021b PMF |
182 | va_end(args); |
183 | } | |
184 | } | |
fd0a1aea | 185 | rcu_read_unlock(); |
68c1021b | 186 | } |
68c1021b PMF |
187 | |
188 | /* | |
b521931e MD |
189 | * ust_marker_probe_cb Callback that does not prepare the variable argument list. |
190 | * @mdata: pointer of type struct ust_marker | |
68c1021b PMF |
191 | * @call_private: caller site private data |
192 | * @...: Variable argument list. | |
193 | * | |
b521931e | 194 | * Should be connected to ust_marker "UST_MARKER_NOARGS". |
68c1021b | 195 | */ |
b521931e | 196 | static notrace void ust_marker_probe_cb_noarg(const struct ust_marker *mdata, |
fe566790 | 197 | void *call_private, ...) |
68c1021b PMF |
198 | { |
199 | va_list args; /* not initialized */ | |
200 | char ptype; | |
201 | ||
fd0a1aea | 202 | rcu_read_lock(); |
68c1021b PMF |
203 | ptype = mdata->ptype; |
204 | if (likely(!ptype)) { | |
b521931e | 205 | ust_marker_probe_func *func; |
68c1021b | 206 | /* Must read the ptype before ptr. They are not data dependant, |
0222e121 MD |
207 | * so we put an explicit cmm_smp_rmb() here. */ |
208 | cmm_smp_rmb(); | |
68c1021b PMF |
209 | func = mdata->single.func; |
210 | /* Must read the ptr before private data. They are not data | |
0222e121 MD |
211 | * dependant, so we put an explicit cmm_smp_rmb() here. */ |
212 | cmm_smp_rmb(); | |
fe566790 | 213 | func(mdata, mdata->single.probe_private, call_private, |
68c1021b PMF |
214 | mdata->format, &args); |
215 | } else { | |
fd0a1aea | 216 | struct ust_marker_probe_array *multi; |
68c1021b PMF |
217 | int i; |
218 | /* | |
219 | * Read mdata->ptype before mdata->multi. | |
220 | */ | |
0222e121 | 221 | cmm_smp_rmb(); |
68c1021b PMF |
222 | multi = mdata->multi; |
223 | /* | |
224 | * multi points to an array, therefore accessing the array | |
225 | * depends on reading multi. However, even in this case, | |
226 | * we must insure that the pointer is read _before_ the array | |
0222e121 MD |
227 | * data. Same as rcu_dereference, but we need a full cmm_smp_rmb() |
228 | * in the fast path, so put the explicit cmm_barrier here. | |
68c1021b | 229 | */ |
0222e121 | 230 | cmm_smp_read_barrier_depends(); |
fd0a1aea MD |
231 | for (i = 0; multi->c[i].func; i++) |
232 | multi->c[i].func(mdata, multi->c[i].probe_private, | |
68c1021b PMF |
233 | call_private, mdata->format, &args); |
234 | } | |
fd0a1aea | 235 | rcu_read_unlock(); |
68c1021b PMF |
236 | } |
237 | ||
238 | static void free_old_closure(struct rcu_head *head) | |
239 | { | |
fd0a1aea MD |
240 | struct ust_marker_probe_array *multi = |
241 | _ust_container_of(head, struct ust_marker_probe_array, rcu); | |
242 | free(multi); | |
68c1021b PMF |
243 | } |
244 | ||
b521931e | 245 | static void debug_print_probes(struct ust_marker_entry *entry) |
68c1021b PMF |
246 | { |
247 | int i; | |
248 | ||
b521931e | 249 | if (!ust_marker_debug) |
68c1021b PMF |
250 | return; |
251 | ||
252 | if (!entry->ptype) { | |
c1f20530 | 253 | DBG("Single probe : %p %p", |
68c1021b PMF |
254 | entry->single.func, |
255 | entry->single.probe_private); | |
256 | } else { | |
fd0a1aea | 257 | for (i = 0; entry->multi->c[i].func; i++) |
c1f20530 | 258 | DBG("Multi probe %d : %p %p", i, |
fd0a1aea MD |
259 | entry->multi->c[i].func, |
260 | entry->multi->c[i].probe_private); | |
68c1021b PMF |
261 | } |
262 | } | |
263 | ||
fd0a1aea | 264 | static struct ust_marker_probe_array * |
b521931e MD |
265 | ust_marker_entry_add_probe(struct ust_marker_entry *entry, |
266 | ust_marker_probe_func *probe, void *probe_private) | |
68c1021b PMF |
267 | { |
268 | int nr_probes = 0; | |
fd0a1aea | 269 | struct ust_marker_probe_array *old, *new; |
68c1021b PMF |
270 | |
271 | WARN_ON(!probe); | |
272 | ||
273 | debug_print_probes(entry); | |
274 | old = entry->multi; | |
275 | if (!entry->ptype) { | |
276 | if (entry->single.func == probe && | |
277 | entry->single.probe_private == probe_private) | |
278 | return ERR_PTR(-EBUSY); | |
b521931e | 279 | if (entry->single.func == __ust_marker_empty_function) { |
68c1021b PMF |
280 | /* 0 -> 1 probes */ |
281 | entry->single.func = probe; | |
282 | entry->single.probe_private = probe_private; | |
283 | entry->refcount = 1; | |
284 | entry->ptype = 0; | |
285 | debug_print_probes(entry); | |
286 | return NULL; | |
287 | } else { | |
288 | /* 1 -> 2 probes */ | |
289 | nr_probes = 1; | |
290 | old = NULL; | |
291 | } | |
292 | } else { | |
293 | /* (N -> N+1), (N != 0, 1) probes */ | |
fd0a1aea MD |
294 | for (nr_probes = 0; old->c[nr_probes].func; nr_probes++) |
295 | if (old->c[nr_probes].func == probe | |
296 | && old->c[nr_probes].probe_private | |
68c1021b PMF |
297 | == probe_private) |
298 | return ERR_PTR(-EBUSY); | |
299 | } | |
300 | /* + 2 : one for new probe, one for NULL func */ | |
fd0a1aea MD |
301 | new = zmalloc(sizeof(struct ust_marker_probe_array) |
302 | + ((nr_probes + 2) * sizeof(struct ust_marker_probe_closure))); | |
68c1021b PMF |
303 | if (new == NULL) |
304 | return ERR_PTR(-ENOMEM); | |
305 | if (!old) | |
fd0a1aea | 306 | new->c[0] = entry->single; |
68c1021b | 307 | else |
fd0a1aea | 308 | memcpy(&new->c[0], &old->c[0], |
b521931e | 309 | nr_probes * sizeof(struct ust_marker_probe_closure)); |
fd0a1aea MD |
310 | new->c[nr_probes].func = probe; |
311 | new->c[nr_probes].probe_private = probe_private; | |
68c1021b PMF |
312 | entry->refcount = nr_probes + 1; |
313 | entry->multi = new; | |
314 | entry->ptype = 1; | |
315 | debug_print_probes(entry); | |
316 | return old; | |
317 | } | |
318 | ||
fd0a1aea | 319 | static struct ust_marker_probe_array * |
b521931e MD |
320 | ust_marker_entry_remove_probe(struct ust_marker_entry *entry, |
321 | ust_marker_probe_func *probe, void *probe_private) | |
68c1021b PMF |
322 | { |
323 | int nr_probes = 0, nr_del = 0, i; | |
fd0a1aea | 324 | struct ust_marker_probe_array *old, *new; |
68c1021b PMF |
325 | |
326 | old = entry->multi; | |
327 | ||
328 | debug_print_probes(entry); | |
329 | if (!entry->ptype) { | |
330 | /* 0 -> N is an error */ | |
b521931e | 331 | WARN_ON(entry->single.func == __ust_marker_empty_function); |
68c1021b PMF |
332 | /* 1 -> 0 probes */ |
333 | WARN_ON(probe && entry->single.func != probe); | |
334 | WARN_ON(entry->single.probe_private != probe_private); | |
b521931e | 335 | entry->single.func = __ust_marker_empty_function; |
68c1021b PMF |
336 | entry->refcount = 0; |
337 | entry->ptype = 0; | |
338 | debug_print_probes(entry); | |
339 | return NULL; | |
340 | } else { | |
341 | /* (N -> M), (N > 1, M >= 0) probes */ | |
fd0a1aea MD |
342 | for (nr_probes = 0; old->c[nr_probes].func; nr_probes++) { |
343 | if ((!probe || old->c[nr_probes].func == probe) | |
344 | && old->c[nr_probes].probe_private | |
68c1021b PMF |
345 | == probe_private) |
346 | nr_del++; | |
347 | } | |
348 | } | |
349 | ||
350 | if (nr_probes - nr_del == 0) { | |
351 | /* N -> 0, (N > 1) */ | |
b521931e | 352 | entry->single.func = __ust_marker_empty_function; |
68c1021b PMF |
353 | entry->refcount = 0; |
354 | entry->ptype = 0; | |
355 | } else if (nr_probes - nr_del == 1) { | |
356 | /* N -> 1, (N > 1) */ | |
fd0a1aea MD |
357 | for (i = 0; old->c[i].func; i++) |
358 | if ((probe && old->c[i].func != probe) || | |
359 | old->c[i].probe_private != probe_private) | |
360 | entry->single = old->c[i]; | |
68c1021b PMF |
361 | entry->refcount = 1; |
362 | entry->ptype = 0; | |
363 | } else { | |
364 | int j = 0; | |
365 | /* N -> M, (N > 1, M > 1) */ | |
366 | /* + 1 for NULL */ | |
fd0a1aea MD |
367 | new = zmalloc(sizeof(struct ust_marker_probe_array) |
368 | + ((nr_probes - nr_del + 1) * sizeof(struct ust_marker_probe_closure))); | |
68c1021b PMF |
369 | if (new == NULL) |
370 | return ERR_PTR(-ENOMEM); | |
fd0a1aea MD |
371 | for (i = 0; old->c[i].func; i++) |
372 | if ((probe && old->c[i].func != probe) || | |
373 | old->c[i].probe_private != probe_private) | |
374 | new->c[j++] = old->c[i]; | |
68c1021b PMF |
375 | entry->refcount = nr_probes - nr_del; |
376 | entry->ptype = 1; | |
377 | entry->multi = new; | |
378 | } | |
379 | debug_print_probes(entry); | |
380 | return old; | |
381 | } | |
382 | ||
383 | /* | |
b521931e MD |
384 | * Get ust_marker if the ust_marker is present in the ust_marker hash table. |
385 | * Must be called with ust_marker_mutex held. | |
68c1021b PMF |
386 | * Returns NULL if not present. |
387 | */ | |
b521931e | 388 | static struct ust_marker_entry *get_ust_marker(const char *channel, const char *name) |
68c1021b | 389 | { |
10c56168 DG |
390 | struct cds_hlist_head *head; |
391 | struct cds_hlist_node *node; | |
b521931e | 392 | struct ust_marker_entry *e; |
68c1021b PMF |
393 | size_t channel_len = strlen(channel) + 1; |
394 | size_t name_len = strlen(name) + 1; | |
395 | u32 hash; | |
396 | ||
397 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
5cfbb58f | 398 | head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)]; |
10c56168 | 399 | cds_hlist_for_each_entry(e, node, head, hlist) { |
68c1021b PMF |
400 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) |
401 | return e; | |
402 | } | |
403 | return NULL; | |
404 | } | |
405 | ||
406 | /* | |
b521931e MD |
407 | * Add the ust_marker to the ust_marker hash table. Must be called with |
408 | * ust_marker_mutex held. | |
68c1021b | 409 | */ |
b521931e | 410 | static struct ust_marker_entry *add_ust_marker(const char *channel, const char *name, |
68c1021b PMF |
411 | const char *format) |
412 | { | |
10c56168 DG |
413 | struct cds_hlist_head *head; |
414 | struct cds_hlist_node *node; | |
b521931e | 415 | struct ust_marker_entry *e; |
68c1021b PMF |
416 | size_t channel_len = strlen(channel) + 1; |
417 | size_t name_len = strlen(name) + 1; | |
418 | size_t format_len = 0; | |
419 | u32 hash; | |
420 | ||
421 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
422 | if (format) | |
423 | format_len = strlen(format) + 1; | |
5cfbb58f | 424 | head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)]; |
10c56168 | 425 | cds_hlist_for_each_entry(e, node, head, hlist) { |
68c1021b | 426 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { |
b521931e | 427 | DBG("ust_marker %s.%s busy", channel, name); |
68c1021b PMF |
428 | return ERR_PTR(-EBUSY); /* Already there */ |
429 | } | |
430 | } | |
431 | /* | |
1dba3e6c | 432 | * Using zmalloc here to allocate a variable length element. Could |
68c1021b PMF |
433 | * cause some memory fragmentation if overused. |
434 | */ | |
b521931e | 435 | e = zmalloc(sizeof(struct ust_marker_entry) |
909bc43f | 436 | + channel_len + name_len + format_len); |
68c1021b PMF |
437 | if (!e) |
438 | return ERR_PTR(-ENOMEM); | |
439 | memcpy(e->channel, channel, channel_len); | |
440 | e->name = &e->channel[channel_len]; | |
441 | memcpy(e->name, name, name_len); | |
442 | if (format) { | |
eddb0f66 | 443 | e->format = &e->name[name_len]; |
68c1021b | 444 | memcpy(e->format, format, format_len); |
b521931e MD |
445 | if (strcmp(e->format, UST_MARKER_NOARGS) == 0) |
446 | e->call = ust_marker_probe_cb_noarg; | |
68c1021b | 447 | else |
b521931e | 448 | e->call = ust_marker_probe_cb; |
f36c12ab | 449 | __ust_marker(metadata, core_marker_format, NULL, |
68c1021b PMF |
450 | "channel %s name %s format %s", |
451 | e->channel, e->name, e->format); | |
452 | } else { | |
453 | e->format = NULL; | |
b521931e | 454 | e->call = ust_marker_probe_cb; |
68c1021b | 455 | } |
b521931e | 456 | e->single.func = __ust_marker_empty_function; |
68c1021b PMF |
457 | e->single.probe_private = NULL; |
458 | e->multi = NULL; | |
459 | e->ptype = 0; | |
460 | e->format_allocated = 0; | |
461 | e->refcount = 0; | |
10c56168 | 462 | cds_hlist_add_head(&e->hlist, head); |
68c1021b PMF |
463 | return e; |
464 | } | |
465 | ||
466 | /* | |
b521931e | 467 | * Remove the ust_marker from the ust_marker hash table. Must be called with mutex_lock |
68c1021b PMF |
468 | * held. |
469 | */ | |
b521931e | 470 | static int remove_ust_marker(const char *channel, const char *name) |
68c1021b | 471 | { |
10c56168 DG |
472 | struct cds_hlist_head *head; |
473 | struct cds_hlist_node *node; | |
b521931e | 474 | struct ust_marker_entry *e; |
68c1021b PMF |
475 | int found = 0; |
476 | size_t channel_len = strlen(channel) + 1; | |
477 | size_t name_len = strlen(name) + 1; | |
478 | u32 hash; | |
479 | int ret; | |
480 | ||
481 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
5cfbb58f | 482 | head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)]; |
10c56168 | 483 | cds_hlist_for_each_entry(e, node, head, hlist) { |
68c1021b PMF |
484 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { |
485 | found = 1; | |
486 | break; | |
487 | } | |
488 | } | |
489 | if (!found) | |
490 | return -ENOENT; | |
b521931e | 491 | if (e->single.func != __ust_marker_empty_function) |
68c1021b | 492 | return -EBUSY; |
10c56168 | 493 | cds_hlist_del(&e->hlist); |
68c1021b | 494 | if (e->format_allocated) |
909bc43f | 495 | free(e->format); |
68c1021b PMF |
496 | ret = ltt_channels_unregister(e->channel); |
497 | WARN_ON(ret); | |
909bc43f | 498 | free(e); |
68c1021b PMF |
499 | return 0; |
500 | } | |
501 | ||
502 | /* | |
503 | * Set the mark_entry format to the format found in the element. | |
504 | */ | |
b521931e | 505 | static int ust_marker_set_format(struct ust_marker_entry *entry, const char *format) |
68c1021b | 506 | { |
909bc43f | 507 | entry->format = strdup(format); |
68c1021b PMF |
508 | if (!entry->format) |
509 | return -ENOMEM; | |
510 | entry->format_allocated = 1; | |
511 | ||
f36c12ab | 512 | __ust_marker(metadata, core_marker_format, NULL, |
68c1021b PMF |
513 | "channel %s name %s format %s", |
514 | entry->channel, entry->name, entry->format); | |
515 | return 0; | |
516 | } | |
517 | ||
518 | /* | |
b521931e | 519 | * Sets the probe callback corresponding to one ust_marker. |
68c1021b | 520 | */ |
b521931e | 521 | static int set_ust_marker(struct ust_marker_entry *entry, struct ust_marker *elem, |
68c1021b PMF |
522 | int active) |
523 | { | |
524 | int ret = 0; | |
525 | WARN_ON(strcmp(entry->name, elem->name) != 0); | |
526 | ||
527 | if (entry->format) { | |
528 | if (strcmp(entry->format, elem->format) != 0) { | |
b521931e | 529 | ERR("Format mismatch for probe %s (%s), ust_marker (%s)", |
68c1021b PMF |
530 | entry->name, |
531 | entry->format, | |
532 | elem->format); | |
533 | return -EPERM; | |
534 | } | |
535 | } else { | |
b521931e | 536 | ret = ust_marker_set_format(entry, elem->format); |
68c1021b PMF |
537 | if (ret) |
538 | return ret; | |
539 | } | |
540 | ||
541 | /* | |
542 | * probe_cb setup (statically known) is done here. It is | |
543 | * asynchronous with the rest of execution, therefore we only | |
544 | * pass from a "safe" callback (with argument) to an "unsafe" | |
545 | * callback (does not set arguments). | |
546 | */ | |
547 | elem->call = entry->call; | |
548 | elem->channel_id = entry->channel_id; | |
549 | elem->event_id = entry->event_id; | |
550 | /* | |
551 | * Sanity check : | |
552 | * We only update the single probe private data when the ptr is | |
553 | * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1) | |
554 | */ | |
b521931e | 555 | WARN_ON(elem->single.func != __ust_marker_empty_function |
68c1021b PMF |
556 | && elem->single.probe_private != entry->single.probe_private |
557 | && !elem->ptype); | |
558 | elem->single.probe_private = entry->single.probe_private; | |
559 | /* | |
560 | * Make sure the private data is valid when we update the | |
561 | * single probe ptr. | |
562 | */ | |
0222e121 | 563 | cmm_smp_wmb(); |
68c1021b PMF |
564 | elem->single.func = entry->single.func; |
565 | /* | |
566 | * We also make sure that the new probe callbacks array is consistent | |
567 | * before setting a pointer to it. | |
568 | */ | |
569 | rcu_assign_pointer(elem->multi, entry->multi); | |
570 | /* | |
571 | * Update the function or multi probe array pointer before setting the | |
572 | * ptype. | |
573 | */ | |
0222e121 | 574 | cmm_smp_wmb(); |
68c1021b PMF |
575 | elem->ptype = entry->ptype; |
576 | ||
f36c12ab | 577 | if (elem->tp_name && (active ^ elem->state)) { |
12e81b07 PMF |
578 | WARN_ON(!elem->tp_cb); |
579 | /* | |
580 | * It is ok to directly call the probe registration because type | |
686debc3 | 581 | * checking has been done in the __ust_marker_tp() macro. |
12e81b07 PMF |
582 | */ |
583 | ||
584 | if (active) { | |
12e81b07 PMF |
585 | ret = tracepoint_probe_register_noupdate( |
586 | elem->tp_name, | |
9b9e13aa | 587 | elem->tp_cb, NULL); |
12e81b07 | 588 | } else { |
12e81b07 PMF |
589 | /* |
590 | * tracepoint_probe_update_all() must be called | |
fd0a1aea | 591 | * before the library containing tp_cb is unloaded. |
12e81b07 | 592 | */ |
fd0a1aea MD |
593 | ret = tracepoint_probe_unregister_noupdate( |
594 | elem->tp_name, | |
595 | elem->tp_cb, NULL); | |
12e81b07 PMF |
596 | } |
597 | } | |
f36c12ab | 598 | elem->state = active; |
68c1021b PMF |
599 | |
600 | return ret; | |
601 | } | |
602 | ||
603 | /* | |
b521931e | 604 | * Disable a ust_marker and its probe callback. |
68c1021b PMF |
605 | * Note: only waiting an RCU period after setting elem->call to the empty |
606 | * function insures that the original callback is not used anymore. This insured | |
fd0a1aea | 607 | * by rcu_read_lock around the call site. |
68c1021b | 608 | */ |
b521931e | 609 | static void disable_ust_marker(struct ust_marker *elem) |
68c1021b | 610 | { |
12e81b07 PMF |
611 | int ret; |
612 | ||
613 | /* leave "call" as is. It is known statically. */ | |
f36c12ab | 614 | if (elem->tp_name && elem->state) { |
12e81b07 PMF |
615 | WARN_ON(!elem->tp_cb); |
616 | /* | |
617 | * It is ok to directly call the probe registration because type | |
686debc3 | 618 | * checking has been done in the __ust_marker_tp() macro. |
12e81b07 | 619 | */ |
12e81b07 PMF |
620 | /* |
621 | * tracepoint_probe_update_all() must be called | |
622 | * before the module containing tp_cb is unloaded. | |
623 | */ | |
fd0a1aea MD |
624 | ret = tracepoint_probe_unregister_noupdate(elem->tp_name, |
625 | elem->tp_cb, NULL); | |
626 | WARN_ON(ret); | |
12e81b07 | 627 | } |
f36c12ab | 628 | elem->state = 0; |
b521931e | 629 | elem->single.func = __ust_marker_empty_function; |
68c1021b | 630 | /* Update the function before setting the ptype */ |
0222e121 | 631 | cmm_smp_wmb(); |
68c1021b PMF |
632 | elem->ptype = 0; /* single probe */ |
633 | /* | |
634 | * Leave the private data and channel_id/event_id there, because removal | |
635 | * is racy and should be done only after an RCU period. These are never | |
636 | * used until the next initialization anyway. | |
637 | */ | |
638 | } | |
639 | ||
a5311005 | 640 | /* |
b521931e | 641 | * is_ust_marker_enabled - Check if a ust_marker is enabled |
a5311005 | 642 | * @channel: channel name |
b521931e | 643 | * @name: ust_marker name |
a5311005 | 644 | * |
b521931e | 645 | * Returns 1 if the ust_marker is enabled, 0 if disabled. |
a5311005 | 646 | */ |
b521931e | 647 | int is_ust_marker_enabled(const char *channel, const char *name) |
a5311005 | 648 | { |
b521931e | 649 | struct ust_marker_entry *entry; |
a5311005 | 650 | |
b27f8e75 | 651 | lock_ust_marker(); |
b521931e | 652 | entry = get_ust_marker(channel, name); |
b27f8e75 | 653 | unlock_ust_marker(); |
a5311005 PMF |
654 | |
655 | return entry && !!entry->refcount; | |
656 | } | |
657 | ||
68c1021b | 658 | /** |
b521931e | 659 | * ust_marker_update_probe_range - Update a probe range |
68c1021b PMF |
660 | * @begin: beginning of the range |
661 | * @end: end of the range | |
662 | * | |
b521931e | 663 | * Updates the probe callback corresponding to a range of ust_marker. |
68c1021b | 664 | */ |
fd0a1aea | 665 | static |
b521931e MD |
666 | void ust_marker_update_probe_range(struct ust_marker * const *begin, |
667 | struct ust_marker * const *end) | |
68c1021b | 668 | { |
b521931e MD |
669 | struct ust_marker * const *iter; |
670 | struct ust_marker_entry *mark_entry; | |
68c1021b | 671 | |
68c1021b | 672 | for (iter = begin; iter < end; iter++) { |
f08ebbe2 MD |
673 | if (!*iter) |
674 | continue; /* skip dummy */ | |
b521931e | 675 | mark_entry = get_ust_marker((*iter)->channel, (*iter)->name); |
68c1021b | 676 | if (mark_entry) { |
b521931e | 677 | set_ust_marker(mark_entry, *iter, !!mark_entry->refcount); |
68c1021b PMF |
678 | /* |
679 | * ignore error, continue | |
680 | */ | |
681 | } else { | |
b521931e | 682 | disable_ust_marker(*iter); |
68c1021b PMF |
683 | } |
684 | } | |
68c1021b PMF |
685 | } |
686 | ||
b521931e | 687 | static void lib_update_ust_marker(void) |
772030fe | 688 | { |
b521931e | 689 | struct ust_marker_lib *lib; |
772030fe | 690 | |
b27f8e75 | 691 | lock_ust_marker(); |
b521931e MD |
692 | cds_list_for_each_entry(lib, &ust_marker_libs, list) |
693 | ust_marker_update_probe_range(lib->ust_marker_start, | |
694 | lib->ust_marker_start + lib->ust_marker_count); | |
b27f8e75 | 695 | unlock_ust_marker(); |
772030fe PMF |
696 | } |
697 | ||
68c1021b PMF |
698 | /* |
699 | * Update probes, removing the faulty probes. | |
700 | * | |
701 | * Internal callback only changed before the first probe is connected to it. | |
702 | * Single probe private data can only be changed on 0 -> 1 and 2 -> 1 | |
703 | * transitions. All other transitions will leave the old private data valid. | |
704 | * This makes the non-atomicity of the callback/private data updates valid. | |
705 | * | |
706 | * "special case" updates : | |
707 | * 0 -> 1 callback | |
708 | * 1 -> 0 callback | |
709 | * 1 -> 2 callbacks | |
710 | * 2 -> 1 callbacks | |
711 | * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates. | |
b521931e | 712 | * Site effect : ust_marker_set_format may delete the ust_marker entry (creating a |
68c1021b PMF |
713 | * replacement). |
714 | */ | |
b521931e | 715 | static void ust_marker_update_probes(void) |
68c1021b | 716 | { |
b521931e | 717 | lib_update_ust_marker(); |
12e81b07 | 718 | tracepoint_probe_update_all(); |
68c1021b PMF |
719 | } |
720 | ||
721 | /** | |
b521931e MD |
722 | * ust_marker_probe_register - Connect a probe to a ust_marker |
723 | * @channel: ust_marker channel | |
724 | * @name: ust_marker name | |
68c1021b PMF |
725 | * @format: format string |
726 | * @probe: probe handler | |
727 | * @probe_private: probe private data | |
728 | * | |
729 | * private data must be a valid allocated memory address, or NULL. | |
730 | * Returns 0 if ok, error value on error. | |
731 | * The probe address must at least be aligned on the architecture pointer size. | |
732 | */ | |
b521931e MD |
733 | int ust_marker_probe_register(const char *channel, const char *name, |
734 | const char *format, ust_marker_probe_func *probe, | |
68c1021b PMF |
735 | void *probe_private) |
736 | { | |
b521931e | 737 | struct ust_marker_entry *entry; |
68c1021b | 738 | int ret = 0, ret_err; |
fd0a1aea | 739 | struct ust_marker_probe_array *old; |
68c1021b PMF |
740 | int first_probe = 0; |
741 | ||
b27f8e75 | 742 | lock_ust_marker(); |
b521931e | 743 | entry = get_ust_marker(channel, name); |
68c1021b PMF |
744 | if (!entry) { |
745 | first_probe = 1; | |
b521931e | 746 | entry = add_ust_marker(channel, name, format); |
68c1021b PMF |
747 | if (IS_ERR(entry)) |
748 | ret = PTR_ERR(entry); | |
749 | if (ret) | |
750 | goto end; | |
751 | ret = ltt_channels_register(channel); | |
752 | if (ret) | |
b521931e | 753 | goto error_remove_ust_marker; |
68c1021b PMF |
754 | ret = ltt_channels_get_index_from_name(channel); |
755 | if (ret < 0) | |
756 | goto error_unregister_channel; | |
757 | entry->channel_id = ret; | |
758 | ret = ltt_channels_get_event_id(channel, name); | |
759 | if (ret < 0) | |
760 | goto error_unregister_channel; | |
761 | entry->event_id = ret; | |
762 | ret = 0; | |
f36c12ab | 763 | __ust_marker(metadata, core_marker_id, NULL, |
68c1021b PMF |
764 | "channel %s name %s event_id %hu " |
765 | "int #1u%zu long #1u%zu pointer #1u%zu " | |
766 | "size_t #1u%zu alignment #1u%u", | |
767 | channel, name, entry->event_id, | |
768 | sizeof(int), sizeof(long), sizeof(void *), | |
769 | sizeof(size_t), ltt_get_alignment()); | |
770 | } else if (format) { | |
771 | if (!entry->format) | |
b521931e | 772 | ret = ust_marker_set_format(entry, format); |
68c1021b PMF |
773 | else if (strcmp(entry->format, format)) |
774 | ret = -EPERM; | |
775 | if (ret) | |
776 | goto end; | |
777 | } | |
778 | ||
b521931e | 779 | old = ust_marker_entry_add_probe(entry, probe, probe_private); |
68c1021b PMF |
780 | if (IS_ERR(old)) { |
781 | ret = PTR_ERR(old); | |
782 | if (first_probe) | |
783 | goto error_unregister_channel; | |
784 | else | |
785 | goto end; | |
786 | } | |
b27f8e75 | 787 | unlock_ust_marker(); |
68c1021b | 788 | |
b521931e MD |
789 | /* Activate ust_marker if necessary */ |
790 | ust_marker_update_probes(); | |
68c1021b | 791 | |
fd0a1aea MD |
792 | if (old) { |
793 | synchronize_rcu(); | |
794 | free_old_closure(&old->rcu); | |
795 | } | |
796 | return ret; | |
68c1021b PMF |
797 | |
798 | error_unregister_channel: | |
799 | ret_err = ltt_channels_unregister(channel); | |
800 | WARN_ON(ret_err); | |
b521931e MD |
801 | error_remove_ust_marker: |
802 | ret_err = remove_ust_marker(channel, name); | |
68c1021b PMF |
803 | WARN_ON(ret_err); |
804 | end: | |
b27f8e75 | 805 | unlock_ust_marker(); |
68c1021b PMF |
806 | return ret; |
807 | } | |
68c1021b PMF |
808 | |
809 | /** | |
b521931e MD |
810 | * ust_marker_probe_unregister - Disconnect a probe from a ust_marker |
811 | * @channel: ust_marker channel | |
812 | * @name: ust_marker name | |
68c1021b PMF |
813 | * @probe: probe function pointer |
814 | * @probe_private: probe private data | |
815 | * | |
b521931e | 816 | * Returns the private data given to ust_marker_probe_register, or an ERR_PTR(). |
68c1021b PMF |
817 | * We do not need to call a synchronize_sched to make sure the probes have |
818 | * finished running before doing a module unload, because the module unload | |
819 | * itself uses stop_machine(), which insures that every preempt disabled section | |
820 | * have finished. | |
821 | */ | |
b521931e MD |
822 | int ust_marker_probe_unregister(const char *channel, const char *name, |
823 | ust_marker_probe_func *probe, void *probe_private) | |
68c1021b | 824 | { |
b521931e | 825 | struct ust_marker_entry *entry; |
fd0a1aea MD |
826 | struct ust_marker_probe_array *old; |
827 | int ret = 0; | |
68c1021b | 828 | |
b27f8e75 | 829 | lock_ust_marker(); |
b521931e | 830 | entry = get_ust_marker(channel, name); |
fd0a1aea MD |
831 | if (!entry) { |
832 | ret = -ENOENT; | |
68c1021b | 833 | goto end; |
fd0a1aea | 834 | } |
b521931e | 835 | old = ust_marker_entry_remove_probe(entry, probe, probe_private); |
b27f8e75 | 836 | unlock_ust_marker(); |
68c1021b | 837 | |
b521931e | 838 | ust_marker_update_probes(); |
68c1021b | 839 | |
fd0a1aea MD |
840 | if (old) { |
841 | synchronize_rcu(); | |
842 | free_old_closure(&old->rcu); | |
843 | } | |
844 | return ret; | |
845 | ||
68c1021b | 846 | end: |
b27f8e75 | 847 | unlock_ust_marker(); |
68c1021b PMF |
848 | return ret; |
849 | } | |
68c1021b | 850 | |
b521931e | 851 | static struct ust_marker_entry * |
fd0a1aea MD |
852 | get_ust_marker_from_private_data(ust_marker_probe_func *probe, |
853 | void *probe_private) | |
68c1021b | 854 | { |
b521931e | 855 | struct ust_marker_entry *entry; |
68c1021b | 856 | unsigned int i; |
10c56168 DG |
857 | struct cds_hlist_head *head; |
858 | struct cds_hlist_node *node; | |
68c1021b | 859 | |
5cfbb58f | 860 | for (i = 0; i < UST_MARKER_TABLE_SIZE; i++) { |
b521931e | 861 | head = &ust_marker_table[i]; |
10c56168 | 862 | cds_hlist_for_each_entry(entry, node, head, hlist) { |
68c1021b PMF |
863 | if (!entry->ptype) { |
864 | if (entry->single.func == probe | |
865 | && entry->single.probe_private | |
866 | == probe_private) | |
867 | return entry; | |
868 | } else { | |
fd0a1aea | 869 | struct ust_marker_probe_array *closure; |
68c1021b | 870 | closure = entry->multi; |
fd0a1aea MD |
871 | for (i = 0; closure->c[i].func; i++) { |
872 | if (closure->c[i].func == probe && | |
873 | closure->c[i].probe_private | |
68c1021b PMF |
874 | == probe_private) |
875 | return entry; | |
876 | } | |
877 | } | |
878 | } | |
879 | } | |
880 | return NULL; | |
881 | } | |
882 | ||
883 | /** | |
b521931e | 884 | * ust_marker_probe_unregister_private_data - Disconnect a probe from a ust_marker |
68c1021b PMF |
885 | * @probe: probe function |
886 | * @probe_private: probe private data | |
887 | * | |
888 | * Unregister a probe by providing the registered private data. | |
b521931e | 889 | * Only removes the first ust_marker found in hash table. |
68c1021b PMF |
890 | * Return 0 on success or error value. |
891 | * We do not need to call a synchronize_sched to make sure the probes have | |
892 | * finished running before doing a module unload, because the module unload | |
893 | * itself uses stop_machine(), which insures that every preempt disabled section | |
894 | * have finished. | |
895 | */ | |
b521931e | 896 | int ust_marker_probe_unregister_private_data(ust_marker_probe_func *probe, |
68c1021b PMF |
897 | void *probe_private) |
898 | { | |
b521931e | 899 | struct ust_marker_entry *entry; |
68c1021b | 900 | int ret = 0; |
fd0a1aea | 901 | struct ust_marker_probe_array *old; |
909bc43f | 902 | char *channel = NULL, *name = NULL; |
68c1021b | 903 | |
b27f8e75 | 904 | lock_ust_marker(); |
b521931e | 905 | entry = get_ust_marker_from_private_data(probe, probe_private); |
68c1021b PMF |
906 | if (!entry) { |
907 | ret = -ENOENT; | |
fd0a1aea | 908 | goto unlock; |
68c1021b | 909 | } |
b521931e | 910 | old = ust_marker_entry_remove_probe(entry, NULL, probe_private); |
909bc43f PMF |
911 | channel = strdup(entry->channel); |
912 | name = strdup(entry->name); | |
fd0a1aea MD |
913 | /* Ignore busy error message */ |
914 | remove_ust_marker(channel, name); | |
b27f8e75 | 915 | unlock_ust_marker(); |
68c1021b | 916 | |
b521931e | 917 | ust_marker_update_probes(); |
68c1021b | 918 | |
fd0a1aea MD |
919 | if (old) { |
920 | synchronize_rcu(); | |
921 | free_old_closure(&old->rcu); | |
922 | } | |
923 | goto end; | |
924 | ||
925 | unlock: | |
b27f8e75 | 926 | unlock_ust_marker(); |
fd0a1aea | 927 | end: |
909bc43f PMF |
928 | free(channel); |
929 | free(name); | |
68c1021b PMF |
930 | return ret; |
931 | } | |
68c1021b PMF |
932 | |
933 | /** | |
b521931e MD |
934 | * ust_marker_get_private_data - Get a ust_marker's probe private data |
935 | * @channel: ust_marker channel | |
936 | * @name: ust_marker name | |
68c1021b PMF |
937 | * @probe: probe to match |
938 | * @num: get the nth matching probe's private data | |
939 | * | |
940 | * Returns the nth private data pointer (starting from 0) matching, or an | |
941 | * ERR_PTR. | |
942 | * Returns the private data pointer, or an ERR_PTR. | |
943 | * The private data pointer should _only_ be dereferenced if the caller is the | |
944 | * owner of the data, or its content could vanish. This is mostly used to | |
945 | * confirm that a caller is the owner of a registered probe. | |
946 | */ | |
b521931e MD |
947 | void *ust_marker_get_private_data(const char *channel, const char *name, |
948 | ust_marker_probe_func *probe, int num) | |
68c1021b | 949 | { |
10c56168 DG |
950 | struct cds_hlist_head *head; |
951 | struct cds_hlist_node *node; | |
b521931e | 952 | struct ust_marker_entry *e; |
68c1021b PMF |
953 | size_t channel_len = strlen(channel) + 1; |
954 | size_t name_len = strlen(name) + 1; | |
955 | int i; | |
956 | u32 hash; | |
957 | ||
958 | hash = jhash(channel, channel_len-1, 0) ^ jhash(name, name_len-1, 0); | |
5cfbb58f | 959 | head = &ust_marker_table[hash & ((1 << UST_MARKER_HASH_BITS)-1)]; |
10c56168 | 960 | cds_hlist_for_each_entry(e, node, head, hlist) { |
68c1021b PMF |
961 | if (!strcmp(channel, e->channel) && !strcmp(name, e->name)) { |
962 | if (!e->ptype) { | |
963 | if (num == 0 && e->single.func == probe) | |
964 | return e->single.probe_private; | |
965 | } else { | |
fd0a1aea | 966 | struct ust_marker_probe_array *closure; |
68c1021b PMF |
967 | int match = 0; |
968 | closure = e->multi; | |
fd0a1aea MD |
969 | for (i = 0; closure->c[i].func; i++) { |
970 | if (closure->c[i].func != probe) | |
68c1021b PMF |
971 | continue; |
972 | if (match++ == num) | |
fd0a1aea | 973 | return closure->c[i].probe_private; |
68c1021b PMF |
974 | } |
975 | } | |
976 | break; | |
977 | } | |
978 | } | |
979 | return ERR_PTR(-ENOENT); | |
980 | } | |
68c1021b PMF |
981 | |
982 | /** | |
fd0a1aea MD |
983 | * ust_marker_get_iter_range - Get a next ust_marker iterator given a range. |
984 | * @ust_marker: current ust_marker (in), next ust_marker (out) | |
985 | * @begin: beginning of the range | |
986 | * @end: end of the range | |
68c1021b | 987 | * |
fd0a1aea MD |
988 | * Returns whether a next ust_marker has been found (1) or not (0). |
989 | * Will return the first ust_marker in the range if the input ust_marker is NULL. | |
990 | * Called with markers mutex held. | |
68c1021b | 991 | */ |
fd0a1aea MD |
992 | static |
993 | int ust_marker_get_iter_range(struct ust_marker * const **ust_marker, | |
994 | struct ust_marker * const *begin, | |
995 | struct ust_marker * const *end) | |
996 | { | |
997 | if (!*ust_marker && begin != end) | |
998 | *ust_marker = begin; | |
999 | while (*ust_marker >= begin && *ust_marker < end) { | |
1000 | if (!**ust_marker) | |
1001 | (*ust_marker)++; /* skip dummy */ | |
1002 | else | |
1003 | return 1; | |
1004 | } | |
1005 | return 0; | |
1006 | } | |
68c1021b | 1007 | |
772030fe PMF |
1008 | /* |
1009 | * Returns 0 if current not found. | |
1010 | * Returns 1 if current found. | |
fd0a1aea | 1011 | * Called with markers mutex held. |
772030fe | 1012 | */ |
fd0a1aea | 1013 | static |
b521931e | 1014 | int lib_get_iter_ust_marker(struct ust_marker_iter *iter) |
772030fe | 1015 | { |
b521931e | 1016 | struct ust_marker_lib *iter_lib; |
772030fe PMF |
1017 | int found = 0; |
1018 | ||
b521931e | 1019 | cds_list_for_each_entry(iter_lib, &ust_marker_libs, list) { |
772030fe PMF |
1020 | if (iter_lib < iter->lib) |
1021 | continue; | |
1022 | else if (iter_lib > iter->lib) | |
b521931e MD |
1023 | iter->ust_marker = NULL; |
1024 | found = ust_marker_get_iter_range(&iter->ust_marker, | |
1025 | iter_lib->ust_marker_start, | |
1026 | iter_lib->ust_marker_start + iter_lib->ust_marker_count); | |
772030fe PMF |
1027 | if (found) { |
1028 | iter->lib = iter_lib; | |
1029 | break; | |
1030 | } | |
1031 | } | |
772030fe PMF |
1032 | return found; |
1033 | } | |
1034 | ||
fd0a1aea | 1035 | /* Called with markers mutex held. */ |
b521931e | 1036 | static void ust_marker_get_iter(struct ust_marker_iter *iter) |
68c1021b PMF |
1037 | { |
1038 | int found = 0; | |
1039 | ||
b521931e | 1040 | found = lib_get_iter_ust_marker(iter); |
68c1021b | 1041 | if (!found) |
b521931e | 1042 | ust_marker_iter_reset(iter); |
68c1021b PMF |
1043 | } |
1044 | ||
b521931e | 1045 | void ust_marker_iter_start(struct ust_marker_iter *iter) |
68c1021b | 1046 | { |
b27f8e75 | 1047 | lock_ust_marker(); |
b521931e | 1048 | ust_marker_get_iter(iter); |
68c1021b | 1049 | } |
68c1021b | 1050 | |
fd0a1aea | 1051 | /* Called with markers mutex held. */ |
b521931e | 1052 | void ust_marker_iter_next(struct ust_marker_iter *iter) |
68c1021b | 1053 | { |
b521931e | 1054 | iter->ust_marker++; |
68c1021b | 1055 | /* |
b521931e MD |
1056 | * iter->ust_marker may be invalid because we blindly incremented it. |
1057 | * Make sure it is valid by marshalling on the ust_marker, getting the | |
1058 | * ust_marker from following modules if necessary. | |
68c1021b | 1059 | */ |
b521931e | 1060 | ust_marker_get_iter(iter); |
68c1021b | 1061 | } |
68c1021b | 1062 | |
b521931e | 1063 | void ust_marker_iter_stop(struct ust_marker_iter *iter) |
68c1021b | 1064 | { |
b27f8e75 | 1065 | unlock_ust_marker(); |
68c1021b | 1066 | } |
68c1021b | 1067 | |
b521931e | 1068 | void ust_marker_iter_reset(struct ust_marker_iter *iter) |
68c1021b | 1069 | { |
98963de4 | 1070 | iter->lib = NULL; |
b521931e | 1071 | iter->ust_marker = NULL; |
68c1021b | 1072 | } |
68c1021b | 1073 | |
b521931e | 1074 | void ltt_dump_ust_marker_state(struct ust_trace *trace) |
9c67dc50 | 1075 | { |
b521931e | 1076 | struct ust_marker_entry *entry; |
9c67dc50 | 1077 | struct ltt_probe_private_data call_data; |
10c56168 DG |
1078 | struct cds_hlist_head *head; |
1079 | struct cds_hlist_node *node; | |
48454c95 | 1080 | unsigned int i; |
9c67dc50 | 1081 | |
b27f8e75 | 1082 | lock_ust_marker(); |
9c67dc50 PMF |
1083 | call_data.trace = trace; |
1084 | call_data.serializer = NULL; | |
1085 | ||
5cfbb58f | 1086 | for (i = 0; i < UST_MARKER_TABLE_SIZE; i++) { |
b521931e | 1087 | head = &ust_marker_table[i]; |
10c56168 | 1088 | cds_hlist_for_each_entry(entry, node, head, hlist) { |
f36c12ab | 1089 | __ust_marker(metadata, core_marker_id, |
9c67dc50 | 1090 | &call_data, |
48454c95 PMF |
1091 | "channel %s name %s event_id %hu " |
1092 | "int #1u%zu long #1u%zu pointer #1u%zu " | |
1093 | "size_t #1u%zu alignment #1u%u", | |
1094 | entry->channel, | |
1095 | entry->name, | |
1096 | entry->event_id, | |
1097 | sizeof(int), sizeof(long), | |
1098 | sizeof(void *), sizeof(size_t), | |
1099 | ltt_get_alignment()); | |
1100 | if (entry->format) | |
f36c12ab | 1101 | __ust_marker(metadata, |
48454c95 PMF |
1102 | core_marker_format, |
1103 | &call_data, | |
1104 | "channel %s name %s format %s", | |
1105 | entry->channel, | |
1106 | entry->name, | |
1107 | entry->format); | |
1108 | } | |
9c67dc50 | 1109 | } |
b27f8e75 | 1110 | unlock_ust_marker(); |
9c67dc50 | 1111 | } |
98963de4 | 1112 | |
b521931e | 1113 | void ust_marker_set_new_ust_marker_cb(void (*cb)(struct ust_marker *)) |
20b37a31 | 1114 | { |
b521931e | 1115 | new_ust_marker_cb = cb; |
20b37a31 PMF |
1116 | } |
1117 | ||
fd0a1aea MD |
1118 | static void new_ust_marker(struct ust_marker * const *start, |
1119 | struct ust_marker * const *end) | |
20b37a31 | 1120 | { |
b521931e MD |
1121 | if (new_ust_marker_cb) { |
1122 | struct ust_marker * const *m; | |
f08ebbe2 | 1123 | |
b27f8e75 | 1124 | for (m = start; m < end; m++) { |
f08ebbe2 | 1125 | if (*m) |
b521931e | 1126 | new_ust_marker_cb(*m); |
20b37a31 PMF |
1127 | } |
1128 | } | |
1129 | } | |
1130 | ||
fd0a1aea MD |
1131 | int ust_marker_register_lib(struct ust_marker * const *ust_marker_start, |
1132 | int ust_marker_count) | |
98963de4 | 1133 | { |
b521931e | 1134 | struct ust_marker_lib *pl, *iter; |
98963de4 | 1135 | |
b521931e | 1136 | pl = (struct ust_marker_lib *) zmalloc(sizeof(struct ust_marker_lib)); |
98963de4 | 1137 | |
b521931e MD |
1138 | pl->ust_marker_start = ust_marker_start; |
1139 | pl->ust_marker_count = ust_marker_count; | |
98963de4 | 1140 | |
b521931e | 1141 | lock_ust_marker(); |
b467f7a7 MD |
1142 | |
1143 | /* | |
1144 | * We sort the libs by struct lib pointer address. | |
1145 | */ | |
b521931e | 1146 | cds_list_for_each_entry_reverse(iter, &ust_marker_libs, list) { |
b467f7a7 MD |
1147 | BUG_ON(iter == pl); /* Should never be in the list twice */ |
1148 | if (iter < pl) { | |
1149 | /* We belong to the location right after iter. */ | |
1150 | cds_list_add(&pl->list, &iter->list); | |
1151 | goto lib_added; | |
1152 | } | |
1153 | } | |
1154 | /* We should be added at the head of the list */ | |
b521931e | 1155 | cds_list_add(&pl->list, &ust_marker_libs); |
b467f7a7 | 1156 | lib_added: |
b521931e | 1157 | unlock_ust_marker(); |
98963de4 | 1158 | |
b521931e | 1159 | new_ust_marker(ust_marker_start, ust_marker_start + ust_marker_count); |
20b37a31 | 1160 | |
fd0a1aea | 1161 | /* TODO: update just the loaded lib */ |
b521931e | 1162 | lib_update_ust_marker(); |
4db647c5 | 1163 | |
b521931e | 1164 | DBG("just registered a ust_marker section from %p and having %d ust_marker (minus dummy ust_marker)", ust_marker_start, ust_marker_count); |
98963de4 PMF |
1165 | |
1166 | return 0; | |
1167 | } | |
c463904d | 1168 | |
b521931e | 1169 | int ust_marker_unregister_lib(struct ust_marker * const *ust_marker_start) |
0b5207fa | 1170 | { |
b521931e | 1171 | struct ust_marker_lib *lib; |
24b6668c | 1172 | |
b521931e | 1173 | lock_ust_marker(); |
b521931e MD |
1174 | cds_list_for_each_entry(lib, &ust_marker_libs, list) { |
1175 | if(lib->ust_marker_start == ust_marker_start) { | |
1176 | struct ust_marker_lib *lib2free = lib; | |
0222e121 | 1177 | cds_list_del(&lib->list); |
24b6668c PMF |
1178 | free(lib2free); |
1179 | break; | |
1180 | } | |
1181 | } | |
b521931e | 1182 | unlock_ust_marker(); |
24b6668c | 1183 | |
0b5207fa PMF |
1184 | return 0; |
1185 | } | |
1186 | ||
b521931e | 1187 | void __attribute__((constructor)) init_ust_marker(void) |
c463904d | 1188 | { |
900e307e | 1189 | if (!initialized) { |
b27f8e75 | 1190 | init_tracepoint(); |
b521931e MD |
1191 | ust_marker_register_lib(__start___ust_marker_ptrs, |
1192 | __stop___ust_marker_ptrs | |
1193 | - __start___ust_marker_ptrs); | |
4db647c5 PMF |
1194 | initialized = 1; |
1195 | } | |
c463904d | 1196 | } |
24b6668c | 1197 | |
b521931e | 1198 | void __attribute__((destructor)) destroy_ust_marker(void) |
24b6668c | 1199 | { |
b521931e | 1200 | ust_marker_unregister_lib(__start___ust_marker_ptrs); |
24b6668c | 1201 | } |