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 bool check_type_provider(const struct lttng_ust_type_common
*type
);
76 bool check_type_provider(const struct lttng_ust_type_common
*type
)
79 case lttng_ust_type_integer
:
81 case lttng_ust_type_string
:
83 case lttng_ust_type_float
:
85 case lttng_ust_type_dynamic
:
87 case lttng_ust_type_enum
:
89 const struct lttng_ust_type_enum
*enum_type
= caa_container_of(type
, const struct lttng_ust_type_enum
, parent
);
91 return check_provider_version(enum_type
->desc
->probe_desc
);
93 case lttng_ust_type_array
:
95 const struct lttng_ust_type_array
*array_type
= caa_container_of(type
, const struct lttng_ust_type_array
, parent
);
97 return check_type_provider(array_type
->elem_type
);
99 case lttng_ust_type_sequence
:
101 const struct lttng_ust_type_sequence
*sequence_type
= caa_container_of(type
, const struct lttng_ust_type_sequence
, parent
);
103 return check_type_provider(sequence_type
->elem_type
);
105 case lttng_ust_type_struct
:
107 const struct lttng_ust_type_struct
*struct_type
= caa_container_of(type
, const struct lttng_ust_type_struct
, parent
);
110 for (i
= 0; i
< struct_type
->nr_fields
; i
++) {
111 if (!check_type_provider(struct_type
->fields
[i
]->type
))
122 * Validate that each event within the probe provider refers to the
123 * right probe, that the resulting name is not too long, and that the
124 * event class belongs to a provider with compatible version.
127 bool check_event_provider(const struct lttng_ust_probe_desc
*probe_desc
)
131 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
132 const struct lttng_ust_event_desc
*event_desc
= probe_desc
->event_desc
[i
];
133 const struct lttng_ust_tracepoint_class
*tp_class
= event_desc
->tp_class
;
135 if (event_desc
->probe_desc
!= probe_desc
) {
136 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.",
137 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
138 return false; /* provider mismatch */
140 if (!lttng_ust_validate_event_name(event_desc
)) {
141 ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.",
142 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
143 return false; /* provider mismatch */
145 if (!check_provider_version(tp_class
->probe_desc
)) {
146 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to an event class in a provider with incompatible version.",
147 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
150 for (j
= 0; j
< tp_class
->nr_fields
; j
++) {
151 const struct lttng_ust_event_field
*field
= tp_class
->fields
[j
];
153 if (!check_type_provider(field
->type
)) {
154 ERR("Error registering probe provider '%s'. Event '%s:%s' contains a field which refers to an provider with incompatible version.",
155 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
164 * Called under ust lock.
167 void lttng_lazy_probe_register(struct lttng_ust_registered_probe
*reg_probe
)
169 struct lttng_ust_registered_probe
*iter
;
170 struct cds_list_head
*probe_list
;
173 * The provider ensures there are no duplicate event names.
174 * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a
175 * compile-time error due to duplicated symbol names.
179 * We sort the providers by struct lttng_ust_probe_desc pointer
182 probe_list
= &_probe_list
;
183 cds_list_for_each_entry_reverse(iter
, probe_list
, head
) {
184 BUG_ON(iter
== reg_probe
); /* Should never be in the list twice */
185 if (iter
< reg_probe
) {
186 /* We belong to the location right after iter. */
187 cds_list_add(®_probe
->head
, &iter
->head
);
191 /* We should be added at the head of the list */
192 cds_list_add(®_probe
->head
, probe_list
);
194 DBG("just registered probe %s containing %u events",
195 reg_probe
->desc
->provider_name
, reg_probe
->desc
->nr_events
);
199 * Called under ust lock.
202 void fixup_lazy_probes(void)
204 struct lttng_ust_registered_probe
*iter
, *tmp
;
208 cds_list_for_each_entry_safe(iter
, tmp
,
209 &lazy_probe_init
, lazy_init_head
) {
210 lttng_lazy_probe_register(iter
);
212 cds_list_del(&iter
->lazy_init_head
);
214 ret
= lttng_fix_pending_events();
220 * Called under ust lock.
222 struct cds_list_head
*lttng_get_probe_list_head(void)
224 if (!lazy_nesting
&& !cds_list_empty(&lazy_probe_init
))
230 struct lttng_ust_registered_probe
*lttng_ust_probe_register(const struct lttng_ust_probe_desc
*desc
)
232 struct lttng_ust_registered_probe
*reg_probe
= NULL
;
234 lttng_ust_common_init_thread(0);
237 * If version mismatch, don't register, but don't trigger assert
238 * on caller. The version check just prints an error.
240 if (!check_provider_version(desc
))
242 if (!check_event_provider(desc
))
247 reg_probe
= zmalloc(sizeof(struct lttng_ust_registered_probe
));
250 reg_probe
->desc
= desc
;
251 cds_list_add(®_probe
->lazy_init_head
, &lazy_probe_init
);
254 DBG("adding probe %s containing %u events to lazy registration list",
255 desc
->provider_name
, desc
->nr_events
);
257 * If there is at least one active session, we need to register
258 * the probe immediately, since we cannot delay event
259 * registration because they are needed ASAP.
261 if (lttng_session_active())
264 lttng_fix_pending_event_notifiers();
270 void lttng_ust_probe_unregister(struct lttng_ust_registered_probe
*reg_probe
)
272 lttng_ust_common_init_thread(0);
276 if (!check_provider_version(reg_probe
->desc
))
280 if (!reg_probe
->lazy
)
281 cds_list_del(®_probe
->head
);
283 cds_list_del(®_probe
->lazy_init_head
);
285 lttng_probe_provider_unregister_events(reg_probe
->desc
);
286 DBG("just unregistered probes of provider %s", reg_probe
->desc
->provider_name
);
291 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list
*list
)
293 struct tp_list_entry
*list_entry
, *tmp
;
295 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
296 cds_list_del(&list_entry
->head
);
302 * called with UST lock held.
304 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list
*list
)
306 struct lttng_ust_registered_probe
*reg_probe
;
307 struct cds_list_head
*probe_list
;
310 probe_list
= lttng_get_probe_list_head();
311 CDS_INIT_LIST_HEAD(&list
->head
);
312 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
313 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
315 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
316 const struct lttng_ust_event_desc
*event_desc
=
317 probe_desc
->event_desc
[i
];
318 struct tp_list_entry
*list_entry
;
320 /* Skip event if name is too long. */
321 if (!lttng_ust_validate_event_name(event_desc
))
323 list_entry
= zmalloc(sizeof(*list_entry
));
326 cds_list_add(&list_entry
->head
, &list
->head
);
327 lttng_ust_format_event_name(event_desc
, list_entry
->tp
.name
);
328 if (!event_desc
->loglevel
) {
329 list_entry
->tp
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
331 list_entry
->tp
.loglevel
= *(*event_desc
->loglevel
);
335 if (cds_list_empty(&list
->head
))
339 cds_list_first_entry(&list
->head
, struct tp_list_entry
, head
);
343 lttng_probes_prune_event_list(list
);
348 * Return current iteration position, advance internal iterator to next.
349 * Return NULL if end of list.
351 struct lttng_ust_abi_tracepoint_iter
*
352 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list
*list
)
354 struct tp_list_entry
*entry
;
359 if (entry
->head
.next
== &list
->head
)
362 list
->iter
= cds_list_entry(entry
->head
.next
,
363 struct tp_list_entry
, head
);
367 void lttng_probes_prune_field_list(struct lttng_ust_field_list
*list
)
369 struct tp_field_list_entry
*list_entry
, *tmp
;
371 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
372 cds_list_del(&list_entry
->head
);
378 * called with UST lock held.
380 int lttng_probes_get_field_list(struct lttng_ust_field_list
*list
)
382 struct lttng_ust_registered_probe
*reg_probe
;
383 struct cds_list_head
*probe_list
;
386 probe_list
= lttng_get_probe_list_head();
387 CDS_INIT_LIST_HEAD(&list
->head
);
388 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
389 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
391 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
392 const struct lttng_ust_event_desc
*event_desc
=
393 probe_desc
->event_desc
[i
];
396 if (event_desc
->tp_class
->nr_fields
== 0) {
397 /* Events without fields. */
398 struct tp_field_list_entry
*list_entry
;
400 /* Skip event if name is too long. */
401 if (!lttng_ust_validate_event_name(event_desc
))
403 list_entry
= zmalloc(sizeof(*list_entry
));
406 cds_list_add(&list_entry
->head
, &list
->head
);
407 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
408 list_entry
->field
.field_name
[0] = '\0';
409 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
410 if (!event_desc
->loglevel
) {
411 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
413 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
415 list_entry
->field
.nowrite
= 1;
418 for (j
= 0; j
< event_desc
->tp_class
->nr_fields
; j
++) {
419 const struct lttng_ust_event_field
*event_field
=
420 event_desc
->tp_class
->fields
[j
];
421 struct tp_field_list_entry
*list_entry
;
423 /* Skip event if name is too long. */
424 if (!lttng_ust_validate_event_name(event_desc
))
426 list_entry
= zmalloc(sizeof(*list_entry
));
429 cds_list_add(&list_entry
->head
, &list
->head
);
430 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
431 strncpy(list_entry
->field
.field_name
,
433 LTTNG_UST_ABI_SYM_NAME_LEN
);
434 list_entry
->field
.field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
435 switch (event_field
->type
->type
) {
436 case lttng_ust_type_integer
:
437 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_INTEGER
;
439 case lttng_ust_type_string
:
440 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
442 case lttng_ust_type_array
:
443 if (lttng_ust_get_type_array(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
444 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
446 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
448 case lttng_ust_type_sequence
:
449 if (lttng_ust_get_type_sequence(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
450 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
452 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
454 case lttng_ust_type_float
:
455 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_FLOAT
;
457 case lttng_ust_type_enum
:
458 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_ENUM
;
461 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
463 if (!event_desc
->loglevel
) {
464 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
466 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
468 list_entry
->field
.nowrite
= event_field
->nowrite
;
472 if (cds_list_empty(&list
->head
))
476 cds_list_first_entry(&list
->head
,
477 struct tp_field_list_entry
, head
);
481 lttng_probes_prune_field_list(list
);
486 * Return current iteration position, advance internal iterator to next.
487 * Return NULL if end of list.
489 struct lttng_ust_abi_field_iter
*
490 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list
*list
)
492 struct tp_field_list_entry
*entry
;
497 if (entry
->head
.next
== &list
->head
)
500 list
->iter
= cds_list_entry(entry
->head
.next
,
501 struct tp_field_list_entry
, head
);
502 return &entry
->field
;