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