Speed up probe registration for large amount of events
[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 register_error:
395 free(event);
396 cache_error:
397 exist:
398 full:
399 return ret;
400 }
401
402 static
403 int lttng_desc_match_wildcard_enabler(const struct lttng_event_desc *desc,
404 struct lttng_enabler *enabler)
405 {
406 int loglevel = 0;
407 unsigned int has_loglevel;
408
409 assert(enabler->type == LTTNG_ENABLER_WILDCARD);
410 /* Compare excluding final '*' */
411 if (strncmp(desc->name, enabler->event_param.name,
412 strlen(enabler->event_param.name) - 1))
413 return 0;
414 if (desc->loglevel) {
415 loglevel = *(*desc->loglevel);
416 has_loglevel = 1;
417 }
418 if (!lttng_loglevel_match(loglevel,
419 has_loglevel,
420 enabler->event_param.loglevel_type,
421 enabler->event_param.loglevel))
422 return 0;
423 return 1;
424 }
425
426 static
427 int lttng_desc_match_event_enabler(const struct lttng_event_desc *desc,
428 struct lttng_enabler *enabler)
429 {
430 int loglevel = 0;
431 unsigned int has_loglevel = 0;
432
433 assert(enabler->type == LTTNG_ENABLER_EVENT);
434 if (strcmp(desc->name, enabler->event_param.name))
435 return 0;
436 if (desc->loglevel) {
437 loglevel = *(*desc->loglevel);
438 has_loglevel = 1;
439 }
440 if (!lttng_loglevel_match(loglevel,
441 has_loglevel,
442 enabler->event_param.loglevel_type,
443 enabler->event_param.loglevel))
444 return 0;
445 return 1;
446 }
447
448 static
449 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
450 struct lttng_enabler *enabler)
451 {
452 switch (enabler->type) {
453 case LTTNG_ENABLER_WILDCARD:
454 return lttng_desc_match_wildcard_enabler(desc, enabler);
455 case LTTNG_ENABLER_EVENT:
456 return lttng_desc_match_event_enabler(desc, enabler);
457 default:
458 return -EINVAL;
459 }
460 }
461
462 static
463 int lttng_event_match_enabler(struct lttng_event *event,
464 struct lttng_enabler *enabler)
465 {
466 return lttng_desc_match_enabler(event->desc, enabler);
467 }
468
469 static
470 struct lttng_enabler_ref * lttng_event_enabler_ref(struct lttng_event *event,
471 struct lttng_enabler *enabler)
472 {
473 struct lttng_enabler_ref *enabler_ref;
474
475 cds_list_for_each_entry(enabler_ref,
476 &event->enablers_ref_head, node) {
477 if (enabler_ref->ref == enabler)
478 return enabler_ref;
479 }
480 return NULL;
481 }
482
483 /*
484 * Create struct lttng_event if it is missing and present in the list of
485 * tracepoint probes.
486 */
487 static
488 void lttng_create_event_if_missing(struct lttng_enabler *enabler)
489 {
490 struct lttng_session *session = enabler->chan->session;
491 struct lttng_probe_desc *probe_desc;
492 const struct lttng_event_desc *desc;
493 struct lttng_event *event;
494 int i;
495 struct cds_list_head *probe_list;
496
497 probe_list = lttng_get_probe_list_head();
498 /*
499 * For each probe event, if we find that a probe event matches
500 * our enabler, create an associated lttng_event if not
501 * already present.
502 */
503 cds_list_for_each_entry(probe_desc, probe_list, head) {
504 for (i = 0; i < probe_desc->nr_events; i++) {
505 int found = 0, ret;
506 struct cds_hlist_head *head;
507 struct cds_hlist_node *node;
508 const char *event_name;
509 size_t name_len;
510 uint32_t hash;
511
512 desc = probe_desc->event_desc[i];
513 if (!lttng_desc_match_enabler(desc, enabler))
514 continue;
515 event_name = desc->name;
516 name_len = strlen(event_name);
517
518 /*
519 * Check if already created.
520 */
521 hash = jhash(event_name, name_len, 0);
522 head = &session->events_ht.table[hash & (LTTNG_UST_EVENT_HT_SIZE - 1)];
523 cds_hlist_for_each_entry(event, node, head, hlist) {
524 if (event->desc == desc)
525 found = 1;
526 }
527 if (found)
528 continue;
529
530 /*
531 * We need to create an event for this
532 * event probe.
533 */
534 ret = lttng_event_create(probe_desc->event_desc[i],
535 enabler->chan);
536 if (ret) {
537 DBG("Unable to create event %s\n",
538 probe_desc->event_desc[i]->name);
539 }
540 }
541 }
542 }
543
544 /*
545 * Create events associated with an enabler (if not already present),
546 * and add backward reference from the event to the enabler.
547 */
548 static
549 int lttng_enabler_ref_events(struct lttng_enabler *enabler)
550 {
551 struct lttng_session *session = enabler->chan->session;
552 struct lttng_event *event;
553
554 /* First ensure that probe events are created for this enabler. */
555 lttng_create_event_if_missing(enabler);
556
557 /* For each event matching enabler in session event list. */
558 cds_list_for_each_entry(event, &session->events_head, node) {
559 struct lttng_enabler_ref *enabler_ref;
560
561 if (!lttng_event_match_enabler(event, enabler))
562 continue;
563
564 enabler_ref = lttng_event_enabler_ref(event, enabler);
565 if (!enabler_ref) {
566 /*
567 * If no backward ref, create it.
568 * Add backward ref from event to enabler.
569 */
570 enabler_ref = zmalloc(sizeof(*enabler_ref));
571 if (!enabler_ref)
572 return -ENOMEM;
573 enabler_ref->ref = enabler;
574 cds_list_add(&enabler_ref->node,
575 &event->enablers_ref_head);
576 }
577
578 /*
579 * Link filter bytecodes if not linked yet.
580 */
581 lttng_enabler_event_link_bytecode(event, enabler);
582
583 /* TODO: merge event context. */
584 }
585 return 0;
586 }
587
588 /*
589 * Called at library load: connect the probe on all enablers matching
590 * this event.
591 * called with session mutex held.
592 * TODO: currently, for each desc added, we iterate on all event desc
593 * (inefficient). We should create specific code that only target the
594 * added desc.
595 */
596 int lttng_fix_pending_event_desc(const struct lttng_event_desc *desc)
597 {
598 struct lttng_session *session;
599
600 cds_list_for_each_entry(session, &sessions, node) {
601 lttng_session_lazy_sync_enablers(session);
602 }
603 return 0;
604 }
605
606 /*
607 * Only used internally at session destruction.
608 */
609 int _lttng_event_unregister(struct lttng_event *event)
610 {
611 return __tracepoint_probe_unregister(event->desc->name,
612 event->desc->probe_callback,
613 event);
614 }
615
616 /*
617 * Only used internally at session destruction.
618 */
619 static
620 void _lttng_event_destroy(struct lttng_event *event)
621 {
622 struct lttng_enabler_ref *enabler_ref, *tmp_enabler_ref;
623
624 cds_list_del(&event->node);
625 lttng_destroy_context(event->ctx);
626 lttng_free_event_filter_runtime(event);
627 /* Free event enabler refs */
628 cds_list_for_each_entry_safe(enabler_ref, tmp_enabler_ref,
629 &event->enablers_ref_head, node)
630 free(enabler_ref);
631 free(event);
632 }
633
634 /*
635 * We have exclusive access to our metadata buffer (protected by the
636 * ust_lock), so we can do racy operations such as looking for
637 * remaining space left in packet and write, since mutual exclusion
638 * protects us from concurrent writes.
639 */
640 int lttng_metadata_printf(struct lttng_session *session,
641 const char *fmt, ...)
642 {
643 struct lttng_ust_lib_ring_buffer_ctx ctx;
644 struct lttng_channel *chan = session->metadata;
645 char *str = NULL;
646 int ret = 0, waitret;
647 size_t len, reserve_len, pos;
648 va_list ap;
649
650 WARN_ON_ONCE(!CMM_ACCESS_ONCE(session->active));
651
652 va_start(ap, fmt);
653 ret = vasprintf(&str, fmt, ap);
654 va_end(ap);
655 if (ret < 0)
656 return -ENOMEM;
657
658 len = strlen(str);
659 pos = 0;
660
661 for (pos = 0; pos < len; pos += reserve_len) {
662 reserve_len = min_t(size_t,
663 chan->ops->packet_avail_size(chan->chan, chan->handle),
664 len - pos);
665 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
666 sizeof(char), -1, chan->handle);
667 /*
668 * We don't care about metadata buffer's records lost
669 * count, because we always retry here. Report error if
670 * we need to bail out after timeout or being
671 * interrupted.
672 */
673 waitret = wait_cond_interruptible_timeout(
674 ({
675 ret = chan->ops->event_reserve(&ctx, 0);
676 ret != -ENOBUFS || !ret;
677 }),
678 LTTNG_METADATA_TIMEOUT_MSEC);
679 if (waitret == -ETIMEDOUT || waitret == -EINTR || ret) {
680 DBG("LTTng: Failure to write metadata to buffers (%s)\n",
681 waitret == -EINTR ? "interrupted" :
682 (ret == -ENOBUFS ? "timeout" : "I/O error"));
683 if (waitret == -EINTR)
684 ret = waitret;
685 goto end;
686 }
687 chan->ops->event_write(&ctx, &str[pos], reserve_len);
688 chan->ops->event_commit(&ctx);
689 }
690 end:
691 free(str);
692 return ret;
693 }
694
695 static
696 int _lttng_field_statedump(struct lttng_session *session,
697 const struct lttng_event_field *field)
698 {
699 int ret = 0;
700
701 if (field->nowrite)
702 return 0;
703
704 switch (field->type.atype) {
705 case atype_integer:
706 ret = lttng_metadata_printf(session,
707 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
708 field->type.u.basic.integer.size,
709 field->type.u.basic.integer.alignment,
710 field->type.u.basic.integer.signedness,
711 (field->type.u.basic.integer.encoding == lttng_encode_none)
712 ? "none"
713 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
714 ? "UTF8"
715 : "ASCII",
716 field->type.u.basic.integer.base,
717 #if (BYTE_ORDER == BIG_ENDIAN)
718 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
719 #else
720 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
721 #endif
722 field->name);
723 break;
724 case atype_float:
725 ret = lttng_metadata_printf(session,
726 " floating_point { exp_dig = %u; mant_dig = %u; align = %u;%s } _%s;\n",
727 field->type.u.basic._float.exp_dig,
728 field->type.u.basic._float.mant_dig,
729 field->type.u.basic._float.alignment,
730 #if (BYTE_ORDER == BIG_ENDIAN)
731 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
732 #else
733 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
734 #endif
735 field->name);
736 break;
737 case atype_enum:
738 ret = lttng_metadata_printf(session,
739 " %s %s;\n",
740 field->type.u.basic.enumeration.name,
741 field->name);
742 break;
743 case atype_array:
744 {
745 const struct lttng_basic_type *elem_type;
746
747 elem_type = &field->type.u.array.elem_type;
748 ret = lttng_metadata_printf(session,
749 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
750 elem_type->u.basic.integer.size,
751 elem_type->u.basic.integer.alignment,
752 elem_type->u.basic.integer.signedness,
753 (elem_type->u.basic.integer.encoding == lttng_encode_none)
754 ? "none"
755 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
756 ? "UTF8"
757 : "ASCII",
758 elem_type->u.basic.integer.base,
759 #if (BYTE_ORDER == BIG_ENDIAN)
760 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
761 #else
762 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
763 #endif
764 field->name, field->type.u.array.length);
765 break;
766 }
767 case atype_sequence:
768 {
769 const struct lttng_basic_type *elem_type;
770 const struct lttng_basic_type *length_type;
771
772 elem_type = &field->type.u.sequence.elem_type;
773 length_type = &field->type.u.sequence.length_type;
774 ret = lttng_metadata_printf(session,
775 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
776 length_type->u.basic.integer.size,
777 (unsigned int) length_type->u.basic.integer.alignment,
778 length_type->u.basic.integer.signedness,
779 (length_type->u.basic.integer.encoding == lttng_encode_none)
780 ? "none"
781 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
782 ? "UTF8"
783 : "ASCII"),
784 length_type->u.basic.integer.base,
785 #if (BYTE_ORDER == BIG_ENDIAN)
786 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
787 #else
788 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
789 #endif
790 field->name);
791 if (ret)
792 return ret;
793
794 ret = lttng_metadata_printf(session,
795 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
796 elem_type->u.basic.integer.size,
797 (unsigned int) elem_type->u.basic.integer.alignment,
798 elem_type->u.basic.integer.signedness,
799 (elem_type->u.basic.integer.encoding == lttng_encode_none)
800 ? "none"
801 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
802 ? "UTF8"
803 : "ASCII"),
804 elem_type->u.basic.integer.base,
805 #if (BYTE_ORDER == BIG_ENDIAN)
806 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
807 #else
808 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
809 #endif
810 field->name,
811 field->name);
812 break;
813 }
814
815 case atype_string:
816 /* Default encoding is UTF8 */
817 ret = lttng_metadata_printf(session,
818 " string%s _%s;\n",
819 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
820 " { encoding = ASCII; }" : "",
821 field->name);
822 break;
823 default:
824 WARN_ON_ONCE(1);
825 return -EINVAL;
826 }
827 return ret;
828 }
829
830 static
831 int _lttng_context_metadata_statedump(struct lttng_session *session,
832 struct lttng_ctx *ctx)
833 {
834 int ret = 0;
835 int i;
836
837 if (!ctx)
838 return 0;
839 for (i = 0; i < ctx->nr_fields; i++) {
840 const struct lttng_ctx_field *field = &ctx->fields[i];
841
842 ret = _lttng_field_statedump(session, &field->event_field);
843 if (ret)
844 return ret;
845 }
846 return ret;
847 }
848
849 static
850 int _lttng_fields_metadata_statedump(struct lttng_session *session,
851 struct lttng_event *event)
852 {
853 const struct lttng_event_desc *desc = event->desc;
854 int ret = 0;
855 int i;
856
857 for (i = 0; i < desc->nr_fields; i++) {
858 const struct lttng_event_field *field = &desc->fields[i];
859
860 ret = _lttng_field_statedump(session, field);
861 if (ret)
862 return ret;
863 }
864 return ret;
865 }
866
867 static
868 int _lttng_event_metadata_statedump(struct lttng_session *session,
869 struct lttng_channel *chan,
870 struct lttng_event *event)
871 {
872 int ret = 0;
873 int loglevel = TRACE_DEFAULT;
874
875 if (event->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
876 return 0;
877 if (chan == session->metadata)
878 return 0;
879 /*
880 * Don't print events for which probe load is pending.
881 */
882 if (!event->desc)
883 return 0;
884
885 ret = lttng_metadata_printf(session,
886 "event {\n"
887 " name = \"%s\";\n"
888 " id = %u;\n"
889 " stream_id = %u;\n",
890 event->desc->name,
891 event->id,
892 event->chan->id);
893 if (ret)
894 goto end;
895
896 if (event->desc->loglevel)
897 loglevel = *(*event->desc->loglevel);
898
899 ret = lttng_metadata_printf(session,
900 " loglevel = %d;\n",
901 loglevel);
902 if (ret)
903 goto end;
904
905 if (event->desc->u.ext.model_emf_uri) {
906 ret = lttng_metadata_printf(session,
907 " model.emf.uri = \"%s\";\n",
908 *(event->desc->u.ext.model_emf_uri));
909 if (ret)
910 goto end;
911 }
912
913 if (event->ctx) {
914 ret = lttng_metadata_printf(session,
915 " context := struct {\n");
916 if (ret)
917 goto end;
918 }
919 ret = _lttng_context_metadata_statedump(session, event->ctx);
920 if (ret)
921 goto end;
922 if (event->ctx) {
923 ret = lttng_metadata_printf(session,
924 " };\n");
925 if (ret)
926 goto end;
927 }
928
929 ret = lttng_metadata_printf(session,
930 " fields := struct {\n"
931 );
932 if (ret)
933 goto end;
934
935 ret = _lttng_fields_metadata_statedump(session, event);
936 if (ret)
937 goto end;
938
939 /*
940 * LTTng space reservation can only reserve multiples of the
941 * byte size.
942 */
943 ret = lttng_metadata_printf(session,
944 " };\n"
945 "};\n\n");
946 if (ret)
947 goto end;
948
949 event->metadata_dumped = 1;
950 end:
951 return ret;
952
953 }
954
955 static
956 int _lttng_channel_metadata_statedump(struct lttng_session *session,
957 struct lttng_channel *chan)
958 {
959 int ret = 0;
960
961 if (chan->metadata_dumped || !CMM_ACCESS_ONCE(session->active))
962 return 0;
963 if (chan == session->metadata)
964 return 0;
965
966 WARN_ON_ONCE(!chan->header_type);
967 ret = lttng_metadata_printf(session,
968 "stream {\n"
969 " id = %u;\n"
970 " event.header := %s;\n"
971 " packet.context := struct packet_context;\n",
972 chan->id,
973 chan->header_type == 1 ? "struct event_header_compact" :
974 "struct event_header_large");
975 if (ret)
976 goto end;
977
978 if (chan->ctx) {
979 ret = lttng_metadata_printf(session,
980 " event.context := struct {\n");
981 if (ret)
982 goto end;
983 }
984 ret = _lttng_context_metadata_statedump(session, chan->ctx);
985 if (ret)
986 goto end;
987 if (chan->ctx) {
988 ret = lttng_metadata_printf(session,
989 " };\n");
990 if (ret)
991 goto end;
992 }
993
994 ret = lttng_metadata_printf(session,
995 "};\n\n");
996
997 chan->metadata_dumped = 1;
998 end:
999 return ret;
1000 }
1001
1002 static
1003 int _lttng_stream_packet_context_declare(struct lttng_session *session)
1004 {
1005 return lttng_metadata_printf(session,
1006 "struct packet_context {\n"
1007 " uint64_clock_monotonic_t timestamp_begin;\n"
1008 " uint64_clock_monotonic_t timestamp_end;\n"
1009 " uint64_t content_size;\n"
1010 " uint64_t packet_size;\n"
1011 " unsigned long events_discarded;\n"
1012 " uint32_t cpu_id;\n"
1013 "};\n\n"
1014 );
1015 }
1016
1017 /*
1018 * Compact header:
1019 * id: range: 0 - 30.
1020 * id 31 is reserved to indicate an extended header.
1021 *
1022 * Large header:
1023 * id: range: 0 - 65534.
1024 * id 65535 is reserved to indicate an extended header.
1025 */
1026 static
1027 int _lttng_event_header_declare(struct lttng_session *session)
1028 {
1029 return lttng_metadata_printf(session,
1030 "struct event_header_compact {\n"
1031 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
1032 " variant <id> {\n"
1033 " struct {\n"
1034 " uint27_clock_monotonic_t timestamp;\n"
1035 " } compact;\n"
1036 " struct {\n"
1037 " uint32_t id;\n"
1038 " uint64_clock_monotonic_t timestamp;\n"
1039 " } extended;\n"
1040 " } v;\n"
1041 "} align(%u);\n"
1042 "\n"
1043 "struct event_header_large {\n"
1044 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
1045 " variant <id> {\n"
1046 " struct {\n"
1047 " uint32_clock_monotonic_t timestamp;\n"
1048 " } compact;\n"
1049 " struct {\n"
1050 " uint32_t id;\n"
1051 " uint64_clock_monotonic_t timestamp;\n"
1052 " } extended;\n"
1053 " } v;\n"
1054 "} align(%u);\n\n",
1055 lttng_alignof(uint32_t) * CHAR_BIT,
1056 lttng_alignof(uint16_t) * CHAR_BIT
1057 );
1058 }
1059
1060 /*
1061 * Approximation of NTP time of day to clock monotonic correlation,
1062 * taken at start of trace.
1063 * Yes, this is only an approximation. Yes, we can (and will) do better
1064 * in future versions.
1065 */
1066 static
1067 uint64_t measure_clock_offset(void)
1068 {
1069 uint64_t offset, monotonic[2], realtime;
1070 struct timespec rts = { 0, 0 };
1071 int ret;
1072
1073 monotonic[0] = trace_clock_read64();
1074 ret = clock_gettime(CLOCK_REALTIME, &rts);
1075 if (ret < 0)
1076 return 0;
1077 monotonic[1] = trace_clock_read64();
1078 offset = (monotonic[0] + monotonic[1]) >> 1;
1079 realtime = (uint64_t) rts.tv_sec * 1000000000ULL;
1080 realtime += rts.tv_nsec;
1081 offset = realtime - offset;
1082 return offset;
1083 }
1084
1085 /*
1086 * Output metadata into this session's metadata buffers.
1087 */
1088 static
1089 int _lttng_session_metadata_statedump(struct lttng_session *session)
1090 {
1091 unsigned char *uuid_c = session->uuid;
1092 char uuid_s[LTTNG_UST_UUID_STR_LEN],
1093 clock_uuid_s[LTTNG_UST_UUID_STR_LEN];
1094 struct lttng_channel *chan;
1095 struct lttng_event *event;
1096 int ret = 0;
1097 char procname[LTTNG_UST_PROCNAME_LEN] = "";
1098 char hostname[HOST_NAME_MAX];
1099
1100 if (!CMM_ACCESS_ONCE(session->active))
1101 return 0;
1102 if (session->metadata_dumped)
1103 goto skip_session;
1104 if (!session->metadata) {
1105 DBG("LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
1106 return -EPERM;
1107 }
1108
1109 snprintf(uuid_s, sizeof(uuid_s),
1110 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1111 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1112 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1113 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1114 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
1115
1116 ret = lttng_metadata_printf(session,
1117 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1118 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1119 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1120 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
1121 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
1122 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1123 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
1124 "\n"
1125 "trace {\n"
1126 " major = %u;\n"
1127 " minor = %u;\n"
1128 " uuid = \"%s\";\n"
1129 " byte_order = %s;\n"
1130 " packet.header := struct {\n"
1131 " uint32_t magic;\n"
1132 " uint8_t uuid[16];\n"
1133 " uint32_t stream_id;\n"
1134 " };\n"
1135 "};\n\n",
1136 lttng_alignof(uint8_t) * CHAR_BIT,
1137 lttng_alignof(uint16_t) * CHAR_BIT,
1138 lttng_alignof(uint32_t) * CHAR_BIT,
1139 lttng_alignof(uint64_t) * CHAR_BIT,
1140 sizeof(unsigned long) * CHAR_BIT,
1141 lttng_alignof(unsigned long) * CHAR_BIT,
1142 CTF_SPEC_MAJOR,
1143 CTF_SPEC_MINOR,
1144 uuid_s,
1145 #if (BYTE_ORDER == BIG_ENDIAN)
1146 "be"
1147 #else
1148 "le"
1149 #endif
1150 );
1151 if (ret)
1152 goto end;
1153
1154 /* ignore error, just use empty string if error. */
1155 hostname[0] = '\0';
1156 ret = gethostname(hostname, sizeof(hostname));
1157 if (ret && errno == ENAMETOOLONG)
1158 hostname[HOST_NAME_MAX - 1] = '\0';
1159 lttng_ust_getprocname(procname);
1160 procname[LTTNG_UST_PROCNAME_LEN - 1] = '\0';
1161 ret = lttng_metadata_printf(session,
1162 "env {\n"
1163 " hostname = \"%s\";\n"
1164 " vpid = %d;\n"
1165 " procname = \"%s\";\n"
1166 " domain = \"ust\";\n"
1167 " tracer_name = \"lttng-ust\";\n"
1168 " tracer_major = %u;\n"
1169 " tracer_minor = %u;\n"
1170 " tracer_patchlevel = %u;\n"
1171 "};\n\n",
1172 hostname,
1173 (int) getpid(),
1174 procname,
1175 LTTNG_UST_MAJOR_VERSION,
1176 LTTNG_UST_MINOR_VERSION,
1177 LTTNG_UST_PATCHLEVEL_VERSION
1178 );
1179 if (ret)
1180 goto end;
1181
1182 ret = lttng_metadata_printf(session,
1183 "clock {\n"
1184 " name = %s;\n",
1185 "monotonic"
1186 );
1187 if (ret)
1188 goto end;
1189
1190 if (!trace_clock_uuid(clock_uuid_s)) {
1191 ret = lttng_metadata_printf(session,
1192 " uuid = \"%s\";\n",
1193 clock_uuid_s
1194 );
1195 if (ret)
1196 goto end;
1197 }
1198
1199 ret = lttng_metadata_printf(session,
1200 " description = \"Monotonic Clock\";\n"
1201 " freq = %" PRIu64 "; /* Frequency, in Hz */\n"
1202 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1203 " offset = %" PRIu64 ";\n"
1204 "};\n\n",
1205 trace_clock_freq(),
1206 measure_clock_offset()
1207 );
1208 if (ret)
1209 goto end;
1210
1211 ret = lttng_metadata_printf(session,
1212 "typealias integer {\n"
1213 " size = 27; align = 1; signed = false;\n"
1214 " map = clock.monotonic.value;\n"
1215 "} := uint27_clock_monotonic_t;\n"
1216 "\n"
1217 "typealias integer {\n"
1218 " size = 32; align = %u; signed = false;\n"
1219 " map = clock.monotonic.value;\n"
1220 "} := uint32_clock_monotonic_t;\n"
1221 "\n"
1222 "typealias integer {\n"
1223 " size = 64; align = %u; signed = false;\n"
1224 " map = clock.monotonic.value;\n"
1225 "} := uint64_clock_monotonic_t;\n\n",
1226 lttng_alignof(uint32_t) * CHAR_BIT,
1227 lttng_alignof(uint64_t) * CHAR_BIT
1228 );
1229 if (ret)
1230 goto end;
1231
1232 ret = _lttng_stream_packet_context_declare(session);
1233 if (ret)
1234 goto end;
1235
1236 ret = _lttng_event_header_declare(session);
1237 if (ret)
1238 goto end;
1239
1240 skip_session:
1241 cds_list_for_each_entry(chan, &session->chan_head, node) {
1242 ret = _lttng_channel_metadata_statedump(session, chan);
1243 if (ret)
1244 goto end;
1245 }
1246
1247 cds_list_for_each_entry(event, &session->events_head, node) {
1248 ret = _lttng_event_metadata_statedump(session, event->chan, event);
1249 if (ret)
1250 goto end;
1251 }
1252 session->metadata_dumped = 1;
1253 end:
1254 return ret;
1255 }
1256
1257 void lttng_ust_events_exit(void)
1258 {
1259 struct lttng_session *session, *tmpsession;
1260
1261 cds_list_for_each_entry_safe(session, tmpsession, &sessions, node)
1262 lttng_session_destroy(session);
1263 }
1264
1265 /*
1266 * Enabler management.
1267 */
1268 struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1269 struct lttng_ust_event *event_param,
1270 struct lttng_channel *chan)
1271 {
1272 struct lttng_enabler *enabler;
1273
1274 enabler = zmalloc(sizeof(*enabler));
1275 if (!enabler)
1276 return NULL;
1277 enabler->type = type;
1278 CDS_INIT_LIST_HEAD(&enabler->filter_bytecode_head);
1279 memcpy(&enabler->event_param, event_param,
1280 sizeof(enabler->event_param));
1281 enabler->chan = chan;
1282 /* ctx left NULL */
1283 enabler->enabled = 1;
1284 cds_list_add(&enabler->node, &enabler->chan->session->enablers_head);
1285 lttng_session_lazy_sync_enablers(enabler->chan->session);
1286 return enabler;
1287 }
1288
1289 int lttng_enabler_enable(struct lttng_enabler *enabler)
1290 {
1291 enabler->enabled = 1;
1292 lttng_session_lazy_sync_enablers(enabler->chan->session);
1293 return 0;
1294 }
1295
1296 int lttng_enabler_disable(struct lttng_enabler *enabler)
1297 {
1298 if (enabler->chan == enabler->chan->session->metadata)
1299 return -EPERM;
1300 enabler->enabled = 0;
1301 lttng_session_lazy_sync_enablers(enabler->chan->session);
1302 return 0;
1303 }
1304
1305 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1306 struct lttng_ust_filter_bytecode_node *bytecode)
1307 {
1308 bytecode->enabler = enabler;
1309 cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head);
1310 lttng_session_lazy_sync_enablers(enabler->chan->session);
1311 return 0;
1312 }
1313
1314 int lttng_attach_context(struct lttng_ust_context *context_param,
1315 struct lttng_ctx **ctx, struct lttng_session *session)
1316 {
1317 /*
1318 * We cannot attach a context after trace has been started for a
1319 * session because the metadata does not allow expressing this
1320 * information outside of the original channel scope.
1321 */
1322 if (session->been_active)
1323 return -EPERM;
1324
1325 switch (context_param->ctx) {
1326 case LTTNG_UST_CONTEXT_PTHREAD_ID:
1327 return lttng_add_pthread_id_to_ctx(ctx);
1328 case LTTNG_UST_CONTEXT_VTID:
1329 return lttng_add_vtid_to_ctx(ctx);
1330 case LTTNG_UST_CONTEXT_VPID:
1331 return lttng_add_vpid_to_ctx(ctx);
1332 case LTTNG_UST_CONTEXT_PROCNAME:
1333 return lttng_add_procname_to_ctx(ctx);
1334 default:
1335 return -EINVAL;
1336 }
1337 }
1338
1339 int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1340 struct lttng_ust_context *context_param)
1341 {
1342 #if 0 // disabled for now.
1343 struct lttng_session *session = enabler->chan->session;
1344 int ret;
1345
1346 ret = lttng_attach_context(context_param, &enabler->ctx,
1347 session);
1348 if (ret)
1349 return ret;
1350 lttng_session_lazy_sync_enablers(enabler->chan->session);
1351 #endif
1352 return -ENOSYS;
1353 }
1354
1355 static
1356 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1357 {
1358 struct lttng_ust_filter_bytecode_node *filter_node, *tmp_filter_node;
1359
1360 /* Destroy filter bytecode */
1361 cds_list_for_each_entry_safe(filter_node, tmp_filter_node,
1362 &enabler->filter_bytecode_head, node) {
1363 free(filter_node);
1364 }
1365
1366 /* Destroy contexts */
1367 lttng_destroy_context(enabler->ctx);
1368
1369 cds_list_del(&enabler->node);
1370 free(enabler);
1371 }
1372
1373 /*
1374 * lttng_session_sync_enablers should be called just before starting a
1375 * session.
1376 */
1377 static
1378 void lttng_session_sync_enablers(struct lttng_session *session)
1379 {
1380 struct lttng_enabler *enabler;
1381 struct lttng_event *event;
1382
1383 cds_list_for_each_entry(enabler, &session->enablers_head, node)
1384 lttng_enabler_ref_events(enabler);
1385 /*
1386 * For each event, if at least one of its enablers is enabled,
1387 * we enable the event, else we disable it.
1388 */
1389 cds_list_for_each_entry(event, &session->events_head, node) {
1390 struct lttng_enabler_ref *enabler_ref;
1391 struct lttng_bytecode_runtime *runtime;
1392 int enabled = 0, has_enablers_without_bytecode = 0;
1393
1394 /* Enable events */
1395 cds_list_for_each_entry(enabler_ref,
1396 &event->enablers_ref_head, node) {
1397 if (enabler_ref->ref->enabled) {
1398 enabled = 1;
1399 break;
1400 }
1401 }
1402 event->enabled = enabled;
1403
1404 /* Check if has enablers without bytecode enabled */
1405 cds_list_for_each_entry(enabler_ref,
1406 &event->enablers_ref_head, node) {
1407 if (enabler_ref->ref->enabled
1408 && cds_list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1409 has_enablers_without_bytecode = 1;
1410 break;
1411 }
1412 }
1413 event->has_enablers_without_bytecode =
1414 has_enablers_without_bytecode;
1415
1416 /* Enable filters */
1417 cds_list_for_each_entry(runtime,
1418 &event->bytecode_runtime_head, node) {
1419 lttng_filter_sync_state(runtime);
1420 }
1421 }
1422 }
1423
1424 /*
1425 * Apply enablers to session events, adding events to session if need
1426 * be. It is required after each modification applied to an active
1427 * session, and right before session "start".
1428 * "lazy" sync means we only sync if required.
1429 */
1430 static
1431 void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1432 {
1433 /* We can skip if session is not active */
1434 if (!session->active)
1435 return;
1436 lttng_session_sync_enablers(session);
1437 }
1438
1439 /*
1440 * Take the TLS "fault" in libuuid if dlopen'd, which can take the
1441 * dynamic linker mutex, outside of the UST lock, since the UST lock is
1442 * taken in constructors, which are called with dynamic linker mutex
1443 * held.
1444 */
1445 void lttng_fixup_event_tls(void)
1446 {
1447 unsigned char uuid[LTTNG_UST_UUID_STR_LEN];
1448
1449 (void) lttng_ust_uuid_generate(uuid);
1450 }
This page took 0.062221 seconds and 5 git commands to generate.