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