lttng_ust_init_thread: initialise cached context values
[lttng-ust.git] / src / lib / lttng-ust / lttng-probes.c
CommitLineData
ce5aef0b 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
ce5aef0b 3 *
c0c0989a 4 * Copyright 2010-2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
e92f3e28 5 *
c0c0989a 6 * Holds LTTng probes registry.
ce5aef0b
MD
7 */
8
3fbec7dc 9#define _LGPL_SOURCE
8d8a24c8
MD
10#include <string.h>
11#include <errno.h>
12#include <urcu/list.h>
1f18504e 13#include <urcu/hlist.h>
4318ae1b 14#include <lttng/ust-events.h>
902e1379 15#include <lttng/tracepoint.h>
8f51c684 16#include "common/tracepoint.h"
a3bb4b27 17#include <assert.h>
9d315d6d 18#include "common/macros.h"
48740cab 19#include <ctype.h>
ce5aef0b 20
7dd08bec 21#include "lttng-tracer-core.h"
e58e5ad5 22#include "common/jhash.h"
36c52fff 23#include "lib/lttng-ust/events.h"
8165c8da
MD
24
25/*
17dfb34b 26 * probe list is protected by ust_lock()/ust_unlock().
8165c8da 27 */
ac69b35b 28static CDS_LIST_HEAD(_probe_list);
e58095ef 29
6715d7d1
MD
30/*
31 * List of probes registered by not yet processed.
32 */
33static CDS_LIST_HEAD(lazy_probe_init);
df854e41 34
6715d7d1
MD
35/*
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
38 * mutex.
39 */
40static int lazy_nesting;
df854e41 41
f6173469
MD
42static
43int check_provider_version(const struct lttng_ust_probe_desc *desc)
44{
45 /*
46 * Check tracepoint provider version compatibility.
47 */
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.");
59 }
60 return 1; /* accept */
61 } else {
62 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
63 "with LTTng UST provider version %u.%u. Please upgrade "
64 "LTTng UST.",
65 desc->provider_name, desc->major, desc->minor,
66 LTTNG_UST_PROVIDER_MAJOR,
67 LTTNG_UST_PROVIDER_MINOR);
68 return 0; /* reject */
69 }
70}
71
4c155a06
MD
72static
73bool check_type_provider(const struct lttng_ust_type_common *type);
74
75static
76bool check_type_provider(const struct lttng_ust_type_common *type)
77{
78 switch (type->type) {
79 case lttng_ust_type_integer:
80 return true;
81 case lttng_ust_type_string:
82 return true;
83 case lttng_ust_type_float:
84 return true;
85 case lttng_ust_type_dynamic:
86 return true;
87 case lttng_ust_type_enum:
88 {
89 const struct lttng_ust_type_enum *enum_type = caa_container_of(type, const struct lttng_ust_type_enum, parent);
90
91 return check_provider_version(enum_type->desc->probe_desc);
92 }
93 case lttng_ust_type_array:
94 {
95 const struct lttng_ust_type_array *array_type = caa_container_of(type, const struct lttng_ust_type_array, parent);
96
97 return check_type_provider(array_type->elem_type);
98 }
99 case lttng_ust_type_sequence:
100 {
101 const struct lttng_ust_type_sequence *sequence_type = caa_container_of(type, const struct lttng_ust_type_sequence, parent);
102
103 return check_type_provider(sequence_type->elem_type);
104 }
105 case lttng_ust_type_struct:
106 {
107 const struct lttng_ust_type_struct *struct_type = caa_container_of(type, const struct lttng_ust_type_struct, parent);
108 size_t i;
109
110 for (i = 0; i < struct_type->nr_fields; i++) {
111 if (!check_type_provider(struct_type->fields[i]->type))
112 return false;
113 }
114 return true;
115 }
116 default:
117 return false;
118 }
119}
120
6715d7d1 121/*
5b4c6da4 122 * Validate that each event within the probe provider refers to the
f6173469
MD
123 * right probe, that the resulting name is not too long, and that the
124 * event class belongs to a provider with compatible version.
6715d7d1 125 */
ce5aef0b 126static
4e48b5d2 127bool check_event_provider(const struct lttng_ust_probe_desc *probe_desc)
ce5aef0b 128{
4c155a06 129 int i, j;
5b4c6da4
MD
130
131 for (i = 0; i < probe_desc->nr_events; i++) {
132 const struct lttng_ust_event_desc *event_desc = probe_desc->event_desc[i];
4c155a06 133 const struct lttng_ust_tracepoint_class *tp_class = event_desc->tp_class;
5b4c6da4
MD
134
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 */
139 }
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 */
144 }
4c155a06 145 if (!check_provider_version(tp_class->probe_desc)) {
f6173469
MD
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);
148 return false;
149 }
4c155a06
MD
150 for (j = 0; j < tp_class->nr_fields; j++) {
151 const struct lttng_ust_event_field *field = tp_class->fields[j];
152
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);
156 return false;
157 }
158 }
ce5aef0b 159 }
5b4c6da4 160 return true;
ce5aef0b
MD
161}
162
6715d7d1
MD
163/*
164 * Called under ust lock.
165 */
166static
4e48b5d2 167void lttng_lazy_probe_register(struct lttng_ust_registered_probe *reg_probe)
ce5aef0b 168{
4e48b5d2 169 struct lttng_ust_registered_probe *iter;
ac69b35b 170 struct cds_list_head *probe_list;
48205d60 171
48205d60
MD
172 /*
173 * The provider ensures there are no duplicate event names.
7f2f82c3 174 * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a
48205d60 175 * compile-time error due to duplicated symbol names.
ce5aef0b 176 */
df854e41
MD
177
178 /*
dc11f93f 179 * We sort the providers by struct lttng_ust_probe_desc pointer
df854e41
MD
180 * address.
181 */
6715d7d1 182 probe_list = &_probe_list;
ac69b35b 183 cds_list_for_each_entry_reverse(iter, probe_list, head) {
4e48b5d2
MD
184 BUG_ON(iter == reg_probe); /* Should never be in the list twice */
185 if (iter < reg_probe) {
df854e41 186 /* We belong to the location right after iter. */
4e48b5d2
MD
187 cds_list_add(&reg_probe->head, &iter->head);
188 goto probe_added;
df854e41
MD
189 }
190 }
191 /* We should be added at the head of the list */
4e48b5d2
MD
192 cds_list_add(&reg_probe->head, probe_list);
193probe_added:
e81a53af 194 DBG("just registered probe %s containing %u events",
4e48b5d2 195 reg_probe->desc->provider_name, reg_probe->desc->nr_events);
6715d7d1
MD
196}
197
198/*
199 * Called under ust lock.
200 */
201static
202void fixup_lazy_probes(void)
203{
4e48b5d2 204 struct lttng_ust_registered_probe *iter, *tmp;
5f733922 205 int ret;
6715d7d1
MD
206
207 lazy_nesting++;
208 cds_list_for_each_entry_safe(iter, tmp,
209 &lazy_probe_init, lazy_init_head) {
210 lttng_lazy_probe_register(iter);
211 iter->lazy = 0;
212 cds_list_del(&iter->lazy_init_head);
213 }
5f733922
MD
214 ret = lttng_fix_pending_events();
215 assert(!ret);
6715d7d1
MD
216 lazy_nesting--;
217}
218
219/*
220 * Called under ust lock.
221 */
222struct cds_list_head *lttng_get_probe_list_head(void)
223{
224 if (!lazy_nesting && !cds_list_empty(&lazy_probe_init))
225 fixup_lazy_probes();
226 return &_probe_list;
227}
228
71d31690 229
4e48b5d2 230struct lttng_ust_registered_probe *lttng_ust_probe_register(const struct lttng_ust_probe_desc *desc)
6715d7d1 231{
4e48b5d2 232 struct lttng_ust_registered_probe *reg_probe = NULL;
6715d7d1 233
c246521d 234 lttng_ust_common_init_thread(0);
c362addf 235
71d31690
MD
236 /*
237 * If version mismatch, don't register, but don't trigger assert
238 * on caller. The version check just prints an error.
239 */
240 if (!check_provider_version(desc))
4e48b5d2 241 return NULL;
5b4c6da4 242 if (!check_event_provider(desc))
4e48b5d2 243 return NULL;
71d31690 244
3327ac33 245 ust_lock_nocheck();
6715d7d1 246
4e48b5d2
MD
247 reg_probe = zmalloc(sizeof(struct lttng_ust_registered_probe));
248 if (!reg_probe)
249 goto end;
250 reg_probe->desc = desc;
251 cds_list_add(&reg_probe->lazy_init_head, &lazy_probe_init);
252 reg_probe->lazy = 1;
253
6715d7d1 254 DBG("adding probe %s containing %u events to lazy registration list",
5b4c6da4 255 desc->provider_name, desc->nr_events);
6715d7d1
MD
256 /*
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.
260 */
261 if (lttng_session_active())
262 fixup_lazy_probes();
0fdd0b89 263
d8d2416d 264 lttng_fix_pending_event_notifiers();
4e48b5d2 265end:
17dfb34b 266 ust_unlock();
4e48b5d2 267 return reg_probe;
ce5aef0b 268}
ce5aef0b 269
4e48b5d2 270void lttng_ust_probe_unregister(struct lttng_ust_registered_probe *reg_probe)
ce5aef0b 271{
c246521d 272 lttng_ust_common_init_thread(0);
c362addf 273
4e48b5d2
MD
274 if (!reg_probe)
275 return;
276 if (!check_provider_version(reg_probe->desc))
71d31690
MD
277 return;
278
3327ac33 279 ust_lock_nocheck();
4e48b5d2
MD
280 if (!reg_probe->lazy)
281 cds_list_del(&reg_probe->head);
6715d7d1 282 else
4e48b5d2 283 cds_list_del(&reg_probe->lazy_init_head);
2c05c691 284
4e48b5d2
MD
285 lttng_probe_provider_unregister_events(reg_probe->desc);
286 DBG("just unregistered probes of provider %s", reg_probe->desc->provider_name);
17dfb34b 287 ust_unlock();
4e48b5d2 288 free(reg_probe);
ce5aef0b 289}
ce5aef0b 290
7dd08bec 291void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
292{
293 struct tp_list_entry *list_entry, *tmp;
294
295 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
296 cds_list_del(&list_entry->head);
297 free(list_entry);
298 }
299}
300
301/*
302 * called with UST lock held.
303 */
7dd08bec 304int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224 305{
4e48b5d2 306 struct lttng_ust_registered_probe *reg_probe;
ac69b35b 307 struct cds_list_head *probe_list;
4e48b5d2 308 int i;
c8fcf224 309
ac69b35b 310 probe_list = lttng_get_probe_list_head();
c8fcf224 311 CDS_INIT_LIST_HEAD(&list->head);
4e48b5d2
MD
312 cds_list_for_each_entry(reg_probe, probe_list, head) {
313 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
314
c8fcf224 315 for (i = 0; i < probe_desc->nr_events; i++) {
5b4c6da4
MD
316 const struct lttng_ust_event_desc *event_desc =
317 probe_desc->event_desc[i];
c8fcf224
MD
318 struct tp_list_entry *list_entry;
319
5b4c6da4
MD
320 /* Skip event if name is too long. */
321 if (!lttng_ust_validate_event_name(event_desc))
322 continue;
c8fcf224
MD
323 list_entry = zmalloc(sizeof(*list_entry));
324 if (!list_entry)
325 goto err_nomem;
326 cds_list_add(&list_entry->head, &list->head);
5b4c6da4
MD
327 lttng_ust_format_event_name(event_desc, list_entry->tp.name);
328 if (!event_desc->loglevel) {
612e9ce4 329 list_entry->tp.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
c8fcf224 330 } else {
5b4c6da4 331 list_entry->tp.loglevel = *(*event_desc->loglevel);
c8fcf224
MD
332 }
333 }
334 }
335 if (cds_list_empty(&list->head))
336 list->iter = NULL;
337 else
338 list->iter =
339 cds_list_first_entry(&list->head, struct tp_list_entry, head);
340 return 0;
341
342err_nomem:
7dd08bec 343 lttng_probes_prune_event_list(list);
c8fcf224
MD
344 return -ENOMEM;
345}
346
347/*
348 * Return current iteration position, advance internal iterator to next.
349 * Return NULL if end of list.
350 */
fd17d7ce 351struct lttng_ust_abi_tracepoint_iter *
c8fcf224
MD
352 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
353{
354 struct tp_list_entry *entry;
355
356 if (!list->iter)
357 return NULL;
358 entry = list->iter;
359 if (entry->head.next == &list->head)
360 list->iter = NULL;
361 else
362 list->iter = cds_list_entry(entry->head.next,
363 struct tp_list_entry, head);
364 return &entry->tp;
365}
1f18504e 366
7dd08bec 367void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
368{
369 struct tp_field_list_entry *list_entry, *tmp;
370
371 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
372 cds_list_del(&list_entry->head);
373 free(list_entry);
374 }
375}
376
377/*
378 * called with UST lock held.
379 */
7dd08bec 380int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
06d4f27e 381{
4e48b5d2 382 struct lttng_ust_registered_probe *reg_probe;
ac69b35b 383 struct cds_list_head *probe_list;
4e48b5d2 384 int i;
06d4f27e 385
ac69b35b 386 probe_list = lttng_get_probe_list_head();
06d4f27e 387 CDS_INIT_LIST_HEAD(&list->head);
4e48b5d2
MD
388 cds_list_for_each_entry(reg_probe, probe_list, head) {
389 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
390
06d4f27e 391 for (i = 0; i < probe_desc->nr_events; i++) {
dc11f93f 392 const struct lttng_ust_event_desc *event_desc =
06d4f27e
MD
393 probe_desc->event_desc[i];
394 int j;
395
5b675300 396 if (event_desc->tp_class->nr_fields == 0) {
26329f26
MD
397 /* Events without fields. */
398 struct tp_field_list_entry *list_entry;
399
5b4c6da4
MD
400 /* Skip event if name is too long. */
401 if (!lttng_ust_validate_event_name(event_desc))
402 continue;
26329f26
MD
403 list_entry = zmalloc(sizeof(*list_entry));
404 if (!list_entry)
405 goto err_nomem;
406 cds_list_add(&list_entry->head, &list->head);
5b4c6da4 407 lttng_ust_format_event_name(event_desc, list_entry->field.event_name);
26329f26 408 list_entry->field.field_name[0] = '\0';
fd17d7ce 409 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
26329f26 410 if (!event_desc->loglevel) {
612e9ce4 411 list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
26329f26
MD
412 } else {
413 list_entry->field.loglevel = *(*event_desc->loglevel);
414 }
415 list_entry->field.nowrite = 1;
416 }
417
5b675300 418 for (j = 0; j < event_desc->tp_class->nr_fields; j++) {
25cff019 419 const struct lttng_ust_event_field *event_field =
5b675300 420 event_desc->tp_class->fields[j];
06d4f27e
MD
421 struct tp_field_list_entry *list_entry;
422
5b4c6da4
MD
423 /* Skip event if name is too long. */
424 if (!lttng_ust_validate_event_name(event_desc))
425 continue;
06d4f27e
MD
426 list_entry = zmalloc(sizeof(*list_entry));
427 if (!list_entry)
428 goto err_nomem;
429 cds_list_add(&list_entry->head, &list->head);
5b4c6da4 430 lttng_ust_format_event_name(event_desc, list_entry->field.event_name);
06d4f27e
MD
431 strncpy(list_entry->field.field_name,
432 event_field->name,
fd17d7ce
MD
433 LTTNG_UST_ABI_SYM_NAME_LEN);
434 list_entry->field.field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
a084756d
MD
435 switch (event_field->type->type) {
436 case lttng_ust_type_integer:
fd17d7ce 437 list_entry->field.type = LTTNG_UST_ABI_FIELD_INTEGER;
40003310 438 break;
a084756d 439 case lttng_ust_type_string:
fd17d7ce 440 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
40003310 441 break;
a084756d
MD
442 case lttng_ust_type_array:
443 if (lttng_ust_get_type_array(event_field->type)->encoding == lttng_ust_string_encoding_none)
fd17d7ce 444 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
40003310 445 else
fd17d7ce 446 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
40003310 447 break;
a084756d
MD
448 case lttng_ust_type_sequence:
449 if (lttng_ust_get_type_sequence(event_field->type)->encoding == lttng_ust_string_encoding_none)
fd17d7ce 450 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
40003310 451 else
fd17d7ce 452 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
40003310 453 break;
a084756d 454 case lttng_ust_type_float:
fd17d7ce 455 list_entry->field.type = LTTNG_UST_ABI_FIELD_FLOAT;
40003310 456 break;
a084756d 457 case lttng_ust_type_enum:
fd17d7ce 458 list_entry->field.type = LTTNG_UST_ABI_FIELD_ENUM;
40003310
MD
459 break;
460 default:
fd17d7ce 461 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
40003310 462 }
06d4f27e 463 if (!event_desc->loglevel) {
612e9ce4 464 list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
06d4f27e
MD
465 } else {
466 list_entry->field.loglevel = *(*event_desc->loglevel);
467 }
180901e6 468 list_entry->field.nowrite = event_field->nowrite;
06d4f27e
MD
469 }
470 }
471 }
472 if (cds_list_empty(&list->head))
473 list->iter = NULL;
474 else
475 list->iter =
476 cds_list_first_entry(&list->head,
477 struct tp_field_list_entry, head);
478 return 0;
479
480err_nomem:
7dd08bec 481 lttng_probes_prune_field_list(list);
06d4f27e
MD
482 return -ENOMEM;
483}
484
485/*
486 * Return current iteration position, advance internal iterator to next.
487 * Return NULL if end of list.
488 */
fd17d7ce 489struct lttng_ust_abi_field_iter *
06d4f27e
MD
490 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
491{
492 struct tp_field_list_entry *entry;
493
494 if (!list->iter)
495 return NULL;
496 entry = list->iter;
497 if (entry->head.next == &list->head)
498 list->iter = NULL;
499 else
500 list->iter = cds_list_entry(entry->head.next,
501 struct tp_field_list_entry, head);
502 return &entry->field;
503}
This page took 0.065018 seconds and 4 git commands to generate.