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