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 "tracepoint-internal.h"
18 #include <ust-helper.h>
21 #include "lttng-tracer-core.h"
24 #include "ust-events-internal.h"
27 * probe list is protected by ust_lock()/ust_unlock().
29 static CDS_LIST_HEAD(_probe_list
);
32 * List of probes registered by not yet processed.
34 static CDS_LIST_HEAD(lazy_probe_init
);
37 * lazy_nesting counter ensures we don't trigger lazy probe registration
38 * fixup while we are performing the fixup. It is protected by the ust
41 static int lazy_nesting
;
44 * Called under ust lock.
47 int check_event_provider(struct lttng_ust_probe_desc
*desc
)
50 size_t provider_name_len
;
52 provider_name_len
= strnlen(desc
->provider
,
53 LTTNG_UST_ABI_SYM_NAME_LEN
- 1);
54 for (i
= 0; i
< desc
->nr_events
; i
++) {
55 if (strncmp(desc
->event_desc
[i
]->name
,
58 return 0; /* provider mismatch */
64 * Called under ust lock.
67 void lttng_lazy_probe_register(struct lttng_ust_probe_desc
*desc
)
69 struct lttng_ust_probe_desc
*iter
;
70 struct cds_list_head
*probe_list
;
73 * Each provider enforce that every event name begins with the
74 * provider name. Check this in an assertion for extra
75 * carefulness. This ensures we cannot have duplicate event
76 * names across providers.
78 assert(check_event_provider(desc
));
81 * The provider ensures there are no duplicate event names.
82 * Duplicated TRACEPOINT_EVENT event names would generate a
83 * compile-time error due to duplicated symbol names.
87 * We sort the providers by struct lttng_ust_probe_desc pointer
90 probe_list
= &_probe_list
;
91 cds_list_for_each_entry_reverse(iter
, probe_list
, head
) {
92 BUG_ON(iter
== desc
); /* Should never be in the list twice */
94 /* We belong to the location right after iter. */
95 cds_list_add(&desc
->head
, &iter
->head
);
99 /* We should be added at the head of the list */
100 cds_list_add(&desc
->head
, probe_list
);
102 DBG("just registered probe %s containing %u events",
103 desc
->provider
, desc
->nr_events
);
107 * Called under ust lock.
110 void fixup_lazy_probes(void)
112 struct lttng_ust_probe_desc
*iter
, *tmp
;
116 cds_list_for_each_entry_safe(iter
, tmp
,
117 &lazy_probe_init
, lazy_init_head
) {
118 lttng_lazy_probe_register(iter
);
120 cds_list_del(&iter
->lazy_init_head
);
122 ret
= lttng_fix_pending_events();
128 * Called under ust lock.
130 struct cds_list_head
*lttng_get_probe_list_head(void)
132 if (!lazy_nesting
&& !cds_list_empty(&lazy_probe_init
))
138 int check_provider_version(struct lttng_ust_probe_desc
*desc
)
141 * Check tracepoint provider version compatibility.
143 if (desc
->major
<= LTTNG_UST_PROVIDER_MAJOR
) {
144 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
145 "with LTTng UST provider version %u.%u.",
146 desc
->provider
, desc
->major
, desc
->minor
,
147 LTTNG_UST_PROVIDER_MAJOR
,
148 LTTNG_UST_PROVIDER_MINOR
);
149 if (desc
->major
< LTTNG_UST_PROVIDER_MAJOR
) {
150 DBG("However, some LTTng UST features might not be "
151 "available for this provider unless it is "
152 "recompiled against a more recent LTTng UST.");
154 return 1; /* accept */
156 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
157 "with LTTng UST provider version %u.%u. Please upgrade "
159 desc
->provider
, desc
->major
, desc
->minor
,
160 LTTNG_UST_PROVIDER_MAJOR
,
161 LTTNG_UST_PROVIDER_MINOR
);
162 return 0; /* reject */
167 int lttng_ust_probe_register(struct lttng_ust_probe_desc
*desc
)
171 lttng_ust_fixup_tls();
174 * If version mismatch, don't register, but don't trigger assert
175 * on caller. The version check just prints an error.
177 if (!check_provider_version(desc
))
182 cds_list_add(&desc
->lazy_init_head
, &lazy_probe_init
);
184 DBG("adding probe %s containing %u events to lazy registration list",
185 desc
->provider
, desc
->nr_events
);
187 * If there is at least one active session, we need to register
188 * the probe immediately, since we cannot delay event
189 * registration because they are needed ASAP.
191 if (lttng_session_active())
194 lttng_fix_pending_event_notifiers();
200 void lttng_ust_probe_unregister(struct lttng_ust_probe_desc
*desc
)
202 lttng_ust_fixup_tls();
204 if (!check_provider_version(desc
))
209 cds_list_del(&desc
->head
);
211 cds_list_del(&desc
->lazy_init_head
);
213 lttng_probe_provider_unregister_events(desc
);
214 DBG("just unregistered probes of provider %s", desc
->provider
);
219 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list
*list
)
221 struct tp_list_entry
*list_entry
, *tmp
;
223 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
224 cds_list_del(&list_entry
->head
);
230 * called with UST lock held.
232 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list
*list
)
234 struct lttng_ust_probe_desc
*probe_desc
;
236 struct cds_list_head
*probe_list
;
238 probe_list
= lttng_get_probe_list_head();
239 CDS_INIT_LIST_HEAD(&list
->head
);
240 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
241 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
242 struct tp_list_entry
*list_entry
;
244 list_entry
= zmalloc(sizeof(*list_entry
));
247 cds_list_add(&list_entry
->head
, &list
->head
);
248 strncpy(list_entry
->tp
.name
,
249 probe_desc
->event_desc
[i
]->name
,
250 LTTNG_UST_ABI_SYM_NAME_LEN
);
251 list_entry
->tp
.name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
252 if (!probe_desc
->event_desc
[i
]->loglevel
) {
253 list_entry
->tp
.loglevel
= TRACE_DEFAULT
;
255 list_entry
->tp
.loglevel
= *(*probe_desc
->event_desc
[i
]->loglevel
);
259 if (cds_list_empty(&list
->head
))
263 cds_list_first_entry(&list
->head
, struct tp_list_entry
, head
);
267 lttng_probes_prune_event_list(list
);
272 * Return current iteration position, advance internal iterator to next.
273 * Return NULL if end of list.
275 struct lttng_ust_abi_tracepoint_iter
*
276 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list
*list
)
278 struct tp_list_entry
*entry
;
283 if (entry
->head
.next
== &list
->head
)
286 list
->iter
= cds_list_entry(entry
->head
.next
,
287 struct tp_list_entry
, head
);
291 void lttng_probes_prune_field_list(struct lttng_ust_field_list
*list
)
293 struct tp_field_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_field_list(struct lttng_ust_field_list
*list
)
306 struct lttng_ust_probe_desc
*probe_desc
;
308 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(probe_desc
, probe_list
, head
) {
313 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
314 const struct lttng_ust_event_desc
*event_desc
=
315 probe_desc
->event_desc
[i
];
318 if (event_desc
->nr_fields
== 0) {
319 /* Events without fields. */
320 struct tp_field_list_entry
*list_entry
;
322 list_entry
= zmalloc(sizeof(*list_entry
));
325 cds_list_add(&list_entry
->head
, &list
->head
);
326 strncpy(list_entry
->field
.event_name
,
328 LTTNG_UST_ABI_SYM_NAME_LEN
);
329 list_entry
->field
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
330 list_entry
->field
.field_name
[0] = '\0';
331 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
332 if (!event_desc
->loglevel
) {
333 list_entry
->field
.loglevel
= TRACE_DEFAULT
;
335 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
337 list_entry
->field
.nowrite
= 1;
340 for (j
= 0; j
< event_desc
->nr_fields
; j
++) {
341 const struct lttng_ust_event_field
*event_field
=
342 event_desc
->fields
[j
];
343 struct tp_field_list_entry
*list_entry
;
345 list_entry
= zmalloc(sizeof(*list_entry
));
348 cds_list_add(&list_entry
->head
, &list
->head
);
349 strncpy(list_entry
->field
.event_name
,
351 LTTNG_UST_ABI_SYM_NAME_LEN
);
352 list_entry
->field
.event_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
353 strncpy(list_entry
->field
.field_name
,
355 LTTNG_UST_ABI_SYM_NAME_LEN
);
356 list_entry
->field
.field_name
[LTTNG_UST_ABI_SYM_NAME_LEN
- 1] = '\0';
357 switch (event_field
->type
->type
) {
358 case lttng_ust_type_integer
:
359 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_INTEGER
;
361 case lttng_ust_type_string
:
362 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
364 case lttng_ust_type_array
:
365 if (lttng_ust_get_type_array(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
366 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
368 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
370 case lttng_ust_type_sequence
:
371 if (lttng_ust_get_type_sequence(event_field
->type
)->encoding
== lttng_ust_string_encoding_none
)
372 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
374 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_STRING
;
376 case lttng_ust_type_float
:
377 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_FLOAT
;
379 case lttng_ust_type_enum
:
380 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_ENUM
;
383 list_entry
->field
.type
= LTTNG_UST_ABI_FIELD_OTHER
;
385 if (!event_desc
->loglevel
) {
386 list_entry
->field
.loglevel
= TRACE_DEFAULT
;
388 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
390 list_entry
->field
.nowrite
= event_field
->nowrite
;
394 if (cds_list_empty(&list
->head
))
398 cds_list_first_entry(&list
->head
,
399 struct tp_field_list_entry
, head
);
403 lttng_probes_prune_field_list(list
);
408 * Return current iteration position, advance internal iterator to next.
409 * Return NULL if end of list.
411 struct lttng_ust_abi_field_iter
*
412 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list
*list
)
414 struct tp_field_list_entry
*entry
;
419 if (entry
->head
.next
== &list
->head
)
422 list
->iter
= cds_list_entry(entry
->head
.next
,
423 struct tp_field_list_entry
, head
);
424 return &entry
->field
;