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