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