Commit | Line | Data |
---|---|---|
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> |
d8de1354 | 16 | #include "tracepoint-internal.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" |
1f18504e MD |
22 | #include "jhash.h" |
23 | #include "error.h" | |
36c52fff | 24 | #include "lib/lttng-ust/events.h" |
8165c8da MD |
25 | |
26 | /* | |
17dfb34b | 27 | * probe list is protected by ust_lock()/ust_unlock(). |
8165c8da | 28 | */ |
ac69b35b | 29 | static CDS_LIST_HEAD(_probe_list); |
e58095ef | 30 | |
6715d7d1 MD |
31 | /* |
32 | * List of probes registered by not yet processed. | |
33 | */ | |
34 | static CDS_LIST_HEAD(lazy_probe_init); | |
df854e41 | 35 | |
6715d7d1 MD |
36 | /* |
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 | |
39 | * mutex. | |
40 | */ | |
41 | static int lazy_nesting; | |
df854e41 | 42 | |
6715d7d1 | 43 | /* |
5b4c6da4 MD |
44 | * Validate that each event within the probe provider refers to the |
45 | * right probe, and that the resulting name is not too long. | |
6715d7d1 | 46 | */ |
ce5aef0b | 47 | static |
4e48b5d2 | 48 | bool check_event_provider(const struct lttng_ust_probe_desc *probe_desc) |
ce5aef0b | 49 | { |
ce5aef0b | 50 | int i; |
5b4c6da4 MD |
51 | |
52 | for (i = 0; i < probe_desc->nr_events; i++) { | |
53 | const struct lttng_ust_event_desc *event_desc = probe_desc->event_desc[i]; | |
54 | ||
55 | if (event_desc->probe_desc != probe_desc) { | |
56 | ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.", | |
57 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); | |
58 | return false; /* provider mismatch */ | |
59 | } | |
60 | if (!lttng_ust_validate_event_name(event_desc)) { | |
61 | ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.", | |
62 | probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name); | |
63 | return false; /* provider mismatch */ | |
64 | } | |
ce5aef0b | 65 | } |
5b4c6da4 | 66 | return true; |
ce5aef0b MD |
67 | } |
68 | ||
6715d7d1 MD |
69 | /* |
70 | * Called under ust lock. | |
71 | */ | |
72 | static | |
4e48b5d2 | 73 | void lttng_lazy_probe_register(struct lttng_ust_registered_probe *reg_probe) |
ce5aef0b | 74 | { |
4e48b5d2 | 75 | struct lttng_ust_registered_probe *iter; |
ac69b35b | 76 | struct cds_list_head *probe_list; |
48205d60 | 77 | |
48205d60 MD |
78 | /* |
79 | * The provider ensures there are no duplicate event names. | |
80 | * Duplicated TRACEPOINT_EVENT event names would generate a | |
81 | * compile-time error due to duplicated symbol names. | |
ce5aef0b | 82 | */ |
df854e41 MD |
83 | |
84 | /* | |
dc11f93f | 85 | * We sort the providers by struct lttng_ust_probe_desc pointer |
df854e41 MD |
86 | * address. |
87 | */ | |
6715d7d1 | 88 | probe_list = &_probe_list; |
ac69b35b | 89 | cds_list_for_each_entry_reverse(iter, probe_list, head) { |
4e48b5d2 MD |
90 | BUG_ON(iter == reg_probe); /* Should never be in the list twice */ |
91 | if (iter < reg_probe) { | |
df854e41 | 92 | /* We belong to the location right after iter. */ |
4e48b5d2 MD |
93 | cds_list_add(®_probe->head, &iter->head); |
94 | goto probe_added; | |
df854e41 MD |
95 | } |
96 | } | |
97 | /* We should be added at the head of the list */ | |
4e48b5d2 MD |
98 | cds_list_add(®_probe->head, probe_list); |
99 | probe_added: | |
e81a53af | 100 | DBG("just registered probe %s containing %u events", |
4e48b5d2 | 101 | reg_probe->desc->provider_name, reg_probe->desc->nr_events); |
6715d7d1 MD |
102 | } |
103 | ||
104 | /* | |
105 | * Called under ust lock. | |
106 | */ | |
107 | static | |
108 | void fixup_lazy_probes(void) | |
109 | { | |
4e48b5d2 | 110 | struct lttng_ust_registered_probe *iter, *tmp; |
5f733922 | 111 | int ret; |
6715d7d1 MD |
112 | |
113 | lazy_nesting++; | |
114 | cds_list_for_each_entry_safe(iter, tmp, | |
115 | &lazy_probe_init, lazy_init_head) { | |
116 | lttng_lazy_probe_register(iter); | |
117 | iter->lazy = 0; | |
118 | cds_list_del(&iter->lazy_init_head); | |
119 | } | |
5f733922 MD |
120 | ret = lttng_fix_pending_events(); |
121 | assert(!ret); | |
6715d7d1 MD |
122 | lazy_nesting--; |
123 | } | |
124 | ||
125 | /* | |
126 | * Called under ust lock. | |
127 | */ | |
128 | struct cds_list_head *lttng_get_probe_list_head(void) | |
129 | { | |
130 | if (!lazy_nesting && !cds_list_empty(&lazy_probe_init)) | |
131 | fixup_lazy_probes(); | |
132 | return &_probe_list; | |
133 | } | |
134 | ||
71d31690 | 135 | static |
4e48b5d2 | 136 | int check_provider_version(const struct lttng_ust_probe_desc *desc) |
71d31690 MD |
137 | { |
138 | /* | |
139 | * Check tracepoint provider version compatibility. | |
140 | */ | |
141 | if (desc->major <= LTTNG_UST_PROVIDER_MAJOR) { | |
142 | DBG("Provider \"%s\" accepted, version %u.%u is compatible " | |
143 | "with LTTng UST provider version %u.%u.", | |
5b4c6da4 | 144 | desc->provider_name, desc->major, desc->minor, |
71d31690 MD |
145 | LTTNG_UST_PROVIDER_MAJOR, |
146 | LTTNG_UST_PROVIDER_MINOR); | |
147 | if (desc->major < LTTNG_UST_PROVIDER_MAJOR) { | |
148 | DBG("However, some LTTng UST features might not be " | |
149 | "available for this provider unless it is " | |
150 | "recompiled against a more recent LTTng UST."); | |
151 | } | |
152 | return 1; /* accept */ | |
153 | } else { | |
154 | ERR("Provider \"%s\" rejected, version %u.%u is incompatible " | |
155 | "with LTTng UST provider version %u.%u. Please upgrade " | |
156 | "LTTng UST.", | |
5b4c6da4 | 157 | desc->provider_name, desc->major, desc->minor, |
71d31690 MD |
158 | LTTNG_UST_PROVIDER_MAJOR, |
159 | LTTNG_UST_PROVIDER_MINOR); | |
160 | return 0; /* reject */ | |
161 | } | |
162 | } | |
163 | ||
4e48b5d2 | 164 | struct lttng_ust_registered_probe *lttng_ust_probe_register(const struct lttng_ust_probe_desc *desc) |
6715d7d1 | 165 | { |
4e48b5d2 | 166 | struct lttng_ust_registered_probe *reg_probe = NULL; |
6715d7d1 | 167 | |
c362addf MD |
168 | lttng_ust_fixup_tls(); |
169 | ||
71d31690 MD |
170 | /* |
171 | * If version mismatch, don't register, but don't trigger assert | |
172 | * on caller. The version check just prints an error. | |
173 | */ | |
174 | if (!check_provider_version(desc)) | |
4e48b5d2 | 175 | return NULL; |
5b4c6da4 | 176 | if (!check_event_provider(desc)) |
4e48b5d2 | 177 | return NULL; |
71d31690 | 178 | |
3327ac33 | 179 | ust_lock_nocheck(); |
6715d7d1 | 180 | |
4e48b5d2 MD |
181 | reg_probe = zmalloc(sizeof(struct lttng_ust_registered_probe)); |
182 | if (!reg_probe) | |
183 | goto end; | |
184 | reg_probe->desc = desc; | |
185 | cds_list_add(®_probe->lazy_init_head, &lazy_probe_init); | |
186 | reg_probe->lazy = 1; | |
187 | ||
6715d7d1 | 188 | DBG("adding probe %s containing %u events to lazy registration list", |
5b4c6da4 | 189 | desc->provider_name, desc->nr_events); |
6715d7d1 MD |
190 | /* |
191 | * If there is at least one active session, we need to register | |
192 | * the probe immediately, since we cannot delay event | |
193 | * registration because they are needed ASAP. | |
194 | */ | |
195 | if (lttng_session_active()) | |
196 | fixup_lazy_probes(); | |
0fdd0b89 | 197 | |
d8d2416d | 198 | lttng_fix_pending_event_notifiers(); |
4e48b5d2 | 199 | end: |
17dfb34b | 200 | ust_unlock(); |
4e48b5d2 | 201 | return reg_probe; |
ce5aef0b | 202 | } |
ce5aef0b | 203 | |
4e48b5d2 | 204 | void lttng_ust_probe_unregister(struct lttng_ust_registered_probe *reg_probe) |
ce5aef0b | 205 | { |
c362addf MD |
206 | lttng_ust_fixup_tls(); |
207 | ||
4e48b5d2 MD |
208 | if (!reg_probe) |
209 | return; | |
210 | if (!check_provider_version(reg_probe->desc)) | |
71d31690 MD |
211 | return; |
212 | ||
3327ac33 | 213 | ust_lock_nocheck(); |
4e48b5d2 MD |
214 | if (!reg_probe->lazy) |
215 | cds_list_del(®_probe->head); | |
6715d7d1 | 216 | else |
4e48b5d2 | 217 | cds_list_del(®_probe->lazy_init_head); |
2c05c691 | 218 | |
4e48b5d2 MD |
219 | lttng_probe_provider_unregister_events(reg_probe->desc); |
220 | DBG("just unregistered probes of provider %s", reg_probe->desc->provider_name); | |
17dfb34b | 221 | ust_unlock(); |
4e48b5d2 | 222 | free(reg_probe); |
ce5aef0b | 223 | } |
ce5aef0b | 224 | |
7dd08bec | 225 | void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 MD |
226 | { |
227 | struct tp_list_entry *list_entry, *tmp; | |
228 | ||
229 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
230 | cds_list_del(&list_entry->head); | |
231 | free(list_entry); | |
232 | } | |
233 | } | |
234 | ||
235 | /* | |
236 | * called with UST lock held. | |
237 | */ | |
7dd08bec | 238 | int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 | 239 | { |
4e48b5d2 | 240 | struct lttng_ust_registered_probe *reg_probe; |
ac69b35b | 241 | struct cds_list_head *probe_list; |
4e48b5d2 | 242 | int i; |
c8fcf224 | 243 | |
ac69b35b | 244 | probe_list = lttng_get_probe_list_head(); |
c8fcf224 | 245 | CDS_INIT_LIST_HEAD(&list->head); |
4e48b5d2 MD |
246 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
247 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; | |
248 | ||
c8fcf224 | 249 | for (i = 0; i < probe_desc->nr_events; i++) { |
5b4c6da4 MD |
250 | const struct lttng_ust_event_desc *event_desc = |
251 | probe_desc->event_desc[i]; | |
c8fcf224 MD |
252 | struct tp_list_entry *list_entry; |
253 | ||
5b4c6da4 MD |
254 | /* Skip event if name is too long. */ |
255 | if (!lttng_ust_validate_event_name(event_desc)) | |
256 | continue; | |
c8fcf224 MD |
257 | list_entry = zmalloc(sizeof(*list_entry)); |
258 | if (!list_entry) | |
259 | goto err_nomem; | |
260 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 MD |
261 | lttng_ust_format_event_name(event_desc, list_entry->tp.name); |
262 | if (!event_desc->loglevel) { | |
882a56d7 | 263 | list_entry->tp.loglevel = TRACE_DEFAULT; |
c8fcf224 | 264 | } else { |
5b4c6da4 | 265 | list_entry->tp.loglevel = *(*event_desc->loglevel); |
c8fcf224 MD |
266 | } |
267 | } | |
268 | } | |
269 | if (cds_list_empty(&list->head)) | |
270 | list->iter = NULL; | |
271 | else | |
272 | list->iter = | |
273 | cds_list_first_entry(&list->head, struct tp_list_entry, head); | |
274 | return 0; | |
275 | ||
276 | err_nomem: | |
7dd08bec | 277 | lttng_probes_prune_event_list(list); |
c8fcf224 MD |
278 | return -ENOMEM; |
279 | } | |
280 | ||
281 | /* | |
282 | * Return current iteration position, advance internal iterator to next. | |
283 | * Return NULL if end of list. | |
284 | */ | |
fd17d7ce | 285 | struct lttng_ust_abi_tracepoint_iter * |
c8fcf224 MD |
286 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) |
287 | { | |
288 | struct tp_list_entry *entry; | |
289 | ||
290 | if (!list->iter) | |
291 | return NULL; | |
292 | entry = list->iter; | |
293 | if (entry->head.next == &list->head) | |
294 | list->iter = NULL; | |
295 | else | |
296 | list->iter = cds_list_entry(entry->head.next, | |
297 | struct tp_list_entry, head); | |
298 | return &entry->tp; | |
299 | } | |
1f18504e | 300 | |
7dd08bec | 301 | void lttng_probes_prune_field_list(struct lttng_ust_field_list *list) |
06d4f27e MD |
302 | { |
303 | struct tp_field_list_entry *list_entry, *tmp; | |
304 | ||
305 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
306 | cds_list_del(&list_entry->head); | |
307 | free(list_entry); | |
308 | } | |
309 | } | |
310 | ||
311 | /* | |
312 | * called with UST lock held. | |
313 | */ | |
7dd08bec | 314 | int lttng_probes_get_field_list(struct lttng_ust_field_list *list) |
06d4f27e | 315 | { |
4e48b5d2 | 316 | struct lttng_ust_registered_probe *reg_probe; |
ac69b35b | 317 | struct cds_list_head *probe_list; |
4e48b5d2 | 318 | int i; |
06d4f27e | 319 | |
ac69b35b | 320 | probe_list = lttng_get_probe_list_head(); |
06d4f27e | 321 | CDS_INIT_LIST_HEAD(&list->head); |
4e48b5d2 MD |
322 | cds_list_for_each_entry(reg_probe, probe_list, head) { |
323 | const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc; | |
324 | ||
06d4f27e | 325 | for (i = 0; i < probe_desc->nr_events; i++) { |
dc11f93f | 326 | const struct lttng_ust_event_desc *event_desc = |
06d4f27e MD |
327 | probe_desc->event_desc[i]; |
328 | int j; | |
329 | ||
26329f26 MD |
330 | if (event_desc->nr_fields == 0) { |
331 | /* Events without fields. */ | |
332 | struct tp_field_list_entry *list_entry; | |
333 | ||
5b4c6da4 MD |
334 | /* Skip event if name is too long. */ |
335 | if (!lttng_ust_validate_event_name(event_desc)) | |
336 | continue; | |
26329f26 MD |
337 | list_entry = zmalloc(sizeof(*list_entry)); |
338 | if (!list_entry) | |
339 | goto err_nomem; | |
340 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 | 341 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
26329f26 | 342 | list_entry->field.field_name[0] = '\0'; |
fd17d7ce | 343 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
26329f26 MD |
344 | if (!event_desc->loglevel) { |
345 | list_entry->field.loglevel = TRACE_DEFAULT; | |
346 | } else { | |
347 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
348 | } | |
349 | list_entry->field.nowrite = 1; | |
350 | } | |
351 | ||
06d4f27e | 352 | for (j = 0; j < event_desc->nr_fields; j++) { |
25cff019 MD |
353 | const struct lttng_ust_event_field *event_field = |
354 | event_desc->fields[j]; | |
06d4f27e MD |
355 | struct tp_field_list_entry *list_entry; |
356 | ||
5b4c6da4 MD |
357 | /* Skip event if name is too long. */ |
358 | if (!lttng_ust_validate_event_name(event_desc)) | |
359 | continue; | |
06d4f27e MD |
360 | list_entry = zmalloc(sizeof(*list_entry)); |
361 | if (!list_entry) | |
362 | goto err_nomem; | |
363 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 | 364 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
06d4f27e MD |
365 | strncpy(list_entry->field.field_name, |
366 | event_field->name, | |
fd17d7ce MD |
367 | LTTNG_UST_ABI_SYM_NAME_LEN); |
368 | list_entry->field.field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; | |
a084756d MD |
369 | switch (event_field->type->type) { |
370 | case lttng_ust_type_integer: | |
fd17d7ce | 371 | list_entry->field.type = LTTNG_UST_ABI_FIELD_INTEGER; |
40003310 | 372 | break; |
a084756d | 373 | case lttng_ust_type_string: |
fd17d7ce | 374 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 375 | break; |
a084756d MD |
376 | case lttng_ust_type_array: |
377 | if (lttng_ust_get_type_array(event_field->type)->encoding == lttng_ust_string_encoding_none) | |
fd17d7ce | 378 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 379 | else |
fd17d7ce | 380 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 381 | break; |
a084756d MD |
382 | case lttng_ust_type_sequence: |
383 | if (lttng_ust_get_type_sequence(event_field->type)->encoding == lttng_ust_string_encoding_none) | |
fd17d7ce | 384 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 385 | else |
fd17d7ce | 386 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 387 | break; |
a084756d | 388 | case lttng_ust_type_float: |
fd17d7ce | 389 | list_entry->field.type = LTTNG_UST_ABI_FIELD_FLOAT; |
40003310 | 390 | break; |
a084756d | 391 | case lttng_ust_type_enum: |
fd17d7ce | 392 | list_entry->field.type = LTTNG_UST_ABI_FIELD_ENUM; |
40003310 MD |
393 | break; |
394 | default: | |
fd17d7ce | 395 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 396 | } |
06d4f27e MD |
397 | if (!event_desc->loglevel) { |
398 | list_entry->field.loglevel = TRACE_DEFAULT; | |
399 | } else { | |
400 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
401 | } | |
180901e6 | 402 | list_entry->field.nowrite = event_field->nowrite; |
06d4f27e MD |
403 | } |
404 | } | |
405 | } | |
406 | if (cds_list_empty(&list->head)) | |
407 | list->iter = NULL; | |
408 | else | |
409 | list->iter = | |
410 | cds_list_first_entry(&list->head, | |
411 | struct tp_field_list_entry, head); | |
412 | return 0; | |
413 | ||
414 | err_nomem: | |
7dd08bec | 415 | lttng_probes_prune_field_list(list); |
06d4f27e MD |
416 | return -ENOMEM; |
417 | } | |
418 | ||
419 | /* | |
420 | * Return current iteration position, advance internal iterator to next. | |
421 | * Return NULL if end of list. | |
422 | */ | |
fd17d7ce | 423 | struct lttng_ust_abi_field_iter * |
06d4f27e MD |
424 | lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list) |
425 | { | |
426 | struct tp_field_list_entry *entry; | |
427 | ||
428 | if (!list->iter) | |
429 | return NULL; | |
430 | entry = list->iter; | |
431 | if (entry->head.next == &list->head) | |
432 | list->iter = NULL; | |
433 | else | |
434 | list->iter = cds_list_entry(entry->head.next, | |
435 | struct tp_field_list_entry, head); | |
436 | return &entry->field; | |
437 | } |