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