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