Fix: event-notifier: not propagating error counter indexes
[lttng-ust.git] / liblttng-ust / lttng-probes.c
1 /*
2 * lttng-probes.c
3 *
4 * Holds LTTng probes registry.
5 *
6 * Copyright 2010-2012 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
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.
12 *
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.
17 *
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
21 */
22
23 #define _LGPL_SOURCE
24 #include <string.h>
25 #include <errno.h>
26 #include <urcu/list.h>
27 #include <urcu/hlist.h>
28 #include <lttng/ust-events.h>
29 #include <lttng/tracepoint.h>
30 #include "tracepoint-internal.h"
31 #include <assert.h>
32 #include <helper.h>
33 #include <ctype.h>
34
35 #include "lttng-tracer-core.h"
36 #include "jhash.h"
37 #include "error.h"
38 #include "ust-events-internal.h"
39
40 /*
41 * probe list is protected by ust_lock()/ust_unlock().
42 */
43 static CDS_LIST_HEAD(_probe_list);
44
45 /*
46 * List of probes registered by not yet processed.
47 */
48 static CDS_LIST_HEAD(lazy_probe_init);
49
50 /*
51 * lazy_nesting counter ensures we don't trigger lazy probe registration
52 * fixup while we are performing the fixup. It is protected by the ust
53 * mutex.
54 */
55 static int lazy_nesting;
56
57 /*
58 * Called under ust lock.
59 */
60 static
61 int check_event_provider(struct lttng_probe_desc *desc)
62 {
63 int i;
64 size_t provider_name_len;
65
66 provider_name_len = strnlen(desc->provider,
67 LTTNG_UST_SYM_NAME_LEN - 1);
68 for (i = 0; i < desc->nr_events; i++) {
69 if (strncmp(desc->event_desc[i]->name,
70 desc->provider,
71 provider_name_len))
72 return 0; /* provider mismatch */
73 }
74 return 1;
75 }
76
77 /*
78 * Called under ust lock.
79 */
80 static
81 void lttng_lazy_probe_register(struct lttng_probe_desc *desc)
82 {
83 struct lttng_probe_desc *iter;
84 struct cds_list_head *probe_list;
85
86 /*
87 * Each provider enforce that every event name begins with the
88 * provider name. Check this in an assertion for extra
89 * carefulness. This ensures we cannot have duplicate event
90 * names across providers.
91 */
92 assert(check_event_provider(desc));
93
94 /*
95 * The provider ensures there are no duplicate event names.
96 * Duplicated TRACEPOINT_EVENT event names would generate a
97 * compile-time error due to duplicated symbol names.
98 */
99
100 /*
101 * We sort the providers by struct lttng_probe_desc pointer
102 * address.
103 */
104 probe_list = &_probe_list;
105 cds_list_for_each_entry_reverse(iter, probe_list, head) {
106 BUG_ON(iter == desc); /* Should never be in the list twice */
107 if (iter < desc) {
108 /* We belong to the location right after iter. */
109 cds_list_add(&desc->head, &iter->head);
110 goto desc_added;
111 }
112 }
113 /* We should be added at the head of the list */
114 cds_list_add(&desc->head, probe_list);
115 desc_added:
116 DBG("just registered probe %s containing %u events",
117 desc->provider, desc->nr_events);
118 }
119
120 /*
121 * Called under ust lock.
122 */
123 static
124 void fixup_lazy_probes(void)
125 {
126 struct lttng_probe_desc *iter, *tmp;
127 int ret;
128
129 lazy_nesting++;
130 cds_list_for_each_entry_safe(iter, tmp,
131 &lazy_probe_init, lazy_init_head) {
132 lttng_lazy_probe_register(iter);
133 iter->lazy = 0;
134 cds_list_del(&iter->lazy_init_head);
135 }
136 ret = lttng_fix_pending_events();
137 assert(!ret);
138 lazy_nesting--;
139 }
140
141 /*
142 * Called under ust lock.
143 */
144 struct cds_list_head *lttng_get_probe_list_head(void)
145 {
146 if (!lazy_nesting && !cds_list_empty(&lazy_probe_init))
147 fixup_lazy_probes();
148 return &_probe_list;
149 }
150
151 static
152 int check_provider_version(struct lttng_probe_desc *desc)
153 {
154 /*
155 * Check tracepoint provider version compatibility.
156 */
157 if (desc->major <= LTTNG_UST_PROVIDER_MAJOR) {
158 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
159 "with LTTng UST provider version %u.%u.",
160 desc->provider, desc->major, desc->minor,
161 LTTNG_UST_PROVIDER_MAJOR,
162 LTTNG_UST_PROVIDER_MINOR);
163 if (desc->major < LTTNG_UST_PROVIDER_MAJOR) {
164 DBG("However, some LTTng UST features might not be "
165 "available for this provider unless it is "
166 "recompiled against a more recent LTTng UST.");
167 }
168 return 1; /* accept */
169 } else {
170 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
171 "with LTTng UST provider version %u.%u. Please upgrade "
172 "LTTng UST.",
173 desc->provider, desc->major, desc->minor,
174 LTTNG_UST_PROVIDER_MAJOR,
175 LTTNG_UST_PROVIDER_MINOR);
176 return 0; /* reject */
177 }
178 }
179
180
181 int lttng_probe_register(struct lttng_probe_desc *desc)
182 {
183 int ret = 0;
184
185 lttng_ust_fixup_tls();
186
187 /*
188 * If version mismatch, don't register, but don't trigger assert
189 * on caller. The version check just prints an error.
190 */
191 if (!check_provider_version(desc))
192 return 0;
193
194 ust_lock_nocheck();
195
196 cds_list_add(&desc->lazy_init_head, &lazy_probe_init);
197 desc->lazy = 1;
198 DBG("adding probe %s containing %u events to lazy registration list",
199 desc->provider, desc->nr_events);
200 /*
201 * If there is at least one active session, we need to register
202 * the probe immediately, since we cannot delay event
203 * registration because they are needed ASAP.
204 */
205 if (lttng_session_active())
206 fixup_lazy_probes();
207
208 lttng_fix_pending_event_notifiers();
209
210 ust_unlock();
211 return ret;
212 }
213
214 /* Backward compatibility with UST 2.0 */
215 int ltt_probe_register(struct lttng_probe_desc *desc)
216 {
217 return lttng_probe_register(desc);
218 }
219
220 void lttng_probe_unregister(struct lttng_probe_desc *desc)
221 {
222 lttng_ust_fixup_tls();
223
224 if (!check_provider_version(desc))
225 return;
226
227 ust_lock_nocheck();
228 if (!desc->lazy)
229 cds_list_del(&desc->head);
230 else
231 cds_list_del(&desc->lazy_init_head);
232
233 lttng_probe_provider_unregister_events(desc);
234 DBG("just unregistered probes of provider %s", desc->provider);
235
236 ust_unlock();
237 }
238
239 /* Backward compatibility with UST 2.0 */
240 void ltt_probe_unregister(struct lttng_probe_desc *desc)
241 {
242 lttng_probe_unregister(desc);
243 }
244
245 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
246 {
247 struct tp_list_entry *list_entry, *tmp;
248
249 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
250 cds_list_del(&list_entry->head);
251 free(list_entry);
252 }
253 }
254
255 /*
256 * called with UST lock held.
257 */
258 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
259 {
260 struct lttng_probe_desc *probe_desc;
261 int i;
262 struct cds_list_head *probe_list;
263
264 probe_list = lttng_get_probe_list_head();
265 CDS_INIT_LIST_HEAD(&list->head);
266 cds_list_for_each_entry(probe_desc, probe_list, head) {
267 for (i = 0; i < probe_desc->nr_events; i++) {
268 struct tp_list_entry *list_entry;
269
270 list_entry = zmalloc(sizeof(*list_entry));
271 if (!list_entry)
272 goto err_nomem;
273 cds_list_add(&list_entry->head, &list->head);
274 strncpy(list_entry->tp.name,
275 probe_desc->event_desc[i]->name,
276 LTTNG_UST_SYM_NAME_LEN);
277 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
278 if (!probe_desc->event_desc[i]->loglevel) {
279 list_entry->tp.loglevel = TRACE_DEFAULT;
280 } else {
281 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
282 }
283 }
284 }
285 if (cds_list_empty(&list->head))
286 list->iter = NULL;
287 else
288 list->iter =
289 cds_list_first_entry(&list->head, struct tp_list_entry, head);
290 return 0;
291
292 err_nomem:
293 lttng_probes_prune_event_list(list);
294 return -ENOMEM;
295 }
296
297 /*
298 * Return current iteration position, advance internal iterator to next.
299 * Return NULL if end of list.
300 */
301 struct lttng_ust_tracepoint_iter *
302 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
303 {
304 struct tp_list_entry *entry;
305
306 if (!list->iter)
307 return NULL;
308 entry = list->iter;
309 if (entry->head.next == &list->head)
310 list->iter = NULL;
311 else
312 list->iter = cds_list_entry(entry->head.next,
313 struct tp_list_entry, head);
314 return &entry->tp;
315 }
316
317 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
318 {
319 struct tp_field_list_entry *list_entry, *tmp;
320
321 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
322 cds_list_del(&list_entry->head);
323 free(list_entry);
324 }
325 }
326
327 /*
328 * called with UST lock held.
329 */
330 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
331 {
332 struct lttng_probe_desc *probe_desc;
333 int i;
334 struct cds_list_head *probe_list;
335
336 probe_list = lttng_get_probe_list_head();
337 CDS_INIT_LIST_HEAD(&list->head);
338 cds_list_for_each_entry(probe_desc, probe_list, head) {
339 for (i = 0; i < probe_desc->nr_events; i++) {
340 const struct lttng_event_desc *event_desc =
341 probe_desc->event_desc[i];
342 int j;
343
344 if (event_desc->nr_fields == 0) {
345 /* Events without fields. */
346 struct tp_field_list_entry *list_entry;
347
348 list_entry = zmalloc(sizeof(*list_entry));
349 if (!list_entry)
350 goto err_nomem;
351 cds_list_add(&list_entry->head, &list->head);
352 strncpy(list_entry->field.event_name,
353 event_desc->name,
354 LTTNG_UST_SYM_NAME_LEN);
355 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
356 list_entry->field.field_name[0] = '\0';
357 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
358 if (!event_desc->loglevel) {
359 list_entry->field.loglevel = TRACE_DEFAULT;
360 } else {
361 list_entry->field.loglevel = *(*event_desc->loglevel);
362 }
363 list_entry->field.nowrite = 1;
364 }
365
366 for (j = 0; j < event_desc->nr_fields; j++) {
367 const struct lttng_event_field *event_field =
368 &event_desc->fields[j];
369 struct tp_field_list_entry *list_entry;
370
371 list_entry = zmalloc(sizeof(*list_entry));
372 if (!list_entry)
373 goto err_nomem;
374 cds_list_add(&list_entry->head, &list->head);
375 strncpy(list_entry->field.event_name,
376 event_desc->name,
377 LTTNG_UST_SYM_NAME_LEN);
378 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
379 strncpy(list_entry->field.field_name,
380 event_field->name,
381 LTTNG_UST_SYM_NAME_LEN);
382 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
383 switch (event_field->type.atype) {
384 case atype_integer:
385 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
386 break;
387 case atype_string:
388 list_entry->field.type = LTTNG_UST_FIELD_STRING;
389 break;
390 case atype_array:
391 if (event_field->type.u.legacy.array.elem_type.atype != atype_integer
392 || event_field->type.u.legacy.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
393 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
394 else
395 list_entry->field.type = LTTNG_UST_FIELD_STRING;
396 break;
397 case atype_array_nestable:
398 if (event_field->type.u.array_nestable.elem_type->atype != atype_integer
399 || event_field->type.u.array_nestable.elem_type->u.integer.encoding == lttng_encode_none)
400 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
401 else
402 list_entry->field.type = LTTNG_UST_FIELD_STRING;
403 break;
404 case atype_sequence:
405 if (event_field->type.u.legacy.sequence.elem_type.atype != atype_integer
406 || event_field->type.u.legacy.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
407 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
408 else
409 list_entry->field.type = LTTNG_UST_FIELD_STRING;
410 break;
411 case atype_sequence_nestable:
412 if (event_field->type.u.sequence_nestable.elem_type->atype != atype_integer
413 || event_field->type.u.sequence_nestable.elem_type->u.integer.encoding == lttng_encode_none)
414 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
415 else
416 list_entry->field.type = LTTNG_UST_FIELD_STRING;
417 break;
418 case atype_float:
419 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
420 break;
421 case atype_enum: /* Fall-through */
422 case atype_enum_nestable:
423 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
424 break;
425 default:
426 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
427 }
428 if (!event_desc->loglevel) {
429 list_entry->field.loglevel = TRACE_DEFAULT;
430 } else {
431 list_entry->field.loglevel = *(*event_desc->loglevel);
432 }
433 list_entry->field.nowrite = event_field->nowrite;
434 }
435 }
436 }
437 if (cds_list_empty(&list->head))
438 list->iter = NULL;
439 else
440 list->iter =
441 cds_list_first_entry(&list->head,
442 struct tp_field_list_entry, head);
443 return 0;
444
445 err_nomem:
446 lttng_probes_prune_field_list(list);
447 return -ENOMEM;
448 }
449
450 /*
451 * Return current iteration position, advance internal iterator to next.
452 * Return NULL if end of list.
453 */
454 struct lttng_ust_field_iter *
455 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
456 {
457 struct tp_field_list_entry *entry;
458
459 if (!list->iter)
460 return NULL;
461 entry = list->iter;
462 if (entry->head.next == &list->head)
463 list->iter = NULL;
464 else
465 list->iter = cds_list_entry(entry->head.next,
466 struct tp_field_list_entry, head);
467 return &entry->field;
468 }
This page took 0.038437 seconds and 4 git commands to generate.