8809dac7929b85003398c9d747824951808e8c08
[lttng-ust.git] / src / lib / lttng-ust / lttng-probes.c
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright 2010-2012 (C) Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng probes registry.
7 */
8
9 #define _LGPL_SOURCE
10 #include <string.h>
11 #include <errno.h>
12 #include <urcu/list.h>
13 #include <urcu/hlist.h>
14 #include <lttng/ust-events.h>
15 #include <lttng/tracepoint.h>
16 #include "common/tracepoint.h"
17 #include <assert.h>
18 #include "common/macros.h"
19 #include <ctype.h>
20
21 #include "lttng-tracer-core.h"
22 #include "common/jhash.h"
23 #include "lib/lttng-ust/events.h"
24
25 /*
26 * probe list is protected by ust_lock()/ust_unlock().
27 */
28 static CDS_LIST_HEAD(_probe_list);
29
30 /*
31 * List of probes registered by not yet processed.
32 */
33 static CDS_LIST_HEAD(lazy_probe_init);
34
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 */
40 static int lazy_nesting;
41
42 static
43 int 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
72 /*
73 * Validate that each event within the probe provider refers to the
74 * right probe, that the resulting name is not too long, and that the
75 * event class belongs to a provider with compatible version.
76 */
77 static
78 bool check_event_provider(const struct lttng_ust_probe_desc *probe_desc)
79 {
80 int i;
81
82 for (i = 0; i < probe_desc->nr_events; i++) {
83 const struct lttng_ust_event_desc *event_desc = probe_desc->event_desc[i];
84
85 if (event_desc->probe_desc != probe_desc) {
86 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to the wrong provider descriptor.",
87 probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name);
88 return false; /* provider mismatch */
89 }
90 if (!lttng_ust_validate_event_name(event_desc)) {
91 ERR("Error registering probe provider '%s'. Event '%s:%s' name is too long.",
92 probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name);
93 return false; /* provider mismatch */
94 }
95 if (!check_provider_version(event_desc->tp_class->probe_desc)) {
96 ERR("Error registering probe provider '%s'. Event '%s:%s' refers to an event class in a provider with incompatible version.",
97 probe_desc->provider_name, probe_desc->provider_name, event_desc->event_name);
98 return false;
99 }
100 }
101 return true;
102 }
103
104 /*
105 * Called under ust lock.
106 */
107 static
108 void lttng_lazy_probe_register(struct lttng_ust_registered_probe *reg_probe)
109 {
110 struct lttng_ust_registered_probe *iter;
111 struct cds_list_head *probe_list;
112
113 /*
114 * The provider ensures there are no duplicate event names.
115 * Duplicated LTTNG_UST_TRACEPOINT_EVENT event names would generate a
116 * compile-time error due to duplicated symbol names.
117 */
118
119 /*
120 * We sort the providers by struct lttng_ust_probe_desc pointer
121 * address.
122 */
123 probe_list = &_probe_list;
124 cds_list_for_each_entry_reverse(iter, probe_list, head) {
125 BUG_ON(iter == reg_probe); /* Should never be in the list twice */
126 if (iter < reg_probe) {
127 /* We belong to the location right after iter. */
128 cds_list_add(&reg_probe->head, &iter->head);
129 goto probe_added;
130 }
131 }
132 /* We should be added at the head of the list */
133 cds_list_add(&reg_probe->head, probe_list);
134 probe_added:
135 DBG("just registered probe %s containing %u events",
136 reg_probe->desc->provider_name, reg_probe->desc->nr_events);
137 }
138
139 /*
140 * Called under ust lock.
141 */
142 static
143 void fixup_lazy_probes(void)
144 {
145 struct lttng_ust_registered_probe *iter, *tmp;
146 int ret;
147
148 lazy_nesting++;
149 cds_list_for_each_entry_safe(iter, tmp,
150 &lazy_probe_init, lazy_init_head) {
151 lttng_lazy_probe_register(iter);
152 iter->lazy = 0;
153 cds_list_del(&iter->lazy_init_head);
154 }
155 ret = lttng_fix_pending_events();
156 assert(!ret);
157 lazy_nesting--;
158 }
159
160 /*
161 * Called under ust lock.
162 */
163 struct cds_list_head *lttng_get_probe_list_head(void)
164 {
165 if (!lazy_nesting && !cds_list_empty(&lazy_probe_init))
166 fixup_lazy_probes();
167 return &_probe_list;
168 }
169
170
171 struct lttng_ust_registered_probe *lttng_ust_probe_register(const struct lttng_ust_probe_desc *desc)
172 {
173 struct lttng_ust_registered_probe *reg_probe = NULL;
174
175 lttng_ust_alloc_tls();
176
177 /*
178 * If version mismatch, don't register, but don't trigger assert
179 * on caller. The version check just prints an error.
180 */
181 if (!check_provider_version(desc))
182 return NULL;
183 if (!check_event_provider(desc))
184 return NULL;
185
186 ust_lock_nocheck();
187
188 reg_probe = zmalloc(sizeof(struct lttng_ust_registered_probe));
189 if (!reg_probe)
190 goto end;
191 reg_probe->desc = desc;
192 cds_list_add(&reg_probe->lazy_init_head, &lazy_probe_init);
193 reg_probe->lazy = 1;
194
195 DBG("adding probe %s containing %u events to lazy registration list",
196 desc->provider_name, desc->nr_events);
197 /*
198 * If there is at least one active session, we need to register
199 * the probe immediately, since we cannot delay event
200 * registration because they are needed ASAP.
201 */
202 if (lttng_session_active())
203 fixup_lazy_probes();
204
205 lttng_fix_pending_event_notifiers();
206 end:
207 ust_unlock();
208 return reg_probe;
209 }
210
211 void lttng_ust_probe_unregister(struct lttng_ust_registered_probe *reg_probe)
212 {
213 lttng_ust_alloc_tls();
214
215 if (!reg_probe)
216 return;
217 if (!check_provider_version(reg_probe->desc))
218 return;
219
220 ust_lock_nocheck();
221 if (!reg_probe->lazy)
222 cds_list_del(&reg_probe->head);
223 else
224 cds_list_del(&reg_probe->lazy_init_head);
225
226 lttng_probe_provider_unregister_events(reg_probe->desc);
227 DBG("just unregistered probes of provider %s", reg_probe->desc->provider_name);
228 ust_unlock();
229 free(reg_probe);
230 }
231
232 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
233 {
234 struct tp_list_entry *list_entry, *tmp;
235
236 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
237 cds_list_del(&list_entry->head);
238 free(list_entry);
239 }
240 }
241
242 /*
243 * called with UST lock held.
244 */
245 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
246 {
247 struct lttng_ust_registered_probe *reg_probe;
248 struct cds_list_head *probe_list;
249 int i;
250
251 probe_list = lttng_get_probe_list_head();
252 CDS_INIT_LIST_HEAD(&list->head);
253 cds_list_for_each_entry(reg_probe, probe_list, head) {
254 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
255
256 for (i = 0; i < probe_desc->nr_events; i++) {
257 const struct lttng_ust_event_desc *event_desc =
258 probe_desc->event_desc[i];
259 struct tp_list_entry *list_entry;
260
261 /* Skip event if name is too long. */
262 if (!lttng_ust_validate_event_name(event_desc))
263 continue;
264 list_entry = zmalloc(sizeof(*list_entry));
265 if (!list_entry)
266 goto err_nomem;
267 cds_list_add(&list_entry->head, &list->head);
268 lttng_ust_format_event_name(event_desc, list_entry->tp.name);
269 if (!event_desc->loglevel) {
270 list_entry->tp.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
271 } else {
272 list_entry->tp.loglevel = *(*event_desc->loglevel);
273 }
274 }
275 }
276 if (cds_list_empty(&list->head))
277 list->iter = NULL;
278 else
279 list->iter =
280 cds_list_first_entry(&list->head, struct tp_list_entry, head);
281 return 0;
282
283 err_nomem:
284 lttng_probes_prune_event_list(list);
285 return -ENOMEM;
286 }
287
288 /*
289 * Return current iteration position, advance internal iterator to next.
290 * Return NULL if end of list.
291 */
292 struct lttng_ust_abi_tracepoint_iter *
293 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
294 {
295 struct tp_list_entry *entry;
296
297 if (!list->iter)
298 return NULL;
299 entry = list->iter;
300 if (entry->head.next == &list->head)
301 list->iter = NULL;
302 else
303 list->iter = cds_list_entry(entry->head.next,
304 struct tp_list_entry, head);
305 return &entry->tp;
306 }
307
308 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
309 {
310 struct tp_field_list_entry *list_entry, *tmp;
311
312 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
313 cds_list_del(&list_entry->head);
314 free(list_entry);
315 }
316 }
317
318 /*
319 * called with UST lock held.
320 */
321 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
322 {
323 struct lttng_ust_registered_probe *reg_probe;
324 struct cds_list_head *probe_list;
325 int i;
326
327 probe_list = lttng_get_probe_list_head();
328 CDS_INIT_LIST_HEAD(&list->head);
329 cds_list_for_each_entry(reg_probe, probe_list, head) {
330 const struct lttng_ust_probe_desc *probe_desc = reg_probe->desc;
331
332 for (i = 0; i < probe_desc->nr_events; i++) {
333 const struct lttng_ust_event_desc *event_desc =
334 probe_desc->event_desc[i];
335 int j;
336
337 if (event_desc->tp_class->nr_fields == 0) {
338 /* Events without fields. */
339 struct tp_field_list_entry *list_entry;
340
341 /* Skip event if name is too long. */
342 if (!lttng_ust_validate_event_name(event_desc))
343 continue;
344 list_entry = zmalloc(sizeof(*list_entry));
345 if (!list_entry)
346 goto err_nomem;
347 cds_list_add(&list_entry->head, &list->head);
348 lttng_ust_format_event_name(event_desc, list_entry->field.event_name);
349 list_entry->field.field_name[0] = '\0';
350 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
351 if (!event_desc->loglevel) {
352 list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
353 } else {
354 list_entry->field.loglevel = *(*event_desc->loglevel);
355 }
356 list_entry->field.nowrite = 1;
357 }
358
359 for (j = 0; j < event_desc->tp_class->nr_fields; j++) {
360 const struct lttng_ust_event_field *event_field =
361 event_desc->tp_class->fields[j];
362 struct tp_field_list_entry *list_entry;
363
364 /* Skip event if name is too long. */
365 if (!lttng_ust_validate_event_name(event_desc))
366 continue;
367 list_entry = zmalloc(sizeof(*list_entry));
368 if (!list_entry)
369 goto err_nomem;
370 cds_list_add(&list_entry->head, &list->head);
371 lttng_ust_format_event_name(event_desc, list_entry->field.event_name);
372 strncpy(list_entry->field.field_name,
373 event_field->name,
374 LTTNG_UST_ABI_SYM_NAME_LEN);
375 list_entry->field.field_name[LTTNG_UST_ABI_SYM_NAME_LEN - 1] = '\0';
376 switch (event_field->type->type) {
377 case lttng_ust_type_integer:
378 list_entry->field.type = LTTNG_UST_ABI_FIELD_INTEGER;
379 break;
380 case lttng_ust_type_string:
381 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
382 break;
383 case lttng_ust_type_array:
384 if (lttng_ust_get_type_array(event_field->type)->encoding == lttng_ust_string_encoding_none)
385 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
386 else
387 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
388 break;
389 case lttng_ust_type_sequence:
390 if (lttng_ust_get_type_sequence(event_field->type)->encoding == lttng_ust_string_encoding_none)
391 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
392 else
393 list_entry->field.type = LTTNG_UST_ABI_FIELD_STRING;
394 break;
395 case lttng_ust_type_float:
396 list_entry->field.type = LTTNG_UST_ABI_FIELD_FLOAT;
397 break;
398 case lttng_ust_type_enum:
399 list_entry->field.type = LTTNG_UST_ABI_FIELD_ENUM;
400 break;
401 default:
402 list_entry->field.type = LTTNG_UST_ABI_FIELD_OTHER;
403 }
404 if (!event_desc->loglevel) {
405 list_entry->field.loglevel = LTTNG_UST_TRACEPOINT_LOGLEVEL_DEFAULT;
406 } else {
407 list_entry->field.loglevel = *(*event_desc->loglevel);
408 }
409 list_entry->field.nowrite = event_field->nowrite;
410 }
411 }
412 }
413 if (cds_list_empty(&list->head))
414 list->iter = NULL;
415 else
416 list->iter =
417 cds_list_first_entry(&list->head,
418 struct tp_field_list_entry, head);
419 return 0;
420
421 err_nomem:
422 lttng_probes_prune_field_list(list);
423 return -ENOMEM;
424 }
425
426 /*
427 * Return current iteration position, advance internal iterator to next.
428 * Return NULL if end of list.
429 */
430 struct lttng_ust_abi_field_iter *
431 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
432 {
433 struct tp_field_list_entry *entry;
434
435 if (!list->iter)
436 return NULL;
437 entry = list->iter;
438 if (entry->head.next == &list->head)
439 list->iter = NULL;
440 else
441 list->iter = cds_list_entry(entry->head.next,
442 struct tp_field_list_entry, head);
443 return &entry->field;
444 }
This page took 0.037699 seconds and 3 git commands to generate.