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