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.
27 #include <urcu/arch.h>
29 #include <urcu/hlist.h>
30 #include <urcu/uatomic.h>
31 #include <urcu/compiler.h>
33 #include <lttng/tracepoint.h>
34 #include <lttng/ust-abi.h> /* for LTTNG_UST_SYM_NAME_LEN */
36 #include <usterr-signal-safe.h>
39 #include "tracepoint-internal.h"
40 #include "ltt-tracer-core.h"
44 /* Set to 1 to enable tracepoint debug output */
45 static const int tracepoint_debug
;
46 static int initialized
;
47 static void (*new_tracepoint_cb
)(struct tracepoint
*);
50 * libraries that contain tracepoints (struct tracepoint_lib).
51 * Protected by UST lock.
53 static CDS_LIST_HEAD(libs
);
56 * The UST lock protects the library tracepoints, the hash table, and
58 * All calls to the tracepoint API must be protected by the UST lock,
59 * excepts calls to tracepoint_register_lib and
60 * tracepoint_unregister_lib, which take the UST lock themselves.
64 * Tracepoint hash table, containing the active tracepoints.
65 * Protected by ust lock.
67 #define TRACEPOINT_HASH_BITS 6
68 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
69 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
71 static CDS_LIST_HEAD(old_probes
);
72 static int need_update
;
76 * It is used to to delay the free of multiple probes array until a quiescent
78 * Tracepoint entries modifications are protected by the ust lock.
80 struct tracepoint_entry
{
81 struct cds_hlist_node hlist
;
82 struct tracepoint_probe
*probes
;
83 int refcount
; /* Number of times armed. 0 if disarmed. */
89 struct cds_list_head list
;
90 /* Field below only used for call_rcu scheme */
91 /* struct rcu_head head; */
93 struct tracepoint_probe probes
[0];
96 static void *allocate_probes(int count
)
98 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
99 + sizeof(struct tp_probes
));
100 return p
== NULL
? NULL
: p
->probes
;
103 static void release_probes(void *old
)
106 struct tp_probes
*tp_probes
= caa_container_of(old
,
107 struct tp_probes
, probes
[0]);
113 static void debug_print_probes(struct tracepoint_entry
*entry
)
117 if (!tracepoint_debug
|| !entry
->probes
)
120 for (i
= 0; entry
->probes
[i
].func
; i
++)
121 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
125 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
126 void *probe
, void *data
)
129 struct tracepoint_probe
*old
, *new;
133 debug_print_probes(entry
);
136 /* (N -> N+1), (N != 0, 1) probes */
137 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
138 if (old
[nr_probes
].func
== probe
&&
139 old
[nr_probes
].data
== data
)
140 return ERR_PTR(-EEXIST
);
142 /* + 2 : one for new probe, one for NULL func */
143 new = allocate_probes(nr_probes
+ 2);
145 return ERR_PTR(-ENOMEM
);
147 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
148 new[nr_probes
].func
= probe
;
149 new[nr_probes
].data
= data
;
150 new[nr_probes
+ 1].func
= NULL
;
151 entry
->refcount
= nr_probes
+ 1;
153 debug_print_probes(entry
);
158 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
161 int nr_probes
= 0, nr_del
= 0, i
;
162 struct tracepoint_probe
*old
, *new;
167 return ERR_PTR(-ENOENT
);
169 debug_print_probes(entry
);
170 /* (N -> M), (N > 1, M >= 0) probes */
171 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
173 (old
[nr_probes
].func
== probe
&&
174 old
[nr_probes
].data
== data
))
178 if (nr_probes
- nr_del
== 0) {
179 /* N -> 0, (N > 1) */
180 entry
->probes
= NULL
;
182 debug_print_probes(entry
);
186 /* N -> M, (N > 1, M > 0) */
188 new = allocate_probes(nr_probes
- nr_del
+ 1);
190 return ERR_PTR(-ENOMEM
);
191 for (i
= 0; old
[i
].func
; i
++)
193 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
195 new[nr_probes
- nr_del
].func
= NULL
;
196 entry
->refcount
= nr_probes
- nr_del
;
199 debug_print_probes(entry
);
204 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
205 * Must be called with ust lock held.
206 * Returns NULL if not present.
208 static struct tracepoint_entry
*get_tracepoint(const char *name
)
210 struct cds_hlist_head
*head
;
211 struct cds_hlist_node
*node
;
212 struct tracepoint_entry
*e
;
213 size_t name_len
= strlen(name
);
216 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
217 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
218 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
220 hash
= jhash(name
, name_len
, 0);
221 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
222 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
223 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1))
230 * Add the tracepoint to the tracepoint hash table. Must be called with
233 static struct tracepoint_entry
*add_tracepoint(const char *name
)
235 struct cds_hlist_head
*head
;
236 struct cds_hlist_node
*node
;
237 struct tracepoint_entry
*e
;
238 size_t name_len
= strlen(name
);
241 if (name_len
> LTTNG_UST_SYM_NAME_LEN
- 1) {
242 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name
, LTTNG_UST_SYM_NAME_LEN
- 1);
243 name_len
= LTTNG_UST_SYM_NAME_LEN
- 1;
245 hash
= jhash(name
, name_len
, 0);
246 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
247 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
248 if (!strncmp(name
, e
->name
, LTTNG_UST_SYM_NAME_LEN
- 1)) {
249 DBG("tracepoint %s busy", name
);
250 return ERR_PTR(-EEXIST
); /* Already there */
254 * Using zmalloc here to allocate a variable length element. Could
255 * cause some memory fragmentation if overused.
257 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
+ 1);
259 return ERR_PTR(-ENOMEM
);
260 memcpy(&e
->name
[0], name
, name_len
+ 1);
261 e
->name
[name_len
] = '\0';
264 cds_hlist_add_head(&e
->hlist
, head
);
269 * Remove the tracepoint from the tracepoint hash table. Must be called with
272 static void remove_tracepoint(struct tracepoint_entry
*e
)
274 cds_hlist_del(&e
->hlist
);
279 * Sets the probe callback corresponding to one tracepoint.
281 static void set_tracepoint(struct tracepoint_entry
**entry
,
282 struct tracepoint
*elem
, int active
)
284 WARN_ON(strncmp((*entry
)->name
, elem
->name
, LTTNG_UST_SYM_NAME_LEN
- 1) != 0);
287 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
288 * probe callbacks array is consistent before setting a pointer to it.
289 * This array is referenced by __DO_TRACE from
290 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
293 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
294 elem
->state
= active
;
298 * Disable a tracepoint and its probe callback.
299 * Note: only waiting an RCU period after setting elem->call to the empty
300 * function insures that the original callback is not used anymore. This insured
301 * by preempt_disable around the call site.
303 static void disable_tracepoint(struct tracepoint
*elem
)
306 rcu_assign_pointer(elem
->probes
, NULL
);
310 * tracepoint_update_probe_range - Update a probe range
311 * @begin: beginning of the range
312 * @end: end of the range
314 * Updates the probe callback corresponding to a range of tracepoints.
317 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
318 struct tracepoint
* const *end
)
320 struct tracepoint
* const *iter
;
321 struct tracepoint_entry
*mark_entry
;
323 for (iter
= begin
; iter
< end
; iter
++) {
325 continue; /* skip dummy */
326 if (!(*iter
)->name
) {
327 disable_tracepoint(*iter
);
330 mark_entry
= get_tracepoint((*iter
)->name
);
332 set_tracepoint(&mark_entry
, *iter
,
333 !!mark_entry
->refcount
);
335 disable_tracepoint(*iter
);
340 static void lib_update_tracepoints(void)
342 struct tracepoint_lib
*lib
;
344 cds_list_for_each_entry(lib
, &libs
, list
) {
345 tracepoint_update_probe_range(lib
->tracepoints_start
,
346 lib
->tracepoints_start
+ lib
->tracepoints_count
);
351 * Update probes, removing the faulty probes.
353 static void tracepoint_update_probes(void)
355 /* tracepoints registered from libraries and executable. */
356 lib_update_tracepoints();
359 static struct tracepoint_probe
*
360 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
362 struct tracepoint_entry
*entry
;
363 struct tracepoint_probe
*old
;
365 entry
= get_tracepoint(name
);
367 entry
= add_tracepoint(name
);
369 return (struct tracepoint_probe
*)entry
;
371 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
372 if (IS_ERR(old
) && !entry
->refcount
)
373 remove_tracepoint(entry
);
378 * __tracepoint_probe_register - Connect a probe to a tracepoint
379 * @name: tracepoint name
380 * @probe: probe handler
382 * Returns 0 if ok, error value on error.
383 * The probe address must at least be aligned on the architecture pointer size.
384 * Called with the UST lock held.
386 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
390 old
= tracepoint_add_probe(name
, probe
, data
);
394 tracepoint_update_probes(); /* may update entry */
399 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
401 struct tracepoint_entry
*entry
;
404 entry
= get_tracepoint(name
);
406 return ERR_PTR(-ENOENT
);
407 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
410 if (!entry
->refcount
)
411 remove_tracepoint(entry
);
416 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
417 * @name: tracepoint name
418 * @probe: probe function pointer
419 * @probe: probe data pointer
421 * Called with the UST lock held.
423 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
427 old
= tracepoint_remove_probe(name
, probe
, data
);
431 tracepoint_update_probes(); /* may update entry */
436 static void tracepoint_add_old_probes(void *old
)
440 struct tp_probes
*tp_probes
= caa_container_of(old
,
441 struct tp_probes
, probes
[0]);
442 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
447 * tracepoint_probe_register_noupdate - register a probe but not connect
448 * @name: tracepoint name
449 * @probe: probe handler
451 * caller must call tracepoint_probe_update_all()
452 * Called with the UST lock held.
454 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
459 old
= tracepoint_add_probe(name
, probe
, data
);
463 tracepoint_add_old_probes(old
);
468 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
469 * @name: tracepoint name
470 * @probe: probe function pointer
472 * caller must call tracepoint_probe_update_all()
473 * Called with the UST lock held.
475 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
480 old
= tracepoint_remove_probe(name
, probe
, data
);
484 tracepoint_add_old_probes(old
);
489 * tracepoint_probe_update_all - update tracepoints
490 * Called with the UST lock held.
492 void tracepoint_probe_update_all(void)
494 CDS_LIST_HEAD(release_probes
);
495 struct tp_probes
*pos
, *next
;
500 if (!cds_list_empty(&old_probes
))
501 cds_list_replace_init(&old_probes
, &release_probes
);
504 tracepoint_update_probes();
505 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
506 cds_list_del(&pos
->u
.list
);
512 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
514 new_tracepoint_cb
= cb
;
517 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
519 if (new_tracepoint_cb
) {
520 struct tracepoint
* const *t
;
522 for (t
= start
; t
< end
; t
++) {
524 new_tracepoint_cb(*t
);
530 void lib_disable_tracepoints(struct tracepoint
* const *begin
,
531 struct tracepoint
* const *end
)
533 struct tracepoint
* const *iter
;
535 for (iter
= begin
; iter
< end
; iter
++) {
537 continue; /* skip dummy */
538 disable_tracepoint(*iter
);
543 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
544 int tracepoints_count
)
546 struct tracepoint_lib
*pl
, *iter
;
550 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
552 pl
->tracepoints_start
= tracepoints_start
;
553 pl
->tracepoints_count
= tracepoints_count
;
557 * We sort the libs by struct lib pointer address.
559 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
560 BUG_ON(iter
== pl
); /* Should never be in the list twice */
562 /* We belong to the location right after iter. */
563 cds_list_add(&pl
->list
, &iter
->list
);
567 /* We should be added at the head of the list */
568 cds_list_add(&pl
->list
, &libs
);
570 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
572 /* TODO: update just the loaded lib */
573 lib_update_tracepoints();
576 DBG("just registered a tracepoints section from %p and having %d tracepoints",
577 tracepoints_start
, tracepoints_count
);
582 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
584 struct tracepoint_lib
*lib
;
585 int tracepoints_count
;
588 cds_list_for_each_entry(lib
, &libs
, list
) {
589 if (lib
->tracepoints_start
== tracepoints_start
) {
590 struct tracepoint_lib
*lib2free
= lib
;
592 cds_list_del(&lib
->list
);
593 tracepoints_count
= lib
->tracepoints_count
;
601 * Force tracepoint disarm for all tracepoints of this lib.
602 * This takes care of destructor of library that would leave a
603 * LD_PRELOAD wrapper override function enabled for tracing, but
604 * the session teardown would not be able to reach the
605 * tracepoint anymore to disable it.
607 lib_disable_tracepoints(tracepoints_start
,
608 tracepoints_start
+ tracepoints_count
);
609 DBG("just unregistered a tracepoints section from %p",
616 void init_tracepoint(void)
618 if (uatomic_xchg(&initialized
, 1) == 1)
623 void exit_tracepoint(void)
629 * Create the wrapper symbols.
631 #undef tp_rcu_read_lock_bp
632 #undef tp_rcu_read_unlock_bp
633 #undef tp_rcu_dereference_bp
635 void tp_rcu_read_lock_bp(void)
640 void tp_rcu_read_unlock_bp(void)
642 rcu_read_unlock_bp();
645 void *tp_rcu_dereference_sym_bp(void *p
)
647 return rcu_dereference_bp(p
);