Commit | Line | Data |
---|---|---|
f99be407 | 1 | /* |
b27f8e75 | 2 | * Copyright (C) 2008-2011 Mathieu Desnoyers |
1f8b0dff | 3 | * Copyright (C) 2009 Pierre-Marc Fournier |
f99be407 | 4 | * |
34e4b7db PMF |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public | |
f37142a4 MD |
7 | * License as published by the Free Software Foundation; |
8 | * version 2.1 of the License. | |
f99be407 | 9 | * |
34e4b7db | 10 | * This library is distributed in the hope that it will be useful, |
f99be407 | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
34e4b7db PMF |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | * Lesser General Public License for more details. | |
f99be407 | 14 | * |
34e4b7db PMF |
15 | * You should have received a copy of the GNU Lesser General Public |
16 | * License along with this library; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
1f8b0dff PMF |
18 | * |
19 | * Ported to userspace by Pierre-Marc Fournier. | |
f99be407 | 20 | */ |
1f8b0dff | 21 | |
b0c4126f | 22 | #define _LGPL_SOURCE |
474d745f | 23 | #include <errno.h> |
4318ae1b | 24 | #include <lttng/tracepoint.h> |
4318ae1b | 25 | #include <lttng/core.h> |
b728d87e MD |
26 | #include <stdint.h> |
27 | #include <stddef.h> | |
28 | #include <urcu/arch.h> | |
b7ea1a1c | 29 | #include <urcu-bp.h> |
10c56168 | 30 | #include <urcu/hlist.h> |
edaa1431 | 31 | #include <urcu/uatomic.h> |
b728d87e | 32 | #include <urcu/compiler.h> |
474d745f | 33 | |
4318ae1b | 34 | #include <lttng/usterr-signal-safe.h> |
23c8854a | 35 | #include "tracepoint-internal.h" |
b751f722 | 36 | #include "ltt-tracer-core.h" |
b0c4126f | 37 | |
f99be407 PMF |
38 | /* Set to 1 to enable tracepoint debug output */ |
39 | static const int tracepoint_debug; | |
b27f8e75 MD |
40 | static int initialized; |
41 | static void (*new_tracepoint_cb)(struct tracepoint *); | |
f99be407 | 42 | |
474d745f | 43 | /* libraries that contain tracepoints (struct tracepoint_lib) */ |
0222e121 | 44 | static CDS_LIST_HEAD(libs); |
474d745f | 45 | |
f99be407 | 46 | /* |
17dfb34b MD |
47 | * The UST lock protects the library tracepoints, the hash table, and |
48 | * the library list. | |
49 | * All calls to the tracepoint API must be protected by the UST lock, | |
50 | * excepts calls to tracepoint_register_lib and | |
51 | * tracepoint_unregister_lib, which take the UST lock themselves. | |
f99be407 | 52 | */ |
f99be407 PMF |
53 | |
54 | /* | |
55 | * Tracepoint hash table, containing the active tracepoints. | |
56 | * Protected by tracepoints_mutex. | |
57 | */ | |
58 | #define TRACEPOINT_HASH_BITS 6 | |
59 | #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS) | |
10c56168 | 60 | static struct cds_hlist_head tracepoint_table[TRACEPOINT_TABLE_SIZE]; |
f99be407 | 61 | |
b27f8e75 MD |
62 | static CDS_LIST_HEAD(old_probes); |
63 | static int need_update; | |
64 | ||
f99be407 PMF |
65 | /* |
66 | * Note about RCU : | |
67 | * It is used to to delay the free of multiple probes array until a quiescent | |
68 | * state is reached. | |
69 | * Tracepoint entries modifications are protected by the tracepoints_mutex. | |
70 | */ | |
71 | struct tracepoint_entry { | |
10c56168 | 72 | struct cds_hlist_node hlist; |
b979b346 | 73 | struct tracepoint_probe *probes; |
f99be407 PMF |
74 | int refcount; /* Number of times armed. 0 if disarmed. */ |
75 | char name[0]; | |
76 | }; | |
77 | ||
78 | struct tp_probes { | |
79 | union { | |
0222e121 | 80 | struct cds_list_head list; |
f99be407 | 81 | } u; |
b979b346 | 82 | struct tracepoint_probe probes[0]; |
f99be407 PMF |
83 | }; |
84 | ||
85 | static inline void *allocate_probes(int count) | |
86 | { | |
b979b346 | 87 | struct tp_probes *p = zmalloc(count * sizeof(struct tracepoint_probe) |
909bc43f | 88 | + sizeof(struct tp_probes)); |
f99be407 PMF |
89 | return p == NULL ? NULL : p->probes; |
90 | } | |
91 | ||
f99be407 PMF |
92 | static inline void release_probes(void *old) |
93 | { | |
94 | if (old) { | |
b728d87e | 95 | struct tp_probes *tp_probes = caa_container_of(old, |
f99be407 | 96 | struct tp_probes, probes[0]); |
474d745f | 97 | synchronize_rcu(); |
909bc43f | 98 | free(tp_probes); |
f99be407 PMF |
99 | } |
100 | } | |
101 | ||
102 | static void debug_print_probes(struct tracepoint_entry *entry) | |
103 | { | |
104 | int i; | |
105 | ||
9dec086e | 106 | if (!tracepoint_debug || !entry->probes) |
f99be407 PMF |
107 | return; |
108 | ||
9dec086e NC |
109 | for (i = 0; entry->probes[i].func; i++) |
110 | DBG("Probe %d : %p", i, entry->probes[i].func); | |
f99be407 PMF |
111 | } |
112 | ||
113 | static void * | |
9dec086e NC |
114 | tracepoint_entry_add_probe(struct tracepoint_entry *entry, |
115 | void *probe, void *data) | |
f99be407 PMF |
116 | { |
117 | int nr_probes = 0; | |
b979b346 | 118 | struct tracepoint_probe *old, *new; |
f99be407 PMF |
119 | |
120 | WARN_ON(!probe); | |
121 | ||
122 | debug_print_probes(entry); | |
9dec086e | 123 | old = entry->probes; |
f99be407 PMF |
124 | if (old) { |
125 | /* (N -> N+1), (N != 0, 1) probes */ | |
9dec086e NC |
126 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) |
127 | if (old[nr_probes].func == probe && | |
128 | old[nr_probes].data == data) | |
f99be407 PMF |
129 | return ERR_PTR(-EEXIST); |
130 | } | |
131 | /* + 2 : one for new probe, one for NULL func */ | |
132 | new = allocate_probes(nr_probes + 2); | |
133 | if (new == NULL) | |
134 | return ERR_PTR(-ENOMEM); | |
135 | if (old) | |
b979b346 | 136 | memcpy(new, old, nr_probes * sizeof(struct tracepoint_probe)); |
9dec086e NC |
137 | new[nr_probes].func = probe; |
138 | new[nr_probes].data = data; | |
139 | new[nr_probes + 1].func = NULL; | |
f99be407 | 140 | entry->refcount = nr_probes + 1; |
9dec086e | 141 | entry->probes = new; |
f99be407 PMF |
142 | debug_print_probes(entry); |
143 | return old; | |
144 | } | |
145 | ||
146 | static void * | |
9dec086e NC |
147 | tracepoint_entry_remove_probe(struct tracepoint_entry *entry, void *probe, |
148 | void *data) | |
f99be407 PMF |
149 | { |
150 | int nr_probes = 0, nr_del = 0, i; | |
b979b346 | 151 | struct tracepoint_probe *old, *new; |
f99be407 | 152 | |
9dec086e | 153 | old = entry->probes; |
f99be407 PMF |
154 | |
155 | if (!old) | |
156 | return ERR_PTR(-ENOENT); | |
157 | ||
158 | debug_print_probes(entry); | |
159 | /* (N -> M), (N > 1, M >= 0) probes */ | |
9dec086e | 160 | for (nr_probes = 0; old[nr_probes].func; nr_probes++) { |
c66428ac DG |
161 | if (!probe || |
162 | (old[nr_probes].func == probe && | |
9dec086e | 163 | old[nr_probes].data == data)) |
f99be407 PMF |
164 | nr_del++; |
165 | } | |
166 | ||
167 | if (nr_probes - nr_del == 0) { | |
168 | /* N -> 0, (N > 1) */ | |
9dec086e | 169 | entry->probes = NULL; |
f99be407 PMF |
170 | entry->refcount = 0; |
171 | debug_print_probes(entry); | |
172 | return old; | |
173 | } else { | |
174 | int j = 0; | |
175 | /* N -> M, (N > 1, M > 0) */ | |
176 | /* + 1 for NULL */ | |
177 | new = allocate_probes(nr_probes - nr_del + 1); | |
178 | if (new == NULL) | |
179 | return ERR_PTR(-ENOMEM); | |
9dec086e NC |
180 | for (i = 0; old[i].func; i++) |
181 | if (probe && | |
182 | (old[i].func != probe || old[i].data != data)) | |
f99be407 | 183 | new[j++] = old[i]; |
9dec086e | 184 | new[nr_probes - nr_del].func = NULL; |
f99be407 | 185 | entry->refcount = nr_probes - nr_del; |
9dec086e | 186 | entry->probes = new; |
f99be407 PMF |
187 | } |
188 | debug_print_probes(entry); | |
189 | return old; | |
190 | } | |
191 | ||
192 | /* | |
193 | * Get tracepoint if the tracepoint is present in the tracepoint hash table. | |
194 | * Must be called with tracepoints_mutex held. | |
195 | * Returns NULL if not present. | |
196 | */ | |
197 | static struct tracepoint_entry *get_tracepoint(const char *name) | |
198 | { | |
10c56168 DG |
199 | struct cds_hlist_head *head; |
200 | struct cds_hlist_node *node; | |
f99be407 | 201 | struct tracepoint_entry *e; |
23c8854a | 202 | uint32_t hash = jhash(name, strlen(name), 0); |
f99be407 PMF |
203 | |
204 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; | |
10c56168 | 205 | cds_hlist_for_each_entry(e, node, head, hlist) { |
f99be407 PMF |
206 | if (!strcmp(name, e->name)) |
207 | return e; | |
208 | } | |
209 | return NULL; | |
210 | } | |
211 | ||
212 | /* | |
213 | * Add the tracepoint to the tracepoint hash table. Must be called with | |
214 | * tracepoints_mutex held. | |
215 | */ | |
216 | static struct tracepoint_entry *add_tracepoint(const char *name) | |
217 | { | |
10c56168 DG |
218 | struct cds_hlist_head *head; |
219 | struct cds_hlist_node *node; | |
f99be407 PMF |
220 | struct tracepoint_entry *e; |
221 | size_t name_len = strlen(name) + 1; | |
23c8854a | 222 | uint32_t hash = jhash(name, name_len-1, 0); |
f99be407 PMF |
223 | |
224 | head = &tracepoint_table[hash & (TRACEPOINT_TABLE_SIZE - 1)]; | |
10c56168 | 225 | cds_hlist_for_each_entry(e, node, head, hlist) { |
f99be407 | 226 | if (!strcmp(name, e->name)) { |
c1f20530 | 227 | DBG("tracepoint %s busy", name); |
f99be407 PMF |
228 | return ERR_PTR(-EEXIST); /* Already there */ |
229 | } | |
230 | } | |
231 | /* | |
1dba3e6c | 232 | * Using zmalloc here to allocate a variable length element. Could |
f99be407 PMF |
233 | * cause some memory fragmentation if overused. |
234 | */ | |
1dba3e6c | 235 | e = zmalloc(sizeof(struct tracepoint_entry) + name_len); |
f99be407 PMF |
236 | if (!e) |
237 | return ERR_PTR(-ENOMEM); | |
238 | memcpy(&e->name[0], name, name_len); | |
9dec086e | 239 | e->probes = NULL; |
f99be407 | 240 | e->refcount = 0; |
10c56168 | 241 | cds_hlist_add_head(&e->hlist, head); |
f99be407 PMF |
242 | return e; |
243 | } | |
244 | ||
245 | /* | |
246 | * Remove the tracepoint from the tracepoint hash table. Must be called with | |
17dfb34b | 247 | * ust_lock held. |
f99be407 PMF |
248 | */ |
249 | static inline void remove_tracepoint(struct tracepoint_entry *e) | |
250 | { | |
10c56168 | 251 | cds_hlist_del(&e->hlist); |
909bc43f | 252 | free(e); |
f99be407 PMF |
253 | } |
254 | ||
255 | /* | |
256 | * Sets the probe callback corresponding to one tracepoint. | |
257 | */ | |
258 | static void set_tracepoint(struct tracepoint_entry **entry, | |
259 | struct tracepoint *elem, int active) | |
260 | { | |
261 | WARN_ON(strcmp((*entry)->name, elem->name) != 0); | |
262 | ||
263 | /* | |
0222e121 | 264 | * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new |
f99be407 PMF |
265 | * probe callbacks array is consistent before setting a pointer to it. |
266 | * This array is referenced by __DO_TRACE from | |
0222e121 | 267 | * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends() |
f99be407 PMF |
268 | * is used. |
269 | */ | |
9dec086e | 270 | rcu_assign_pointer(elem->probes, (*entry)->probes); |
f36c12ab | 271 | elem->state = active; |
f99be407 PMF |
272 | } |
273 | ||
274 | /* | |
275 | * Disable a tracepoint and its probe callback. | |
276 | * Note: only waiting an RCU period after setting elem->call to the empty | |
277 | * function insures that the original callback is not used anymore. This insured | |
278 | * by preempt_disable around the call site. | |
279 | */ | |
280 | static void disable_tracepoint(struct tracepoint *elem) | |
281 | { | |
f36c12ab | 282 | elem->state = 0; |
9dec086e | 283 | rcu_assign_pointer(elem->probes, NULL); |
f99be407 PMF |
284 | } |
285 | ||
286 | /** | |
287 | * tracepoint_update_probe_range - Update a probe range | |
288 | * @begin: beginning of the range | |
289 | * @end: end of the range | |
290 | * | |
291 | * Updates the probe callback corresponding to a range of tracepoints. | |
292 | */ | |
b27f8e75 | 293 | static |
f218ff28 MD |
294 | void tracepoint_update_probe_range(struct tracepoint * const *begin, |
295 | struct tracepoint * const *end) | |
f99be407 | 296 | { |
f218ff28 | 297 | struct tracepoint * const *iter; |
f99be407 PMF |
298 | struct tracepoint_entry *mark_entry; |
299 | ||
f99be407 | 300 | for (iter = begin; iter < end; iter++) { |
f08ebbe2 MD |
301 | if (!*iter) |
302 | continue; /* skip dummy */ | |
f218ff28 MD |
303 | if (!(*iter)->name) { |
304 | disable_tracepoint(*iter); | |
9dec086e NC |
305 | continue; |
306 | } | |
f218ff28 | 307 | mark_entry = get_tracepoint((*iter)->name); |
f99be407 | 308 | if (mark_entry) { |
f218ff28 | 309 | set_tracepoint(&mark_entry, *iter, |
f99be407 PMF |
310 | !!mark_entry->refcount); |
311 | } else { | |
f218ff28 | 312 | disable_tracepoint(*iter); |
f99be407 PMF |
313 | } |
314 | } | |
f99be407 PMF |
315 | } |
316 | ||
772030fe PMF |
317 | static void lib_update_tracepoints(void) |
318 | { | |
319 | struct tracepoint_lib *lib; | |
320 | ||
b27f8e75 | 321 | cds_list_for_each_entry(lib, &libs, list) { |
772030fe PMF |
322 | tracepoint_update_probe_range(lib->tracepoints_start, |
323 | lib->tracepoints_start + lib->tracepoints_count); | |
b27f8e75 | 324 | } |
772030fe PMF |
325 | } |
326 | ||
f99be407 PMF |
327 | /* |
328 | * Update probes, removing the faulty probes. | |
329 | */ | |
330 | static void tracepoint_update_probes(void) | |
331 | { | |
b27f8e75 | 332 | /* tracepoints registered from libraries and executable. */ |
474d745f | 333 | lib_update_tracepoints(); |
f99be407 PMF |
334 | } |
335 | ||
b979b346 | 336 | static struct tracepoint_probe * |
9dec086e | 337 | tracepoint_add_probe(const char *name, void *probe, void *data) |
f99be407 PMF |
338 | { |
339 | struct tracepoint_entry *entry; | |
b979b346 | 340 | struct tracepoint_probe *old; |
f99be407 PMF |
341 | |
342 | entry = get_tracepoint(name); | |
343 | if (!entry) { | |
344 | entry = add_tracepoint(name); | |
345 | if (IS_ERR(entry)) | |
b979b346 | 346 | return (struct tracepoint_probe *)entry; |
f99be407 | 347 | } |
9dec086e | 348 | old = tracepoint_entry_add_probe(entry, probe, data); |
f99be407 PMF |
349 | if (IS_ERR(old) && !entry->refcount) |
350 | remove_tracepoint(entry); | |
351 | return old; | |
352 | } | |
353 | ||
354 | /** | |
81614639 | 355 | * __tracepoint_probe_register - Connect a probe to a tracepoint |
f99be407 PMF |
356 | * @name: tracepoint name |
357 | * @probe: probe handler | |
358 | * | |
359 | * Returns 0 if ok, error value on error. | |
360 | * The probe address must at least be aligned on the architecture pointer size. | |
17dfb34b | 361 | * Called with the UST lock held. |
f99be407 | 362 | */ |
81614639 | 363 | int __tracepoint_probe_register(const char *name, void *probe, void *data) |
f99be407 PMF |
364 | { |
365 | void *old; | |
366 | ||
9dec086e | 367 | old = tracepoint_add_probe(name, probe, data); |
f99be407 PMF |
368 | if (IS_ERR(old)) |
369 | return PTR_ERR(old); | |
370 | ||
371 | tracepoint_update_probes(); /* may update entry */ | |
372 | release_probes(old); | |
373 | return 0; | |
374 | } | |
f99be407 | 375 | |
9dec086e | 376 | static void *tracepoint_remove_probe(const char *name, void *probe, void *data) |
f99be407 PMF |
377 | { |
378 | struct tracepoint_entry *entry; | |
379 | void *old; | |
380 | ||
381 | entry = get_tracepoint(name); | |
382 | if (!entry) | |
383 | return ERR_PTR(-ENOENT); | |
9dec086e | 384 | old = tracepoint_entry_remove_probe(entry, probe, data); |
f99be407 PMF |
385 | if (IS_ERR(old)) |
386 | return old; | |
387 | if (!entry->refcount) | |
388 | remove_tracepoint(entry); | |
389 | return old; | |
390 | } | |
391 | ||
392 | /** | |
393 | * tracepoint_probe_unregister - Disconnect a probe from a tracepoint | |
394 | * @name: tracepoint name | |
395 | * @probe: probe function pointer | |
9dec086e | 396 | * @probe: probe data pointer |
f99be407 | 397 | * |
17dfb34b | 398 | * Called with the UST lock held. |
f99be407 | 399 | */ |
81614639 | 400 | int __tracepoint_probe_unregister(const char *name, void *probe, void *data) |
f99be407 PMF |
401 | { |
402 | void *old; | |
403 | ||
9dec086e | 404 | old = tracepoint_remove_probe(name, probe, data); |
f99be407 PMF |
405 | if (IS_ERR(old)) |
406 | return PTR_ERR(old); | |
407 | ||
408 | tracepoint_update_probes(); /* may update entry */ | |
409 | release_probes(old); | |
410 | return 0; | |
411 | } | |
f99be407 | 412 | |
f99be407 PMF |
413 | static void tracepoint_add_old_probes(void *old) |
414 | { | |
415 | need_update = 1; | |
416 | if (old) { | |
b728d87e | 417 | struct tp_probes *tp_probes = caa_container_of(old, |
f99be407 | 418 | struct tp_probes, probes[0]); |
0222e121 | 419 | cds_list_add(&tp_probes->u.list, &old_probes); |
f99be407 PMF |
420 | } |
421 | } | |
422 | ||
423 | /** | |
424 | * tracepoint_probe_register_noupdate - register a probe but not connect | |
425 | * @name: tracepoint name | |
426 | * @probe: probe handler | |
427 | * | |
428 | * caller must call tracepoint_probe_update_all() | |
17dfb34b | 429 | * Called with the UST lock held. |
f99be407 | 430 | */ |
9dec086e NC |
431 | int tracepoint_probe_register_noupdate(const char *name, void *probe, |
432 | void *data) | |
f99be407 PMF |
433 | { |
434 | void *old; | |
435 | ||
9dec086e | 436 | old = tracepoint_add_probe(name, probe, data); |
f99be407 | 437 | if (IS_ERR(old)) { |
f99be407 PMF |
438 | return PTR_ERR(old); |
439 | } | |
440 | tracepoint_add_old_probes(old); | |
f99be407 PMF |
441 | return 0; |
442 | } | |
f99be407 PMF |
443 | |
444 | /** | |
445 | * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect | |
446 | * @name: tracepoint name | |
447 | * @probe: probe function pointer | |
448 | * | |
449 | * caller must call tracepoint_probe_update_all() | |
17dfb34b | 450 | * Called with the UST lock held. |
f99be407 | 451 | */ |
9dec086e NC |
452 | int tracepoint_probe_unregister_noupdate(const char *name, void *probe, |
453 | void *data) | |
f99be407 PMF |
454 | { |
455 | void *old; | |
456 | ||
9dec086e | 457 | old = tracepoint_remove_probe(name, probe, data); |
f99be407 | 458 | if (IS_ERR(old)) { |
f99be407 PMF |
459 | return PTR_ERR(old); |
460 | } | |
461 | tracepoint_add_old_probes(old); | |
f99be407 PMF |
462 | return 0; |
463 | } | |
f99be407 PMF |
464 | |
465 | /** | |
466 | * tracepoint_probe_update_all - update tracepoints | |
17dfb34b | 467 | * Called with the UST lock held. |
f99be407 PMF |
468 | */ |
469 | void tracepoint_probe_update_all(void) | |
470 | { | |
0222e121 | 471 | CDS_LIST_HEAD(release_probes); |
f99be407 PMF |
472 | struct tp_probes *pos, *next; |
473 | ||
f99be407 | 474 | if (!need_update) { |
f99be407 PMF |
475 | return; |
476 | } | |
0222e121 MD |
477 | if (!cds_list_empty(&old_probes)) |
478 | cds_list_replace_init(&old_probes, &release_probes); | |
f99be407 | 479 | need_update = 0; |
f99be407 PMF |
480 | |
481 | tracepoint_update_probes(); | |
0222e121 MD |
482 | cds_list_for_each_entry_safe(pos, next, &release_probes, u.list) { |
483 | cds_list_del(&pos->u.list); | |
474d745f | 484 | synchronize_rcu(); |
909bc43f | 485 | free(pos); |
f99be407 PMF |
486 | } |
487 | } | |
f99be407 | 488 | |
772030fe PMF |
489 | /* |
490 | * Returns 0 if current not found. | |
491 | * Returns 1 if current found. | |
b27f8e75 MD |
492 | * |
493 | * Called with tracepoint mutex held | |
772030fe PMF |
494 | */ |
495 | int lib_get_iter_tracepoints(struct tracepoint_iter *iter) | |
496 | { | |
497 | struct tracepoint_lib *iter_lib; | |
498 | int found = 0; | |
499 | ||
0222e121 | 500 | cds_list_for_each_entry(iter_lib, &libs, list) { |
772030fe PMF |
501 | if (iter_lib < iter->lib) |
502 | continue; | |
503 | else if (iter_lib > iter->lib) | |
504 | iter->tracepoint = NULL; | |
505 | found = tracepoint_get_iter_range(&iter->tracepoint, | |
506 | iter_lib->tracepoints_start, | |
507 | iter_lib->tracepoints_start + iter_lib->tracepoints_count); | |
508 | if (found) { | |
509 | iter->lib = iter_lib; | |
510 | break; | |
511 | } | |
512 | } | |
772030fe PMF |
513 | return found; |
514 | } | |
515 | ||
f99be407 PMF |
516 | /** |
517 | * tracepoint_get_iter_range - Get a next tracepoint iterator given a range. | |
518 | * @tracepoint: current tracepoints (in), next tracepoint (out) | |
519 | * @begin: beginning of the range | |
520 | * @end: end of the range | |
521 | * | |
522 | * Returns whether a next tracepoint has been found (1) or not (0). | |
523 | * Will return the first tracepoint in the range if the input tracepoint is | |
524 | * NULL. | |
b27f8e75 | 525 | * Called with tracepoint mutex held. |
f99be407 | 526 | */ |
f218ff28 MD |
527 | int tracepoint_get_iter_range(struct tracepoint * const **tracepoint, |
528 | struct tracepoint * const *begin, struct tracepoint * const *end) | |
f99be407 | 529 | { |
f08ebbe2 | 530 | if (!*tracepoint && begin != end) |
f99be407 | 531 | *tracepoint = begin; |
f08ebbe2 MD |
532 | while (*tracepoint >= begin && *tracepoint < end) { |
533 | if (!**tracepoint) | |
534 | (*tracepoint)++; /* skip dummy */ | |
535 | else | |
536 | return 1; | |
f99be407 | 537 | } |
f99be407 PMF |
538 | return 0; |
539 | } | |
f99be407 | 540 | |
b27f8e75 MD |
541 | /* |
542 | * Called with tracepoint mutex held. | |
543 | */ | |
f99be407 PMF |
544 | static void tracepoint_get_iter(struct tracepoint_iter *iter) |
545 | { | |
546 | int found = 0; | |
547 | ||
474d745f PMF |
548 | /* tracepoints in libs. */ |
549 | found = lib_get_iter_tracepoints(iter); | |
f99be407 PMF |
550 | if (!found) |
551 | tracepoint_iter_reset(iter); | |
552 | } | |
553 | ||
17dfb34b MD |
554 | /* |
555 | * Called with UST lock held. | |
556 | */ | |
f99be407 PMF |
557 | void tracepoint_iter_start(struct tracepoint_iter *iter) |
558 | { | |
559 | tracepoint_get_iter(iter); | |
560 | } | |
f99be407 | 561 | |
b27f8e75 | 562 | /* |
17dfb34b | 563 | * Called with UST lock held. |
b27f8e75 | 564 | */ |
f99be407 PMF |
565 | void tracepoint_iter_next(struct tracepoint_iter *iter) |
566 | { | |
567 | iter->tracepoint++; | |
568 | /* | |
569 | * iter->tracepoint may be invalid because we blindly incremented it. | |
570 | * Make sure it is valid by marshalling on the tracepoints, getting the | |
571 | * tracepoints from following modules if necessary. | |
572 | */ | |
573 | tracepoint_get_iter(iter); | |
574 | } | |
f99be407 | 575 | |
17dfb34b MD |
576 | /* |
577 | * Called with UST lock held. | |
578 | */ | |
f99be407 PMF |
579 | void tracepoint_iter_stop(struct tracepoint_iter *iter) |
580 | { | |
581 | } | |
f99be407 PMF |
582 | |
583 | void tracepoint_iter_reset(struct tracepoint_iter *iter) | |
584 | { | |
f99be407 PMF |
585 | iter->tracepoint = NULL; |
586 | } | |
474d745f PMF |
587 | |
588 | void tracepoint_set_new_tracepoint_cb(void (*cb)(struct tracepoint *)) | |
589 | { | |
590 | new_tracepoint_cb = cb; | |
591 | } | |
f99be407 | 592 | |
f218ff28 | 593 | static void new_tracepoints(struct tracepoint * const *start, struct tracepoint * const *end) |
f99be407 | 594 | { |
f218ff28 MD |
595 | if (new_tracepoint_cb) { |
596 | struct tracepoint * const *t; | |
f08ebbe2 | 597 | |
b27f8e75 | 598 | for (t = start; t < end; t++) { |
f08ebbe2 MD |
599 | if (*t) |
600 | new_tracepoint_cb(*t); | |
474d745f PMF |
601 | } |
602 | } | |
f99be407 | 603 | } |
f99be407 | 604 | |
b27f8e75 MD |
605 | int tracepoint_register_lib(struct tracepoint * const *tracepoints_start, |
606 | int tracepoints_count) | |
474d745f | 607 | { |
b467f7a7 | 608 | struct tracepoint_lib *pl, *iter; |
474d745f | 609 | |
edaa1431 MD |
610 | init_tracepoint(); |
611 | ||
1dba3e6c | 612 | pl = (struct tracepoint_lib *) zmalloc(sizeof(struct tracepoint_lib)); |
474d745f PMF |
613 | |
614 | pl->tracepoints_start = tracepoints_start; | |
615 | pl->tracepoints_count = tracepoints_count; | |
616 | ||
17dfb34b | 617 | ust_lock(); |
b467f7a7 MD |
618 | /* |
619 | * We sort the libs by struct lib pointer address. | |
620 | */ | |
621 | cds_list_for_each_entry_reverse(iter, &libs, list) { | |
622 | BUG_ON(iter == pl); /* Should never be in the list twice */ | |
623 | if (iter < pl) { | |
624 | /* We belong to the location right after iter. */ | |
625 | cds_list_add(&pl->list, &iter->list); | |
626 | goto lib_added; | |
627 | } | |
628 | } | |
629 | /* We should be added at the head of the list */ | |
0222e121 | 630 | cds_list_add(&pl->list, &libs); |
b467f7a7 | 631 | lib_added: |
474d745f PMF |
632 | new_tracepoints(tracepoints_start, tracepoints_start + tracepoints_count); |
633 | ||
b27f8e75 | 634 | /* TODO: update just the loaded lib */ |
474d745f | 635 | lib_update_tracepoints(); |
17dfb34b | 636 | ust_unlock(); |
474d745f | 637 | |
1fcf7ad7 MD |
638 | DBG("just registered a tracepoints section from %p and having %d tracepoints", |
639 | tracepoints_start, tracepoints_count); | |
9dec086e | 640 | |
474d745f PMF |
641 | return 0; |
642 | } | |
643 | ||
f218ff28 | 644 | int tracepoint_unregister_lib(struct tracepoint * const *tracepoints_start) |
474d745f | 645 | { |
24b6668c PMF |
646 | struct tracepoint_lib *lib; |
647 | ||
17dfb34b | 648 | ust_lock(); |
0222e121 | 649 | cds_list_for_each_entry(lib, &libs, list) { |
f218ff28 | 650 | if (lib->tracepoints_start == tracepoints_start) { |
24b6668c | 651 | struct tracepoint_lib *lib2free = lib; |
0222e121 | 652 | cds_list_del(&lib->list); |
24b6668c PMF |
653 | free(lib2free); |
654 | break; | |
655 | } | |
656 | } | |
17dfb34b | 657 | ust_unlock(); |
474d745f PMF |
658 | |
659 | return 0; | |
660 | } | |
b27f8e75 | 661 | |
edaa1431 | 662 | void init_tracepoint(void) |
b27f8e75 | 663 | { |
edaa1431 MD |
664 | if (uatomic_xchg(&initialized, 1) == 1) |
665 | return; | |
5e96a467 | 666 | init_usterr(); |
b27f8e75 MD |
667 | } |
668 | ||
edaa1431 | 669 | void exit_tracepoint(void) |
b27f8e75 | 670 | { |
17dfb34b | 671 | initialized = 0; |
b27f8e75 | 672 | } |