2 * Copyright (C) 2008-2011 Mathieu Desnoyers
3 * Copyright (C) 2009 Pierre-Marc Fournier
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation;
8 * version 2.1 of the License.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
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
19 * Ported to userspace by Pierre-Marc Fournier.
24 #include <ust/tracepoint.h>
25 #include <ust/tracepoint-internal.h>
27 #include <ust/kcompat/kcompat.h>
29 #include <urcu/hlist.h>
30 #include <urcu/uatomic.h>
32 #include <ust/usterr-signal-safe.h>
34 /* Set to 1 to enable tracepoint debug output */
35 static const int tracepoint_debug
;
36 static int initialized
;
37 static void (*new_tracepoint_cb
)(struct tracepoint
*);
39 /* libraries that contain tracepoints (struct tracepoint_lib) */
40 static CDS_LIST_HEAD(libs
);
43 * The UST lock protects the library tracepoints, the hash table, and
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.
51 * Tracepoint hash table, containing the active tracepoints.
52 * Protected by tracepoints_mutex.
54 #define TRACEPOINT_HASH_BITS 6
55 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
56 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
58 static CDS_LIST_HEAD(old_probes
);
59 static int need_update
;
63 * It is used to to delay the free of multiple probes array until a quiescent
65 * Tracepoint entries modifications are protected by the tracepoints_mutex.
67 struct tracepoint_entry
{
68 struct cds_hlist_node hlist
;
69 struct tracepoint_probe
*probes
;
70 int refcount
; /* Number of times armed. 0 if disarmed. */
76 struct cds_list_head list
;
78 struct tracepoint_probe probes
[0];
81 static inline void *allocate_probes(int count
)
83 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
84 + sizeof(struct tp_probes
));
85 return p
== NULL
? NULL
: p
->probes
;
88 static inline void release_probes(void *old
)
91 struct tp_probes
*tp_probes
= _ust_container_of(old
,
92 struct tp_probes
, probes
[0]);
98 static void debug_print_probes(struct tracepoint_entry
*entry
)
102 if (!tracepoint_debug
|| !entry
->probes
)
105 for (i
= 0; entry
->probes
[i
].func
; i
++)
106 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
110 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
111 void *probe
, void *data
)
114 struct tracepoint_probe
*old
, *new;
118 debug_print_probes(entry
);
121 /* (N -> N+1), (N != 0, 1) probes */
122 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
123 if (old
[nr_probes
].func
== probe
&&
124 old
[nr_probes
].data
== data
)
125 return ERR_PTR(-EEXIST
);
127 /* + 2 : one for new probe, one for NULL func */
128 new = allocate_probes(nr_probes
+ 2);
130 return ERR_PTR(-ENOMEM
);
132 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
133 new[nr_probes
].func
= probe
;
134 new[nr_probes
].data
= data
;
135 new[nr_probes
+ 1].func
= NULL
;
136 entry
->refcount
= nr_probes
+ 1;
138 debug_print_probes(entry
);
143 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
146 int nr_probes
= 0, nr_del
= 0, i
;
147 struct tracepoint_probe
*old
, *new;
152 return ERR_PTR(-ENOENT
);
154 debug_print_probes(entry
);
155 /* (N -> M), (N > 1, M >= 0) probes */
156 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
158 (old
[nr_probes
].func
== probe
&&
159 old
[nr_probes
].data
== data
))
163 if (nr_probes
- nr_del
== 0) {
164 /* N -> 0, (N > 1) */
165 entry
->probes
= NULL
;
167 debug_print_probes(entry
);
171 /* N -> M, (N > 1, M > 0) */
173 new = allocate_probes(nr_probes
- nr_del
+ 1);
175 return ERR_PTR(-ENOMEM
);
176 for (i
= 0; old
[i
].func
; i
++)
178 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
180 new[nr_probes
- nr_del
].func
= NULL
;
181 entry
->refcount
= nr_probes
- nr_del
;
184 debug_print_probes(entry
);
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.
193 static struct tracepoint_entry
*get_tracepoint(const char *name
)
195 struct cds_hlist_head
*head
;
196 struct cds_hlist_node
*node
;
197 struct tracepoint_entry
*e
;
198 u32 hash
= jhash(name
, strlen(name
), 0);
200 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
201 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
202 if (!strcmp(name
, e
->name
))
209 * Add the tracepoint to the tracepoint hash table. Must be called with
210 * tracepoints_mutex held.
212 static struct tracepoint_entry
*add_tracepoint(const char *name
)
214 struct cds_hlist_head
*head
;
215 struct cds_hlist_node
*node
;
216 struct tracepoint_entry
*e
;
217 size_t name_len
= strlen(name
) + 1;
218 u32 hash
= jhash(name
, name_len
-1, 0);
220 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
221 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
222 if (!strcmp(name
, e
->name
)) {
223 DBG("tracepoint %s busy", name
);
224 return ERR_PTR(-EEXIST
); /* Already there */
228 * Using zmalloc here to allocate a variable length element. Could
229 * cause some memory fragmentation if overused.
231 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
233 return ERR_PTR(-ENOMEM
);
234 memcpy(&e
->name
[0], name
, name_len
);
237 cds_hlist_add_head(&e
->hlist
, head
);
242 * Remove the tracepoint from the tracepoint hash table. Must be called with
245 static inline void remove_tracepoint(struct tracepoint_entry
*e
)
247 cds_hlist_del(&e
->hlist
);
252 * Sets the probe callback corresponding to one tracepoint.
254 static void set_tracepoint(struct tracepoint_entry
**entry
,
255 struct tracepoint
*elem
, int active
)
257 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
260 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
261 * probe callbacks array is consistent before setting a pointer to it.
262 * This array is referenced by __DO_TRACE from
263 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
266 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
267 elem
->state
= active
;
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.
276 static void disable_tracepoint(struct tracepoint
*elem
)
279 rcu_assign_pointer(elem
->probes
, NULL
);
283 * tracepoint_update_probe_range - Update a probe range
284 * @begin: beginning of the range
285 * @end: end of the range
287 * Updates the probe callback corresponding to a range of tracepoints.
290 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
291 struct tracepoint
* const *end
)
293 struct tracepoint
* const *iter
;
294 struct tracepoint_entry
*mark_entry
;
296 for (iter
= begin
; iter
< end
; iter
++) {
298 continue; /* skip dummy */
299 if (!(*iter
)->name
) {
300 disable_tracepoint(*iter
);
303 mark_entry
= get_tracepoint((*iter
)->name
);
305 set_tracepoint(&mark_entry
, *iter
,
306 !!mark_entry
->refcount
);
308 disable_tracepoint(*iter
);
313 static void lib_update_tracepoints(void)
315 struct tracepoint_lib
*lib
;
317 cds_list_for_each_entry(lib
, &libs
, list
) {
318 tracepoint_update_probe_range(lib
->tracepoints_start
,
319 lib
->tracepoints_start
+ lib
->tracepoints_count
);
324 * Update probes, removing the faulty probes.
326 static void tracepoint_update_probes(void)
328 /* tracepoints registered from libraries and executable. */
329 lib_update_tracepoints();
332 static struct tracepoint_probe
*
333 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
335 struct tracepoint_entry
*entry
;
336 struct tracepoint_probe
*old
;
338 entry
= get_tracepoint(name
);
340 entry
= add_tracepoint(name
);
342 return (struct tracepoint_probe
*)entry
;
344 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
345 if (IS_ERR(old
) && !entry
->refcount
)
346 remove_tracepoint(entry
);
351 * __tracepoint_probe_register - Connect a probe to a tracepoint
352 * @name: tracepoint name
353 * @probe: probe handler
355 * Returns 0 if ok, error value on error.
356 * The probe address must at least be aligned on the architecture pointer size.
357 * Called with the UST lock held.
359 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
363 old
= tracepoint_add_probe(name
, probe
, data
);
367 tracepoint_update_probes(); /* may update entry */
372 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
374 struct tracepoint_entry
*entry
;
377 entry
= get_tracepoint(name
);
379 return ERR_PTR(-ENOENT
);
380 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
383 if (!entry
->refcount
)
384 remove_tracepoint(entry
);
389 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
390 * @name: tracepoint name
391 * @probe: probe function pointer
392 * @probe: probe data pointer
394 * Called with the UST lock held.
396 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
400 old
= tracepoint_remove_probe(name
, probe
, data
);
404 tracepoint_update_probes(); /* may update entry */
409 static void tracepoint_add_old_probes(void *old
)
413 struct tp_probes
*tp_probes
= _ust_container_of(old
,
414 struct tp_probes
, probes
[0]);
415 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
420 * tracepoint_probe_register_noupdate - register a probe but not connect
421 * @name: tracepoint name
422 * @probe: probe handler
424 * caller must call tracepoint_probe_update_all()
425 * Called with the UST lock held.
427 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
432 old
= tracepoint_add_probe(name
, probe
, data
);
436 tracepoint_add_old_probes(old
);
441 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
442 * @name: tracepoint name
443 * @probe: probe function pointer
445 * caller must call tracepoint_probe_update_all()
446 * Called with the UST lock held.
448 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
453 old
= tracepoint_remove_probe(name
, probe
, data
);
457 tracepoint_add_old_probes(old
);
462 * tracepoint_probe_update_all - update tracepoints
463 * Called with the UST lock held.
465 void tracepoint_probe_update_all(void)
467 CDS_LIST_HEAD(release_probes
);
468 struct tp_probes
*pos
, *next
;
473 if (!cds_list_empty(&old_probes
))
474 cds_list_replace_init(&old_probes
, &release_probes
);
477 tracepoint_update_probes();
478 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
479 cds_list_del(&pos
->u
.list
);
486 * Returns 0 if current not found.
487 * Returns 1 if current found.
489 * Called with tracepoint mutex held
491 int lib_get_iter_tracepoints(struct tracepoint_iter
*iter
)
493 struct tracepoint_lib
*iter_lib
;
496 cds_list_for_each_entry(iter_lib
, &libs
, list
) {
497 if (iter_lib
< iter
->lib
)
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
);
505 iter
->lib
= iter_lib
;
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
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
521 * Called with tracepoint mutex held.
523 int tracepoint_get_iter_range(struct tracepoint
* const **tracepoint
,
524 struct tracepoint
* const *begin
, struct tracepoint
* const *end
)
526 if (!*tracepoint
&& begin
!= end
)
528 while (*tracepoint
>= begin
&& *tracepoint
< end
) {
530 (*tracepoint
)++; /* skip dummy */
538 * Called with tracepoint mutex held.
540 static void tracepoint_get_iter(struct tracepoint_iter
*iter
)
544 /* tracepoints in libs. */
545 found
= lib_get_iter_tracepoints(iter
);
547 tracepoint_iter_reset(iter
);
551 * Called with UST lock held.
553 void tracepoint_iter_start(struct tracepoint_iter
*iter
)
555 tracepoint_get_iter(iter
);
559 * Called with UST lock held.
561 void tracepoint_iter_next(struct tracepoint_iter
*iter
)
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.
569 tracepoint_get_iter(iter
);
573 * Called with UST lock held.
575 void tracepoint_iter_stop(struct tracepoint_iter
*iter
)
579 void tracepoint_iter_reset(struct tracepoint_iter
*iter
)
581 iter
->tracepoint
= NULL
;
584 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
586 new_tracepoint_cb
= cb
;
589 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
591 if (new_tracepoint_cb
) {
592 struct tracepoint
* const *t
;
594 for (t
= start
; t
< end
; t
++) {
596 new_tracepoint_cb(*t
);
601 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
602 int tracepoints_count
)
604 struct tracepoint_lib
*pl
, *iter
;
608 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
610 pl
->tracepoints_start
= tracepoints_start
;
611 pl
->tracepoints_count
= tracepoints_count
;
615 * We sort the libs by struct lib pointer address.
617 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
618 BUG_ON(iter
== pl
); /* Should never be in the list twice */
620 /* We belong to the location right after iter. */
621 cds_list_add(&pl
->list
, &iter
->list
);
625 /* We should be added at the head of the list */
626 cds_list_add(&pl
->list
, &libs
);
628 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
630 /* TODO: update just the loaded lib */
631 lib_update_tracepoints();
634 DBG("just registered a tracepoints section from %p and having %d tracepoints",
635 tracepoints_start
, tracepoints_count
);
640 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
642 struct tracepoint_lib
*lib
;
645 cds_list_for_each_entry(lib
, &libs
, list
) {
646 if (lib
->tracepoints_start
== tracepoints_start
) {
647 struct tracepoint_lib
*lib2free
= lib
;
648 cds_list_del(&lib
->list
);
658 void init_tracepoint(void)
660 if (uatomic_xchg(&initialized
, 1) == 1)
665 void exit_tracepoint(void)