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