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