8fba8adfa376bb90da2d72e68c0037528a05f22f
[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 void lttng_probe_unregister(struct lttng_probe_desc *desc)
215 {
216 lttng_ust_fixup_tls();
217
218 if (!check_provider_version(desc))
219 return;
220
221 ust_lock_nocheck();
222 if (!desc->lazy)
223 cds_list_del(&desc->head);
224 else
225 cds_list_del(&desc->lazy_init_head);
226
227 lttng_probe_provider_unregister_events(desc);
228 DBG("just unregistered probes of provider %s", desc->provider);
229
230 ust_unlock();
231 }
232
233 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
234 {
235 struct tp_list_entry *list_entry, *tmp;
236
237 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
238 cds_list_del(&list_entry->head);
239 free(list_entry);
240 }
241 }
242
243 /*
244 * called with UST lock held.
245 */
246 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
247 {
248 struct lttng_probe_desc *probe_desc;
249 int i;
250 struct cds_list_head *probe_list;
251
252 probe_list = lttng_get_probe_list_head();
253 CDS_INIT_LIST_HEAD(&list->head);
254 cds_list_for_each_entry(probe_desc, probe_list, head) {
255 for (i = 0; i < probe_desc->nr_events; i++) {
256 struct tp_list_entry *list_entry;
257
258 list_entry = zmalloc(sizeof(*list_entry));
259 if (!list_entry)
260 goto err_nomem;
261 cds_list_add(&list_entry->head, &list->head);
262 strncpy(list_entry->tp.name,
263 probe_desc->event_desc[i]->name,
264 LTTNG_UST_SYM_NAME_LEN);
265 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
266 if (!probe_desc->event_desc[i]->loglevel) {
267 list_entry->tp.loglevel = TRACE_DEFAULT;
268 } else {
269 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
270 }
271 }
272 }
273 if (cds_list_empty(&list->head))
274 list->iter = NULL;
275 else
276 list->iter =
277 cds_list_first_entry(&list->head, struct tp_list_entry, head);
278 return 0;
279
280 err_nomem:
281 lttng_probes_prune_event_list(list);
282 return -ENOMEM;
283 }
284
285 /*
286 * Return current iteration position, advance internal iterator to next.
287 * Return NULL if end of list.
288 */
289 struct lttng_ust_tracepoint_iter *
290 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
291 {
292 struct tp_list_entry *entry;
293
294 if (!list->iter)
295 return NULL;
296 entry = list->iter;
297 if (entry->head.next == &list->head)
298 list->iter = NULL;
299 else
300 list->iter = cds_list_entry(entry->head.next,
301 struct tp_list_entry, head);
302 return &entry->tp;
303 }
304
305 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
306 {
307 struct tp_field_list_entry *list_entry, *tmp;
308
309 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
310 cds_list_del(&list_entry->head);
311 free(list_entry);
312 }
313 }
314
315 /*
316 * called with UST lock held.
317 */
318 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
319 {
320 struct lttng_probe_desc *probe_desc;
321 int i;
322 struct cds_list_head *probe_list;
323
324 probe_list = lttng_get_probe_list_head();
325 CDS_INIT_LIST_HEAD(&list->head);
326 cds_list_for_each_entry(probe_desc, probe_list, head) {
327 for (i = 0; i < probe_desc->nr_events; i++) {
328 const struct lttng_event_desc *event_desc =
329 probe_desc->event_desc[i];
330 int j;
331
332 if (event_desc->nr_fields == 0) {
333 /* Events without fields. */
334 struct tp_field_list_entry *list_entry;
335
336 list_entry = zmalloc(sizeof(*list_entry));
337 if (!list_entry)
338 goto err_nomem;
339 cds_list_add(&list_entry->head, &list->head);
340 strncpy(list_entry->field.event_name,
341 event_desc->name,
342 LTTNG_UST_SYM_NAME_LEN);
343 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
344 list_entry->field.field_name[0] = '\0';
345 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
346 if (!event_desc->loglevel) {
347 list_entry->field.loglevel = TRACE_DEFAULT;
348 } else {
349 list_entry->field.loglevel = *(*event_desc->loglevel);
350 }
351 list_entry->field.nowrite = 1;
352 }
353
354 for (j = 0; j < event_desc->nr_fields; j++) {
355 const struct lttng_event_field *event_field =
356 &event_desc->fields[j];
357 struct tp_field_list_entry *list_entry;
358
359 list_entry = zmalloc(sizeof(*list_entry));
360 if (!list_entry)
361 goto err_nomem;
362 cds_list_add(&list_entry->head, &list->head);
363 strncpy(list_entry->field.event_name,
364 event_desc->name,
365 LTTNG_UST_SYM_NAME_LEN);
366 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
367 strncpy(list_entry->field.field_name,
368 event_field->name,
369 LTTNG_UST_SYM_NAME_LEN);
370 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
371 switch (event_field->type.atype) {
372 case atype_integer:
373 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
374 break;
375 case atype_string:
376 list_entry->field.type = LTTNG_UST_FIELD_STRING;
377 break;
378 case atype_array:
379 if (event_field->type.u.legacy.array.elem_type.atype != atype_integer
380 || event_field->type.u.legacy.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
381 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
382 else
383 list_entry->field.type = LTTNG_UST_FIELD_STRING;
384 break;
385 case atype_array_nestable:
386 if (event_field->type.u.array_nestable.elem_type->atype != atype_integer
387 || event_field->type.u.array_nestable.elem_type->u.integer.encoding == lttng_encode_none)
388 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
389 else
390 list_entry->field.type = LTTNG_UST_FIELD_STRING;
391 break;
392 case atype_sequence:
393 if (event_field->type.u.legacy.sequence.elem_type.atype != atype_integer
394 || event_field->type.u.legacy.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
395 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
396 else
397 list_entry->field.type = LTTNG_UST_FIELD_STRING;
398 break;
399 case atype_sequence_nestable:
400 if (event_field->type.u.sequence_nestable.elem_type->atype != atype_integer
401 || event_field->type.u.sequence_nestable.elem_type->u.integer.encoding == lttng_encode_none)
402 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
403 else
404 list_entry->field.type = LTTNG_UST_FIELD_STRING;
405 break;
406 case atype_float:
407 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
408 break;
409 case atype_enum: /* Fall-through */
410 case atype_enum_nestable:
411 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
412 break;
413 default:
414 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
415 }
416 if (!event_desc->loglevel) {
417 list_entry->field.loglevel = TRACE_DEFAULT;
418 } else {
419 list_entry->field.loglevel = *(*event_desc->loglevel);
420 }
421 list_entry->field.nowrite = event_field->nowrite;
422 }
423 }
424 }
425 if (cds_list_empty(&list->head))
426 list->iter = NULL;
427 else
428 list->iter =
429 cds_list_first_entry(&list->head,
430 struct tp_field_list_entry, head);
431 return 0;
432
433 err_nomem:
434 lttng_probes_prune_field_list(list);
435 return -ENOMEM;
436 }
437
438 /*
439 * Return current iteration position, advance internal iterator to next.
440 * Return NULL if end of list.
441 */
442 struct lttng_ust_field_iter *
443 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
444 {
445 struct tp_field_list_entry *entry;
446
447 if (!list->iter)
448 return NULL;
449 entry = list->iter;
450 if (entry->head.next == &list->head)
451 list->iter = NULL;
452 else
453 list->iter = cds_list_entry(entry->head.next,
454 struct tp_field_list_entry, head);
455 return &entry->field;
456 }
This page took 0.037293 seconds and 3 git commands to generate.