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> |
864a1eda | 18 | #include <ust-helper.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" | |
d8d2416d | 24 | #include "ust-events-internal.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 |
5b4c6da4 | 48 | bool check_event_provider(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 | |
dc11f93f | 73 | void lttng_lazy_probe_register(struct lttng_ust_probe_desc *desc) |
ce5aef0b | 74 | { |
dc11f93f | 75 | struct lttng_ust_probe_desc *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) { |
df854e41 MD |
90 | BUG_ON(iter == desc); /* Should never be in the list twice */ |
91 | if (iter < desc) { | |
92 | /* We belong to the location right after iter. */ | |
93 | cds_list_add(&desc->head, &iter->head); | |
94 | goto desc_added; | |
95 | } | |
96 | } | |
97 | /* We should be added at the head of the list */ | |
ac69b35b | 98 | cds_list_add(&desc->head, probe_list); |
df854e41 | 99 | desc_added: |
e81a53af | 100 | DBG("just registered probe %s containing %u events", |
5b4c6da4 | 101 | desc->provider_name, desc->nr_events); |
6715d7d1 MD |
102 | } |
103 | ||
104 | /* | |
105 | * Called under ust lock. | |
106 | */ | |
107 | static | |
108 | void fixup_lazy_probes(void) | |
109 | { | |
dc11f93f | 110 | struct lttng_ust_probe_desc *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 |
dc11f93f | 136 | int check_provider_version(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 | ||
164 | ||
dc11f93f | 165 | int lttng_ust_probe_register(struct lttng_ust_probe_desc *desc) |
6715d7d1 MD |
166 | { |
167 | int ret = 0; | |
168 | ||
c362addf MD |
169 | lttng_ust_fixup_tls(); |
170 | ||
71d31690 MD |
171 | /* |
172 | * If version mismatch, don't register, but don't trigger assert | |
173 | * on caller. The version check just prints an error. | |
174 | */ | |
175 | if (!check_provider_version(desc)) | |
176 | return 0; | |
5b4c6da4 MD |
177 | if (!check_event_provider(desc)) |
178 | return 0; | |
71d31690 | 179 | |
3327ac33 | 180 | ust_lock_nocheck(); |
6715d7d1 | 181 | |
6715d7d1 MD |
182 | cds_list_add(&desc->lazy_init_head, &lazy_probe_init); |
183 | desc->lazy = 1; | |
184 | DBG("adding probe %s containing %u events to lazy registration list", | |
5b4c6da4 | 185 | desc->provider_name, desc->nr_events); |
6715d7d1 MD |
186 | /* |
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. | |
190 | */ | |
191 | if (lttng_session_active()) | |
192 | fixup_lazy_probes(); | |
0fdd0b89 | 193 | |
d8d2416d FD |
194 | lttng_fix_pending_event_notifiers(); |
195 | ||
17dfb34b | 196 | ust_unlock(); |
ce5aef0b MD |
197 | return ret; |
198 | } | |
ce5aef0b | 199 | |
dc11f93f | 200 | void lttng_ust_probe_unregister(struct lttng_ust_probe_desc *desc) |
ce5aef0b | 201 | { |
c362addf MD |
202 | lttng_ust_fixup_tls(); |
203 | ||
71d31690 MD |
204 | if (!check_provider_version(desc)) |
205 | return; | |
206 | ||
3327ac33 | 207 | ust_lock_nocheck(); |
6715d7d1 MD |
208 | if (!desc->lazy) |
209 | cds_list_del(&desc->head); | |
210 | else | |
211 | cds_list_del(&desc->lazy_init_head); | |
2c05c691 FD |
212 | |
213 | lttng_probe_provider_unregister_events(desc); | |
5b4c6da4 | 214 | DBG("just unregistered probes of provider %s", desc->provider_name); |
2c05c691 | 215 | |
17dfb34b | 216 | ust_unlock(); |
ce5aef0b | 217 | } |
ce5aef0b | 218 | |
7dd08bec | 219 | void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 MD |
220 | { |
221 | struct tp_list_entry *list_entry, *tmp; | |
222 | ||
223 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
224 | cds_list_del(&list_entry->head); | |
225 | free(list_entry); | |
226 | } | |
227 | } | |
228 | ||
229 | /* | |
230 | * called with UST lock held. | |
231 | */ | |
7dd08bec | 232 | int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list) |
c8fcf224 | 233 | { |
dc11f93f | 234 | struct lttng_ust_probe_desc *probe_desc; |
c8fcf224 | 235 | int i; |
ac69b35b | 236 | struct cds_list_head *probe_list; |
c8fcf224 | 237 | |
ac69b35b | 238 | probe_list = lttng_get_probe_list_head(); |
c8fcf224 | 239 | CDS_INIT_LIST_HEAD(&list->head); |
ac69b35b | 240 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
c8fcf224 | 241 | for (i = 0; i < probe_desc->nr_events; i++) { |
5b4c6da4 MD |
242 | const struct lttng_ust_event_desc *event_desc = |
243 | probe_desc->event_desc[i]; | |
c8fcf224 MD |
244 | struct tp_list_entry *list_entry; |
245 | ||
5b4c6da4 MD |
246 | /* Skip event if name is too long. */ |
247 | if (!lttng_ust_validate_event_name(event_desc)) | |
248 | continue; | |
c8fcf224 MD |
249 | list_entry = zmalloc(sizeof(*list_entry)); |
250 | if (!list_entry) | |
251 | goto err_nomem; | |
252 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 MD |
253 | lttng_ust_format_event_name(event_desc, list_entry->tp.name); |
254 | if (!event_desc->loglevel) { | |
882a56d7 | 255 | list_entry->tp.loglevel = TRACE_DEFAULT; |
c8fcf224 | 256 | } else { |
5b4c6da4 | 257 | list_entry->tp.loglevel = *(*event_desc->loglevel); |
c8fcf224 MD |
258 | } |
259 | } | |
260 | } | |
261 | if (cds_list_empty(&list->head)) | |
262 | list->iter = NULL; | |
263 | else | |
264 | list->iter = | |
265 | cds_list_first_entry(&list->head, struct tp_list_entry, head); | |
266 | return 0; | |
267 | ||
268 | err_nomem: | |
7dd08bec | 269 | lttng_probes_prune_event_list(list); |
c8fcf224 MD |
270 | return -ENOMEM; |
271 | } | |
272 | ||
273 | /* | |
274 | * Return current iteration position, advance internal iterator to next. | |
275 | * Return NULL if end of list. | |
276 | */ | |
fd17d7ce | 277 | struct lttng_ust_abi_tracepoint_iter * |
c8fcf224 MD |
278 | lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list) |
279 | { | |
280 | struct tp_list_entry *entry; | |
281 | ||
282 | if (!list->iter) | |
283 | return NULL; | |
284 | entry = list->iter; | |
285 | if (entry->head.next == &list->head) | |
286 | list->iter = NULL; | |
287 | else | |
288 | list->iter = cds_list_entry(entry->head.next, | |
289 | struct tp_list_entry, head); | |
290 | return &entry->tp; | |
291 | } | |
1f18504e | 292 | |
7dd08bec | 293 | void lttng_probes_prune_field_list(struct lttng_ust_field_list *list) |
06d4f27e MD |
294 | { |
295 | struct tp_field_list_entry *list_entry, *tmp; | |
296 | ||
297 | cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) { | |
298 | cds_list_del(&list_entry->head); | |
299 | free(list_entry); | |
300 | } | |
301 | } | |
302 | ||
303 | /* | |
304 | * called with UST lock held. | |
305 | */ | |
7dd08bec | 306 | int lttng_probes_get_field_list(struct lttng_ust_field_list *list) |
06d4f27e | 307 | { |
dc11f93f | 308 | struct lttng_ust_probe_desc *probe_desc; |
06d4f27e | 309 | int i; |
ac69b35b | 310 | struct cds_list_head *probe_list; |
06d4f27e | 311 | |
ac69b35b | 312 | probe_list = lttng_get_probe_list_head(); |
06d4f27e | 313 | CDS_INIT_LIST_HEAD(&list->head); |
ac69b35b | 314 | cds_list_for_each_entry(probe_desc, probe_list, head) { |
06d4f27e | 315 | for (i = 0; i < probe_desc->nr_events; i++) { |
dc11f93f | 316 | const struct lttng_ust_event_desc *event_desc = |
06d4f27e MD |
317 | probe_desc->event_desc[i]; |
318 | int j; | |
319 | ||
26329f26 MD |
320 | if (event_desc->nr_fields == 0) { |
321 | /* Events without fields. */ | |
322 | struct tp_field_list_entry *list_entry; | |
323 | ||
5b4c6da4 MD |
324 | /* Skip event if name is too long. */ |
325 | if (!lttng_ust_validate_event_name(event_desc)) | |
326 | continue; | |
26329f26 MD |
327 | list_entry = zmalloc(sizeof(*list_entry)); |
328 | if (!list_entry) | |
329 | goto err_nomem; | |
330 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 | 331 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
26329f26 | 332 | list_entry->field.field_name[0] = '\0'; |
fd17d7ce | 333 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
26329f26 MD |
334 | if (!event_desc->loglevel) { |
335 | list_entry->field.loglevel = TRACE_DEFAULT; | |
336 | } else { | |
337 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
338 | } | |
339 | list_entry->field.nowrite = 1; | |
340 | } | |
341 | ||
06d4f27e | 342 | for (j = 0; j < event_desc->nr_fields; j++) { |
25cff019 MD |
343 | const struct lttng_ust_event_field *event_field = |
344 | event_desc->fields[j]; | |
06d4f27e MD |
345 | struct tp_field_list_entry *list_entry; |
346 | ||
5b4c6da4 MD |
347 | /* Skip event if name is too long. */ |
348 | if (!lttng_ust_validate_event_name(event_desc)) | |
349 | continue; | |
06d4f27e MD |
350 | list_entry = zmalloc(sizeof(*list_entry)); |
351 | if (!list_entry) | |
352 | goto err_nomem; | |
353 | cds_list_add(&list_entry->head, &list->head); | |
5b4c6da4 | 354 | lttng_ust_format_event_name(event_desc, list_entry->field.event_name); |
06d4f27e MD |
355 | strncpy(list_entry->field.field_name, |
356 | event_field->name, | |
fd17d7ce MD |
357 | LTTNG_UST_ABI_SYM_NAME_LEN); |
358 | list_entry->field.field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0'; | |
a084756d MD |
359 | switch (event_field->type->type) { |
360 | case lttng_ust_type_integer: | |
fd17d7ce | 361 | list_entry->field.type = LTTNG_UST_ABI_FIELD_INTEGER; |
40003310 | 362 | break; |
a084756d | 363 | case lttng_ust_type_string: |
fd17d7ce | 364 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 365 | break; |
a084756d MD |
366 | case lttng_ust_type_array: |
367 | if (lttng_ust_get_type_array(event_field->type)->encoding == lttng_ust_string_encoding_none) | |
fd17d7ce | 368 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 369 | else |
fd17d7ce | 370 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 371 | break; |
a084756d MD |
372 | case lttng_ust_type_sequence: |
373 | if (lttng_ust_get_type_sequence(event_field->type)->encoding == lttng_ust_string_encoding_none) | |
fd17d7ce | 374 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 375 | else |
fd17d7ce | 376 | list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING; |
40003310 | 377 | break; |
a084756d | 378 | case lttng_ust_type_float: |
fd17d7ce | 379 | list_entry->field.type = LTTNG_UST_ABI_FIELD_FLOAT; |
40003310 | 380 | break; |
a084756d | 381 | case lttng_ust_type_enum: |
fd17d7ce | 382 | list_entry->field.type = LTTNG_UST_ABI_FIELD_ENUM; |
40003310 MD |
383 | break; |
384 | default: | |
fd17d7ce | 385 | list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER; |
40003310 | 386 | } |
06d4f27e MD |
387 | if (!event_desc->loglevel) { |
388 | list_entry->field.loglevel = TRACE_DEFAULT; | |
389 | } else { | |
390 | list_entry->field.loglevel = *(*event_desc->loglevel); | |
391 | } | |
180901e6 | 392 | list_entry->field.nowrite = event_field->nowrite; |
06d4f27e MD |
393 | } |
394 | } | |
395 | } | |
396 | if (cds_list_empty(&list->head)) | |
397 | list->iter = NULL; | |
398 | else | |
399 | list->iter = | |
400 | cds_list_first_entry(&list->head, | |
401 | struct tp_field_list_entry, head); | |
402 | return 0; | |
403 | ||
404 | err_nomem: | |
7dd08bec | 405 | lttng_probes_prune_field_list(list); |
06d4f27e MD |
406 | return -ENOMEM; |
407 | } | |
408 | ||
409 | /* | |
410 | * Return current iteration position, advance internal iterator to next. | |
411 | * Return NULL if end of list. | |
412 | */ | |
fd17d7ce | 413 | struct lttng_ust_abi_field_iter * |
06d4f27e MD |
414 | lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list) |
415 | { | |
416 | struct tp_field_list_entry *entry; | |
417 | ||
418 | if (!list->iter) | |
419 | return NULL; | |
420 | entry = list->iter; | |
421 | if (entry->head.next == &list->head) | |
422 | list->iter = NULL; | |
423 | else | |
424 | list->iter = cds_list_entry(entry->head.next, | |
425 | struct tp_field_list_entry, head); | |
426 | return &entry->field; | |
427 | } |