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