2 * Copyright (C) 2007-2011 Mathieu Desnoyers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation;
7 * version 2.1 of the License.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #include <urcu-call-rcu.h>
25 #include <urcu/rculist.h>
26 #include <urcu/hlist.h>
29 #include <ust/marker.h>
30 #include <ust/marker-internal.h>
31 #include <ust/tracepoint.h>
32 #include <ust/tracepoint-internal.h>
34 #include "usterr_signal_safe.h"
36 #include "tracercore.h"
39 extern struct ust_marker
* const __start___ust_marker_ptrs
[] __attribute__((visibility("hidden")));
40 extern struct ust_marker
* const __stop___ust_marker_ptrs
[] __attribute__((visibility("hidden")));
42 /* Set to 1 to enable ust_marker debug output */
43 static const int ust_marker_debug
;
44 static int initialized
;
45 static void (*new_ust_marker_cb
)(struct ust_marker
*);
48 * ust_marker mutex protects the builtin and module ust_marker and the
49 * hash table, as well as the ust_marker_libs list.
51 static DEFINE_MUTEX(ust_marker_mutex
);
52 static CDS_LIST_HEAD(ust_marker_libs
);
55 * Allow nested mutex for mutex listing and nested enable.
57 static __thread
int nested_mutex
;
59 void lock_ust_marker(void)
61 if (!(nested_mutex
++))
62 pthread_mutex_lock(&ust_marker_mutex
);
65 void unlock_ust_marker(void)
67 if (!(--nested_mutex
))
68 pthread_mutex_unlock(&ust_marker_mutex
);
72 * ust_marker hash table, containing the active ust_marker.
73 * Protected by ust_marker mutex.
75 #define UST_MARKER_HASH_BITS 6
76 #define UST_MARKER_TABLE_SIZE (1 << UST_MARKER_HASH_BITS)
77 static struct cds_hlist_head ust_marker_table
[UST_MARKER_TABLE_SIZE
];
79 struct ust_marker_probe_array
{
81 struct ust_marker_probe_closure c
[0];
86 * It is used to make sure every handler has finished using its private
87 * data between two consecutive operation (add or remove) on a given
88 * ust_marker. It is also used to delay the free of multiple probes
89 * array until a quiescent state is reached. ust_marker entries
90 * modifications are protected by the ust_marker_mutex.
92 struct ust_marker_entry
{
93 struct cds_hlist_node hlist
;
97 void (*call
)(const struct ust_marker
*mdata
, void *call_private
, ...);
98 struct ust_marker_probe_closure single
;
99 struct ust_marker_probe_array
*multi
;
100 int refcount
; /* Number of times armed. 0 if disarmed. */
103 unsigned char ptype
:1;
104 unsigned char format_allocated
:1;
105 char channel
[0]; /* Contains channel'\0'name'\0'format'\0' */
109 * __ust_marker_empty_function - Empty probe callback
110 * @mdata: ust_marker data
111 * @probe_private: probe private data
112 * @call_private: call site private data
113 * @fmt: format string
114 * @...: variable argument list
116 * Empty callback provided as a probe to the ust_marker. By providing
117 * this to a disabled ust_marker, we make sure the execution flow is
118 * always valid even though the function pointer change and the
119 * ust_marker enabling are two distinct operations that modifies the
120 * execution flow of preemptible code.
122 notrace
void __ust_marker_empty_function(const struct ust_marker
*mdata
,
123 void *probe_private
, void *call_private
, const char *fmt
, va_list *args
)
128 * ust_marker_probe_cb Callback that prepares the variable argument list for probes.
129 * @mdata: pointer of type struct ust_marker
130 * @call_private: caller site private data
131 * @...: Variable argument list.
133 * Since we do not use "typical" pointer based RCU in the 1 argument case, we
134 * need to put a full cmm_smp_rmb() in this branch. This is why we do not use
135 * rcu_dereference() for the pointer read.
137 notrace
void ust_marker_probe_cb(const struct ust_marker
*mdata
,
138 void *call_private
, ...)
144 * rcu_read_lock_sched does two things : disabling preemption to make
145 * sure the teardown of the callbacks can be done correctly when they
146 * are in modules and they insure RCU read coherency.
149 ptype
= mdata
->ptype
;
150 if (likely(!ptype
)) {
151 ust_marker_probe_func
*func
;
152 /* Must read the ptype before ptr. They are not data dependant,
153 * so we put an explicit cmm_smp_rmb() here. */
155 func
= mdata
->single
.func
;
156 /* Must read the ptr before private data. They are not data
157 * dependant, so we put an explicit cmm_smp_rmb() here. */
159 va_start(args
, call_private
);
160 func(mdata
, mdata
->single
.probe_private
, call_private
,
161 mdata
->format
, &args
);
164 struct ust_marker_probe_array
*multi
;
167 * Read mdata->ptype before mdata->multi.
170 multi
= mdata
->multi
;
172 * multi points to an array, therefore accessing the array
173 * depends on reading multi. However, even in this case,
174 * we must insure that the pointer is read _before_ the array
175 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
176 * in the fast path, so put the explicit cmm_barrier here.
178 cmm_smp_read_barrier_depends();
179 for (i
= 0; multi
->c
[i
].func
; i
++) {
180 va_start(args
, call_private
);
181 multi
->c
[i
].func(mdata
, multi
->c
[i
].probe_private
,
182 call_private
, mdata
->format
, &args
);
190 * ust_marker_probe_cb Callback that does not prepare the variable argument list.
191 * @mdata: pointer of type struct ust_marker
192 * @call_private: caller site private data
193 * @...: Variable argument list.
195 * Should be connected to ust_marker "UST_MARKER_NOARGS".
197 static notrace
void ust_marker_probe_cb_noarg(const struct ust_marker
*mdata
,
198 void *call_private
, ...)
200 va_list args
; /* not initialized */
204 ptype
= mdata
->ptype
;
205 if (likely(!ptype
)) {
206 ust_marker_probe_func
*func
;
207 /* Must read the ptype before ptr. They are not data dependant,
208 * so we put an explicit cmm_smp_rmb() here. */
210 func
= mdata
->single
.func
;
211 /* Must read the ptr before private data. They are not data
212 * dependant, so we put an explicit cmm_smp_rmb() here. */
214 func(mdata
, mdata
->single
.probe_private
, call_private
,
215 mdata
->format
, &args
);
217 struct ust_marker_probe_array
*multi
;
220 * Read mdata->ptype before mdata->multi.
223 multi
= mdata
->multi
;
225 * multi points to an array, therefore accessing the array
226 * depends on reading multi. However, even in this case,
227 * we must insure that the pointer is read _before_ the array
228 * data. Same as rcu_dereference, but we need a full cmm_smp_rmb()
229 * in the fast path, so put the explicit cmm_barrier here.
231 cmm_smp_read_barrier_depends();
232 for (i
= 0; multi
->c
[i
].func
; i
++)
233 multi
->c
[i
].func(mdata
, multi
->c
[i
].probe_private
,
234 call_private
, mdata
->format
, &args
);
239 static void free_old_closure(struct rcu_head
*head
)
241 struct ust_marker_probe_array
*multi
=
242 _ust_container_of(head
, struct ust_marker_probe_array
, rcu
);
246 static void debug_print_probes(struct ust_marker_entry
*entry
)
250 if (!ust_marker_debug
)
254 DBG("Single probe : %p %p",
256 entry
->single
.probe_private
);
258 for (i
= 0; entry
->multi
->c
[i
].func
; i
++)
259 DBG("Multi probe %d : %p %p", i
,
260 entry
->multi
->c
[i
].func
,
261 entry
->multi
->c
[i
].probe_private
);
265 static struct ust_marker_probe_array
*
266 ust_marker_entry_add_probe(struct ust_marker_entry
*entry
,
267 ust_marker_probe_func
*probe
, void *probe_private
)
270 struct ust_marker_probe_array
*old
, *new;
274 debug_print_probes(entry
);
277 if (entry
->single
.func
== probe
&&
278 entry
->single
.probe_private
== probe_private
)
279 return ERR_PTR(-EBUSY
);
280 if (entry
->single
.func
== __ust_marker_empty_function
) {
282 entry
->single
.func
= probe
;
283 entry
->single
.probe_private
= probe_private
;
286 debug_print_probes(entry
);
294 /* (N -> N+1), (N != 0, 1) probes */
295 for (nr_probes
= 0; old
->c
[nr_probes
].func
; nr_probes
++)
296 if (old
->c
[nr_probes
].func
== probe
297 && old
->c
[nr_probes
].probe_private
299 return ERR_PTR(-EBUSY
);
301 /* + 2 : one for new probe, one for NULL func */
302 new = zmalloc(sizeof(struct ust_marker_probe_array
)
303 + ((nr_probes
+ 2) * sizeof(struct ust_marker_probe_closure
)));
305 return ERR_PTR(-ENOMEM
);
307 new->c
[0] = entry
->single
;
309 memcpy(&new->c
[0], &old
->c
[0],
310 nr_probes
* sizeof(struct ust_marker_probe_closure
));
311 new->c
[nr_probes
].func
= probe
;
312 new->c
[nr_probes
].probe_private
= probe_private
;
313 entry
->refcount
= nr_probes
+ 1;
316 debug_print_probes(entry
);
320 static struct ust_marker_probe_array
*
321 ust_marker_entry_remove_probe(struct ust_marker_entry
*entry
,
322 ust_marker_probe_func
*probe
, void *probe_private
)
324 int nr_probes
= 0, nr_del
= 0, i
;
325 struct ust_marker_probe_array
*old
, *new;
329 debug_print_probes(entry
);
331 /* 0 -> N is an error */
332 WARN_ON(entry
->single
.func
== __ust_marker_empty_function
);
334 WARN_ON(probe
&& entry
->single
.func
!= probe
);
335 WARN_ON(entry
->single
.probe_private
!= probe_private
);
336 entry
->single
.func
= __ust_marker_empty_function
;
339 debug_print_probes(entry
);
342 /* (N -> M), (N > 1, M >= 0) probes */
343 for (nr_probes
= 0; old
->c
[nr_probes
].func
; nr_probes
++) {
344 if ((!probe
|| old
->c
[nr_probes
].func
== probe
)
345 && old
->c
[nr_probes
].probe_private
351 if (nr_probes
- nr_del
== 0) {
352 /* N -> 0, (N > 1) */
353 entry
->single
.func
= __ust_marker_empty_function
;
356 } else if (nr_probes
- nr_del
== 1) {
357 /* N -> 1, (N > 1) */
358 for (i
= 0; old
->c
[i
].func
; i
++)
359 if ((probe
&& old
->c
[i
].func
!= probe
) ||
360 old
->c
[i
].probe_private
!= probe_private
)
361 entry
->single
= old
->c
[i
];
366 /* N -> M, (N > 1, M > 1) */
368 new = zmalloc(sizeof(struct ust_marker_probe_array
)
369 + ((nr_probes
- nr_del
+ 1) * sizeof(struct ust_marker_probe_closure
)));
371 return ERR_PTR(-ENOMEM
);
372 for (i
= 0; old
->c
[i
].func
; i
++)
373 if ((probe
&& old
->c
[i
].func
!= probe
) ||
374 old
->c
[i
].probe_private
!= probe_private
)
375 new->c
[j
++] = old
->c
[i
];
376 entry
->refcount
= nr_probes
- nr_del
;
380 debug_print_probes(entry
);
385 * Get ust_marker if the ust_marker is present in the ust_marker hash table.
386 * Must be called with ust_marker_mutex held.
387 * Returns NULL if not present.
389 static struct ust_marker_entry
*get_ust_marker(const char *channel
, const char *name
)
391 struct cds_hlist_head
*head
;
392 struct cds_hlist_node
*node
;
393 struct ust_marker_entry
*e
;
394 size_t channel_len
= strlen(channel
) + 1;
395 size_t name_len
= strlen(name
) + 1;
398 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
399 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
400 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
401 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
))
408 * Add the ust_marker to the ust_marker hash table. Must be called with
409 * ust_marker_mutex held.
411 static struct ust_marker_entry
*add_ust_marker(const char *channel
, const char *name
,
414 struct cds_hlist_head
*head
;
415 struct cds_hlist_node
*node
;
416 struct ust_marker_entry
*e
;
417 size_t channel_len
= strlen(channel
) + 1;
418 size_t name_len
= strlen(name
) + 1;
419 size_t format_len
= 0;
422 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
424 format_len
= strlen(format
) + 1;
425 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
426 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
427 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
428 DBG("ust_marker %s.%s busy", channel
, name
);
429 return ERR_PTR(-EBUSY
); /* Already there */
433 * Using zmalloc here to allocate a variable length element. Could
434 * cause some memory fragmentation if overused.
436 e
= zmalloc(sizeof(struct ust_marker_entry
)
437 + channel_len
+ name_len
+ format_len
);
439 return ERR_PTR(-ENOMEM
);
440 memcpy(e
->channel
, channel
, channel_len
);
441 e
->name
= &e
->channel
[channel_len
];
442 memcpy(e
->name
, name
, name_len
);
444 e
->format
= &e
->name
[name_len
];
445 memcpy(e
->format
, format
, format_len
);
446 if (strcmp(e
->format
, UST_MARKER_NOARGS
) == 0)
447 e
->call
= ust_marker_probe_cb_noarg
;
449 e
->call
= ust_marker_probe_cb
;
450 __ust_marker(metadata
, core_marker_format
, NULL
,
451 "channel %s name %s format %s",
452 e
->channel
, e
->name
, e
->format
);
455 e
->call
= ust_marker_probe_cb
;
457 e
->single
.func
= __ust_marker_empty_function
;
458 e
->single
.probe_private
= NULL
;
461 e
->format_allocated
= 0;
463 cds_hlist_add_head(&e
->hlist
, head
);
468 * Remove the ust_marker from the ust_marker hash table. Must be called with mutex_lock
471 static int remove_ust_marker(const char *channel
, const char *name
)
473 struct cds_hlist_head
*head
;
474 struct cds_hlist_node
*node
;
475 struct ust_marker_entry
*e
;
477 size_t channel_len
= strlen(channel
) + 1;
478 size_t name_len
= strlen(name
) + 1;
482 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
483 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
484 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
485 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
492 if (e
->single
.func
!= __ust_marker_empty_function
)
494 cds_hlist_del(&e
->hlist
);
495 if (e
->format_allocated
)
497 ret
= ltt_channels_unregister(e
->channel
);
504 * Set the mark_entry format to the format found in the element.
506 static int ust_marker_set_format(struct ust_marker_entry
*entry
, const char *format
)
508 entry
->format
= strdup(format
);
511 entry
->format_allocated
= 1;
513 __ust_marker(metadata
, core_marker_format
, NULL
,
514 "channel %s name %s format %s",
515 entry
->channel
, entry
->name
, entry
->format
);
520 * Sets the probe callback corresponding to one ust_marker.
522 static int set_ust_marker(struct ust_marker_entry
*entry
, struct ust_marker
*elem
,
526 WARN_ON(strcmp(entry
->name
, elem
->name
) != 0);
529 if (strcmp(entry
->format
, elem
->format
) != 0) {
530 ERR("Format mismatch for probe %s (%s), ust_marker (%s)",
537 ret
= ust_marker_set_format(entry
, elem
->format
);
543 * probe_cb setup (statically known) is done here. It is
544 * asynchronous with the rest of execution, therefore we only
545 * pass from a "safe" callback (with argument) to an "unsafe"
546 * callback (does not set arguments).
548 elem
->call
= entry
->call
;
549 elem
->channel_id
= entry
->channel_id
;
550 elem
->event_id
= entry
->event_id
;
553 * We only update the single probe private data when the ptr is
554 * set to a _non_ single probe! (0 -> 1 and N -> 1, N != 1)
556 WARN_ON(elem
->single
.func
!= __ust_marker_empty_function
557 && elem
->single
.probe_private
!= entry
->single
.probe_private
559 elem
->single
.probe_private
= entry
->single
.probe_private
;
561 * Make sure the private data is valid when we update the
565 elem
->single
.func
= entry
->single
.func
;
567 * We also make sure that the new probe callbacks array is consistent
568 * before setting a pointer to it.
570 rcu_assign_pointer(elem
->multi
, entry
->multi
);
572 * Update the function or multi probe array pointer before setting the
576 elem
->ptype
= entry
->ptype
;
578 if (elem
->tp_name
&& (active
^ elem
->state
)) {
579 WARN_ON(!elem
->tp_cb
);
581 * It is ok to directly call the probe registration because type
582 * checking has been done in the __ust_marker_tp() macro.
586 ret
= tracepoint_probe_register_noupdate(
591 * tracepoint_probe_update_all() must be called
592 * before the library containing tp_cb is unloaded.
594 ret
= tracepoint_probe_unregister_noupdate(
599 elem
->state
= active
;
605 * Disable a ust_marker and its probe callback.
606 * Note: only waiting an RCU period after setting elem->call to the empty
607 * function insures that the original callback is not used anymore. This insured
608 * by rcu_read_lock around the call site.
610 static void disable_ust_marker(struct ust_marker
*elem
)
614 /* leave "call" as is. It is known statically. */
615 if (elem
->tp_name
&& elem
->state
) {
616 WARN_ON(!elem
->tp_cb
);
618 * It is ok to directly call the probe registration because type
619 * checking has been done in the __ust_marker_tp() macro.
622 * tracepoint_probe_update_all() must be called
623 * before the module containing tp_cb is unloaded.
625 ret
= tracepoint_probe_unregister_noupdate(elem
->tp_name
,
630 elem
->single
.func
= __ust_marker_empty_function
;
631 /* Update the function before setting the ptype */
633 elem
->ptype
= 0; /* single probe */
635 * Leave the private data and channel_id/event_id there, because removal
636 * is racy and should be done only after an RCU period. These are never
637 * used until the next initialization anyway.
642 * is_ust_marker_enabled - Check if a ust_marker is enabled
643 * @channel: channel name
644 * @name: ust_marker name
646 * Returns 1 if the ust_marker is enabled, 0 if disabled.
648 int is_ust_marker_enabled(const char *channel
, const char *name
)
650 struct ust_marker_entry
*entry
;
653 entry
= get_ust_marker(channel
, name
);
656 return entry
&& !!entry
->refcount
;
660 * ust_marker_update_probe_range - Update a probe range
661 * @begin: beginning of the range
662 * @end: end of the range
664 * Updates the probe callback corresponding to a range of ust_marker.
667 void ust_marker_update_probe_range(struct ust_marker
* const *begin
,
668 struct ust_marker
* const *end
)
670 struct ust_marker
* const *iter
;
671 struct ust_marker_entry
*mark_entry
;
673 for (iter
= begin
; iter
< end
; iter
++) {
675 continue; /* skip dummy */
676 mark_entry
= get_ust_marker((*iter
)->channel
, (*iter
)->name
);
678 set_ust_marker(mark_entry
, *iter
, !!mark_entry
->refcount
);
680 * ignore error, continue
683 disable_ust_marker(*iter
);
688 static void lib_update_ust_marker(void)
690 struct ust_marker_lib
*lib
;
693 cds_list_for_each_entry(lib
, &ust_marker_libs
, list
)
694 ust_marker_update_probe_range(lib
->ust_marker_start
,
695 lib
->ust_marker_start
+ lib
->ust_marker_count
);
700 * Update probes, removing the faulty probes.
702 * Internal callback only changed before the first probe is connected to it.
703 * Single probe private data can only be changed on 0 -> 1 and 2 -> 1
704 * transitions. All other transitions will leave the old private data valid.
705 * This makes the non-atomicity of the callback/private data updates valid.
707 * "special case" updates :
712 * Other updates all behave the same, just like the 2 -> 3 or 3 -> 2 updates.
713 * Site effect : ust_marker_set_format may delete the ust_marker entry (creating a
716 static void ust_marker_update_probes(void)
718 lib_update_ust_marker();
719 tracepoint_probe_update_all();
723 * ust_marker_probe_register - Connect a probe to a ust_marker
724 * @channel: ust_marker channel
725 * @name: ust_marker name
726 * @format: format string
727 * @probe: probe handler
728 * @probe_private: probe private data
730 * private data must be a valid allocated memory address, or NULL.
731 * Returns 0 if ok, error value on error.
732 * The probe address must at least be aligned on the architecture pointer size.
734 int ust_marker_probe_register(const char *channel
, const char *name
,
735 const char *format
, ust_marker_probe_func
*probe
,
738 struct ust_marker_entry
*entry
;
739 int ret
= 0, ret_err
;
740 struct ust_marker_probe_array
*old
;
744 entry
= get_ust_marker(channel
, name
);
747 entry
= add_ust_marker(channel
, name
, format
);
749 ret
= PTR_ERR(entry
);
752 ret
= ltt_channels_register(channel
);
754 goto error_remove_ust_marker
;
755 ret
= ltt_channels_get_index_from_name(channel
);
757 goto error_unregister_channel
;
758 entry
->channel_id
= ret
;
759 ret
= ltt_channels_get_event_id(channel
, name
);
761 goto error_unregister_channel
;
762 entry
->event_id
= ret
;
764 __ust_marker(metadata
, core_marker_id
, NULL
,
765 "channel %s name %s event_id %hu "
766 "int #1u%zu long #1u%zu pointer #1u%zu "
767 "size_t #1u%zu alignment #1u%u",
768 channel
, name
, entry
->event_id
,
769 sizeof(int), sizeof(long), sizeof(void *),
770 sizeof(size_t), ltt_get_alignment());
773 ret
= ust_marker_set_format(entry
, format
);
774 else if (strcmp(entry
->format
, format
))
780 old
= ust_marker_entry_add_probe(entry
, probe
, probe_private
);
784 goto error_unregister_channel
;
790 /* Activate ust_marker if necessary */
791 ust_marker_update_probes();
795 free_old_closure(&old
->rcu
);
799 error_unregister_channel
:
800 ret_err
= ltt_channels_unregister(channel
);
802 error_remove_ust_marker
:
803 ret_err
= remove_ust_marker(channel
, name
);
811 * ust_marker_probe_unregister - Disconnect a probe from a ust_marker
812 * @channel: ust_marker channel
813 * @name: ust_marker name
814 * @probe: probe function pointer
815 * @probe_private: probe private data
817 * Returns the private data given to ust_marker_probe_register, or an ERR_PTR().
818 * We do not need to call a synchronize_sched to make sure the probes have
819 * finished running before doing a module unload, because the module unload
820 * itself uses stop_machine(), which insures that every preempt disabled section
823 int ust_marker_probe_unregister(const char *channel
, const char *name
,
824 ust_marker_probe_func
*probe
, void *probe_private
)
826 struct ust_marker_entry
*entry
;
827 struct ust_marker_probe_array
*old
;
831 entry
= get_ust_marker(channel
, name
);
836 old
= ust_marker_entry_remove_probe(entry
, probe
, probe_private
);
839 ust_marker_update_probes();
843 free_old_closure(&old
->rcu
);
852 static struct ust_marker_entry
*
853 get_ust_marker_from_private_data(ust_marker_probe_func
*probe
,
856 struct ust_marker_entry
*entry
;
858 struct cds_hlist_head
*head
;
859 struct cds_hlist_node
*node
;
861 for (i
= 0; i
< UST_MARKER_TABLE_SIZE
; i
++) {
862 head
= &ust_marker_table
[i
];
863 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
865 if (entry
->single
.func
== probe
866 && entry
->single
.probe_private
870 struct ust_marker_probe_array
*closure
;
871 closure
= entry
->multi
;
872 for (i
= 0; closure
->c
[i
].func
; i
++) {
873 if (closure
->c
[i
].func
== probe
&&
874 closure
->c
[i
].probe_private
885 * ust_marker_probe_unregister_private_data - Disconnect a probe from a ust_marker
886 * @probe: probe function
887 * @probe_private: probe private data
889 * Unregister a probe by providing the registered private data.
890 * Only removes the first ust_marker found in hash table.
891 * Return 0 on success or error value.
892 * We do not need to call a synchronize_sched to make sure the probes have
893 * finished running before doing a module unload, because the module unload
894 * itself uses stop_machine(), which insures that every preempt disabled section
897 int ust_marker_probe_unregister_private_data(ust_marker_probe_func
*probe
,
900 struct ust_marker_entry
*entry
;
902 struct ust_marker_probe_array
*old
;
903 char *channel
= NULL
, *name
= NULL
;
906 entry
= get_ust_marker_from_private_data(probe
, probe_private
);
911 old
= ust_marker_entry_remove_probe(entry
, NULL
, probe_private
);
912 channel
= strdup(entry
->channel
);
913 name
= strdup(entry
->name
);
914 /* Ignore busy error message */
915 remove_ust_marker(channel
, name
);
918 ust_marker_update_probes();
922 free_old_closure(&old
->rcu
);
935 * ust_marker_get_private_data - Get a ust_marker's probe private data
936 * @channel: ust_marker channel
937 * @name: ust_marker name
938 * @probe: probe to match
939 * @num: get the nth matching probe's private data
941 * Returns the nth private data pointer (starting from 0) matching, or an
943 * Returns the private data pointer, or an ERR_PTR.
944 * The private data pointer should _only_ be dereferenced if the caller is the
945 * owner of the data, or its content could vanish. This is mostly used to
946 * confirm that a caller is the owner of a registered probe.
948 void *ust_marker_get_private_data(const char *channel
, const char *name
,
949 ust_marker_probe_func
*probe
, int num
)
951 struct cds_hlist_head
*head
;
952 struct cds_hlist_node
*node
;
953 struct ust_marker_entry
*e
;
954 size_t channel_len
= strlen(channel
) + 1;
955 size_t name_len
= strlen(name
) + 1;
959 hash
= jhash(channel
, channel_len
-1, 0) ^ jhash(name
, name_len
-1, 0);
960 head
= &ust_marker_table
[hash
& ((1 << UST_MARKER_HASH_BITS
)-1)];
961 cds_hlist_for_each_entry(e
, node
, head
, hlist
) {
962 if (!strcmp(channel
, e
->channel
) && !strcmp(name
, e
->name
)) {
964 if (num
== 0 && e
->single
.func
== probe
)
965 return e
->single
.probe_private
;
967 struct ust_marker_probe_array
*closure
;
970 for (i
= 0; closure
->c
[i
].func
; i
++) {
971 if (closure
->c
[i
].func
!= probe
)
974 return closure
->c
[i
].probe_private
;
980 return ERR_PTR(-ENOENT
);
984 * ust_marker_get_iter_range - Get a next ust_marker iterator given a range.
985 * @ust_marker: current ust_marker (in), next ust_marker (out)
986 * @begin: beginning of the range
987 * @end: end of the range
989 * Returns whether a next ust_marker has been found (1) or not (0).
990 * Will return the first ust_marker in the range if the input ust_marker is NULL.
991 * Called with markers mutex held.
994 int ust_marker_get_iter_range(struct ust_marker
* const **ust_marker
,
995 struct ust_marker
* const *begin
,
996 struct ust_marker
* const *end
)
998 if (!*ust_marker
&& begin
!= end
)
1000 while (*ust_marker
>= begin
&& *ust_marker
< end
) {
1002 (*ust_marker
)++; /* skip dummy */
1010 * Returns 0 if current not found.
1011 * Returns 1 if current found.
1012 * Called with markers mutex held.
1015 int lib_get_iter_ust_marker(struct ust_marker_iter
*iter
)
1017 struct ust_marker_lib
*iter_lib
;
1020 cds_list_for_each_entry(iter_lib
, &ust_marker_libs
, list
) {
1021 if (iter_lib
< iter
->lib
)
1023 else if (iter_lib
> iter
->lib
)
1024 iter
->ust_marker
= NULL
;
1025 found
= ust_marker_get_iter_range(&iter
->ust_marker
,
1026 iter_lib
->ust_marker_start
,
1027 iter_lib
->ust_marker_start
+ iter_lib
->ust_marker_count
);
1029 iter
->lib
= iter_lib
;
1036 /* Called with markers mutex held. */
1037 static void ust_marker_get_iter(struct ust_marker_iter
*iter
)
1041 found
= lib_get_iter_ust_marker(iter
);
1043 ust_marker_iter_reset(iter
);
1046 void ust_marker_iter_start(struct ust_marker_iter
*iter
)
1049 ust_marker_get_iter(iter
);
1052 /* Called with markers mutex held. */
1053 void ust_marker_iter_next(struct ust_marker_iter
*iter
)
1057 * iter->ust_marker may be invalid because we blindly incremented it.
1058 * Make sure it is valid by marshalling on the ust_marker, getting the
1059 * ust_marker from following modules if necessary.
1061 ust_marker_get_iter(iter
);
1064 void ust_marker_iter_stop(struct ust_marker_iter
*iter
)
1066 unlock_ust_marker();
1069 void ust_marker_iter_reset(struct ust_marker_iter
*iter
)
1072 iter
->ust_marker
= NULL
;
1075 void ltt_dump_ust_marker_state(struct ust_trace
*trace
)
1077 struct ust_marker_entry
*entry
;
1078 struct ltt_probe_private_data call_data
;
1079 struct cds_hlist_head
*head
;
1080 struct cds_hlist_node
*node
;
1084 call_data
.trace
= trace
;
1085 call_data
.serializer
= NULL
;
1087 for (i
= 0; i
< UST_MARKER_TABLE_SIZE
; i
++) {
1088 head
= &ust_marker_table
[i
];
1089 cds_hlist_for_each_entry(entry
, node
, head
, hlist
) {
1090 __ust_marker(metadata
, core_marker_id
,
1092 "channel %s name %s event_id %hu "
1093 "int #1u%zu long #1u%zu pointer #1u%zu "
1094 "size_t #1u%zu alignment #1u%u",
1098 sizeof(int), sizeof(long),
1099 sizeof(void *), sizeof(size_t),
1100 ltt_get_alignment());
1102 __ust_marker(metadata
,
1105 "channel %s name %s format %s",
1111 unlock_ust_marker();
1114 void ust_marker_set_new_ust_marker_cb(void (*cb
)(struct ust_marker
*))
1116 new_ust_marker_cb
= cb
;
1119 static void new_ust_marker(struct ust_marker
* const *start
,
1120 struct ust_marker
* const *end
)
1122 if (new_ust_marker_cb
) {
1123 struct ust_marker
* const *m
;
1125 for (m
= start
; m
< end
; m
++) {
1127 new_ust_marker_cb(*m
);
1132 int ust_marker_register_lib(struct ust_marker
* const *ust_marker_start
,
1133 int ust_marker_count
)
1135 struct ust_marker_lib
*pl
, *iter
;
1137 pl
= (struct ust_marker_lib
*) zmalloc(sizeof(struct ust_marker_lib
));
1139 pl
->ust_marker_start
= ust_marker_start
;
1140 pl
->ust_marker_count
= ust_marker_count
;
1145 * We sort the libs by struct lib pointer address.
1147 cds_list_for_each_entry_reverse(iter
, &ust_marker_libs
, list
) {
1148 BUG_ON(iter
== pl
); /* Should never be in the list twice */
1150 /* We belong to the location right after iter. */
1151 cds_list_add(&pl
->list
, &iter
->list
);
1155 /* We should be added at the head of the list */
1156 cds_list_add(&pl
->list
, &ust_marker_libs
);
1158 unlock_ust_marker();
1160 new_ust_marker(ust_marker_start
, ust_marker_start
+ ust_marker_count
);
1162 /* TODO: update just the loaded lib */
1163 lib_update_ust_marker();
1165 DBG("just registered a ust_marker section from %p and having %d ust_marker (minus dummy ust_marker)", ust_marker_start
, ust_marker_count
);
1170 int ust_marker_unregister_lib(struct ust_marker
* const *ust_marker_start
)
1172 struct ust_marker_lib
*lib
;
1175 cds_list_for_each_entry(lib
, &ust_marker_libs
, list
) {
1176 if(lib
->ust_marker_start
== ust_marker_start
) {
1177 struct ust_marker_lib
*lib2free
= lib
;
1178 cds_list_del(&lib
->list
);
1183 unlock_ust_marker();
1188 void __attribute__((constructor
)) init_ust_marker(void)
1192 ust_marker_register_lib(__start___ust_marker_ptrs
,
1193 __stop___ust_marker_ptrs
1194 - __start___ust_marker_ptrs
);
1199 void __attribute__((destructor
)) destroy_ust_marker(void)
1201 ust_marker_unregister_lib(__start___ust_marker_ptrs
);