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