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