Fix: tracepoint.h: Disable address sanitizer on pointer array section variables
[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 int check_provider_version(struct lttng_probe_desc *desc)
152 {
153 /*
154 * Check tracepoint provider version compatibility.
155 */
156 if (desc->major <= LTTNG_UST_PROVIDER_MAJOR) {
157 DBG("Provider \"%s\" accepted, version %u.%u is compatible "
158 "with LTTng UST provider version %u.%u.",
159 desc->provider, desc->major, desc->minor,
160 LTTNG_UST_PROVIDER_MAJOR,
161 LTTNG_UST_PROVIDER_MINOR);
162 if (desc->major < LTTNG_UST_PROVIDER_MAJOR) {
163 DBG("However, some LTTng UST features might not be "
164 "available for this provider unless it is "
165 "recompiled against a more recent LTTng UST.");
166 }
167 return 1; /* accept */
168 } else {
169 ERR("Provider \"%s\" rejected, version %u.%u is incompatible "
170 "with LTTng UST provider version %u.%u. Please upgrade "
171 "LTTng UST.",
172 desc->provider, desc->major, desc->minor,
173 LTTNG_UST_PROVIDER_MAJOR,
174 LTTNG_UST_PROVIDER_MINOR);
175 return 0; /* reject */
176 }
177 }
178
179
180 int lttng_probe_register(struct lttng_probe_desc *desc)
181 {
182 int ret = 0;
183
184 lttng_ust_fixup_tls();
185
186 /*
187 * If version mismatch, don't register, but don't trigger assert
188 * on caller. The version check just prints an error.
189 */
190 if (!check_provider_version(desc))
191 return 0;
192
193 ust_lock_nocheck();
194
195 cds_list_add(&desc->lazy_init_head, &lazy_probe_init);
196 desc->lazy = 1;
197 DBG("adding probe %s containing %u events to lazy registration list",
198 desc->provider, desc->nr_events);
199 /*
200 * If there is at least one active session, we need to register
201 * the probe immediately, since we cannot delay event
202 * registration because they are needed ASAP.
203 */
204 if (lttng_session_active())
205 fixup_lazy_probes();
206
207 ust_unlock();
208 return ret;
209 }
210
211 /* Backward compatibility with UST 2.0 */
212 int ltt_probe_register(struct lttng_probe_desc *desc)
213 {
214 return lttng_probe_register(desc);
215 }
216
217 void lttng_probe_unregister(struct lttng_probe_desc *desc)
218 {
219 lttng_ust_fixup_tls();
220
221 if (!check_provider_version(desc))
222 return;
223
224 ust_lock_nocheck();
225 if (!desc->lazy)
226 cds_list_del(&desc->head);
227 else
228 cds_list_del(&desc->lazy_init_head);
229
230 lttng_probe_provider_unregister_events(desc);
231 DBG("just unregistered probes of provider %s", desc->provider);
232
233 ust_unlock();
234 }
235
236 /* Backward compatibility with UST 2.0 */
237 void ltt_probe_unregister(struct lttng_probe_desc *desc)
238 {
239 lttng_probe_unregister(desc);
240 }
241
242 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
243 {
244 struct tp_list_entry *list_entry, *tmp;
245
246 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
247 cds_list_del(&list_entry->head);
248 free(list_entry);
249 }
250 }
251
252 /*
253 * called with UST lock held.
254 */
255 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
256 {
257 struct lttng_probe_desc *probe_desc;
258 int i;
259 struct cds_list_head *probe_list;
260
261 probe_list = lttng_get_probe_list_head();
262 CDS_INIT_LIST_HEAD(&list->head);
263 cds_list_for_each_entry(probe_desc, probe_list, head) {
264 for (i = 0; i < probe_desc->nr_events; i++) {
265 struct tp_list_entry *list_entry;
266
267 list_entry = zmalloc(sizeof(*list_entry));
268 if (!list_entry)
269 goto err_nomem;
270 cds_list_add(&list_entry->head, &list->head);
271 strncpy(list_entry->tp.name,
272 probe_desc->event_desc[i]->name,
273 LTTNG_UST_SYM_NAME_LEN);
274 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
275 if (!probe_desc->event_desc[i]->loglevel) {
276 list_entry->tp.loglevel = TRACE_DEFAULT;
277 } else {
278 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
279 }
280 }
281 }
282 if (cds_list_empty(&list->head))
283 list->iter = NULL;
284 else
285 list->iter =
286 cds_list_first_entry(&list->head, struct tp_list_entry, head);
287 return 0;
288
289 err_nomem:
290 lttng_probes_prune_event_list(list);
291 return -ENOMEM;
292 }
293
294 /*
295 * Return current iteration position, advance internal iterator to next.
296 * Return NULL if end of list.
297 */
298 struct lttng_ust_tracepoint_iter *
299 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
300 {
301 struct tp_list_entry *entry;
302
303 if (!list->iter)
304 return NULL;
305 entry = list->iter;
306 if (entry->head.next == &list->head)
307 list->iter = NULL;
308 else
309 list->iter = cds_list_entry(entry->head.next,
310 struct tp_list_entry, head);
311 return &entry->tp;
312 }
313
314 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
315 {
316 struct tp_field_list_entry *list_entry, *tmp;
317
318 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
319 cds_list_del(&list_entry->head);
320 free(list_entry);
321 }
322 }
323
324 /*
325 * called with UST lock held.
326 */
327 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
328 {
329 struct lttng_probe_desc *probe_desc;
330 int i;
331 struct cds_list_head *probe_list;
332
333 probe_list = lttng_get_probe_list_head();
334 CDS_INIT_LIST_HEAD(&list->head);
335 cds_list_for_each_entry(probe_desc, probe_list, head) {
336 for (i = 0; i < probe_desc->nr_events; i++) {
337 const struct lttng_event_desc *event_desc =
338 probe_desc->event_desc[i];
339 int j;
340
341 if (event_desc->nr_fields == 0) {
342 /* Events without fields. */
343 struct tp_field_list_entry *list_entry;
344
345 list_entry = zmalloc(sizeof(*list_entry));
346 if (!list_entry)
347 goto err_nomem;
348 cds_list_add(&list_entry->head, &list->head);
349 strncpy(list_entry->field.event_name,
350 event_desc->name,
351 LTTNG_UST_SYM_NAME_LEN);
352 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
353 list_entry->field.field_name[0] = '\0';
354 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
355 if (!event_desc->loglevel) {
356 list_entry->field.loglevel = TRACE_DEFAULT;
357 } else {
358 list_entry->field.loglevel = *(*event_desc->loglevel);
359 }
360 list_entry->field.nowrite = 1;
361 }
362
363 for (j = 0; j < event_desc->nr_fields; j++) {
364 const struct lttng_event_field *event_field =
365 &event_desc->fields[j];
366 struct tp_field_list_entry *list_entry;
367
368 list_entry = zmalloc(sizeof(*list_entry));
369 if (!list_entry)
370 goto err_nomem;
371 cds_list_add(&list_entry->head, &list->head);
372 strncpy(list_entry->field.event_name,
373 event_desc->name,
374 LTTNG_UST_SYM_NAME_LEN);
375 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
376 strncpy(list_entry->field.field_name,
377 event_field->name,
378 LTTNG_UST_SYM_NAME_LEN);
379 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
380 switch (event_field->type.atype) {
381 case atype_integer:
382 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
383 break;
384 case atype_string:
385 list_entry->field.type = LTTNG_UST_FIELD_STRING;
386 break;
387 case atype_array:
388 if (event_field->type.u.array.elem_type.atype != atype_integer
389 || event_field->type.u.array.elem_type.u.basic.integer.encoding == lttng_encode_none)
390 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
391 else
392 list_entry->field.type = LTTNG_UST_FIELD_STRING;
393 break;
394 case atype_sequence:
395 if (event_field->type.u.sequence.elem_type.atype != atype_integer
396 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
397 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
398 else
399 list_entry->field.type = LTTNG_UST_FIELD_STRING;
400 break;
401 case atype_float:
402 list_entry->field.type = LTTNG_UST_FIELD_FLOAT;
403 break;
404 case atype_enum:
405 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
406 break;
407 default:
408 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
409 }
410 if (!event_desc->loglevel) {
411 list_entry->field.loglevel = TRACE_DEFAULT;
412 } else {
413 list_entry->field.loglevel = *(*event_desc->loglevel);
414 }
415 list_entry->field.nowrite = event_field->nowrite;
416 }
417 }
418 }
419 if (cds_list_empty(&list->head))
420 list->iter = NULL;
421 else
422 list->iter =
423 cds_list_first_entry(&list->head,
424 struct tp_field_list_entry, head);
425 return 0;
426
427 err_nomem:
428 lttng_probes_prune_field_list(list);
429 return -ENOMEM;
430 }
431
432 /*
433 * Return current iteration position, advance internal iterator to next.
434 * Return NULL if end of list.
435 */
436 struct lttng_ust_field_iter *
437 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
438 {
439 struct tp_field_list_entry *entry;
440
441 if (!list->iter)
442 return NULL;
443 entry = list->iter;
444 if (entry->head.next == &list->head)
445 list->iter = NULL;
446 else
447 list->iter = cds_list_entry(entry->head.next,
448 struct tp_field_list_entry, head);
449 return &entry->field;
450 }
This page took 0.042135 seconds and 5 git commands to generate.