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>
35 #include <usterr-signal-safe.h>
38 #include "tracepoint-internal.h"
39 #include "ltt-tracer-core.h"
43 /* Set to 1 to enable tracepoint debug output */
44 static const int tracepoint_debug
;
45 static int initialized
;
46 static void (*new_tracepoint_cb
)(struct tracepoint
*);
49 * libraries that contain tracepoints (struct tracepoint_lib).
50 * Protected by UST lock.
52 static CDS_LIST_HEAD(libs
);
55 * The UST lock protects the library tracepoints, the hash table, and
57 * All calls to the tracepoint API must be protected by the UST lock,
58 * excepts calls to tracepoint_register_lib and
59 * tracepoint_unregister_lib, which take the UST lock themselves.
63 * Tracepoint hash table, containing the active tracepoints.
64 * Protected by ust lock.
66 #define TRACEPOINT_HASH_BITS 6
67 #define TRACEPOINT_TABLE_SIZE (1 << TRACEPOINT_HASH_BITS)
68 static struct cds_hlist_head tracepoint_table
[TRACEPOINT_TABLE_SIZE
];
70 static CDS_LIST_HEAD(old_probes
);
71 static int need_update
;
75 * It is used to to delay the free of multiple probes array until a quiescent
77 * Tracepoint entries modifications are protected by the ust lock.
79 struct tracepoint_entry
{
80 struct cds_hlist_node hlist
;
81 struct tracepoint_probe
*probes
;
82 int refcount
; /* Number of times armed. 0 if disarmed. */
88 struct cds_list_head list
;
90 struct tracepoint_probe probes
[0];
93 static void *allocate_probes(int count
)
95 struct tp_probes
*p
= zmalloc(count
* sizeof(struct tracepoint_probe
)
96 + sizeof(struct tp_probes
));
97 return p
== NULL
? NULL
: p
->probes
;
100 static void release_probes(void *old
)
103 struct tp_probes
*tp_probes
= caa_container_of(old
,
104 struct tp_probes
, probes
[0]);
110 static void debug_print_probes(struct tracepoint_entry
*entry
)
114 if (!tracepoint_debug
|| !entry
->probes
)
117 for (i
= 0; entry
->probes
[i
].func
; i
++)
118 DBG("Probe %d : %p", i
, entry
->probes
[i
].func
);
122 tracepoint_entry_add_probe(struct tracepoint_entry
*entry
,
123 void *probe
, void *data
)
126 struct tracepoint_probe
*old
, *new;
130 debug_print_probes(entry
);
133 /* (N -> N+1), (N != 0, 1) probes */
134 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++)
135 if (old
[nr_probes
].func
== probe
&&
136 old
[nr_probes
].data
== data
)
137 return ERR_PTR(-EEXIST
);
139 /* + 2 : one for new probe, one for NULL func */
140 new = allocate_probes(nr_probes
+ 2);
142 return ERR_PTR(-ENOMEM
);
144 memcpy(new, old
, nr_probes
* sizeof(struct tracepoint_probe
));
145 new[nr_probes
].func
= probe
;
146 new[nr_probes
].data
= data
;
147 new[nr_probes
+ 1].func
= NULL
;
148 entry
->refcount
= nr_probes
+ 1;
150 debug_print_probes(entry
);
155 tracepoint_entry_remove_probe(struct tracepoint_entry
*entry
, void *probe
,
158 int nr_probes
= 0, nr_del
= 0, i
;
159 struct tracepoint_probe
*old
, *new;
164 return ERR_PTR(-ENOENT
);
166 debug_print_probes(entry
);
167 /* (N -> M), (N > 1, M >= 0) probes */
168 for (nr_probes
= 0; old
[nr_probes
].func
; nr_probes
++) {
170 (old
[nr_probes
].func
== probe
&&
171 old
[nr_probes
].data
== data
))
175 if (nr_probes
- nr_del
== 0) {
176 /* N -> 0, (N > 1) */
177 entry
->probes
= NULL
;
179 debug_print_probes(entry
);
183 /* N -> M, (N > 1, M > 0) */
185 new = allocate_probes(nr_probes
- nr_del
+ 1);
187 return ERR_PTR(-ENOMEM
);
188 for (i
= 0; old
[i
].func
; i
++)
190 (old
[i
].func
!= probe
|| old
[i
].data
!= data
))
192 new[nr_probes
- nr_del
].func
= NULL
;
193 entry
->refcount
= nr_probes
- nr_del
;
196 debug_print_probes(entry
);
201 * Get tracepoint if the tracepoint is present in the tracepoint hash table.
202 * Must be called with ust lock held.
203 * Returns NULL if not present.
205 static struct tracepoint_entry
*get_tracepoint(const char *name
)
207 struct cds_hlist_head
*head
;
208 struct cds_hlist_node
*node
;
209 struct tracepoint_entry
*e
;
210 uint32_t hash
= jhash(name
, strlen(name
), 0);
212 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
213 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
214 if (!strcmp(name
, e
->name
))
221 * Add the tracepoint to the tracepoint hash table. Must be called with
224 static struct tracepoint_entry
*add_tracepoint(const char *name
)
226 struct cds_hlist_head
*head
;
227 struct cds_hlist_node
*node
;
228 struct tracepoint_entry
*e
;
229 size_t name_len
= strlen(name
) + 1;
230 uint32_t hash
= jhash(name
, name_len
-1, 0);
232 head
= &tracepoint_table
[hash
& (TRACEPOINT_TABLE_SIZE
- 1)];
233 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
234 if (!strcmp(name
, e
->name
)) {
235 DBG("tracepoint %s busy", name
);
236 return ERR_PTR(-EEXIST
); /* Already there */
240 * Using zmalloc here to allocate a variable length element. Could
241 * cause some memory fragmentation if overused.
243 e
= zmalloc(sizeof(struct tracepoint_entry
) + name_len
);
245 return ERR_PTR(-ENOMEM
);
246 memcpy(&e
->name
[0], name
, name_len
);
249 cds_hlist_add_head(&e
->hlist
, head
);
254 * Remove the tracepoint from the tracepoint hash table. Must be called with
257 static void remove_tracepoint(struct tracepoint_entry
*e
)
259 cds_hlist_del(&e
->hlist
);
264 * Sets the probe callback corresponding to one tracepoint.
266 static void set_tracepoint(struct tracepoint_entry
**entry
,
267 struct tracepoint
*elem
, int active
)
269 WARN_ON(strcmp((*entry
)->name
, elem
->name
) != 0);
272 * rcu_assign_pointer has a cmm_smp_wmb() which makes sure that the new
273 * probe callbacks array is consistent before setting a pointer to it.
274 * This array is referenced by __DO_TRACE from
275 * include/linux/tracepoints.h. A matching cmm_smp_read_barrier_depends()
278 rcu_assign_pointer(elem
->probes
, (*entry
)->probes
);
279 elem
->state
= active
;
283 * Disable a tracepoint and its probe callback.
284 * Note: only waiting an RCU period after setting elem->call to the empty
285 * function insures that the original callback is not used anymore. This insured
286 * by preempt_disable around the call site.
288 static void disable_tracepoint(struct tracepoint
*elem
)
291 rcu_assign_pointer(elem
->probes
, NULL
);
295 * tracepoint_update_probe_range - Update a probe range
296 * @begin: beginning of the range
297 * @end: end of the range
299 * Updates the probe callback corresponding to a range of tracepoints.
302 void tracepoint_update_probe_range(struct tracepoint
* const *begin
,
303 struct tracepoint
* const *end
)
305 struct tracepoint
* const *iter
;
306 struct tracepoint_entry
*mark_entry
;
308 for (iter
= begin
; iter
< end
; iter
++) {
310 continue; /* skip dummy */
311 if (!(*iter
)->name
) {
312 disable_tracepoint(*iter
);
315 mark_entry
= get_tracepoint((*iter
)->name
);
317 set_tracepoint(&mark_entry
, *iter
,
318 !!mark_entry
->refcount
);
320 disable_tracepoint(*iter
);
325 static void lib_update_tracepoints(void)
327 struct tracepoint_lib
*lib
;
329 cds_list_for_each_entry(lib
, &libs
, list
) {
330 tracepoint_update_probe_range(lib
->tracepoints_start
,
331 lib
->tracepoints_start
+ lib
->tracepoints_count
);
336 * Update probes, removing the faulty probes.
338 static void tracepoint_update_probes(void)
340 /* tracepoints registered from libraries and executable. */
341 lib_update_tracepoints();
344 static struct tracepoint_probe
*
345 tracepoint_add_probe(const char *name
, void *probe
, void *data
)
347 struct tracepoint_entry
*entry
;
348 struct tracepoint_probe
*old
;
350 entry
= get_tracepoint(name
);
352 entry
= add_tracepoint(name
);
354 return (struct tracepoint_probe
*)entry
;
356 old
= tracepoint_entry_add_probe(entry
, probe
, data
);
357 if (IS_ERR(old
) && !entry
->refcount
)
358 remove_tracepoint(entry
);
363 * __tracepoint_probe_register - Connect a probe to a tracepoint
364 * @name: tracepoint name
365 * @probe: probe handler
367 * Returns 0 if ok, error value on error.
368 * The probe address must at least be aligned on the architecture pointer size.
369 * Called with the UST lock held.
371 int __tracepoint_probe_register(const char *name
, void *probe
, void *data
)
375 old
= tracepoint_add_probe(name
, probe
, data
);
379 tracepoint_update_probes(); /* may update entry */
384 static void *tracepoint_remove_probe(const char *name
, void *probe
, void *data
)
386 struct tracepoint_entry
*entry
;
389 entry
= get_tracepoint(name
);
391 return ERR_PTR(-ENOENT
);
392 old
= tracepoint_entry_remove_probe(entry
, probe
, data
);
395 if (!entry
->refcount
)
396 remove_tracepoint(entry
);
401 * tracepoint_probe_unregister - Disconnect a probe from a tracepoint
402 * @name: tracepoint name
403 * @probe: probe function pointer
404 * @probe: probe data pointer
406 * Called with the UST lock held.
408 int __tracepoint_probe_unregister(const char *name
, void *probe
, void *data
)
412 old
= tracepoint_remove_probe(name
, probe
, data
);
416 tracepoint_update_probes(); /* may update entry */
421 static void tracepoint_add_old_probes(void *old
)
425 struct tp_probes
*tp_probes
= caa_container_of(old
,
426 struct tp_probes
, probes
[0]);
427 cds_list_add(&tp_probes
->u
.list
, &old_probes
);
432 * tracepoint_probe_register_noupdate - register a probe but not connect
433 * @name: tracepoint name
434 * @probe: probe handler
436 * caller must call tracepoint_probe_update_all()
437 * Called with the UST lock held.
439 int tracepoint_probe_register_noupdate(const char *name
, void *probe
,
444 old
= tracepoint_add_probe(name
, probe
, data
);
448 tracepoint_add_old_probes(old
);
453 * tracepoint_probe_unregister_noupdate - remove a probe but not disconnect
454 * @name: tracepoint name
455 * @probe: probe function pointer
457 * caller must call tracepoint_probe_update_all()
458 * Called with the UST lock held.
460 int tracepoint_probe_unregister_noupdate(const char *name
, void *probe
,
465 old
= tracepoint_remove_probe(name
, probe
, data
);
469 tracepoint_add_old_probes(old
);
474 * tracepoint_probe_update_all - update tracepoints
475 * Called with the UST lock held.
477 void tracepoint_probe_update_all(void)
479 CDS_LIST_HEAD(release_probes
);
480 struct tp_probes
*pos
, *next
;
485 if (!cds_list_empty(&old_probes
))
486 cds_list_replace_init(&old_probes
, &release_probes
);
489 tracepoint_update_probes();
490 cds_list_for_each_entry_safe(pos
, next
, &release_probes
, u
.list
) {
491 cds_list_del(&pos
->u
.list
);
497 void tracepoint_set_new_tracepoint_cb(void (*cb
)(struct tracepoint
*))
499 new_tracepoint_cb
= cb
;
502 static void new_tracepoints(struct tracepoint
* const *start
, struct tracepoint
* const *end
)
504 if (new_tracepoint_cb
) {
505 struct tracepoint
* const *t
;
507 for (t
= start
; t
< end
; t
++) {
509 new_tracepoint_cb(*t
);
514 int tracepoint_register_lib(struct tracepoint
* const *tracepoints_start
,
515 int tracepoints_count
)
517 struct tracepoint_lib
*pl
, *iter
;
521 pl
= (struct tracepoint_lib
*) zmalloc(sizeof(struct tracepoint_lib
));
523 pl
->tracepoints_start
= tracepoints_start
;
524 pl
->tracepoints_count
= tracepoints_count
;
528 * We sort the libs by struct lib pointer address.
530 cds_list_for_each_entry_reverse(iter
, &libs
, list
) {
531 BUG_ON(iter
== pl
); /* Should never be in the list twice */
533 /* We belong to the location right after iter. */
534 cds_list_add(&pl
->list
, &iter
->list
);
538 /* We should be added at the head of the list */
539 cds_list_add(&pl
->list
, &libs
);
541 new_tracepoints(tracepoints_start
, tracepoints_start
+ tracepoints_count
);
543 /* TODO: update just the loaded lib */
544 lib_update_tracepoints();
547 DBG("just registered a tracepoints section from %p and having %d tracepoints",
548 tracepoints_start
, tracepoints_count
);
553 int tracepoint_unregister_lib(struct tracepoint
* const *tracepoints_start
)
555 struct tracepoint_lib
*lib
;
558 cds_list_for_each_entry(lib
, &libs
, list
) {
559 if (lib
->tracepoints_start
== tracepoints_start
) {
560 struct tracepoint_lib
*lib2free
= lib
;
561 cds_list_del(&lib
->list
);
571 void init_tracepoint(void)
573 if (uatomic_xchg(&initialized
, 1) == 1)
578 void exit_tracepoint(void)
584 * Create the wrapper symbols.
586 #undef tp_rcu_read_lock_bp
587 #undef tp_rcu_read_unlock_bp
588 #undef tp_rcu_dereference_bp
590 void tp_rcu_read_lock_bp(void)
595 void tp_rcu_read_unlock_bp(void)
597 rcu_read_unlock_bp();
600 void *tp_rcu_dereference_sym_bp(void *p
)
602 return rcu_dereference_bp(p
);