4 * Holds LTTng probes registry.
6 * Copyright 2010-2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <urcu/list.h>
26 #include <urcu/hlist.h>
27 #include <lttng/ust-events.h>
28 #include <lttng/tracepoint.h>
29 #include "tracepoint-internal.h"
34 #include "lttng-tracer-core.h"
39 * probe list is protected by ust_lock()/ust_unlock().
41 static CDS_LIST_HEAD(_probe_list
);
44 * List of probes registered by not yet processed.
46 static CDS_LIST_HEAD(lazy_probe_init
);
49 * lazy_nesting counter ensures we don't trigger lazy probe registration
50 * fixup while we are performing the fixup. It is protected by the ust
53 static int lazy_nesting
;
56 * Called under ust lock.
59 int check_event_provider(struct lttng_probe_desc
*desc
)
62 size_t provider_name_len
;
64 provider_name_len
= strnlen(desc
->provider
,
65 LTTNG_UST_SYM_NAME_LEN
- 1);
66 for (i
= 0; i
< desc
->nr_events
; i
++) {
67 if (strncmp(desc
->event_desc
[i
]->name
,
70 return 0; /* provider mismatch */
76 * Called under ust lock.
79 void lttng_lazy_probe_register(struct lttng_probe_desc
*desc
)
81 struct lttng_probe_desc
*iter
;
82 struct cds_list_head
*probe_list
;
85 * Each provider enforce that every event name begins with the
86 * provider name. Check this in an assertion for extra
87 * carefulness. This ensures we cannot have duplicate event
88 * names across providers.
90 assert(check_event_provider(desc
));
93 * The provider ensures there are no duplicate event names.
94 * Duplicated TRACEPOINT_EVENT event names would generate a
95 * compile-time error due to duplicated symbol names.
99 * We sort the providers by struct lttng_probe_desc pointer
102 probe_list
= &_probe_list
;
103 cds_list_for_each_entry_reverse(iter
, probe_list
, head
) {
104 BUG_ON(iter
== desc
); /* Should never be in the list twice */
106 /* We belong to the location right after iter. */
107 cds_list_add(&desc
->head
, &iter
->head
);
111 /* We should be added at the head of the list */
112 cds_list_add(&desc
->head
, probe_list
);
114 DBG("just registered probe %s containing %u events",
115 desc
->provider
, desc
->nr_events
);
119 * Called under ust lock.
122 void fixup_lazy_probes(void)
124 struct lttng_probe_desc
*iter
, *tmp
;
128 cds_list_for_each_entry_safe(iter
, tmp
,
129 &lazy_probe_init
, lazy_init_head
) {
130 lttng_lazy_probe_register(iter
);
132 cds_list_del(&iter
->lazy_init_head
);
134 ret
= lttng_fix_pending_events();
140 * Called under ust lock.
142 struct cds_list_head
*lttng_get_probe_list_head(void)
144 if (!lazy_nesting
&& !cds_list_empty(&lazy_probe_init
))
150 const struct lttng_probe_desc
*find_provider(const char *provider
)
152 struct lttng_probe_desc
*iter
;
153 struct cds_list_head
*probe_list
;
155 probe_list
= lttng_get_probe_list_head();
156 cds_list_for_each_entry(iter
, probe_list
, head
) {
157 if (!strcmp(iter
->provider
, provider
))
164 int check_provider_version(struct lttng_probe_desc
*desc
)
167 * Check tracepoint provider version compatibility.
169 if (desc
->major
<= LTTNG_UST_PROVIDER_MAJOR
) {
170 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
171 "with LTTng UST provider version %u.%u.",
172 desc
->provider
, desc
->major
, desc
->minor
,
173 LTTNG_UST_PROVIDER_MAJOR
,
174 LTTNG_UST_PROVIDER_MINOR
);
175 if (desc
->major
< LTTNG_UST_PROVIDER_MAJOR
) {
176 DBG("However, some LTTng UST features might not be "
177 "available for this provider unless it is "
178 "recompiled against a more recent LTTng UST.");
180 return 1; /* accept */
182 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
183 "with LTTng UST provider version %u.%u. Please upgrade "
185 desc
->provider
, desc
->major
, desc
->minor
,
186 LTTNG_UST_PROVIDER_MAJOR
,
187 LTTNG_UST_PROVIDER_MINOR
);
188 return 0; /* reject */
193 int lttng_probe_register(struct lttng_probe_desc
*desc
)
197 lttng_ust_fixup_tls();
200 * If version mismatch, don't register, but don't trigger assert
201 * on caller. The version check just prints an error.
203 if (!check_provider_version(desc
))
209 * Check if the provider has already been registered.
211 if (find_provider(desc
->provider
)) {
215 cds_list_add(&desc
->lazy_init_head
, &lazy_probe_init
);
217 DBG("adding probe %s containing %u events to lazy registration list",
218 desc
->provider
, desc
->nr_events
);
220 * If there is at least one active session, we need to register
221 * the probe immediately, since we cannot delay event
222 * registration because they are needed ASAP.
224 if (lttng_session_active())
231 /* Backward compatibility with UST 2.0 */
232 int ltt_probe_register(struct lttng_probe_desc
*desc
)
234 return lttng_probe_register(desc
);
237 void lttng_probe_unregister(struct lttng_probe_desc
*desc
)
239 lttng_ust_fixup_tls();
241 if (!check_provider_version(desc
))
246 cds_list_del(&desc
->head
);
248 cds_list_del(&desc
->lazy_init_head
);
249 DBG("just unregistered probe %s", desc
->provider
);
253 /* Backward compatibility with UST 2.0 */
254 void ltt_probe_unregister(struct lttng_probe_desc
*desc
)
256 lttng_probe_unregister(desc
);
259 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list
*list
)
261 struct tp_list_entry
*list_entry
, *tmp
;
263 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
264 cds_list_del(&list_entry
->head
);
270 * called with UST lock held.
272 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list
*list
)
274 struct lttng_probe_desc
*probe_desc
;
276 struct cds_list_head
*probe_list
;
278 probe_list
= lttng_get_probe_list_head();
279 CDS_INIT_LIST_HEAD(&list
->head
);
280 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
281 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
282 struct tp_list_entry
*list_entry
;
284 list_entry
= zmalloc(sizeof(*list_entry
));
287 cds_list_add(&list_entry
->head
, &list
->head
);
288 strncpy(list_entry
->tp
.name
,
289 probe_desc
->event_desc
[i
]->name
,
290 LTTNG_UST_SYM_NAME_LEN
);
291 list_entry
->tp
.name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
292 if (!probe_desc
->event_desc
[i
]->loglevel
) {
293 list_entry
->tp
.loglevel
= TRACE_DEFAULT
;
295 list_entry
->tp
.loglevel
= *(*probe_desc
->event_desc
[i
]->loglevel
);
299 if (cds_list_empty(&list
->head
))
303 cds_list_first_entry(&list
->head
, struct tp_list_entry
, head
);
307 lttng_probes_prune_event_list(list
);
312 * Return current iteration position, advance internal iterator to next.
313 * Return NULL if end of list.
315 struct lttng_ust_tracepoint_iter
*
316 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list
*list
)
318 struct tp_list_entry
*entry
;
323 if (entry
->head
.next
== &list
->head
)
326 list
->iter
= cds_list_entry(entry
->head
.next
,
327 struct tp_list_entry
, head
);
331 void lttng_probes_prune_field_list(struct lttng_ust_field_list
*list
)
333 struct tp_field_list_entry
*list_entry
, *tmp
;
335 cds_list_for_each_entry_safe(list_entry
, tmp
, &list
->head
, head
) {
336 cds_list_del(&list_entry
->head
);
342 * called with UST lock held.
344 int lttng_probes_get_field_list(struct lttng_ust_field_list
*list
)
346 struct lttng_probe_desc
*probe_desc
;
348 struct cds_list_head
*probe_list
;
350 probe_list
= lttng_get_probe_list_head();
351 CDS_INIT_LIST_HEAD(&list
->head
);
352 cds_list_for_each_entry(probe_desc
, probe_list
, head
) {
353 for (i
= 0; i
< probe_desc
->nr_events
; i
++) {
354 const struct lttng_event_desc
*event_desc
=
355 probe_desc
->event_desc
[i
];
358 if (event_desc
->nr_fields
== 0) {
359 /* Events without fields. */
360 struct tp_field_list_entry
*list_entry
;
362 list_entry
= zmalloc(sizeof(*list_entry
));
365 cds_list_add(&list_entry
->head
, &list
->head
);
366 strncpy(list_entry
->field
.event_name
,
368 LTTNG_UST_SYM_NAME_LEN
);
369 list_entry
->field
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
370 list_entry
->field
.field_name
[0] = '\0';
371 list_entry
->field
.type
= LTTNG_UST_FIELD_OTHER
;
372 if (!event_desc
->loglevel
) {
373 list_entry
->field
.loglevel
= TRACE_DEFAULT
;
375 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
377 list_entry
->field
.nowrite
= 1;
380 for (j
= 0; j
< event_desc
->nr_fields
; j
++) {
381 const struct lttng_event_field
*event_field
=
382 &event_desc
->fields
[j
];
383 struct tp_field_list_entry
*list_entry
;
385 list_entry
= zmalloc(sizeof(*list_entry
));
388 cds_list_add(&list_entry
->head
, &list
->head
);
389 strncpy(list_entry
->field
.event_name
,
391 LTTNG_UST_SYM_NAME_LEN
);
392 list_entry
->field
.event_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
393 strncpy(list_entry
->field
.field_name
,
395 LTTNG_UST_SYM_NAME_LEN
);
396 list_entry
->field
.field_name
[LTTNG_UST_SYM_NAME_LEN
- 1] = '\0';
397 switch (event_field
->type
.atype
) {
399 list_entry
->field
.type
= LTTNG_UST_FIELD_INTEGER
;
402 list_entry
->field
.type
= LTTNG_UST_FIELD_STRING
;
405 if (event_field
->type
.u
.array
.elem_type
.atype
!= atype_integer
406 || event_field
->type
.u
.array
.elem_type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
407 list_entry
->field
.type
= LTTNG_UST_FIELD_OTHER
;
409 list_entry
->field
.type
= LTTNG_UST_FIELD_STRING
;
412 if (event_field
->type
.u
.sequence
.elem_type
.atype
!= atype_integer
413 || event_field
->type
.u
.sequence
.elem_type
.u
.basic
.integer
.encoding
== lttng_encode_none
)
414 list_entry
->field
.type
= LTTNG_UST_FIELD_OTHER
;
416 list_entry
->field
.type
= LTTNG_UST_FIELD_STRING
;
419 list_entry
->field
.type
= LTTNG_UST_FIELD_FLOAT
;
422 list_entry
->field
.type
= LTTNG_UST_FIELD_ENUM
;
425 list_entry
->field
.type
= LTTNG_UST_FIELD_OTHER
;
427 if (!event_desc
->loglevel
) {
428 list_entry
->field
.loglevel
= TRACE_DEFAULT
;
430 list_entry
->field
.loglevel
= *(*event_desc
->loglevel
);
432 list_entry
->field
.nowrite
= event_field
->nowrite
;
436 if (cds_list_empty(&list
->head
))
440 cds_list_first_entry(&list
->head
,
441 struct tp_field_list_entry
, head
);
445 lttng_probes_prune_field_list(list
);
450 * Return current iteration position, advance internal iterator to next.
451 * Return NULL if end of list.
453 struct lttng_ust_field_iter
*
454 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list
*list
)
456 struct tp_field_list_entry
*entry
;
461 if (entry
->head
.next
== &list
->head
)
464 list
->iter
= cds_list_entry(entry
->head
.next
,
465 struct tp_field_list_entry
, head
);
466 return &entry
->field
;