Fix: event-notifier: not propagating error counter indexes
[lttng-ust.git] / liblttng-ust / lttng-probes.c
CommitLineData
ce5aef0b 1/*
7dd08bec 2 * lttng-probes.c
ce5aef0b 3 *
ce5aef0b
MD
4 * Holds LTTng probes registry.
5 *
e92f3e28
MD
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
ce5aef0b
MD
21 */
22
3fbec7dc 23#define _LGPL_SOURCE
8d8a24c8
MD
24#include <string.h>
25#include <errno.h>
26#include <urcu/list.h>
1f18504e 27#include <urcu/hlist.h>
4318ae1b 28#include <lttng/ust-events.h>
902e1379 29#include <lttng/tracepoint.h>
d8de1354 30#include "tracepoint-internal.h"
a3bb4b27 31#include <assert.h>
c8fcf224 32#include <helper.h>
48740cab 33#include <ctype.h>
ce5aef0b 34
7dd08bec 35#include "lttng-tracer-core.h"
1f18504e
MD
36#include "jhash.h"
37#include "error.h"
d8d2416d 38#include "ust-events-internal.h"
8165c8da
MD
39
40/*
17dfb34b 41 * probe list is protected by ust_lock()/ust_unlock().
8165c8da 42 */
ac69b35b 43static CDS_LIST_HEAD(_probe_list);
e58095ef 44
6715d7d1
MD
45/*
46 * List of probes registered by not yet processed.
47 */
48static CDS_LIST_HEAD(lazy_probe_init);
df854e41 49
6715d7d1
MD
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 */
55static int lazy_nesting;
df854e41 56
6715d7d1
MD
57/*
58 * Called under ust lock.
59 */
ce5aef0b 60static
48205d60 61int check_event_provider(struct lttng_probe_desc *desc)
ce5aef0b 62{
ce5aef0b 63 int i;
48205d60 64 size_t provider_name_len;
ce5aef0b 65
48205d60
MD
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 */
ce5aef0b 73 }
48205d60 74 return 1;
ce5aef0b
MD
75}
76
6715d7d1
MD
77/*
78 * Called under ust lock.
79 */
80static
81void lttng_lazy_probe_register(struct lttng_probe_desc *desc)
ce5aef0b 82{
df854e41 83 struct lttng_probe_desc *iter;
ac69b35b 84 struct cds_list_head *probe_list;
48205d60 85
ce5aef0b 86 /*
48205d60
MD
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.
ce5aef0b 98 */
df854e41
MD
99
100 /*
101 * We sort the providers by struct lttng_probe_desc pointer
102 * address.
103 */
6715d7d1 104 probe_list = &_probe_list;
ac69b35b 105 cds_list_for_each_entry_reverse(iter, probe_list, head) {
df854e41
MD
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 */
ac69b35b 114 cds_list_add(&desc->head, probe_list);
df854e41 115desc_added:
e81a53af
MD
116 DBG("just registered probe %s containing %u events",
117 desc->provider, desc->nr_events);
6715d7d1
MD
118}
119
120/*
121 * Called under ust lock.
122 */
123static
124void fixup_lazy_probes(void)
125{
126 struct lttng_probe_desc *iter, *tmp;
5f733922 127 int ret;
6715d7d1
MD
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 }
5f733922
MD
136 ret = lttng_fix_pending_events();
137 assert(!ret);
6715d7d1
MD
138 lazy_nesting--;
139}
140
141/*
142 * Called under ust lock.
143 */
144struct 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
71d31690
MD
151static
152int 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
6715d7d1
MD
181int lttng_probe_register(struct lttng_probe_desc *desc)
182{
183 int ret = 0;
184
c362addf
MD
185 lttng_ust_fixup_tls();
186
71d31690
MD
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
3327ac33 194 ust_lock_nocheck();
6715d7d1 195
6715d7d1
MD
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();
0fdd0b89 207
d8d2416d
FD
208 lttng_fix_pending_event_notifiers();
209
17dfb34b 210 ust_unlock();
ce5aef0b
MD
211 return ret;
212}
ce5aef0b 213
7dd08bec
MD
214/* Backward compatibility with UST 2.0 */
215int ltt_probe_register(struct lttng_probe_desc *desc)
216{
217 return lttng_probe_register(desc);
218}
219
220void lttng_probe_unregister(struct lttng_probe_desc *desc)
ce5aef0b 221{
c362addf
MD
222 lttng_ust_fixup_tls();
223
71d31690
MD
224 if (!check_provider_version(desc))
225 return;
226
3327ac33 227 ust_lock_nocheck();
6715d7d1
MD
228 if (!desc->lazy)
229 cds_list_del(&desc->head);
230 else
231 cds_list_del(&desc->lazy_init_head);
2c05c691
FD
232
233 lttng_probe_provider_unregister_events(desc);
234 DBG("just unregistered probes of provider %s", desc->provider);
235
17dfb34b 236 ust_unlock();
ce5aef0b 237}
ce5aef0b 238
7dd08bec
MD
239/* Backward compatibility with UST 2.0 */
240void ltt_probe_unregister(struct lttng_probe_desc *desc)
241{
242 lttng_probe_unregister(desc);
243}
244
7dd08bec 245void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
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 */
7dd08bec 258int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
c8fcf224
MD
259{
260 struct lttng_probe_desc *probe_desc;
261 int i;
ac69b35b 262 struct cds_list_head *probe_list;
c8fcf224 263
ac69b35b 264 probe_list = lttng_get_probe_list_head();
c8fcf224 265 CDS_INIT_LIST_HEAD(&list->head);
ac69b35b 266 cds_list_for_each_entry(probe_desc, probe_list, head) {
c8fcf224
MD
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) {
882a56d7 279 list_entry->tp.loglevel = TRACE_DEFAULT;
c8fcf224 280 } else {
882a56d7 281 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
c8fcf224
MD
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
292err_nomem:
7dd08bec 293 lttng_probes_prune_event_list(list);
c8fcf224
MD
294 return -ENOMEM;
295}
296
297/*
298 * Return current iteration position, advance internal iterator to next.
299 * Return NULL if end of list.
300 */
301struct 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}
1f18504e 316
7dd08bec 317void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
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 */
7dd08bec 330int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
06d4f27e
MD
331{
332 struct lttng_probe_desc *probe_desc;
333 int i;
ac69b35b 334 struct cds_list_head *probe_list;
06d4f27e 335
ac69b35b 336 probe_list = lttng_get_probe_list_head();
06d4f27e 337 CDS_INIT_LIST_HEAD(&list->head);
ac69b35b 338 cds_list_for_each_entry(probe_desc, probe_list, head) {
06d4f27e
MD
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
26329f26
MD
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
06d4f27e
MD
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';
40003310
MD
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:
218deb69
MD
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)
40003310
MD
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:
218deb69
MD
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)
40003310
MD
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;
218deb69
MD
421 case atype_enum: /* Fall-through */
422 case atype_enum_nestable:
40003310
MD
423 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
424 break;
425 default:
426 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
427 }
06d4f27e
MD
428 if (!event_desc->loglevel) {
429 list_entry->field.loglevel = TRACE_DEFAULT;
430 } else {
431 list_entry->field.loglevel = *(*event_desc->loglevel);
432 }
180901e6 433 list_entry->field.nowrite = event_field->nowrite;
06d4f27e
MD
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
445err_nomem:
7dd08bec 446 lttng_probes_prune_field_list(list);
06d4f27e
MD
447 return -ENOMEM;
448}
449
450/*
451 * Return current iteration position, advance internal iterator to next.
452 * Return NULL if end of list.
453 */
454struct 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.054589 seconds and 4 git commands to generate.