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 * Validate that each event within the probe provider refers to the
44 * right probe, and that the resulting name is not too long.
47 bool check_event_provider(const struct lttng_ust_probe_desc
*probe_desc
)
51 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
52 const struct lttng_ust_event_desc
*event_desc
= probe_desc
->event_desc
[i
];
54 if (event_desc
->probe_desc
!= probe_desc
) {
55 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.",
56 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
57 return false; /* provider mismatch */
59 if (!lttng_ust_validate_event_name(event_desc
)) {
60 ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.",
61 probe_desc
->provider_name
, probe_desc
->provider_name
, event_desc
->event_name
);
62 return false; /* provider mismatch */
69 * Called under ust lock.
72 void lttng_lazy_probe_register(struct lttng_ust_registered_probe
*reg_probe
)
74 struct lttng_ust_registered_probe
*iter
;
75 struct cds_list_head
*probe_list
;
78 * The provider ensures there are no duplicate event names.
79 * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a
80 * compile-time error due to duplicated symbol names.
84 * We sort the providers by struct lttng_ust_probe_desc pointer
87 probe_list
= &_probe_list
;
88 cds_list_for_each_entry_reverse(iter
, probe_list
, head
) {
89 BUG_ON(iter
== reg_probe
); /* Should never be in the list twice */
90 if (iter
< reg_probe
) {
91 /* We belong to the location right after iter. */
92 cds_list_add(®_probe
->head
, &iter
->head
);
96 /* We should be added at the head of the list */
97 cds_list_add(®_probe
->head
, probe_list
);
99 DBG("just registered probe %s containing %u events",
100 reg_probe
->desc
->provider_name
, reg_probe
->desc
->nr_events
);
104 * Called under ust lock.
107 void fixup_lazy_probes(void)
109 struct lttng_ust_registered_probe
*iter
, *tmp
;
113 cds_list_for_each_entry_safe(iter
, tmp
,
114 &lazy_probe_init
, lazy_init_head
) {
115 lttng_lazy_probe_register(iter
);
117 cds_list_del(&iter
->lazy_init_head
);
119 ret
= lttng_fix_pending_events();
125 * Called under ust lock.
127 struct cds_list_head
*lttng_get_probe_list_head(void)
129 if (!lazy_nesting
&& !cds_list_empty(&lazy_probe_init
))
135 int check_provider_version(const struct lttng_ust_probe_desc
*desc
)
138 * Check tracepoint provider version compatibility.
140 if (desc
->major
<= LTTNG_UST_PROVIDER_MAJOR
) {
141 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
142 "with LTTng UST provider version %u.%u.",
143 desc
->provider_name
, desc
->major
, desc
->minor
,
144 LTTNG_UST_PROVIDER_MAJOR
,
145 LTTNG_UST_PROVIDER_MINOR
);
146 if (desc
->major
< LTTNG_UST_PROVIDER_MAJOR
) {
147 DBG("However, some LTTng UST features might not be "
148 "available for this provider unless it is "
149 "recompiled against a more recent LTTng UST.");
151 return 1; /* accept */
153 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
154 "with LTTng UST provider version %u.%u. Please upgrade "
156 desc
->provider_name
, desc
->major
, desc
->minor
,
157 LTTNG_UST_PROVIDER_MAJOR
,
158 LTTNG_UST_PROVIDER_MINOR
);
159 return 0; /* reject */
163 struct lttng_ust_registered_probe
*lttng_ust_probe_register(const struct lttng_ust_probe_desc
*desc
)
165 struct lttng_ust_registered_probe
*reg_probe
= NULL
;
167 lttng_ust_alloc_tls();
170 * If version mismatch, don't register, but don't trigger assert
171 * on caller. The version check just prints an error.
173 if (!check_provider_version(desc
))
175 if (!check_event_provider(desc
))
180 reg_probe
= zmalloc(sizeof(struct lttng_ust_registered_probe
));
183 reg_probe
->desc
= desc
;
184 cds_list_add(®_probe
->lazy_init_head
, &lazy_probe_init
);
187 DBG("adding probe %s containing %u events to lazy registration list",
188 desc
->provider_name
, desc
->nr_events
);
190 * If there is at least one active session, we need to register
191 * the probe immediately, since we cannot delay event
192 * registration because they are needed ASAP.
194 if (lttng_session_active())
197 lttng_fix_pending_event_notifiers();
203 void lttng_ust_probe_unregister(struct lttng_ust_registered_probe
*reg_probe
)
205 lttng_ust_alloc_tls();
209 if (!check_provider_version(reg_probe
->desc
))
213 if (!reg_probe
->lazy
)
214 cds_list_del(®_probe
->head
);
216 cds_list_del(®_probe
->lazy_init_head
);
218 lttng_probe_provider_unregister_events(reg_probe
->desc
);
219 DBG("just unregistered probes of provider %s", reg_probe
->desc
->provider_name
);
224 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list
*list
)
226 struct tp_list_entry
*list_entry
, *tmp
;
228 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
229 cds_list_del(&list_entry
->head
);
235 * called with UST lock held.
237 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list
*list
)
239 struct lttng_ust_registered_probe
*reg_probe
;
240 struct cds_list_head
*probe_list
;
243 probe_list
= lttng_get_probe_list_head();
244 CDS_INIT_LIST_HEAD(&list
->head
);
245 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
246 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
248 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
249 const struct lttng_ust_event_desc
*event_desc
=
250 probe_desc
->event_desc
[i
];
251 struct tp_list_entry
*list_entry
;
253 /* Skip event if name is too long. */
254 if (!lttng_ust_validate_event_name(event_desc
))
256 list_entry
= zmalloc(sizeof(*list_entry
));
259 cds_list_add(&list_entry
->head
, &list
->head
);
260 lttng_ust_format_event_name(event_desc
, list_entry
->tp
.name
);
261 if (!event_desc
->loglevel
) {
262 list_entry
->tp
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
264 list_entry
->tp
.loglevel
= *(*event_desc
->loglevel
);
268 if (cds_list_empty(&list
->head
))
272 cds_list_first_entry(&list
->head
, struct tp_list_entry
, head
);
276 lttng_probes_prune_event_list(list
);
281 * Return current iteration position, advance internal iterator to next.
282 * Return NULL if end of list.
284 struct lttng_ust_abi_tracepoint_iter
*
285 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list
*list
)
287 struct tp_list_entry
*entry
;
292 if (entry
->head
.next
== &list
->head
)
295 list
->iter
= cds_list_entry(entry
->head
.next
,
296 struct tp_list_entry
, head
);
300 void lttng_probes_prune_field_list(struct lttng_ust_field_list
*list
)
302 struct tp_field_list_entry
*list_entry
, *tmp
;
304 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
305 cds_list_del(&list_entry
->head
);
311 * called with UST lock held.
313 int lttng_probes_get_field_list(struct lttng_ust_field_list
*list
)
315 struct lttng_ust_registered_probe
*reg_probe
;
316 struct cds_list_head
*probe_list
;
319 probe_list
= lttng_get_probe_list_head();
320 CDS_INIT_LIST_HEAD(&list
->head
);
321 cds_list_for_each_entry(reg_probe
, probe_list
, head
) {
322 const struct lttng_ust_probe_desc
*probe_desc
= reg_probe
->desc
;
324 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
325 const struct lttng_ust_event_desc
*event_desc
=
326 probe_desc
->event_desc
[i
];
329 if (event_desc
->nr_fields
== 0) {
330 /* Events without fields. */
331 struct tp_field_list_entry
*list_entry
;
333 /* Skip event if name is too long. */
334 if (!lttng_ust_validate_event_name(event_desc
))
336 list_entry
= zmalloc(sizeof(*list_entry
));
339 cds_list_add(&list_entry
->head
, &list
->head
);
340 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
341 list_entry
->field
.field_name
[0] = '\0';
342 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
343 if (!event_desc
->loglevel
) {
344 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
346 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
348 list_entry
->field
.nowrite
= 1;
351 for (j
= 0; j
< event_desc
->nr_fields
; j
++) {
352 const struct lttng_ust_event_field
*event_field
=
353 event_desc
->fields
[j
];
354 struct tp_field_list_entry
*list_entry
;
356 /* Skip event if name is too long. */
357 if (!lttng_ust_validate_event_name(event_desc
))
359 list_entry
= zmalloc(sizeof(*list_entry
));
362 cds_list_add(&list_entry
->head
, &list
->head
);
363 lttng_ust_format_event_name(event_desc
, list_entry
->field
.event_name
);
364 strncpy(list_entry
->field
.field_name
,
366 LTTNG_UST_ABI_SYM_NAME_LEN
);
367 list_entry
->field
.field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
368 switch (event_field
->type
->type
) {
369 case lttng_ust_type_integer
:
370 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_INTEGER
;
372 case lttng_ust_type_string
:
373 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
375 case lttng_ust_type_array
:
376 if (lttng_ust_get_type_array(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
377 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
379 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
381 case lttng_ust_type_sequence
:
382 if (lttng_ust_get_type_sequence(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
383 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
385 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
387 case lttng_ust_type_float
:
388 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_FLOAT
;
390 case lttng_ust_type_enum
:
391 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_ENUM
;
394 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
396 if (!event_desc
->loglevel
) {
397 list_entry
->field
.loglevel
= LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT
;
399 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
401 list_entry
->field
.nowrite
= event_field
->nowrite
;
405 if (cds_list_empty(&list
->head
))
409 cds_list_first_entry(&list
->head
,
410 struct tp_field_list_entry
, head
);
414 lttng_probes_prune_field_list(list
);
419 * Return current iteration position, advance internal iterator to next.
420 * Return NULL if end of list.
422 struct lttng_ust_abi_field_iter
*
423 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list
*list
)
425 struct tp_field_list_entry
*entry
;
430 if (entry
->head
.next
== &list
->head
)
433 list
->iter
= cds_list_entry(entry
->head
.next
,
434 struct tp_field_list_entry
, head
);
435 return &entry
->field
;