2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright 2010-2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6 * Holds LTTng probes registry.
12 #include <urcu/list.h>
13 #include <urcu/hlist.h>
14 #include <lttng/ust-events.h>
15 #include <lttng/tracepoint.h>
16 #include "common/tracepoint.h"
18 #include "common/macros.h"
21 #include "lttng-tracer-core.h"
22 #include "common/jhash.h"
23 #include "lib/lttng-ust/events.h"
26 * probe list is protected by ust_lock()/ust_unlock().
28 static CDS_LIST_HEAD(_probe_list
);
31 * List of probes registered by not yet processed.
33 static CDS_LIST_HEAD(lazy_probe_init
);
36 * lazy_nesting counter ensures we don't trigger lazy probe registration
37 * fixup while we are performing the fixup. It is protected by the ust
40 static int lazy_nesting
;
43 int check_provider_version(const struct lttng_ust_probe_desc
*desc
)
46 * Check tracepoint provider version compatibility.
48 if (desc
->major
<= LTTNG_UST_PROVIDER_MAJOR
&&
49 desc
->major
>= LTTNG_UST_PROVIDER_MAJOR_OLDEST_COMPATIBLE
) {
50 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
51 "with LTTng UST provider version %u.%u.",
52 desc
->provider_name
, desc
->major
, desc
->minor
,
53 LTTNG_UST_PROVIDER_MAJOR
,
54 LTTNG_UST_PROVIDER_MINOR
);
55 if (desc
->major
< LTTNG_UST_PROVIDER_MAJOR
) {
56 DBG("However, some LTTng UST features might not be "
57 "available for this provider unless it is "
58 "recompiled against a more recent LTTng UST.");
60 return 1; /* accept */
62 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
63 "with LTTng UST provider version %u.%u. Please upgrade "
65 desc
->provider_name
, desc
->major
, desc
->minor
,
66 LTTNG_UST_PROVIDER_MAJOR
,
67 LTTNG_UST_PROVIDER_MINOR
);
68 return 0; /* reject */
73 * Validate that each event within the probe provider refers to the
74 * right probe, that the resulting name is not too long, and that the
75 * event class belongs to a provider with compatible version.
78 bool check_event_provider(const struct lttng_ust_probe_desc
*probe_desc
)
82 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
83 const struct lttng_ust_event_desc
*event_desc
= probe_desc
->event_desc
[i
];
85 if (event_desc
->probe_desc
!= probe_desc
) {
86 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.",
87 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
88 return false; /* provider mismatch */
90 if (!lttng_ust_validate_event_name(event_desc
)) {
91 ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.",
92 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
93 return false; /* provider mismatch */
95 if (!check_provider_version(event_desc
->tp_class
->probe_desc
)) {
96 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to an event class in a provider with incompatible version.",
97 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
105 * Called under ust lock.
108 void lttng_lazy_probe_register(struct lttng_ust_registered_probe
*reg_probe
)
110 struct lttng_ust_registered_probe
*iter
;
111 struct cds_list_head
*probe_list
;
114 * The provider ensures there are no duplicate event names.
115 * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a
116 * compile-time error due to duplicated symbol names.
120 * We sort the providers by struct lttng_ust_probe_desc pointer
123 probe_list
= &_probe_list
;
124 cds_list_for_each_entry_reverse(iter
, probe_list
, head
) {
125 BUG_ON(iter
== reg_probe
); /* Should never be in the list twice */
126 if (iter
< reg_probe
) {
127 /* We belong to the location right after iter. */
128 cds_list_add(®_probe
->head
, &iter
->head
);
132 /* We should be added at the head of the list */
133 cds_list_add(®_probe
->head
, probe_list
);
135 DBG("just registered probe %s containing %u events",
136 reg_probe
->desc
->provider_name
, reg_probe
->desc
->nr_events
);
140 * Called under ust lock.
143 void fixup_lazy_probes(void)
145 struct lttng_ust_registered_probe
*iter
, *tmp
;
149 cds_list_for_each_entry_safe(iter
, tmp
,
150 &lazy_probe_init
, lazy_init_head
) {
151 lttng_lazy_probe_register(iter
);
153 cds_list_del(&iter
->lazy_init_head
);
155 ret
= lttng_fix_pending_events();
161 * Called under ust lock.
163 struct cds_list_head
*lttng_get_probe_list_head(void)
165 if (!lazy_nesting
&& !cds_list_empty(&lazy_probe_init
))
171 struct lttng_ust_registered_probe
*lttng_ust_probe_register(const struct lttng_ust_probe_desc
*desc
)
173 struct lttng_ust_registered_probe
*reg_probe
= NULL
;
175 lttng_ust_alloc_tls();
178 * If version mismatch, don't register, but don't trigger assert
179 * on caller. The version check just prints an error.
181 if (!check_provider_version(desc
))
183 if (!check_event_provider(desc
))
188 reg_probe
= zmalloc(sizeof(struct lttng_ust_registered_probe
));
191 reg_probe
->desc
= desc
;
192 cds_list_add(®_probe
->lazy_init_head
, &lazy_probe_init
);
195 DBG("adding probe %s containing %u events to lazy registration list",
196 desc
->provider_name
, desc
->nr_events
);
198 * If there is at least one active session, we need to register
199 * the probe immediately, since we cannot delay event
200 * registration because they are needed ASAP.
202 if (lttng_session_active())
205 lttng_fix_pending_event_notifiers();
211 void lttng_ust_probe_unregister(struct lttng_ust_registered_probe
*reg_probe
)
213 lttng_ust_alloc_tls();
217 if (!check_provider_version(reg_probe
->desc
))
221 if (!reg_probe
->lazy
)
222 cds_list_del(®_probe
->head
);
224 cds_list_del(®_probe
->lazy_init_head
);
226 lttng_probe_provider_unregister_events(reg_probe
->desc
);
227 DBG("just unregistered probes of provider %s", reg_probe
->desc
->provider_name
);
232 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list
*list
)
234 struct tp_list_entry
*list_entry
, *tmp
;
236 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
237 cds_list_del(&list_entry
->head
);
243 * called with UST lock held.
245 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list
*list
)
247 struct lttng_ust_registered_probe
*reg_probe
;
248 struct cds_list_head
*probe_list
;
251 probe_list
= lttng_get_probe_list_head();
252 CDS_INIT_LIST_HEAD(&list
->head
);
253 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
254 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
256 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
257 const struct lttng_ust_event_desc
*event_desc
=
258 probe_desc
->event_desc
[i
];
259 struct tp_list_entry
*list_entry
;
261 /* Skip event if name is too long. */
262 if (!lttng_ust_validate_event_name(event_desc
))
264 list_entry
= zmalloc(sizeof(*list_entry
));
267 cds_list_add(&list_entry
->head
, &list
->head
);
268 lttng_ust_format_event_name(event_desc
, list_entry
->tp
.name
);
269 if (!event_desc
->loglevel
) {
270 list_entry
->tp
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
272 list_entry
->tp
.loglevel
= *(*event_desc
->loglevel
);
276 if (cds_list_empty(&list
->head
))
280 cds_list_first_entry(&list
->head
, struct tp_list_entry
, head
);
284 lttng_probes_prune_event_list(list
);
289 * Return current iteration position, advance internal iterator to next.
290 * Return NULL if end of list.
292 struct lttng_ust_abi_tracepoint_iter
*
293 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list
*list
)
295 struct tp_list_entry
*entry
;
300 if (entry
->head
.next
== &list
->head
)
303 list
->iter
= cds_list_entry(entry
->head
.next
,
304 struct tp_list_entry
, head
);
308 void lttng_probes_prune_field_list(struct lttng_ust_field_list
*list
)
310 struct tp_field_list_entry
*list_entry
, *tmp
;
312 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
313 cds_list_del(&list_entry
->head
);
319 * called with UST lock held.
321 int lttng_probes_get_field_list(struct lttng_ust_field_list
*list
)
323 struct lttng_ust_registered_probe
*reg_probe
;
324 struct cds_list_head
*probe_list
;
327 probe_list
= lttng_get_probe_list_head();
328 CDS_INIT_LIST_HEAD(&list
->head
);
329 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
330 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
332 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
333 const struct lttng_ust_event_desc
*event_desc
=
334 probe_desc
->event_desc
[i
];
337 if (event_desc
->tp_class
->nr_fields
== 0) {
338 /* Events without fields. */
339 struct tp_field_list_entry
*list_entry
;
341 /* Skip event if name is too long. */
342 if (!lttng_ust_validate_event_name(event_desc
))
344 list_entry
= zmalloc(sizeof(*list_entry
));
347 cds_list_add(&list_entry
->head
, &list
->head
);
348 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
349 list_entry
->field
.field_name
[0] = '\0';
350 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
351 if (!event_desc
->loglevel
) {
352 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
354 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
356 list_entry
->field
.nowrite
= 1;
359 for (j
= 0; j
< event_desc
->tp_class
->nr_fields
; j
++) {
360 const struct lttng_ust_event_field
*event_field
=
361 event_desc
->tp_class
->fields
[j
];
362 struct tp_field_list_entry
*list_entry
;
364 /* Skip event if name is too long. */
365 if (!lttng_ust_validate_event_name(event_desc
))
367 list_entry
= zmalloc(sizeof(*list_entry
));
370 cds_list_add(&list_entry
->head
, &list
->head
);
371 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
372 strncpy(list_entry
->field
.field_name
,
374 LTTNG_UST_ABI_SYM_NAME_LEN
);
375 list_entry
->field
.field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
376 switch (event_field
->type
->type
) {
377 case lttng_ust_type_integer
:
378 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_INTEGER
;
380 case lttng_ust_type_string
:
381 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
383 case lttng_ust_type_array
:
384 if (lttng_ust_get_type_array(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
385 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
387 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
389 case lttng_ust_type_sequence
:
390 if (lttng_ust_get_type_sequence(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
391 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
393 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
395 case lttng_ust_type_float
:
396 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_FLOAT
;
398 case lttng_ust_type_enum
:
399 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_ENUM
;
402 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
404 if (!event_desc
->loglevel
) {
405 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
407 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
409 list_entry
->field
.nowrite
= event_field
->nowrite
;
413 if (cds_list_empty(&list
->head
))
417 cds_list_first_entry(&list
->head
,
418 struct tp_field_list_entry
, head
);
422 lttng_probes_prune_field_list(list
);
427 * Return current iteration position, advance internal iterator to next.
428 * Return NULL if end of list.
430 struct lttng_ust_abi_field_iter
*
431 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list
*list
)
433 struct tp_field_list_entry
*entry
;
438 if (entry
->head
.next
== &list
->head
)
441 list
->iter
= cds_list_entry(entry
->head
.next
,
442 struct tp_field_list_entry
, head
);
443 return &entry
->field
;