Fix: perform TLS fixup in all UST entry points from each thread
[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 lttng_ust_fixup_tls();
198
199 /*
200 * If version mismatch, don't register, but don't trigger assert
201 * on caller. The version check just prints an error.
202 */
203 if (!check_provider_version(desc))
204 return 0;
205
206 ust_lock_nocheck();
207
208 /*
209 * Check if the provider has already been registered.
210 */
211 if (find_provider(desc->provider)) {
212 ret = -EEXIST;
213 goto end;
214 }
215 cds_list_add(&desc->lazy_init_head, &lazy_probe_init);
216 desc->lazy = 1;
217 DBG("adding probe %s containing %u events to lazy registration list",
218 desc->provider, desc->nr_events);
219 /*
220 * If there is at least one active session, we need to register
221 * the probe immediately, since we cannot delay event
222 * registration because they are needed ASAP.
223 */
224 if (lttng_session_active())
225 fixup_lazy_probes();
226 end:
227 ust_unlock();
228 return ret;
229 }
230
231 /* Backward compatibility with UST 2.0 */
232 int ltt_probe_register(struct lttng_probe_desc *desc)
233 {
234 return lttng_probe_register(desc);
235 }
236
237 void lttng_probe_unregister(struct lttng_probe_desc *desc)
238 {
239 lttng_ust_fixup_tls();
240
241 if (!check_provider_version(desc))
242 return;
243
244 ust_lock_nocheck();
245 if (!desc->lazy)
246 cds_list_del(&desc->head);
247 else
248 cds_list_del(&desc->lazy_init_head);
249 DBG("just unregistered probe %s", desc->provider);
250 ust_unlock();
251 }
252
253 /* Backward compatibility with UST 2.0 */
254 void ltt_probe_unregister(struct lttng_probe_desc *desc)
255 {
256 lttng_probe_unregister(desc);
257 }
258
259 void lttng_probes_prune_event_list(struct lttng_ust_tracepoint_list *list)
260 {
261 struct tp_list_entry *list_entry, *tmp;
262
263 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
264 cds_list_del(&list_entry->head);
265 free(list_entry);
266 }
267 }
268
269 /*
270 * called with UST lock held.
271 */
272 int lttng_probes_get_event_list(struct lttng_ust_tracepoint_list *list)
273 {
274 struct lttng_probe_desc *probe_desc;
275 int i;
276 struct cds_list_head *probe_list;
277
278 probe_list = lttng_get_probe_list_head();
279 CDS_INIT_LIST_HEAD(&list->head);
280 cds_list_for_each_entry(probe_desc, probe_list, head) {
281 for (i = 0; i < probe_desc->nr_events; i++) {
282 struct tp_list_entry *list_entry;
283
284 list_entry = zmalloc(sizeof(*list_entry));
285 if (!list_entry)
286 goto err_nomem;
287 cds_list_add(&list_entry->head, &list->head);
288 strncpy(list_entry->tp.name,
289 probe_desc->event_desc[i]->name,
290 LTTNG_UST_SYM_NAME_LEN);
291 list_entry->tp.name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
292 if (!probe_desc->event_desc[i]->loglevel) {
293 list_entry->tp.loglevel = TRACE_DEFAULT;
294 } else {
295 list_entry->tp.loglevel = *(*probe_desc->event_desc[i]->loglevel);
296 }
297 }
298 }
299 if (cds_list_empty(&list->head))
300 list->iter = NULL;
301 else
302 list->iter =
303 cds_list_first_entry(&list->head, struct tp_list_entry, head);
304 return 0;
305
306 err_nomem:
307 lttng_probes_prune_event_list(list);
308 return -ENOMEM;
309 }
310
311 /*
312 * Return current iteration position, advance internal iterator to next.
313 * Return NULL if end of list.
314 */
315 struct lttng_ust_tracepoint_iter *
316 lttng_ust_tracepoint_list_get_iter_next(struct lttng_ust_tracepoint_list *list)
317 {
318 struct tp_list_entry *entry;
319
320 if (!list->iter)
321 return NULL;
322 entry = list->iter;
323 if (entry->head.next == &list->head)
324 list->iter = NULL;
325 else
326 list->iter = cds_list_entry(entry->head.next,
327 struct tp_list_entry, head);
328 return &entry->tp;
329 }
330
331 void lttng_probes_prune_field_list(struct lttng_ust_field_list *list)
332 {
333 struct tp_field_list_entry *list_entry, *tmp;
334
335 cds_list_for_each_entry_safe(list_entry, tmp, &list->head, head) {
336 cds_list_del(&list_entry->head);
337 free(list_entry);
338 }
339 }
340
341 /*
342 * called with UST lock held.
343 */
344 int lttng_probes_get_field_list(struct lttng_ust_field_list *list)
345 {
346 struct lttng_probe_desc *probe_desc;
347 int i;
348 struct cds_list_head *probe_list;
349
350 probe_list = lttng_get_probe_list_head();
351 CDS_INIT_LIST_HEAD(&list->head);
352 cds_list_for_each_entry(probe_desc, probe_list, head) {
353 for (i = 0; i < probe_desc->nr_events; i++) {
354 const struct lttng_event_desc *event_desc =
355 probe_desc->event_desc[i];
356 int j;
357
358 if (event_desc->nr_fields == 0) {
359 /* Events without fields. */
360 struct tp_field_list_entry *list_entry;
361
362 list_entry = zmalloc(sizeof(*list_entry));
363 if (!list_entry)
364 goto err_nomem;
365 cds_list_add(&list_entry->head, &list->head);
366 strncpy(list_entry->field.event_name,
367 event_desc->name,
368 LTTNG_UST_SYM_NAME_LEN);
369 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
370 list_entry->field.field_name[0] = '\0';
371 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
372 if (!event_desc->loglevel) {
373 list_entry->field.loglevel = TRACE_DEFAULT;
374 } else {
375 list_entry->field.loglevel = *(*event_desc->loglevel);
376 }
377 list_entry->field.nowrite = 1;
378 }
379
380 for (j = 0; j < event_desc->nr_fields; j++) {
381 const struct lttng_event_field *event_field =
382 &event_desc->fields[j];
383 struct tp_field_list_entry *list_entry;
384
385 list_entry = zmalloc(sizeof(*list_entry));
386 if (!list_entry)
387 goto err_nomem;
388 cds_list_add(&list_entry->head, &list->head);
389 strncpy(list_entry->field.event_name,
390 event_desc->name,
391 LTTNG_UST_SYM_NAME_LEN);
392 list_entry->field.event_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
393 strncpy(list_entry->field.field_name,
394 event_field->name,
395 LTTNG_UST_SYM_NAME_LEN);
396 list_entry->field.field_name[LTTNG_UST_SYM_NAME_LEN - 1] = '\0';
397 switch (event_field->type.atype) {
398 case atype_integer:
399 list_entry->field.type = LTTNG_UST_FIELD_INTEGER;
400 break;
401 case atype_string:
402 list_entry->field.type = LTTNG_UST_FIELD_STRING;
403 break;
404 case atype_array:
405 if (event_field->type.u.array.elem_type.atype != atype_integer
406 || event_field->type.u.array.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:
412 if (event_field->type.u.sequence.elem_type.atype != atype_integer
413 || event_field->type.u.sequence.elem_type.u.basic.integer.encoding == lttng_encode_none)
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;
421 case atype_enum:
422 list_entry->field.type = LTTNG_UST_FIELD_ENUM;
423 break;
424 default:
425 list_entry->field.type = LTTNG_UST_FIELD_OTHER;
426 }
427 if (!event_desc->loglevel) {
428 list_entry->field.loglevel = TRACE_DEFAULT;
429 } else {
430 list_entry->field.loglevel = *(*event_desc->loglevel);
431 }
432 list_entry->field.nowrite = event_field->nowrite;
433 }
434 }
435 }
436 if (cds_list_empty(&list->head))
437 list->iter = NULL;
438 else
439 list->iter =
440 cds_list_first_entry(&list->head,
441 struct tp_field_list_entry, head);
442 return 0;
443
444 err_nomem:
445 lttng_probes_prune_field_list(list);
446 return -ENOMEM;
447 }
448
449 /*
450 * Return current iteration position, advance internal iterator to next.
451 * Return NULL if end of list.
452 */
453 struct lttng_ust_field_iter *
454 lttng_ust_field_list_get_iter_next(struct lttng_ust_field_list *list)
455 {
456 struct tp_field_list_entry *entry;
457
458 if (!list->iter)
459 return NULL;
460 entry = list->iter;
461 if (entry->head.next == &list->head)
462 list->iter = NULL;
463 else
464 list->iter = cds_list_entry(entry->head.next,
465 struct tp_field_list_entry, head);
466 return &entry->field;
467 }
This page took 0.038403 seconds and 4 git commands to generate.