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