Fix event-specific enabling
[lttng-ust.git] / liblttng-ust / ltt-events.c
1 /*
2 * ltt-events.c
3 *
4 * Copyright 2010 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 *
6 * Holds LTTng per-session event registry.
7 *
8 * Dual LGPL v2.1/GPL v2 license.
9 */
10
11 #define _GNU_SOURCE
12 #include <stdio.h>
13 #include <endian.h>
14 #include <urcu/list.h>
15 #include <urcu/hlist.h>
16 #include <pthread.h>
17 #include <uuid/uuid.h>
18 #include <errno.h>
19 #include <sys/shm.h>
20 #include <sys/ipc.h>
21 #include <stdint.h>
22 #include <stddef.h>
23 #include <inttypes.h>
24 #include <time.h>
25 #include "clock.h"
26
27 #include <urcu-bp.h>
28 #include <urcu/compiler.h>
29 #include <urcu/uatomic.h>
30 #include <urcu/arch.h>
31
32 #include <lttng/tracepoint.h>
33 #include <lttng/ust-events.h>
34
35 #include <usterr-signal-safe.h>
36 #include <helper.h>
37 #include "error.h"
38
39 #include "tracepoint-internal.h"
40 #include "ltt-tracer.h"
41 #include "ltt-tracer-core.h"
42 #include "wait.h"
43 #include "../libringbuffer/shm.h"
44 #include "jhash.h"
45
46 /*
47 * The sessions mutex is the centralized mutex across UST tracing
48 * control and probe registration. All operations within this file are
49 * called by the communication thread, under ust_lock protection.
50 */
51 static pthread_mutex_t sessions_mutex = PTHREAD_MUTEX_INITIALIZER;
52
53 void ust_lock(void)
54 {
55 pthread_mutex_lock(&sessions_mutex);
56 }
57
58 void ust_unlock(void)
59 {
60 pthread_mutex_unlock(&sessions_mutex);
61 }
62
63 static CDS_LIST_HEAD(sessions);
64
65 /*
66 * Wildcard list, containing the active wildcards.
67 * Protected by ust lock.
68 */
69 static CDS_LIST_HEAD(wildcard_list);
70
71 /*
72 * Pending probes hash table, containing the registered ltt events for
73 * which tracepoint probes are still missing. Protected by the sessions
74 * mutex.
75 */
76 #define PENDING_PROBE_HASH_BITS 6
77 #define PENDING_PROBE_HASH_SIZE (1 << PENDING_PROBE_HASH_BITS)
78 static struct cds_hlist_head pending_probe_table[PENDING_PROBE_HASH_SIZE];
79
80 struct ust_pending_probe {
81 struct ltt_event *event;
82 struct cds_hlist_node node;
83 enum lttng_ust_loglevel_type loglevel_type;
84 int loglevel;
85 char name[];
86 };
87
88 static void _ltt_event_destroy(struct ltt_event *event);
89 static void _ltt_wildcard_destroy(struct session_wildcard *sw);
90 static void _ltt_channel_destroy(struct ltt_channel *chan);
91 static int _ltt_event_unregister(struct ltt_event *event);
92 static
93 int _ltt_event_metadata_statedump(struct ltt_session *session,
94 struct ltt_channel *chan,
95 struct ltt_event *event);
96 static
97 int _ltt_session_metadata_statedump(struct ltt_session *session);
98
99 int ltt_loglevel_match(const struct lttng_event_desc *desc,
100 enum lttng_ust_loglevel_type req_type,
101 int req_loglevel)
102 {
103 int ev_loglevel;
104
105 if (req_type == LTTNG_UST_LOGLEVEL_ALL)
106 return 1;
107 if (!desc->loglevel)
108 ev_loglevel = TRACE_DEFAULT;
109 else
110 ev_loglevel = *(*desc->loglevel);
111 switch (req_type) {
112 case LTTNG_UST_LOGLEVEL_RANGE:
113 if (ev_loglevel <= req_loglevel || req_loglevel == -1)
114 return 1;
115 else
116 return 0;
117 case LTTNG_UST_LOGLEVEL_SINGLE:
118 if (ev_loglevel == req_loglevel || req_loglevel == -1)
119 return 1;
120 else
121 return 0;
122 case LTTNG_UST_LOGLEVEL_ALL:
123 default:
124 return 1;
125 }
126 }
127
128 /*
129 * Return wildcard for a given event name if the event name match the
130 * one of the wildcards.
131 * Must be called with ust lock held.
132 * Returns NULL if not present.
133 */
134 static
135 struct wildcard_entry *match_wildcard(const struct lttng_event_desc *desc)
136 {
137 struct wildcard_entry *e;
138
139 cds_list_for_each_entry(e, &wildcard_list, list) {
140 /* If only contain '*' */
141 if (strlen(e->name) == 1)
142 goto possible_match;
143 /* Compare excluding final '*' */
144 if (!strncmp(desc->name, e->name, strlen(e->name) - 1))
145 goto possible_match;
146 continue; /* goto next, no match */
147 possible_match:
148 if (ltt_loglevel_match(desc,
149 e->loglevel_type,
150 e->loglevel)) {
151 return e;
152 }
153 /* no match, loop to next */
154 }
155 return NULL;
156 }
157
158 /*
159 * called at event creation if probe is missing.
160 * called with session mutex held.
161 */
162 static
163 int add_pending_probe(struct ltt_event *event, const char *name,
164 enum lttng_ust_loglevel_type loglevel_type,
165 int loglevel)
166 {
167 struct cds_hlist_head *head;
168 struct ust_pending_probe *e;
169 size_t name_len = strlen(name);
170 uint32_t hash;
171
172 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
173 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
174 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
175 }
176 hash = jhash(name, name_len, 0);
177 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
178 e = zmalloc(sizeof(struct ust_pending_probe) + name_len);
179 if (!e)
180 return -ENOMEM;
181 memcpy(&e->name[0], name, name_len + 1);
182 e->name[name_len] = '\0';
183 e->loglevel_type = loglevel_type;
184 e->loglevel = loglevel;
185 cds_hlist_add_head(&e->node, head);
186 e->event = event;
187 event->pending_probe = e;
188 return 0;
189 }
190
191 /*
192 * remove a pending probe. called when at event teardown and when an
193 * event is fixed (probe is loaded).
194 * called with session mutex held.
195 */
196 static
197 void remove_pending_probe(struct ust_pending_probe *e)
198 {
199 if (!e)
200 return;
201 cds_hlist_del(&e->node);
202 free(e);
203 }
204
205 /*
206 * Called at library load: connect the probe on the events pending on
207 * probe load.
208 * called with session mutex held.
209 */
210 int pending_probe_fix_events(const struct lttng_event_desc *desc)
211 {
212 struct cds_hlist_head *head;
213 struct cds_hlist_node *node, *p;
214 struct ust_pending_probe *e;
215 const char *name = desc->name;
216 int ret = 0;
217 struct lttng_ust_event event_param;
218 size_t name_len = strlen(name);
219 uint32_t hash;
220
221 /* Wildcard */
222 {
223 struct wildcard_entry *wildcard;
224
225 wildcard = match_wildcard(desc);
226 if (strcmp(desc->name, "lttng_ust:metadata") && wildcard) {
227 struct session_wildcard *sw;
228
229 cds_list_for_each_entry(sw, &wildcard->session_list,
230 session_list) {
231 struct ltt_event *ev;
232 int ret;
233
234 memcpy(&event_param, &sw->event_param,
235 sizeof(event_param));
236 memcpy(event_param.name,
237 desc->name,
238 sizeof(event_param.name));
239 /* create event */
240 ret = ltt_event_create(sw->chan,
241 &event_param, NULL,
242 &ev);
243 if (ret) {
244 DBG("Error creating event");
245 continue;
246 }
247 cds_list_add(&ev->wildcard_list,
248 &sw->events);
249 }
250 }
251 }
252
253 if (name_len > LTTNG_UST_SYM_NAME_LEN - 1) {
254 WARN("Truncating tracepoint name %s which exceeds size limits of %u chars", name, LTTNG_UST_SYM_NAME_LEN - 1);
255 name_len = LTTNG_UST_SYM_NAME_LEN - 1;
256 }
257 hash = jhash(name, name_len, 0);
258 head = &pending_probe_table[hash & (PENDING_PROBE_HASH_SIZE - 1)];
259 cds_hlist_for_each_entry_safe(e, node, p, head, node) {
260 struct ltt_event *event;
261 struct ltt_channel *chan;
262
263 if (!ltt_loglevel_match(desc,
264 e->loglevel_type,
265 e->loglevel)) {
266 continue;
267 }
268 if (strncmp(name, e->name, LTTNG_UST_SYM_NAME_LEN - 1)) {
269 continue;
270 }
271 event = e->event;
272 chan = event->chan;
273 assert(!event->desc);
274 event->desc = desc;
275 event->pending_probe = NULL;
276 remove_pending_probe(e);
277 ret |= __tracepoint_probe_register(name,
278 event->desc->probe_callback,
279 event);
280 if (ret)
281 continue;
282 event->id = chan->free_event_id++;
283 ret |= _ltt_event_metadata_statedump(chan->session, chan,
284 event);
285 }
286 return ret;
287 }
288
289 void synchronize_trace(void)
290 {
291 synchronize_rcu();
292 }
293
294 struct ltt_session *ltt_session_create(void)
295 {
296 struct ltt_session *session;
297
298 session = zmalloc(sizeof(struct ltt_session));
299 if (!session)
300 return NULL;
301 CDS_INIT_LIST_HEAD(&session->chan);
302 CDS_INIT_LIST_HEAD(&session->events);
303 CDS_INIT_LIST_HEAD(&session->wildcards);
304 uuid_generate(session->uuid);
305 cds_list_add(&session->list, &sessions);
306 return session;
307 }
308
309 void ltt_session_destroy(struct ltt_session *session)
310 {
311 struct ltt_channel *chan, *tmpchan;
312 struct ltt_event *event, *tmpevent;
313 struct session_wildcard *wildcard, *tmpwildcard;
314 int ret;
315
316 CMM_ACCESS_ONCE(session->active) = 0;
317 cds_list_for_each_entry(event, &session->events, list) {
318 ret = _ltt_event_unregister(event);
319 WARN_ON(ret);
320 }
321 synchronize_trace(); /* Wait for in-flight events to complete */
322 cds_list_for_each_entry_safe(wildcard, tmpwildcard, &session->wildcards, list)
323 _ltt_wildcard_destroy(wildcard);
324 cds_list_for_each_entry_safe(event, tmpevent, &session->events, list)
325 _ltt_event_destroy(event);
326 cds_list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
327 _ltt_channel_destroy(chan);
328 cds_list_del(&session->list);
329 free(session);
330 }
331
332 int ltt_session_enable(struct ltt_session *session)
333 {
334 int ret = 0;
335 struct ltt_channel *chan;
336
337 if (session->active) {
338 ret = -EBUSY;
339 goto end;
340 }
341
342 /*
343 * Snapshot the number of events per channel to know the type of header
344 * we need to use.
345 */
346 cds_list_for_each_entry(chan, &session->chan, list) {
347 if (chan->header_type)
348 continue; /* don't change it if session stop/restart */
349 if (chan->free_event_id < 31)
350 chan->header_type = 1; /* compact */
351 else
352 chan->header_type = 2; /* large */
353 }
354
355 CMM_ACCESS_ONCE(session->active) = 1;
356 CMM_ACCESS_ONCE(session->been_active) = 1;
357 ret = _ltt_session_metadata_statedump(session);
358 if (ret)
359 CMM_ACCESS_ONCE(session->active) = 0;
360 end:
361 return ret;
362 }
363
364 int ltt_session_disable(struct ltt_session *session)
365 {
366 int ret = 0;
367
368 if (!session->active) {
369 ret = -EBUSY;
370 goto end;
371 }
372 CMM_ACCESS_ONCE(session->active) = 0;
373 end:
374 return ret;
375 }
376
377 int ltt_channel_enable(struct ltt_channel *channel)
378 {
379 int old;
380
381 if (channel == channel->session->metadata)
382 return -EPERM;
383 old = uatomic_xchg(&channel->enabled, 1);
384 if (old)
385 return -EEXIST;
386 return 0;
387 }
388
389 int ltt_channel_disable(struct ltt_channel *channel)
390 {
391 int old;
392
393 if (channel == channel->session->metadata)
394 return -EPERM;
395 old = uatomic_xchg(&channel->enabled, 0);
396 if (!old)
397 return -EEXIST;
398 return 0;
399 }
400
401 int ltt_event_enable(struct ltt_event *event)
402 {
403 int old;
404
405 if (event->chan == event->chan->session->metadata)
406 return -EPERM;
407 old = uatomic_xchg(&event->enabled, 1);
408 if (old)
409 return -EEXIST;
410 return 0;
411 }
412
413 int ltt_event_disable(struct ltt_event *event)
414 {
415 int old;
416
417 if (event->chan == event->chan->session->metadata)
418 return -EPERM;
419 old = uatomic_xchg(&event->enabled, 0);
420 if (!old)
421 return -EEXIST;
422 return 0;
423 }
424
425 struct ltt_channel *ltt_channel_create(struct ltt_session *session,
426 const char *transport_name,
427 void *buf_addr,
428 size_t subbuf_size, size_t num_subbuf,
429 unsigned int switch_timer_interval,
430 unsigned int read_timer_interval,
431 int **shm_fd, int **wait_fd,
432 uint64_t **memory_map_size,
433 struct ltt_channel *chan_priv_init)
434 {
435 struct ltt_channel *chan = NULL;
436 struct ltt_transport *transport;
437
438 if (session->been_active)
439 goto active; /* Refuse to add channel to active session */
440 transport = ltt_transport_find(transport_name);
441 if (!transport) {
442 DBG("LTTng transport %s not found\n",
443 transport_name);
444 goto notransport;
445 }
446 chan_priv_init->id = session->free_chan_id++;
447 chan_priv_init->session = session;
448 /*
449 * Note: the channel creation op already writes into the packet
450 * headers. Therefore the "chan" information used as input
451 * should be already accessible.
452 */
453 chan = transport->ops.channel_create("[lttng]", buf_addr,
454 subbuf_size, num_subbuf, switch_timer_interval,
455 read_timer_interval, shm_fd, wait_fd,
456 memory_map_size, chan_priv_init);
457 if (!chan)
458 goto create_error;
459 chan->enabled = 1;
460 chan->ops = &transport->ops;
461 cds_list_add(&chan->list, &session->chan);
462 return chan;
463
464 create_error:
465 notransport:
466 active:
467 return NULL;
468 }
469
470 /*
471 * Only used internally at session destruction.
472 */
473 static
474 void _ltt_channel_destroy(struct ltt_channel *chan)
475 {
476 cds_list_del(&chan->list);
477 lttng_destroy_context(chan->ctx);
478 chan->ops->channel_destroy(chan);
479 }
480
481 /*
482 * Supports event creation while tracing session is active.
483 */
484 int ltt_event_create(struct ltt_channel *chan,
485 struct lttng_ust_event *event_param,
486 void *filter,
487 struct ltt_event **_event)
488 {
489 const struct lttng_event_desc *desc = NULL; /* silence gcc */
490 struct ltt_event *event;
491 int ret = 0;
492
493 if (chan->used_event_id == -1UL) {
494 ret = -ENOMEM;
495 goto full;
496 }
497 /*
498 * This is O(n^2) (for each event, the loop is called at event
499 * creation). Might require a hash if we have lots of events.
500 */
501 cds_list_for_each_entry(event, &chan->session->events, list) {
502 if (event->desc && !strncmp(event->desc->name,
503 event_param->name,
504 LTTNG_UST_SYM_NAME_LEN - 1)) {
505 ret = -EEXIST;
506 goto exist;
507 }
508 }
509
510 /*
511 * Check if loglevel match. Refuse to connect event if not.
512 */
513 if (event_param->instrumentation == LTTNG_UST_TRACEPOINT) {
514 desc = ltt_event_get(event_param->name);
515 if (desc) {
516 if (!ltt_loglevel_match(desc,
517 event_param->loglevel_type,
518 event_param->loglevel)) {
519 ret = -EPERM;
520 goto no_loglevel_match;
521 }
522 }
523 /*
524 * If descriptor is not there, it will be added to
525 * pending probes.
526 */
527 }
528 event = zmalloc(sizeof(struct ltt_event));
529 if (!event) {
530 ret = -ENOMEM;
531 goto cache_error;
532 }
533 event->chan = chan;
534 event->filter = filter;
535 /*
536 * used_event_id counts the maximum number of event IDs that can
537 * register if all probes register.
538 */
539 chan->used_event_id++;
540 event->enabled = 1;
541 event->instrumentation = event_param->instrumentation;
542 /* Populate ltt_event structure before tracepoint registration. */
543 cmm_smp_wmb();
544 switch (event_param->instrumentation) {
545 case LTTNG_UST_TRACEPOINT:
546 event->desc = desc;
547 if (event->desc) {
548 ret = __tracepoint_probe_register(event_param->name,
549 event->desc->probe_callback,
550 event);
551 if (ret)
552 goto register_error;
553 event->id = chan->free_event_id++;
554 } else {
555 /*
556 * If the probe is not present, event->desc stays NULL,
557 * waiting for the probe to register, and the event->id
558 * stays unallocated.
559 */
560 ret = add_pending_probe(event, event_param->name,
561 event_param->loglevel_type,
562 event_param->loglevel);
563 if (ret)
564 goto add_pending_error;
565 }
566 break;
567 default:
568 WARN_ON_ONCE(1);
569 }
570 if (event->desc) {
571 ret = _ltt_event_metadata_statedump(chan->session, chan, event);
572 if (ret)
573 goto statedump_error;
574 }
575 cds_list_add(&event->list, &chan->session->events);
576 *_event = event;
577 return 0;
578
579 statedump_error:
580 if (event->desc) {
581 WARN_ON_ONCE(__tracepoint_probe_unregister(event_param->name,
582 event->desc->probe_callback,
583 event));
584 ltt_event_put(event->desc);
585 }
586 add_pending_error:
587 register_error:
588 free(event);
589 cache_error:
590 no_loglevel_match:
591 exist:
592 full:
593 return ret;
594 }
595
596 /*
597 * Only used internally at session destruction.
598 */
599 int _ltt_event_unregister(struct ltt_event *event)
600 {
601 int ret = -EINVAL;
602
603 switch (event->instrumentation) {
604 case LTTNG_UST_TRACEPOINT:
605 if (event->desc) {
606 ret = __tracepoint_probe_unregister(event->desc->name,
607 event->desc->probe_callback,
608 event);
609 if (ret)
610 return ret;
611 } else {
612 remove_pending_probe(event->pending_probe);
613 ret = 0;
614 }
615 break;
616 default:
617 WARN_ON_ONCE(1);
618 }
619 return ret;
620 }
621
622 /*
623 * Only used internally at session destruction.
624 */
625 static
626 void _ltt_event_destroy(struct ltt_event *event)
627 {
628 switch (event->instrumentation) {
629 case LTTNG_UST_TRACEPOINT:
630 if (event->desc) {
631 ltt_event_put(event->desc);
632 }
633 break;
634 default:
635 WARN_ON_ONCE(1);
636 }
637 cds_list_del(&event->list);
638 lttng_destroy_context(event->ctx);
639 free(event);
640 }
641
642 /*
643 * We have exclusive access to our metadata buffer (protected by the
644 * ust_lock), so we can do racy operations such as looking for
645 * remaining space left in packet and write, since mutual exclusion
646 * protects us from concurrent writes.
647 */
648 int lttng_metadata_printf(struct ltt_session *session,
649 const char *fmt, ...)
650 {
651 struct lttng_ust_lib_ring_buffer_ctx ctx;
652 struct ltt_channel *chan = session->metadata;
653 char *str = NULL;
654 int ret = 0, waitret;
655 size_t len, reserve_len, pos;
656 va_list ap;
657
658 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
659
660 va_start(ap, fmt);
661 ret = vasprintf(&str, fmt, ap);
662 va_end(ap);
663 if (ret < 0)
664 return -ENOMEM;
665
666 len = strlen(str);
667 pos = 0;
668
669 for (pos = 0; pos < len; pos += reserve_len) {
670 reserve_len = min_t(size_t,
671 chan->ops->packet_avail_size(chan->chan, chan->handle),
672 len - pos);
673 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
674 sizeof(char), -1, chan->handle);
675 /*
676 * We don't care about metadata buffer's records lost
677 * count, because we always retry here. Report error if
678 * we need to bail out after timeout or being
679 * interrupted.
680 */
681 waitret = wait_cond_interruptible_timeout(
682 ({
683 ret = chan->ops->event_reserve(&ctx, 0);
684 ret != -ENOBUFS || !ret;
685 }),
686 LTTNG_METADATA_TIMEOUT_MSEC);
687 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
688 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
689 waitret == -EINTR ? "interrupted" :
690 (ret == -ENOBUFS ? "timeout" : "I/O error"));
691 if (waitret == -EINTR)
692 ret = waitret;
693 goto end;
694 }
695 chan->ops->event_write(&ctx, &str[pos], reserve_len);
696 chan->ops->event_commit(&ctx);
697 }
698 end:
699 free(str);
700 return ret;
701 }
702
703 static
704 int _ltt_field_statedump(struct ltt_session *session,
705 const struct lttng_event_field *field)
706 {
707 int ret = 0;
708
709 switch (field->type.atype) {
710 case atype_integer:
711 ret = lttng_metadata_printf(session,
712 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
713 field->type.u.basic.integer.size,
714 field->type.u.basic.integer.alignment,
715 field->type.u.basic.integer.signedness,
716 (field->type.u.basic.integer.encoding == lttng_encode_none)
717 ? "none"
718 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
719 ? "UTF8"
720 : "ASCII",
721 field->type.u.basic.integer.base,
722 #if (BYTE_ORDER == BIG_ENDIAN)
723 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
724 #else
725 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
726 #endif
727 field->name);
728 break;
729 case atype_float:
730 ret = lttng_metadata_printf(session,
731 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
732 field->type.u.basic._float.exp_dig,
733 field->type.u.basic._float.mant_dig,
734 field->type.u.basic._float.alignment,
735 #if (BYTE_ORDER == BIG_ENDIAN)
736 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
737 #else
738 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
739 #endif
740 field->name);
741 break;
742 case atype_enum:
743 ret = lttng_metadata_printf(session,
744 " %s %s;\n",
745 field->type.u.basic.enumeration.name,
746 field->name);
747 break;
748 case atype_array:
749 {
750 const struct lttng_basic_type *elem_type;
751
752 elem_type = &field->type.u.array.elem_type;
753 ret = lttng_metadata_printf(session,
754 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
755 elem_type->u.basic.integer.size,
756 elem_type->u.basic.integer.alignment,
757 elem_type->u.basic.integer.signedness,
758 (elem_type->u.basic.integer.encoding == lttng_encode_none)
759 ? "none"
760 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
761 ? "UTF8"
762 : "ASCII",
763 elem_type->u.basic.integer.base,
764 #if (BYTE_ORDER == BIG_ENDIAN)
765 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
766 #else
767 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
768 #endif
769 field->name, field->type.u.array.length);
770 break;
771 }
772 case atype_sequence:
773 {
774 const struct lttng_basic_type *elem_type;
775 const struct lttng_basic_type *length_type;
776
777 elem_type = &field->type.u.sequence.elem_type;
778 length_type = &field->type.u.sequence.length_type;
779 ret = lttng_metadata_printf(session,
780 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
781 length_type->u.basic.integer.size,
782 (unsigned int) length_type->u.basic.integer.alignment,
783 length_type->u.basic.integer.signedness,
784 (length_type->u.basic.integer.encoding == lttng_encode_none)
785 ? "none"
786 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
787 ? "UTF8"
788 : "ASCII"),
789 length_type->u.basic.integer.base,
790 #if (BYTE_ORDER == BIG_ENDIAN)
791 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
792 #else
793 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
794 #endif
795 field->name);
796 if (ret)
797 return ret;
798
799 ret = lttng_metadata_printf(session,
800 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
801 elem_type->u.basic.integer.size,
802 (unsigned int) elem_type->u.basic.integer.alignment,
803 elem_type->u.basic.integer.signedness,
804 (elem_type->u.basic.integer.encoding == lttng_encode_none)
805 ? "none"
806 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
807 ? "UTF8"
808 : "ASCII"),
809 elem_type->u.basic.integer.base,
810 #if (BYTE_ORDER == BIG_ENDIAN)
811 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
812 #else
813 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
814 #endif
815 field->name,
816 field->name);
817 break;
818 }
819
820 case atype_string:
821 /* Default encoding is UTF8 */
822 ret = lttng_metadata_printf(session,
823 " string%s _%s;\n",
824 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
825 " { encoding = ASCII; }" : "",
826 field->name);
827 break;
828 default:
829 WARN_ON_ONCE(1);
830 return -EINVAL;
831 }
832 return ret;
833 }
834
835 static
836 int _ltt_context_metadata_statedump(struct ltt_session *session,
837 struct lttng_ctx *ctx)
838 {
839 int ret = 0;
840 int i;
841
842 if (!ctx)
843 return 0;
844 for (i = 0; i < ctx->nr_fields; i++) {
845 const struct lttng_ctx_field *field = &ctx->fields[i];
846
847 ret = _ltt_field_statedump(session, &field->event_field);
848 if (ret)
849 return ret;
850 }
851 return ret;
852 }
853
854 static
855 int _ltt_fields_metadata_statedump(struct ltt_session *session,
856 struct ltt_event *event)
857 {
858 const struct lttng_event_desc *desc = event->desc;
859 int ret = 0;
860 int i;
861
862 for (i = 0; i < desc->nr_fields; i++) {
863 const struct lttng_event_field *field = &desc->fields[i];
864
865 ret = _ltt_field_statedump(session, field);
866 if (ret)
867 return ret;
868 }
869 return ret;
870 }
871
872 static
873 int _ltt_event_metadata_statedump(struct ltt_session *session,
874 struct ltt_channel *chan,
875 struct ltt_event *event)
876 {
877 int ret = 0;
878
879 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
880 return 0;
881 if (chan == session->metadata)
882 return 0;
883 /*
884 * Don't print events for which probe load is pending.
885 */
886 if (!event->desc)
887 return 0;
888
889 ret = lttng_metadata_printf(session,
890 "event {\n"
891 " name = \"%s\";\n"
892 " id = %u;\n"
893 " stream_id = %u;\n",
894 event->desc->name,
895 event->id,
896 event->chan->id);
897 if (ret)
898 goto end;
899
900 if (event->desc->loglevel) {
901 ret = lttng_metadata_printf(session,
902 " loglevel = %d;\n",
903 *(*event->desc->loglevel));
904 if (ret)
905 goto end;
906 }
907
908 if (event->ctx) {
909 ret = lttng_metadata_printf(session,
910 " context := struct {\n");
911 if (ret)
912 goto end;
913 }
914 ret = _ltt_context_metadata_statedump(session, event->ctx);
915 if (ret)
916 goto end;
917 if (event->ctx) {
918 ret = lttng_metadata_printf(session,
919 " };\n");
920 if (ret)
921 goto end;
922 }
923
924 ret = lttng_metadata_printf(session,
925 " fields := struct {\n"
926 );
927 if (ret)
928 goto end;
929
930 ret = _ltt_fields_metadata_statedump(session, event);
931 if (ret)
932 goto end;
933
934 /*
935 * LTTng space reservation can only reserve multiples of the
936 * byte size.
937 */
938 ret = lttng_metadata_printf(session,
939 " };\n"
940 "};\n\n");
941 if (ret)
942 goto end;
943
944 event->metadata_dumped = 1;
945 end:
946 return ret;
947
948 }
949
950 static
951 int _ltt_channel_metadata_statedump(struct ltt_session *session,
952 struct ltt_channel *chan)
953 {
954 int ret = 0;
955
956 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
957 return 0;
958 if (chan == session->metadata)
959 return 0;
960
961 WARN_ON_ONCE(!chan->header_type);
962 ret = lttng_metadata_printf(session,
963 "stream {\n"
964 " id = %u;\n"
965 " event.header := %s;\n"
966 " packet.context := struct packet_context;\n",
967 chan->id,
968 chan->header_type == 1 ? "struct event_header_compact" :
969 "struct event_header_large");
970 if (ret)
971 goto end;
972
973 if (chan->ctx) {
974 ret = lttng_metadata_printf(session,
975 " event.context := struct {\n");
976 if (ret)
977 goto end;
978 }
979 ret = _ltt_context_metadata_statedump(session, chan->ctx);
980 if (ret)
981 goto end;
982 if (chan->ctx) {
983 ret = lttng_metadata_printf(session,
984 " };\n");
985 if (ret)
986 goto end;
987 }
988
989 ret = lttng_metadata_printf(session,
990 "};\n\n");
991
992 chan->metadata_dumped = 1;
993 end:
994 return ret;
995 }
996
997 static
998 int _ltt_stream_packet_context_declare(struct ltt_session *session)
999 {
1000 return lttng_metadata_printf(session,
1001 "struct packet_context {\n"
1002 " uint64_clock_monotonic_t timestamp_begin;\n"
1003 " uint64_clock_monotonic_t timestamp_end;\n"
1004 " uint32_t events_discarded;\n"
1005 " uint32_t content_size;\n"
1006 " uint32_t packet_size;\n"
1007 " uint32_t cpu_id;\n"
1008 "};\n\n"
1009 );
1010 }
1011
1012 /*
1013 * Compact header:
1014 * id: range: 0 - 30.
1015 * id 31 is reserved to indicate an extended header.
1016 *
1017 * Large header:
1018 * id: range: 0 - 65534.
1019 * id 65535 is reserved to indicate an extended header.
1020 */
1021 static
1022 int _ltt_event_header_declare(struct ltt_session *session)
1023 {
1024 return lttng_metadata_printf(session,
1025 "struct event_header_compact {\n"
1026 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1027 " variant <id> {\n"
1028 " struct {\n"
1029 " uint27_clock_monotonic_t timestamp;\n"
1030 " } compact;\n"
1031 " struct {\n"
1032 " uint32_t id;\n"
1033 " uint64_clock_monotonic_t timestamp;\n"
1034 " } extended;\n"
1035 " } v;\n"
1036 "} align(%u);\n"
1037 "\n"
1038 "struct event_header_large {\n"
1039 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1040 " variant <id> {\n"
1041 " struct {\n"
1042 " uint32_clock_monotonic_t timestamp;\n"
1043 " } compact;\n"
1044 " struct {\n"
1045 " uint32_t id;\n"
1046 " uint64_clock_monotonic_t timestamp;\n"
1047 " } extended;\n"
1048 " } v;\n"
1049 "} align(%u);\n\n",
1050 lttng_alignof(uint32_t) * CHAR_BIT,
1051 lttng_alignof(uint16_t) * CHAR_BIT
1052 );
1053 }
1054
1055 /*
1056 * Approximation of NTP time of day to clock monotonic correlation,
1057 * taken at start of trace.
1058 * Yes, this is only an approximation. Yes, we can (and will) do better
1059 * in future versions.
1060 */
1061 static
1062 uint64_t measure_clock_offset(void)
1063 {
1064 uint64_t offset, monotonic[2], realtime;
1065 struct timespec rts = { 0, 0 };
1066 int ret;
1067
1068 monotonic[0] = trace_clock_read64();
1069 ret = clock_gettime(CLOCK_REALTIME, &rts);
1070 if (ret < 0)
1071 return 0;
1072 monotonic[1] = trace_clock_read64();
1073 offset = (monotonic[0] + monotonic[1]) >> 1;
1074 realtime = rts.tv_sec * 1000000000ULL;
1075 realtime += rts.tv_nsec;
1076 offset = realtime - offset;
1077 return offset;
1078 }
1079
1080 /*
1081 * Output metadata into this session's metadata buffers.
1082 */
1083 static
1084 int _ltt_session_metadata_statedump(struct ltt_session *session)
1085 {
1086 unsigned char *uuid_c = session->uuid;
1087 char uuid_s[37], clock_uuid_s[CLOCK_UUID_LEN];
1088 struct ltt_channel *chan;
1089 struct ltt_event *event;
1090 int ret = 0;
1091
1092 if (!CMM_ACCESS_ONCE(session->active))
1093 return 0;
1094 if (session->metadata_dumped)
1095 goto skip_session;
1096 if (!session->metadata) {
1097 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1098 return -EPERM;
1099 }
1100
1101 snprintf(uuid_s, sizeof(uuid_s),
1102 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1103 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1104 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1105 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1106 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1107
1108 ret = lttng_metadata_printf(session,
1109 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1110 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1111 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1112 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1113 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1114 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1115 "\n"
1116 "trace {\n"
1117 " major = %u;\n"
1118 " minor = %u;\n"
1119 " uuid = \"%s\";\n"
1120 " byte_order = %s;\n"
1121 " packet.header := struct {\n"
1122 " uint32_t magic;\n"
1123 " uint8_t uuid[16];\n"
1124 " uint32_t stream_id;\n"
1125 " };\n"
1126 "};\n\n",
1127 lttng_alignof(uint8_t) * CHAR_BIT,
1128 lttng_alignof(uint16_t) * CHAR_BIT,
1129 lttng_alignof(uint32_t) * CHAR_BIT,
1130 lttng_alignof(uint64_t) * CHAR_BIT,
1131 CTF_VERSION_MAJOR,
1132 CTF_VERSION_MINOR,
1133 uuid_s,
1134 #if (BYTE_ORDER == BIG_ENDIAN)
1135 "be"
1136 #else
1137 "le"
1138 #endif
1139 );
1140 if (ret)
1141 goto end;
1142
1143 ret = lttng_metadata_printf(session,
1144 "clock {\n"
1145 " name = %s;\n",
1146 "monotonic"
1147 );
1148 if (ret)
1149 goto end;
1150
1151 if (!trace_clock_uuid(clock_uuid_s)) {
1152 ret = lttng_metadata_printf(session,
1153 " uuid = \"%s\";\n",
1154 clock_uuid_s
1155 );
1156 if (ret)
1157 goto end;
1158 }
1159
1160 ret = lttng_metadata_printf(session,
1161 " description = \"Monotonic Clock\";\n"
1162 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1163 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1164 " offset = %" PRIu64 ";\n"
1165 "};\n\n",
1166 trace_clock_freq(),
1167 measure_clock_offset()
1168 );
1169 if (ret)
1170 goto end;
1171
1172 ret = lttng_metadata_printf(session,
1173 "typealias integer {\n"
1174 " size = 27; align = 1; signed = false;\n"
1175 " map = clock.monotonic.value;\n"
1176 "} := uint27_clock_monotonic_t;\n"
1177 "\n"
1178 "typealias integer {\n"
1179 " size = 32; align = %u; signed = false;\n"
1180 " map = clock.monotonic.value;\n"
1181 "} := uint32_clock_monotonic_t;\n"
1182 "\n"
1183 "typealias integer {\n"
1184 " size = 64; align = %u; signed = false;\n"
1185 " map = clock.monotonic.value;\n"
1186 "} := uint64_clock_monotonic_t;\n\n",
1187 lttng_alignof(uint32_t) * CHAR_BIT,
1188 lttng_alignof(uint64_t) * CHAR_BIT
1189 );
1190 if (ret)
1191 goto end;
1192
1193 ret = _ltt_stream_packet_context_declare(session);
1194 if (ret)
1195 goto end;
1196
1197 ret = _ltt_event_header_declare(session);
1198 if (ret)
1199 goto end;
1200
1201 skip_session:
1202 cds_list_for_each_entry(chan, &session->chan, list) {
1203 ret = _ltt_channel_metadata_statedump(session, chan);
1204 if (ret)
1205 goto end;
1206 }
1207
1208 cds_list_for_each_entry(event, &session->events, list) {
1209 ret = _ltt_event_metadata_statedump(session, event->chan, event);
1210 if (ret)
1211 goto end;
1212 }
1213 session->metadata_dumped = 1;
1214 end:
1215 return ret;
1216 }
1217
1218 void lttng_ust_events_exit(void)
1219 {
1220 struct ltt_session *session, *tmpsession;
1221
1222 cds_list_for_each_entry_safe(session, tmpsession, &sessions, list)
1223 ltt_session_destroy(session);
1224 }
1225
1226 /* WILDCARDS */
1227
1228 static
1229 int wildcard_same_loglevel(struct wildcard_entry *e,
1230 enum lttng_ust_loglevel_type loglevel_type,
1231 int loglevel)
1232 {
1233 if (e->loglevel_type == loglevel_type && e->loglevel == loglevel)
1234 return 1;
1235 else
1236 return 0;
1237 }
1238
1239 #if 0
1240 static
1241 int wildcard_is_within(struct wildcard_entry *e,
1242 enum lttng_ust_loglevel_type loglevel_type,
1243 int loglevel)
1244 {
1245 if (e->loglevel_type == LTTNG_UST_LOGLEVEL_ALL
1246 || e->loglevel == -1)
1247 return 1;
1248 switch (e->loglevel_type) {
1249 case LTTNG_UST_LOGLEVEL_RANGE:
1250 switch (loglevel_type) {
1251 case LTTNG_UST_LOGLEVEL_RANGE:
1252 if (e->loglevel >= loglevel)
1253 return 1;
1254 else
1255 return 0;
1256 case LTTNG_UST_LOGLEVEL_SINGLE:
1257 if (e->loglevel <= 0 && loglevel == 0)
1258 return 1;
1259 else
1260 return 0;
1261 }
1262 case LTTNG_UST_LOGLEVEL_SINGLE:
1263 switch (loglevel_type) {
1264 case LTTNG_UST_LOGLEVEL_RANGE:
1265 if (loglevel <= 0)
1266 return 1;
1267 else
1268 return 0;
1269 case LTTNG_UST_LOGLEVEL_SINGLE:
1270 if (e->loglevel == loglevel)
1271 return 1;
1272 else
1273 return 0;
1274 }
1275 }
1276 }
1277 #endif
1278
1279 /*
1280 * Add the wildcard to the wildcard list. Must be called with
1281 * ust lock held.
1282 */
1283 static
1284 struct session_wildcard *add_wildcard(struct ltt_channel *chan,
1285 struct lttng_ust_event *event_param)
1286 {
1287 struct wildcard_entry *e;
1288 struct session_wildcard *sw;
1289 size_t name_len = strlen(event_param->name) + 1;
1290 int found = 0;
1291
1292 /*
1293 * Try to find global wildcard entry. Given that this is shared
1294 * across all sessions, we need to check for exact loglevel
1295 * match, not just whether contained within the existing ones.
1296 */
1297 cds_list_for_each_entry(e, &wildcard_list, list) {
1298 if (!strncmp(event_param->name, e->name,
1299 LTTNG_UST_SYM_NAME_LEN - 1)) {
1300 if (wildcard_same_loglevel(e,
1301 event_param->loglevel_type,
1302 event_param->loglevel)) {
1303 found = 1;
1304 break;
1305 }
1306 }
1307 }
1308
1309 if (!found) {
1310 /*
1311 * Create global wildcard entry if not found. Using
1312 * zmalloc here to allocate a variable length element.
1313 * Could cause some memory fragmentation if overused.
1314 */
1315 e = zmalloc(sizeof(struct wildcard_entry) + name_len);
1316 if (!e)
1317 return ERR_PTR(-ENOMEM);
1318 memcpy(&e->name[0], event_param->name, name_len);
1319 cds_list_add(&e->list, &wildcard_list);
1320 CDS_INIT_LIST_HEAD(&e->session_list);
1321 }
1322
1323 /* session wildcard */
1324 cds_list_for_each_entry(sw, &e->session_list, session_list) {
1325 if (chan == sw->chan) {
1326 DBG("wildcard %s busy for this channel",
1327 event_param->name);
1328 return ERR_PTR(-EEXIST); /* Already there */
1329 }
1330 }
1331 sw = zmalloc(sizeof(struct session_wildcard));
1332 if (!sw)
1333 return ERR_PTR(-ENOMEM);
1334 sw->chan = chan;
1335 sw->enabled = 1;
1336 memcpy(&sw->event_param, event_param, sizeof(sw->event_param));
1337 sw->event_param.instrumentation = LTTNG_UST_TRACEPOINT;
1338 sw->event_param.loglevel_type = event_param->loglevel_type;
1339 sw->event_param.loglevel = event_param->loglevel;
1340 CDS_INIT_LIST_HEAD(&sw->events);
1341 cds_list_add(&sw->list, &chan->session->wildcards);
1342 cds_list_add(&sw->session_list, &e->session_list);
1343 sw->entry = e;
1344 ltt_probes_create_wildcard_events(e, sw);
1345 return sw;
1346 }
1347
1348 /*
1349 * Remove the wildcard from the wildcard list. Must be called with
1350 * ust_lock held. Only called at session teardown.
1351 */
1352 static
1353 void _remove_wildcard(struct session_wildcard *wildcard)
1354 {
1355 struct ltt_event *ev, *tmp;
1356
1357 /*
1358 * Just remove the events owned (for enable/disable) by this
1359 * wildcard from the list. The session teardown will take care
1360 * of freeing the event memory.
1361 */
1362 cds_list_for_each_entry_safe(ev, tmp, &wildcard->events,
1363 wildcard_list) {
1364 cds_list_del(&ev->wildcard_list);
1365 }
1366 cds_list_del(&wildcard->session_list);
1367 cds_list_del(&wildcard->list);
1368 if (cds_list_empty(&wildcard->entry->session_list)) {
1369 cds_list_del(&wildcard->entry->list);
1370 free(wildcard->entry);
1371 }
1372 free(wildcard);
1373 }
1374
1375 int ltt_wildcard_create(struct ltt_channel *chan,
1376 struct lttng_ust_event *event_param,
1377 struct session_wildcard **_sw)
1378 {
1379 struct session_wildcard *sw;
1380
1381 sw = add_wildcard(chan, event_param);
1382 if (!sw || IS_ERR(sw)) {
1383 return PTR_ERR(sw);
1384 }
1385 *_sw = sw;
1386 return 0;
1387 }
1388
1389 static
1390 void _ltt_wildcard_destroy(struct session_wildcard *sw)
1391 {
1392 _remove_wildcard(sw);
1393 }
1394
1395 int ltt_wildcard_enable(struct session_wildcard *wildcard)
1396 {
1397 struct ltt_event *ev;
1398 int ret;
1399
1400 if (wildcard->enabled)
1401 return -EEXIST;
1402 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1403 ret = ltt_event_enable(ev);
1404 if (ret) {
1405 DBG("Error: enable error.\n");
1406 return ret;
1407 }
1408 }
1409 wildcard->enabled = 1;
1410 return 0;
1411 }
1412
1413 int ltt_wildcard_disable(struct session_wildcard *wildcard)
1414 {
1415 struct ltt_event *ev;
1416 int ret;
1417
1418 if (!wildcard->enabled)
1419 return -EEXIST;
1420 cds_list_for_each_entry(ev, &wildcard->events, wildcard_list) {
1421 ret = ltt_event_disable(ev);
1422 if (ret) {
1423 DBG("Error: disable error.\n");
1424 return ret;
1425 }
1426 }
1427 wildcard->enabled = 0;
1428 return 0;
1429 }
This page took 0.059486 seconds and 4 git commands to generate.