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