Rename `lttng_create_*_if_missing()` in anticipation of event notifiers
[lttng-modules.git] / src / lttng-events.c
1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
2 *
3 * lttng-events.c
4 *
5 * Holds LTTng per-session event registry.
6 *
7 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 */
9
10 /*
11 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
12 * overrides a function with a define.
13 */
14 #include "wrapper/page_alloc.h"
15
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/sched.h>
19 #include <linux/slab.h>
20 #include <linux/jiffies.h>
21 #include <linux/utsname.h>
22 #include <linux/err.h>
23 #include <linux/seq_file.h>
24 #include <linux/file.h>
25 #include <linux/anon_inodes.h>
26 #include <wrapper/file.h>
27 #include <linux/jhash.h>
28 #include <linux/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <linux/dmi.h>
31
32 #include <wrapper/uuid.h>
33 #include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_mappings() */
34 #include <wrapper/random.h>
35 #include <wrapper/tracepoint.h>
36 #include <wrapper/list.h>
37 #include <wrapper/types.h>
38 #include <lttng/kernel-version.h>
39 #include <lttng/events.h>
40 #include <lttng/tracer.h>
41 #include <lttng/abi-old.h>
42 #include <lttng/endian.h>
43 #include <lttng/string-utils.h>
44 #include <ringbuffer/backend.h>
45 #include <ringbuffer/frontend.h>
46 #include <wrapper/time.h>
47
48 #define METADATA_CACHE_DEFAULT_SIZE 4096
49
50 static LIST_HEAD(sessions);
51 static LIST_HEAD(lttng_transport_list);
52 /*
53 * Protect the sessions and metadata caches.
54 */
55 static DEFINE_MUTEX(sessions_mutex);
56 static struct kmem_cache *event_cache;
57
58 static void lttng_session_lazy_sync_event_enablers(struct lttng_session *session);
59 static void lttng_session_sync_event_enablers(struct lttng_session *session);
60 static void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler);
61
62 static void _lttng_event_destroy(struct lttng_event *event);
63 static void _lttng_channel_destroy(struct lttng_channel *chan);
64 static int _lttng_event_unregister(struct lttng_event *event);
65 static
66 int _lttng_event_metadata_statedump(struct lttng_session *session,
67 struct lttng_channel *chan,
68 struct lttng_event *event);
69 static
70 int _lttng_session_metadata_statedump(struct lttng_session *session);
71 static
72 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
73 static
74 int _lttng_type_statedump(struct lttng_session *session,
75 const struct lttng_type *type,
76 size_t nesting);
77 static
78 int _lttng_field_statedump(struct lttng_session *session,
79 const struct lttng_event_field *field,
80 size_t nesting);
81
82 void synchronize_trace(void)
83 {
84 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(5,1,0))
85 synchronize_rcu();
86 #else
87 synchronize_sched();
88 #endif
89
90 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
91 #ifdef CONFIG_PREEMPT_RT_FULL
92 synchronize_rcu();
93 #endif
94 #else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
95 #ifdef CONFIG_PREEMPT_RT
96 synchronize_rcu();
97 #endif
98 #endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
99 }
100
101 void lttng_lock_sessions(void)
102 {
103 mutex_lock(&sessions_mutex);
104 }
105
106 void lttng_unlock_sessions(void)
107 {
108 mutex_unlock(&sessions_mutex);
109 }
110
111 /*
112 * Called with sessions lock held.
113 */
114 int lttng_session_active(void)
115 {
116 struct lttng_session *iter;
117
118 list_for_each_entry(iter, &sessions, list) {
119 if (iter->active)
120 return 1;
121 }
122 return 0;
123 }
124
125 struct lttng_session *lttng_session_create(void)
126 {
127 struct lttng_session *session;
128 struct lttng_metadata_cache *metadata_cache;
129 int i;
130
131 mutex_lock(&sessions_mutex);
132 session = lttng_kvzalloc(sizeof(struct lttng_session), GFP_KERNEL);
133 if (!session)
134 goto err;
135 INIT_LIST_HEAD(&session->chan);
136 INIT_LIST_HEAD(&session->events);
137 lttng_guid_gen(&session->uuid);
138
139 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
140 GFP_KERNEL);
141 if (!metadata_cache)
142 goto err_free_session;
143 metadata_cache->data = vzalloc(METADATA_CACHE_DEFAULT_SIZE);
144 if (!metadata_cache->data)
145 goto err_free_cache;
146 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
147 kref_init(&metadata_cache->refcount);
148 mutex_init(&metadata_cache->lock);
149 session->metadata_cache = metadata_cache;
150 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
151 memcpy(&metadata_cache->uuid, &session->uuid,
152 sizeof(metadata_cache->uuid));
153 INIT_LIST_HEAD(&session->enablers_head);
154 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
155 INIT_HLIST_HEAD(&session->events_ht.table[i]);
156 list_add(&session->list, &sessions);
157 session->pid_tracker.session = session;
158 session->pid_tracker.tracker_type = TRACKER_PID;
159 session->vpid_tracker.session = session;
160 session->vpid_tracker.tracker_type = TRACKER_VPID;
161 session->uid_tracker.session = session;
162 session->uid_tracker.tracker_type = TRACKER_UID;
163 session->vuid_tracker.session = session;
164 session->vuid_tracker.tracker_type = TRACKER_VUID;
165 session->gid_tracker.session = session;
166 session->gid_tracker.tracker_type = TRACKER_GID;
167 session->vgid_tracker.session = session;
168 session->vgid_tracker.tracker_type = TRACKER_VGID;
169 mutex_unlock(&sessions_mutex);
170 return session;
171
172 err_free_cache:
173 kfree(metadata_cache);
174 err_free_session:
175 lttng_kvfree(session);
176 err:
177 mutex_unlock(&sessions_mutex);
178 return NULL;
179 }
180
181 void metadata_cache_destroy(struct kref *kref)
182 {
183 struct lttng_metadata_cache *cache =
184 container_of(kref, struct lttng_metadata_cache, refcount);
185 vfree(cache->data);
186 kfree(cache);
187 }
188
189 void lttng_session_destroy(struct lttng_session *session)
190 {
191 struct lttng_channel *chan, *tmpchan;
192 struct lttng_event *event, *tmpevent;
193 struct lttng_metadata_stream *metadata_stream;
194 struct lttng_event_enabler *event_enabler, *tmp_event_enabler;
195 int ret;
196
197 mutex_lock(&sessions_mutex);
198 WRITE_ONCE(session->active, 0);
199 list_for_each_entry(chan, &session->chan, list) {
200 ret = lttng_syscalls_unregister(chan);
201 WARN_ON(ret);
202 }
203 list_for_each_entry(event, &session->events, list) {
204 ret = _lttng_event_unregister(event);
205 WARN_ON(ret);
206 }
207 synchronize_trace(); /* Wait for in-flight events to complete */
208 list_for_each_entry(chan, &session->chan, list) {
209 ret = lttng_syscalls_destroy(chan);
210 WARN_ON(ret);
211 }
212 list_for_each_entry_safe(event_enabler, tmp_event_enabler,
213 &session->enablers_head, node)
214 lttng_event_enabler_destroy(event_enabler);
215 list_for_each_entry_safe(event, tmpevent, &session->events, list)
216 _lttng_event_destroy(event);
217 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
218 BUG_ON(chan->channel_type == METADATA_CHANNEL);
219 _lttng_channel_destroy(chan);
220 }
221 mutex_lock(&session->metadata_cache->lock);
222 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
223 _lttng_metadata_channel_hangup(metadata_stream);
224 mutex_unlock(&session->metadata_cache->lock);
225 lttng_id_tracker_destroy(&session->pid_tracker, false);
226 lttng_id_tracker_destroy(&session->vpid_tracker, false);
227 lttng_id_tracker_destroy(&session->uid_tracker, false);
228 lttng_id_tracker_destroy(&session->vuid_tracker, false);
229 lttng_id_tracker_destroy(&session->gid_tracker, false);
230 lttng_id_tracker_destroy(&session->vgid_tracker, false);
231 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
232 list_del(&session->list);
233 mutex_unlock(&sessions_mutex);
234 lttng_kvfree(session);
235 }
236
237 int lttng_session_statedump(struct lttng_session *session)
238 {
239 int ret;
240
241 mutex_lock(&sessions_mutex);
242 ret = lttng_statedump_start(session);
243 mutex_unlock(&sessions_mutex);
244 return ret;
245 }
246
247 int lttng_session_enable(struct lttng_session *session)
248 {
249 int ret = 0;
250 struct lttng_channel *chan;
251
252 mutex_lock(&sessions_mutex);
253 if (session->active) {
254 ret = -EBUSY;
255 goto end;
256 }
257
258 /* Set transient enabler state to "enabled" */
259 session->tstate = 1;
260
261 /* We need to sync enablers with session before activation. */
262 lttng_session_sync_event_enablers(session);
263
264 /*
265 * Snapshot the number of events per channel to know the type of header
266 * we need to use.
267 */
268 list_for_each_entry(chan, &session->chan, list) {
269 if (chan->header_type)
270 continue; /* don't change it if session stop/restart */
271 if (chan->free_event_id < 31)
272 chan->header_type = 1; /* compact */
273 else
274 chan->header_type = 2; /* large */
275 }
276
277 /* Clear each stream's quiescent state. */
278 list_for_each_entry(chan, &session->chan, list) {
279 if (chan->channel_type != METADATA_CHANNEL)
280 lib_ring_buffer_clear_quiescent_channel(chan->chan);
281 }
282
283 WRITE_ONCE(session->active, 1);
284 WRITE_ONCE(session->been_active, 1);
285 ret = _lttng_session_metadata_statedump(session);
286 if (ret) {
287 WRITE_ONCE(session->active, 0);
288 goto end;
289 }
290 ret = lttng_statedump_start(session);
291 if (ret)
292 WRITE_ONCE(session->active, 0);
293 end:
294 mutex_unlock(&sessions_mutex);
295 return ret;
296 }
297
298 int lttng_session_disable(struct lttng_session *session)
299 {
300 int ret = 0;
301 struct lttng_channel *chan;
302
303 mutex_lock(&sessions_mutex);
304 if (!session->active) {
305 ret = -EBUSY;
306 goto end;
307 }
308 WRITE_ONCE(session->active, 0);
309
310 /* Set transient enabler state to "disabled" */
311 session->tstate = 0;
312 lttng_session_sync_event_enablers(session);
313
314 /* Set each stream's quiescent state. */
315 list_for_each_entry(chan, &session->chan, list) {
316 if (chan->channel_type != METADATA_CHANNEL)
317 lib_ring_buffer_set_quiescent_channel(chan->chan);
318 }
319 end:
320 mutex_unlock(&sessions_mutex);
321 return ret;
322 }
323
324 int lttng_session_metadata_regenerate(struct lttng_session *session)
325 {
326 int ret = 0;
327 struct lttng_channel *chan;
328 struct lttng_event *event;
329 struct lttng_metadata_cache *cache = session->metadata_cache;
330 struct lttng_metadata_stream *stream;
331
332 mutex_lock(&sessions_mutex);
333 if (!session->active) {
334 ret = -EBUSY;
335 goto end;
336 }
337
338 mutex_lock(&cache->lock);
339 memset(cache->data, 0, cache->cache_alloc);
340 cache->metadata_written = 0;
341 cache->version++;
342 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
343 stream->metadata_out = 0;
344 stream->metadata_in = 0;
345 }
346 mutex_unlock(&cache->lock);
347
348 session->metadata_dumped = 0;
349 list_for_each_entry(chan, &session->chan, list) {
350 chan->metadata_dumped = 0;
351 }
352
353 list_for_each_entry(event, &session->events, list) {
354 event->metadata_dumped = 0;
355 }
356
357 ret = _lttng_session_metadata_statedump(session);
358
359 end:
360 mutex_unlock(&sessions_mutex);
361 return ret;
362 }
363
364 int lttng_channel_enable(struct lttng_channel *channel)
365 {
366 int ret = 0;
367
368 mutex_lock(&sessions_mutex);
369 if (channel->channel_type == METADATA_CHANNEL) {
370 ret = -EPERM;
371 goto end;
372 }
373 if (channel->enabled) {
374 ret = -EEXIST;
375 goto end;
376 }
377 /* Set transient enabler state to "enabled" */
378 channel->tstate = 1;
379 lttng_session_sync_event_enablers(channel->session);
380 /* Set atomically the state to "enabled" */
381 WRITE_ONCE(channel->enabled, 1);
382 end:
383 mutex_unlock(&sessions_mutex);
384 return ret;
385 }
386
387 int lttng_channel_disable(struct lttng_channel *channel)
388 {
389 int ret = 0;
390
391 mutex_lock(&sessions_mutex);
392 if (channel->channel_type == METADATA_CHANNEL) {
393 ret = -EPERM;
394 goto end;
395 }
396 if (!channel->enabled) {
397 ret = -EEXIST;
398 goto end;
399 }
400 /* Set atomically the state to "disabled" */
401 WRITE_ONCE(channel->enabled, 0);
402 /* Set transient enabler state to "enabled" */
403 channel->tstate = 0;
404 lttng_session_sync_event_enablers(channel->session);
405 end:
406 mutex_unlock(&sessions_mutex);
407 return ret;
408 }
409
410 int lttng_event_enable(struct lttng_event *event)
411 {
412 int ret = 0;
413
414 mutex_lock(&sessions_mutex);
415 if (event->chan->channel_type == METADATA_CHANNEL) {
416 ret = -EPERM;
417 goto end;
418 }
419 if (event->enabled) {
420 ret = -EEXIST;
421 goto end;
422 }
423 switch (event->instrumentation) {
424 case LTTNG_KERNEL_TRACEPOINT:
425 case LTTNG_KERNEL_SYSCALL:
426 ret = -EINVAL;
427 break;
428 case LTTNG_KERNEL_KPROBE:
429 case LTTNG_KERNEL_UPROBE:
430 case LTTNG_KERNEL_NOOP:
431 WRITE_ONCE(event->enabled, 1);
432 break;
433 case LTTNG_KERNEL_KRETPROBE:
434 ret = lttng_kretprobes_event_enable_state(event, 1);
435 break;
436 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
437 default:
438 WARN_ON_ONCE(1);
439 ret = -EINVAL;
440 }
441 end:
442 mutex_unlock(&sessions_mutex);
443 return ret;
444 }
445
446 int lttng_event_disable(struct lttng_event *event)
447 {
448 int ret = 0;
449
450 mutex_lock(&sessions_mutex);
451 if (event->chan->channel_type == METADATA_CHANNEL) {
452 ret = -EPERM;
453 goto end;
454 }
455 if (!event->enabled) {
456 ret = -EEXIST;
457 goto end;
458 }
459 switch (event->instrumentation) {
460 case LTTNG_KERNEL_TRACEPOINT:
461 case LTTNG_KERNEL_SYSCALL:
462 ret = -EINVAL;
463 break;
464 case LTTNG_KERNEL_KPROBE:
465 case LTTNG_KERNEL_UPROBE:
466 case LTTNG_KERNEL_NOOP:
467 WRITE_ONCE(event->enabled, 0);
468 break;
469 case LTTNG_KERNEL_KRETPROBE:
470 ret = lttng_kretprobes_event_enable_state(event, 0);
471 break;
472 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
473 default:
474 WARN_ON_ONCE(1);
475 ret = -EINVAL;
476 }
477 end:
478 mutex_unlock(&sessions_mutex);
479 return ret;
480 }
481
482 static struct lttng_transport *lttng_transport_find(const char *name)
483 {
484 struct lttng_transport *transport;
485
486 list_for_each_entry(transport, &lttng_transport_list, node) {
487 if (!strcmp(transport->name, name))
488 return transport;
489 }
490 return NULL;
491 }
492
493 struct lttng_channel *lttng_channel_create(struct lttng_session *session,
494 const char *transport_name,
495 void *buf_addr,
496 size_t subbuf_size, size_t num_subbuf,
497 unsigned int switch_timer_interval,
498 unsigned int read_timer_interval,
499 enum channel_type channel_type)
500 {
501 struct lttng_channel *chan;
502 struct lttng_transport *transport = NULL;
503
504 mutex_lock(&sessions_mutex);
505 if (session->been_active && channel_type != METADATA_CHANNEL)
506 goto active; /* Refuse to add channel to active session */
507 transport = lttng_transport_find(transport_name);
508 if (!transport) {
509 printk(KERN_WARNING "LTTng: transport %s not found\n",
510 transport_name);
511 goto notransport;
512 }
513 if (!try_module_get(transport->owner)) {
514 printk(KERN_WARNING "LTTng: Can't lock transport module.\n");
515 goto notransport;
516 }
517 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
518 if (!chan)
519 goto nomem;
520 chan->session = session;
521 chan->id = session->free_chan_id++;
522 chan->ops = &transport->ops;
523 /*
524 * Note: the channel creation op already writes into the packet
525 * headers. Therefore the "chan" information used as input
526 * should be already accessible.
527 */
528 chan->chan = transport->ops.channel_create(transport_name,
529 chan, buf_addr, subbuf_size, num_subbuf,
530 switch_timer_interval, read_timer_interval);
531 if (!chan->chan)
532 goto create_error;
533 chan->tstate = 1;
534 chan->enabled = 1;
535 chan->transport = transport;
536 chan->channel_type = channel_type;
537 list_add(&chan->list, &session->chan);
538 mutex_unlock(&sessions_mutex);
539 return chan;
540
541 create_error:
542 kfree(chan);
543 nomem:
544 if (transport)
545 module_put(transport->owner);
546 notransport:
547 active:
548 mutex_unlock(&sessions_mutex);
549 return NULL;
550 }
551
552 /*
553 * Only used internally at session destruction for per-cpu channels, and
554 * when metadata channel is released.
555 * Needs to be called with sessions mutex held.
556 */
557 static
558 void _lttng_channel_destroy(struct lttng_channel *chan)
559 {
560 chan->ops->channel_destroy(chan->chan);
561 module_put(chan->transport->owner);
562 list_del(&chan->list);
563 lttng_destroy_context(chan->ctx);
564 kfree(chan);
565 }
566
567 void lttng_metadata_channel_destroy(struct lttng_channel *chan)
568 {
569 BUG_ON(chan->channel_type != METADATA_CHANNEL);
570
571 /* Protect the metadata cache with the sessions_mutex. */
572 mutex_lock(&sessions_mutex);
573 _lttng_channel_destroy(chan);
574 mutex_unlock(&sessions_mutex);
575 }
576 EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
577
578 static
579 void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
580 {
581 stream->finalized = 1;
582 wake_up_interruptible(&stream->read_wait);
583 }
584
585 /*
586 * Supports event creation while tracing session is active.
587 * Needs to be called with sessions mutex held.
588 */
589 struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
590 struct lttng_kernel_event *event_param,
591 void *filter,
592 const struct lttng_event_desc *event_desc,
593 enum lttng_kernel_instrumentation itype)
594 {
595 struct lttng_session *session = chan->session;
596 struct lttng_event *event;
597 const char *event_name;
598 struct hlist_head *head;
599 size_t name_len;
600 uint32_t hash;
601 int ret;
602
603 if (chan->free_event_id == -1U) {
604 ret = -EMFILE;
605 goto full;
606 }
607
608 switch (itype) {
609 case LTTNG_KERNEL_TRACEPOINT:
610 event_name = event_desc->name;
611 break;
612 case LTTNG_KERNEL_KPROBE:
613 case LTTNG_KERNEL_UPROBE:
614 case LTTNG_KERNEL_KRETPROBE:
615 case LTTNG_KERNEL_NOOP:
616 case LTTNG_KERNEL_SYSCALL:
617 event_name = event_param->name;
618 break;
619 case LTTNG_KERNEL_FUNCTION: /* Fall-through. */
620 default:
621 WARN_ON_ONCE(1);
622 ret = -EINVAL;
623 goto type_error;
624 }
625 name_len = strlen(event_name);
626 hash = jhash(event_name, name_len, 0);
627 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
628 lttng_hlist_for_each_entry(event, head, hlist) {
629 WARN_ON_ONCE(!event->desc);
630 if (!strncmp(event->desc->name, event_name,
631 LTTNG_KERNEL_SYM_NAME_LEN - 1)
632 && chan == event->chan) {
633 ret = -EEXIST;
634 goto exist;
635 }
636 }
637
638 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
639 if (!event) {
640 ret = -ENOMEM;
641 goto cache_error;
642 }
643 event->chan = chan;
644 event->filter = filter;
645 event->id = chan->free_event_id++;
646 event->instrumentation = itype;
647 event->evtype = LTTNG_TYPE_EVENT;
648 INIT_LIST_HEAD(&event->bytecode_runtime_head);
649 INIT_LIST_HEAD(&event->enablers_ref_head);
650
651 switch (itype) {
652 case LTTNG_KERNEL_TRACEPOINT:
653 /* Event will be enabled by enabler sync. */
654 event->enabled = 0;
655 event->registered = 0;
656 event->desc = lttng_event_get(event_name);
657 if (!event->desc) {
658 ret = -ENOENT;
659 goto register_error;
660 }
661 /* Populate lttng_event structure before event registration. */
662 smp_wmb();
663 break;
664 case LTTNG_KERNEL_KPROBE:
665 /*
666 * Needs to be explicitly enabled after creation, since
667 * we may want to apply filters.
668 */
669 event->enabled = 0;
670 event->registered = 1;
671 /*
672 * Populate lttng_event structure before event
673 * registration.
674 */
675 smp_wmb();
676 ret = lttng_kprobes_register(event_name,
677 event_param->u.kprobe.symbol_name,
678 event_param->u.kprobe.offset,
679 event_param->u.kprobe.addr,
680 event);
681 if (ret) {
682 ret = -EINVAL;
683 goto register_error;
684 }
685 ret = try_module_get(event->desc->owner);
686 WARN_ON_ONCE(!ret);
687 break;
688 case LTTNG_KERNEL_KRETPROBE:
689 {
690 struct lttng_event *event_return;
691
692 /* kretprobe defines 2 events */
693 /*
694 * Needs to be explicitly enabled after creation, since
695 * we may want to apply filters.
696 */
697 event->enabled = 0;
698 event->registered = 1;
699 event_return =
700 kmem_cache_zalloc(event_cache, GFP_KERNEL);
701 if (!event_return) {
702 ret = -ENOMEM;
703 goto register_error;
704 }
705 event_return->chan = chan;
706 event_return->filter = filter;
707 event_return->id = chan->free_event_id++;
708 event_return->enabled = 0;
709 event_return->registered = 1;
710 event_return->instrumentation = itype;
711 /*
712 * Populate lttng_event structure before kretprobe registration.
713 */
714 smp_wmb();
715 ret = lttng_kretprobes_register(event_name,
716 event_param->u.kretprobe.symbol_name,
717 event_param->u.kretprobe.offset,
718 event_param->u.kretprobe.addr,
719 event, event_return);
720 if (ret) {
721 kmem_cache_free(event_cache, event_return);
722 ret = -EINVAL;
723 goto register_error;
724 }
725 /* Take 2 refs on the module: one per event. */
726 ret = try_module_get(event->desc->owner);
727 WARN_ON_ONCE(!ret);
728 ret = try_module_get(event->desc->owner);
729 WARN_ON_ONCE(!ret);
730 ret = _lttng_event_metadata_statedump(chan->session, chan,
731 event_return);
732 WARN_ON_ONCE(ret > 0);
733 if (ret) {
734 kmem_cache_free(event_cache, event_return);
735 module_put(event->desc->owner);
736 module_put(event->desc->owner);
737 goto statedump_error;
738 }
739 list_add(&event_return->list, &chan->session->events);
740 break;
741 }
742 case LTTNG_KERNEL_NOOP:
743 case LTTNG_KERNEL_SYSCALL:
744 /*
745 * Needs to be explicitly enabled after creation, since
746 * we may want to apply filters.
747 */
748 event->enabled = 0;
749 event->registered = 0;
750 event->desc = event_desc;
751 switch (event_param->u.syscall.entryexit) {
752 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
753 ret = -EINVAL;
754 goto register_error;
755 case LTTNG_KERNEL_SYSCALL_ENTRY:
756 event->u.syscall.entryexit = LTTNG_SYSCALL_ENTRY;
757 break;
758 case LTTNG_KERNEL_SYSCALL_EXIT:
759 event->u.syscall.entryexit = LTTNG_SYSCALL_EXIT;
760 break;
761 }
762 switch (event_param->u.syscall.abi) {
763 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
764 ret = -EINVAL;
765 goto register_error;
766 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE:
767 event->u.syscall.abi = LTTNG_SYSCALL_ABI_NATIVE;
768 break;
769 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT:
770 event->u.syscall.abi = LTTNG_SYSCALL_ABI_COMPAT;
771 break;
772 }
773 if (!event->desc) {
774 ret = -EINVAL;
775 goto register_error;
776 }
777 break;
778 case LTTNG_KERNEL_UPROBE:
779 /*
780 * Needs to be explicitly enabled after creation, since
781 * we may want to apply filters.
782 */
783 event->enabled = 0;
784 event->registered = 1;
785
786 /*
787 * Populate lttng_event structure before event
788 * registration.
789 */
790 smp_wmb();
791
792 ret = lttng_uprobes_register(event_param->name,
793 event_param->u.uprobe.fd,
794 event);
795 if (ret)
796 goto register_error;
797 ret = try_module_get(event->desc->owner);
798 WARN_ON_ONCE(!ret);
799 break;
800 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
801 default:
802 WARN_ON_ONCE(1);
803 ret = -EINVAL;
804 goto register_error;
805 }
806 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
807 WARN_ON_ONCE(ret > 0);
808 if (ret) {
809 goto statedump_error;
810 }
811 hlist_add_head(&event->hlist, head);
812 list_add(&event->list, &chan->session->events);
813 return event;
814
815 statedump_error:
816 /* If a statedump error occurs, events will not be readable. */
817 register_error:
818 kmem_cache_free(event_cache, event);
819 cache_error:
820 exist:
821 type_error:
822 full:
823 return ERR_PTR(ret);
824 }
825
826 struct lttng_event *lttng_event_create(struct lttng_channel *chan,
827 struct lttng_kernel_event *event_param,
828 void *filter,
829 const struct lttng_event_desc *event_desc,
830 enum lttng_kernel_instrumentation itype)
831 {
832 struct lttng_event *event;
833
834 mutex_lock(&sessions_mutex);
835 event = _lttng_event_create(chan, event_param, filter, event_desc,
836 itype);
837 mutex_unlock(&sessions_mutex);
838 return event;
839 }
840
841 /* Only used for tracepoints for now. */
842 static
843 void register_event(struct lttng_event *event)
844 {
845 const struct lttng_event_desc *desc;
846 int ret = -EINVAL;
847
848 if (event->registered)
849 return;
850
851 desc = event->desc;
852 switch (event->instrumentation) {
853 case LTTNG_KERNEL_TRACEPOINT:
854 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
855 desc->probe_callback,
856 event);
857 break;
858 case LTTNG_KERNEL_SYSCALL:
859 ret = lttng_syscall_filter_enable(event->chan, event);
860 break;
861 case LTTNG_KERNEL_KPROBE:
862 case LTTNG_KERNEL_UPROBE:
863 case LTTNG_KERNEL_KRETPROBE:
864 case LTTNG_KERNEL_NOOP:
865 ret = 0;
866 break;
867 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
868 default:
869 WARN_ON_ONCE(1);
870 }
871 if (!ret)
872 event->registered = 1;
873 }
874
875 /*
876 * Only used internally at session destruction.
877 */
878 int _lttng_event_unregister(struct lttng_event *event)
879 {
880 const struct lttng_event_desc *desc;
881 int ret = -EINVAL;
882
883 if (!event->registered)
884 return 0;
885
886 desc = event->desc;
887 switch (event->instrumentation) {
888 case LTTNG_KERNEL_TRACEPOINT:
889 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
890 event->desc->probe_callback,
891 event);
892 break;
893 case LTTNG_KERNEL_KPROBE:
894 lttng_kprobes_unregister(event);
895 ret = 0;
896 break;
897 case LTTNG_KERNEL_KRETPROBE:
898 lttng_kretprobes_unregister(event);
899 ret = 0;
900 break;
901 case LTTNG_KERNEL_SYSCALL:
902 ret = lttng_syscall_filter_disable(event->chan, event);
903 break;
904 case LTTNG_KERNEL_NOOP:
905 ret = 0;
906 break;
907 case LTTNG_KERNEL_UPROBE:
908 lttng_uprobes_unregister(event);
909 ret = 0;
910 break;
911 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
912 default:
913 WARN_ON_ONCE(1);
914 }
915 if (!ret)
916 event->registered = 0;
917 return ret;
918 }
919
920 /*
921 * Only used internally at session destruction.
922 */
923 static
924 void _lttng_event_destroy(struct lttng_event *event)
925 {
926 switch (event->instrumentation) {
927 case LTTNG_KERNEL_TRACEPOINT:
928 lttng_event_put(event->desc);
929 break;
930 case LTTNG_KERNEL_KPROBE:
931 module_put(event->desc->owner);
932 lttng_kprobes_destroy_private(event);
933 break;
934 case LTTNG_KERNEL_KRETPROBE:
935 module_put(event->desc->owner);
936 lttng_kretprobes_destroy_private(event);
937 break;
938 case LTTNG_KERNEL_NOOP:
939 case LTTNG_KERNEL_SYSCALL:
940 break;
941 case LTTNG_KERNEL_UPROBE:
942 module_put(event->desc->owner);
943 lttng_uprobes_destroy_private(event);
944 break;
945 case LTTNG_KERNEL_FUNCTION: /* Fall-through */
946 default:
947 WARN_ON_ONCE(1);
948 }
949 list_del(&event->list);
950 lttng_destroy_context(event->ctx);
951 kmem_cache_free(event_cache, event);
952 }
953
954 struct lttng_id_tracker *get_tracker(struct lttng_session *session,
955 enum tracker_type tracker_type)
956 {
957 switch (tracker_type) {
958 case TRACKER_PID:
959 return &session->pid_tracker;
960 case TRACKER_VPID:
961 return &session->vpid_tracker;
962 case TRACKER_UID:
963 return &session->uid_tracker;
964 case TRACKER_VUID:
965 return &session->vuid_tracker;
966 case TRACKER_GID:
967 return &session->gid_tracker;
968 case TRACKER_VGID:
969 return &session->vgid_tracker;
970 default:
971 WARN_ON_ONCE(1);
972 return NULL;
973 }
974 }
975
976 int lttng_session_track_id(struct lttng_session *session,
977 enum tracker_type tracker_type, int id)
978 {
979 struct lttng_id_tracker *tracker;
980 int ret;
981
982 tracker = get_tracker(session, tracker_type);
983 if (!tracker)
984 return -EINVAL;
985 if (id < -1)
986 return -EINVAL;
987 mutex_lock(&sessions_mutex);
988 if (id == -1) {
989 /* track all ids: destroy tracker. */
990 lttng_id_tracker_destroy(tracker, true);
991 ret = 0;
992 } else {
993 ret = lttng_id_tracker_add(tracker, id);
994 }
995 mutex_unlock(&sessions_mutex);
996 return ret;
997 }
998
999 int lttng_session_untrack_id(struct lttng_session *session,
1000 enum tracker_type tracker_type, int id)
1001 {
1002 struct lttng_id_tracker *tracker;
1003 int ret;
1004
1005 tracker = get_tracker(session, tracker_type);
1006 if (!tracker)
1007 return -EINVAL;
1008 if (id < -1)
1009 return -EINVAL;
1010 mutex_lock(&sessions_mutex);
1011 if (id == -1) {
1012 /* untrack all ids: replace by empty tracker. */
1013 ret = lttng_id_tracker_empty_set(tracker);
1014 } else {
1015 ret = lttng_id_tracker_del(tracker, id);
1016 }
1017 mutex_unlock(&sessions_mutex);
1018 return ret;
1019 }
1020
1021 static
1022 void *id_list_start(struct seq_file *m, loff_t *pos)
1023 {
1024 struct lttng_id_tracker *id_tracker = m->private;
1025 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1026 struct lttng_id_hash_node *e;
1027 int iter = 0, i;
1028
1029 mutex_lock(&sessions_mutex);
1030 if (id_tracker_p) {
1031 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1032 struct hlist_head *head = &id_tracker_p->id_hash[i];
1033
1034 lttng_hlist_for_each_entry(e, head, hlist) {
1035 if (iter++ >= *pos)
1036 return e;
1037 }
1038 }
1039 } else {
1040 /* ID tracker disabled. */
1041 if (iter >= *pos && iter == 0) {
1042 return id_tracker_p; /* empty tracker */
1043 }
1044 iter++;
1045 }
1046 /* End of list */
1047 return NULL;
1048 }
1049
1050 /* Called with sessions_mutex held. */
1051 static
1052 void *id_list_next(struct seq_file *m, void *p, loff_t *ppos)
1053 {
1054 struct lttng_id_tracker *id_tracker = m->private;
1055 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1056 struct lttng_id_hash_node *e;
1057 int iter = 0, i;
1058
1059 (*ppos)++;
1060 if (id_tracker_p) {
1061 for (i = 0; i < LTTNG_ID_TABLE_SIZE; i++) {
1062 struct hlist_head *head = &id_tracker_p->id_hash[i];
1063
1064 lttng_hlist_for_each_entry(e, head, hlist) {
1065 if (iter++ >= *ppos)
1066 return e;
1067 }
1068 }
1069 } else {
1070 /* ID tracker disabled. */
1071 if (iter >= *ppos && iter == 0)
1072 return p; /* empty tracker */
1073 iter++;
1074 }
1075
1076 /* End of list */
1077 return NULL;
1078 }
1079
1080 static
1081 void id_list_stop(struct seq_file *m, void *p)
1082 {
1083 mutex_unlock(&sessions_mutex);
1084 }
1085
1086 static
1087 int id_list_show(struct seq_file *m, void *p)
1088 {
1089 struct lttng_id_tracker *id_tracker = m->private;
1090 struct lttng_id_tracker_rcu *id_tracker_p = id_tracker->p;
1091 int id;
1092
1093 if (p == id_tracker_p) {
1094 /* Tracker disabled. */
1095 id = -1;
1096 } else {
1097 const struct lttng_id_hash_node *e = p;
1098
1099 id = lttng_id_tracker_get_node_id(e);
1100 }
1101 switch (id_tracker->tracker_type) {
1102 case TRACKER_PID:
1103 seq_printf(m, "process { pid = %d; };\n", id);
1104 break;
1105 case TRACKER_VPID:
1106 seq_printf(m, "process { vpid = %d; };\n", id);
1107 break;
1108 case TRACKER_UID:
1109 seq_printf(m, "user { uid = %d; };\n", id);
1110 break;
1111 case TRACKER_VUID:
1112 seq_printf(m, "user { vuid = %d; };\n", id);
1113 break;
1114 case TRACKER_GID:
1115 seq_printf(m, "group { gid = %d; };\n", id);
1116 break;
1117 case TRACKER_VGID:
1118 seq_printf(m, "group { vgid = %d; };\n", id);
1119 break;
1120 default:
1121 seq_printf(m, "UNKNOWN { field = %d };\n", id);
1122 }
1123 return 0;
1124 }
1125
1126 static
1127 const struct seq_operations lttng_tracker_ids_list_seq_ops = {
1128 .start = id_list_start,
1129 .next = id_list_next,
1130 .stop = id_list_stop,
1131 .show = id_list_show,
1132 };
1133
1134 static
1135 int lttng_tracker_ids_list_open(struct inode *inode, struct file *file)
1136 {
1137 return seq_open(file, &lttng_tracker_ids_list_seq_ops);
1138 }
1139
1140 static
1141 int lttng_tracker_ids_list_release(struct inode *inode, struct file *file)
1142 {
1143 struct seq_file *m = file->private_data;
1144 struct lttng_id_tracker *id_tracker = m->private;
1145 int ret;
1146
1147 WARN_ON_ONCE(!id_tracker);
1148 ret = seq_release(inode, file);
1149 if (!ret)
1150 fput(id_tracker->session->file);
1151 return ret;
1152 }
1153
1154 const struct file_operations lttng_tracker_ids_list_fops = {
1155 .owner = THIS_MODULE,
1156 .open = lttng_tracker_ids_list_open,
1157 .read = seq_read,
1158 .llseek = seq_lseek,
1159 .release = lttng_tracker_ids_list_release,
1160 };
1161
1162 int lttng_session_list_tracker_ids(struct lttng_session *session,
1163 enum tracker_type tracker_type)
1164 {
1165 struct file *tracker_ids_list_file;
1166 struct seq_file *m;
1167 int file_fd, ret;
1168
1169 file_fd = lttng_get_unused_fd();
1170 if (file_fd < 0) {
1171 ret = file_fd;
1172 goto fd_error;
1173 }
1174
1175 tracker_ids_list_file = anon_inode_getfile("[lttng_tracker_ids_list]",
1176 &lttng_tracker_ids_list_fops,
1177 NULL, O_RDWR);
1178 if (IS_ERR(tracker_ids_list_file)) {
1179 ret = PTR_ERR(tracker_ids_list_file);
1180 goto file_error;
1181 }
1182 if (!atomic_long_add_unless(&session->file->f_count, 1, LONG_MAX)) {
1183 ret = -EOVERFLOW;
1184 goto refcount_error;
1185 }
1186 ret = lttng_tracker_ids_list_fops.open(NULL, tracker_ids_list_file);
1187 if (ret < 0)
1188 goto open_error;
1189 m = tracker_ids_list_file->private_data;
1190
1191 m->private = get_tracker(session, tracker_type);
1192 BUG_ON(!m->private);
1193 fd_install(file_fd, tracker_ids_list_file);
1194
1195 return file_fd;
1196
1197 open_error:
1198 atomic_long_dec(&session->file->f_count);
1199 refcount_error:
1200 fput(tracker_ids_list_file);
1201 file_error:
1202 put_unused_fd(file_fd);
1203 fd_error:
1204 return ret;
1205 }
1206
1207 /*
1208 * Enabler management.
1209 */
1210 static
1211 int lttng_match_enabler_star_glob(const char *desc_name,
1212 const char *pattern)
1213 {
1214 if (!strutils_star_glob_match(pattern, LTTNG_SIZE_MAX,
1215 desc_name, LTTNG_SIZE_MAX))
1216 return 0;
1217 return 1;
1218 }
1219
1220 static
1221 int lttng_match_enabler_name(const char *desc_name,
1222 const char *name)
1223 {
1224 if (strcmp(desc_name, name))
1225 return 0;
1226 return 1;
1227 }
1228
1229 static
1230 int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1231 struct lttng_enabler *enabler)
1232 {
1233 const char *desc_name, *enabler_name;
1234 bool compat = false, entry = false;
1235
1236 enabler_name = enabler->event_param.name;
1237 switch (enabler->event_param.instrumentation) {
1238 case LTTNG_KERNEL_TRACEPOINT:
1239 desc_name = desc->name;
1240 switch (enabler->format_type) {
1241 case LTTNG_ENABLER_FORMAT_STAR_GLOB:
1242 return lttng_match_enabler_star_glob(desc_name, enabler_name);
1243 case LTTNG_ENABLER_FORMAT_NAME:
1244 return lttng_match_enabler_name(desc_name, enabler_name);
1245 default:
1246 return -EINVAL;
1247 }
1248 break;
1249 case LTTNG_KERNEL_SYSCALL:
1250 desc_name = desc->name;
1251 if (!strncmp(desc_name, "compat_", strlen("compat_"))) {
1252 desc_name += strlen("compat_");
1253 compat = true;
1254 }
1255 if (!strncmp(desc_name, "syscall_exit_",
1256 strlen("syscall_exit_"))) {
1257 desc_name += strlen("syscall_exit_");
1258 } else if (!strncmp(desc_name, "syscall_entry_",
1259 strlen("syscall_entry_"))) {
1260 desc_name += strlen("syscall_entry_");
1261 entry = true;
1262 } else {
1263 WARN_ON_ONCE(1);
1264 return -EINVAL;
1265 }
1266 switch (enabler->event_param.u.syscall.entryexit) {
1267 case LTTNG_KERNEL_SYSCALL_ENTRYEXIT:
1268 break;
1269 case LTTNG_KERNEL_SYSCALL_ENTRY:
1270 if (!entry)
1271 return 0;
1272 break;
1273 case LTTNG_KERNEL_SYSCALL_EXIT:
1274 if (entry)
1275 return 0;
1276 break;
1277 default:
1278 return -EINVAL;
1279 }
1280 switch (enabler->event_param.u.syscall.abi) {
1281 case LTTNG_KERNEL_SYSCALL_ABI_ALL:
1282 break;
1283 case LTTNG_KERNEL_SYSCALL_ABI_NATIVE:
1284 if (compat)
1285 return 0;
1286 break;
1287 case LTTNG_KERNEL_SYSCALL_ABI_COMPAT:
1288 if (!compat)
1289 return 0;
1290 break;
1291 default:
1292 return -EINVAL;
1293 }
1294 switch (enabler->event_param.u.syscall.match) {
1295 case LTTNG_SYSCALL_MATCH_NAME:
1296 switch (enabler->format_type) {
1297 case LTTNG_ENABLER_FORMAT_STAR_GLOB:
1298 return lttng_match_enabler_star_glob(desc_name, enabler_name);
1299 case LTTNG_ENABLER_FORMAT_NAME:
1300 return lttng_match_enabler_name(desc_name, enabler_name);
1301 default:
1302 return -EINVAL;
1303 }
1304 break;
1305 case LTTNG_SYSCALL_MATCH_NR:
1306 return -EINVAL; /* Not implemented. */
1307 default:
1308 return -EINVAL;
1309 }
1310 break;
1311 default:
1312 WARN_ON_ONCE(1);
1313 return -EINVAL;
1314 }
1315 }
1316
1317 static
1318 int lttng_event_enabler_match_event(struct lttng_event_enabler *event_enabler,
1319 struct lttng_event *event)
1320 {
1321 struct lttng_enabler *base_enabler = lttng_event_enabler_as_enabler(
1322 event_enabler);
1323
1324 if (base_enabler->event_param.instrumentation != event->instrumentation)
1325 return 0;
1326 if (lttng_desc_match_enabler(event->desc, base_enabler)
1327 && event->chan == event_enabler->chan)
1328 return 1;
1329 else
1330 return 0;
1331 }
1332
1333 static
1334 struct lttng_enabler_ref *lttng_enabler_ref(
1335 struct list_head *enablers_ref_list,
1336 struct lttng_enabler *enabler)
1337 {
1338 struct lttng_enabler_ref *enabler_ref;
1339
1340 list_for_each_entry(enabler_ref, enablers_ref_list, node) {
1341 if (enabler_ref->ref == enabler)
1342 return enabler_ref;
1343 }
1344 return NULL;
1345 }
1346
1347 static
1348 void lttng_create_tracepoint_event_if_missing(struct lttng_event_enabler *event_enabler)
1349 {
1350 struct lttng_session *session = event_enabler->chan->session;
1351 struct lttng_probe_desc *probe_desc;
1352 const struct lttng_event_desc *desc;
1353 int i;
1354 struct list_head *probe_list;
1355
1356 probe_list = lttng_get_probe_list_head();
1357 /*
1358 * For each probe event, if we find that a probe event matches
1359 * our enabler, create an associated lttng_event if not
1360 * already present.
1361 */
1362 list_for_each_entry(probe_desc, probe_list, head) {
1363 for (i = 0; i < probe_desc->nr_events; i++) {
1364 int found = 0;
1365 struct hlist_head *head;
1366 const char *event_name;
1367 size_t name_len;
1368 uint32_t hash;
1369 struct lttng_event *event;
1370
1371 desc = probe_desc->event_desc[i];
1372 if (!lttng_desc_match_enabler(desc,
1373 lttng_event_enabler_as_enabler(event_enabler)))
1374 continue;
1375 event_name = desc->name;
1376 name_len = strlen(event_name);
1377
1378 /*
1379 * Check if already created.
1380 */
1381 hash = jhash(event_name, name_len, 0);
1382 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
1383 lttng_hlist_for_each_entry(event, head, hlist) {
1384 if (event->desc == desc
1385 && event->chan == event_enabler->chan)
1386 found = 1;
1387 }
1388 if (found)
1389 continue;
1390
1391 /*
1392 * We need to create an event for this
1393 * event probe.
1394 */
1395 event = _lttng_event_create(event_enabler->chan,
1396 NULL, NULL, desc,
1397 LTTNG_KERNEL_TRACEPOINT);
1398 if (!event) {
1399 printk(KERN_INFO "LTTng: Unable to create event %s\n",
1400 probe_desc->event_desc[i]->name);
1401 }
1402 }
1403 }
1404 }
1405
1406 static
1407 void lttng_create_syscall_event_if_missing(struct lttng_event_enabler *event_enabler)
1408 {
1409 int ret;
1410
1411 ret = lttng_syscalls_register(event_enabler->chan, NULL);
1412 WARN_ON_ONCE(ret);
1413 }
1414
1415 /*
1416 * Create struct lttng_event if it is missing and present in the list of
1417 * tracepoint probes.
1418 * Should be called with sessions mutex held.
1419 */
1420 static
1421 void lttng_create_event_if_missing(struct lttng_event_enabler *event_enabler)
1422 {
1423 switch (event_enabler->base.event_param.instrumentation) {
1424 case LTTNG_KERNEL_TRACEPOINT:
1425 lttng_create_tracepoint_event_if_missing(event_enabler);
1426 break;
1427 case LTTNG_KERNEL_SYSCALL:
1428 lttng_create_syscall_event_if_missing(event_enabler);
1429 break;
1430 default:
1431 WARN_ON_ONCE(1);
1432 break;
1433 }
1434 }
1435
1436 /*
1437 * Create events associated with an event_enabler (if not already present),
1438 * and add backward reference from the event to the enabler.
1439 * Should be called with sessions mutex held.
1440 */
1441 static
1442 int lttng_event_enabler_ref_events(struct lttng_event_enabler *event_enabler)
1443 {
1444 struct lttng_channel *chan = event_enabler->chan;
1445 struct lttng_session *session = event_enabler->chan->session;
1446 struct lttng_enabler *base_enabler = lttng_event_enabler_as_enabler(event_enabler);
1447 struct lttng_event *event;
1448
1449 if (base_enabler->event_param.instrumentation == LTTNG_KERNEL_SYSCALL &&
1450 base_enabler->event_param.u.syscall.entryexit == LTTNG_KERNEL_SYSCALL_ENTRYEXIT &&
1451 base_enabler->event_param.u.syscall.abi == LTTNG_KERNEL_SYSCALL_ABI_ALL &&
1452 base_enabler->event_param.u.syscall.match == LTTNG_SYSCALL_MATCH_NAME &&
1453 !strcmp(base_enabler->event_param.name, "*")) {
1454 if (base_enabler->enabled)
1455 WRITE_ONCE(chan->syscall_all, 1);
1456 else
1457 WRITE_ONCE(chan->syscall_all, 0);
1458 }
1459
1460 /* First ensure that probe events are created for this enabler. */
1461 lttng_create_event_if_missing(event_enabler);
1462
1463 /* For each event matching event_enabler in session event list. */
1464 list_for_each_entry(event, &session->events, list) {
1465 struct lttng_enabler_ref *enabler_ref;
1466
1467 if (!lttng_event_enabler_match_event(event_enabler, event))
1468 continue;
1469 enabler_ref = lttng_enabler_ref(&event->enablers_ref_head,
1470 lttng_event_enabler_as_enabler(event_enabler));
1471 if (!enabler_ref) {
1472 /*
1473 * If no backward ref, create it.
1474 * Add backward ref from event to event_enabler.
1475 */
1476 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1477 if (!enabler_ref)
1478 return -ENOMEM;
1479 enabler_ref->ref = lttng_event_enabler_as_enabler(event_enabler);
1480 list_add(&enabler_ref->node,
1481 &event->enablers_ref_head);
1482 }
1483
1484 /*
1485 * Link filter bytecodes if not linked yet.
1486 */
1487 lttng_event_enabler_link_bytecode(event, event_enabler);
1488
1489 /* TODO: merge event context. */
1490 }
1491 return 0;
1492 }
1493
1494 /*
1495 * Called at module load: connect the probe on all enablers matching
1496 * this event.
1497 * Called with sessions lock held.
1498 */
1499 int lttng_fix_pending_events(void)
1500 {
1501 struct lttng_session *session;
1502
1503 list_for_each_entry(session, &sessions, list)
1504 lttng_session_lazy_sync_event_enablers(session);
1505 return 0;
1506 }
1507
1508 struct lttng_event_enabler *lttng_event_enabler_create(
1509 enum lttng_enabler_format_type format_type,
1510 struct lttng_kernel_event *event_param,
1511 struct lttng_channel *chan)
1512 {
1513 struct lttng_event_enabler *event_enabler;
1514
1515 event_enabler = kzalloc(sizeof(*event_enabler), GFP_KERNEL);
1516 if (!event_enabler)
1517 return NULL;
1518 event_enabler->base.format_type = format_type;
1519 INIT_LIST_HEAD(&event_enabler->base.filter_bytecode_head);
1520 memcpy(&event_enabler->base.event_param, event_param,
1521 sizeof(event_enabler->base.event_param));
1522 event_enabler->chan = chan;
1523 /* ctx left NULL */
1524 event_enabler->base.enabled = 0;
1525 event_enabler->base.evtype = LTTNG_TYPE_ENABLER;
1526 mutex_lock(&sessions_mutex);
1527 list_add(&event_enabler->node, &event_enabler->chan->session->enablers_head);
1528 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1529 mutex_unlock(&sessions_mutex);
1530 return event_enabler;
1531 }
1532
1533 int lttng_event_enabler_enable(struct lttng_event_enabler *event_enabler)
1534 {
1535 mutex_lock(&sessions_mutex);
1536 lttng_event_enabler_as_enabler(event_enabler)->enabled = 1;
1537 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1538 mutex_unlock(&sessions_mutex);
1539 return 0;
1540 }
1541
1542 int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler)
1543 {
1544 mutex_lock(&sessions_mutex);
1545 lttng_event_enabler_as_enabler(event_enabler)->enabled = 0;
1546 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1547 mutex_unlock(&sessions_mutex);
1548 return 0;
1549 }
1550
1551 static
1552 int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1553 struct lttng_kernel_filter_bytecode __user *bytecode)
1554 {
1555 struct lttng_filter_bytecode_node *bytecode_node;
1556 uint32_t bytecode_len;
1557 int ret;
1558
1559 ret = get_user(bytecode_len, &bytecode->len);
1560 if (ret)
1561 return ret;
1562 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1563 GFP_KERNEL);
1564 if (!bytecode_node)
1565 return -ENOMEM;
1566 ret = copy_from_user(&bytecode_node->bc, bytecode,
1567 sizeof(*bytecode) + bytecode_len);
1568 if (ret)
1569 goto error_free;
1570
1571 bytecode_node->enabler = enabler;
1572 /* Enforce length based on allocated size */
1573 bytecode_node->bc.len = bytecode_len;
1574 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1575
1576 return 0;
1577
1578 error_free:
1579 kfree(bytecode_node);
1580 return ret;
1581 }
1582
1583 int lttng_event_enabler_attach_bytecode(struct lttng_event_enabler *event_enabler,
1584 struct lttng_kernel_filter_bytecode __user *bytecode)
1585 {
1586 int ret;
1587 ret = lttng_enabler_attach_bytecode(
1588 lttng_event_enabler_as_enabler(event_enabler), bytecode);
1589 if (ret)
1590 goto error;
1591
1592 lttng_session_lazy_sync_event_enablers(event_enabler->chan->session);
1593 return 0;
1594
1595 error:
1596 return ret;
1597 }
1598
1599 int lttng_event_add_callsite(struct lttng_event *event,
1600 struct lttng_kernel_event_callsite __user *callsite)
1601 {
1602
1603 switch (event->instrumentation) {
1604 case LTTNG_KERNEL_UPROBE:
1605 return lttng_uprobes_add_callsite(event, callsite);
1606 default:
1607 return -EINVAL;
1608 }
1609 }
1610
1611 int lttng_event_enabler_attach_context(struct lttng_event_enabler *event_enabler,
1612 struct lttng_kernel_context *context_param)
1613 {
1614 return -ENOSYS;
1615 }
1616
1617 static
1618 void lttng_enabler_destroy(struct lttng_enabler *enabler)
1619 {
1620 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1621
1622 /* Destroy filter bytecode */
1623 list_for_each_entry_safe(filter_node, tmp_filter_node,
1624 &enabler->filter_bytecode_head, node) {
1625 kfree(filter_node);
1626 }
1627 }
1628
1629 static
1630 void lttng_event_enabler_destroy(struct lttng_event_enabler *event_enabler)
1631 {
1632 lttng_enabler_destroy(lttng_event_enabler_as_enabler(event_enabler));
1633
1634 /* Destroy contexts */
1635 lttng_destroy_context(event_enabler->ctx);
1636
1637 list_del(&event_enabler->node);
1638 kfree(event_enabler);
1639 }
1640
1641 /*
1642 * lttng_session_sync_event_enablers should be called just before starting a
1643 * session.
1644 * Should be called with sessions mutex held.
1645 */
1646 static
1647 void lttng_session_sync_event_enablers(struct lttng_session *session)
1648 {
1649 struct lttng_event_enabler *event_enabler;
1650 struct lttng_event *event;
1651
1652 list_for_each_entry(event_enabler, &session->enablers_head, node)
1653 lttng_event_enabler_ref_events(event_enabler);
1654 /*
1655 * For each event, if at least one of its enablers is enabled,
1656 * and its channel and session transient states are enabled, we
1657 * enable the event, else we disable it.
1658 */
1659 list_for_each_entry(event, &session->events, list) {
1660 struct lttng_enabler_ref *enabler_ref;
1661 struct lttng_bytecode_runtime *runtime;
1662 int enabled = 0, has_enablers_without_bytecode = 0;
1663
1664 switch (event->instrumentation) {
1665 case LTTNG_KERNEL_TRACEPOINT:
1666 case LTTNG_KERNEL_SYSCALL:
1667 /* Enable events */
1668 list_for_each_entry(enabler_ref,
1669 &event->enablers_ref_head, node) {
1670 if (enabler_ref->ref->enabled) {
1671 enabled = 1;
1672 break;
1673 }
1674 }
1675 break;
1676 default:
1677 /* Not handled with lazy sync. */
1678 continue;
1679 }
1680 /*
1681 * Enabled state is based on union of enablers, with
1682 * intesection of session and channel transient enable
1683 * states.
1684 */
1685 enabled = enabled && session->tstate && event->chan->tstate;
1686
1687 WRITE_ONCE(event->enabled, enabled);
1688 /*
1689 * Sync tracepoint registration with event enabled
1690 * state.
1691 */
1692 if (enabled) {
1693 register_event(event);
1694 } else {
1695 _lttng_event_unregister(event);
1696 }
1697
1698 /* Check if has enablers without bytecode enabled */
1699 list_for_each_entry(enabler_ref,
1700 &event->enablers_ref_head, node) {
1701 if (enabler_ref->ref->enabled
1702 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1703 has_enablers_without_bytecode = 1;
1704 break;
1705 }
1706 }
1707 event->has_enablers_without_bytecode =
1708 has_enablers_without_bytecode;
1709
1710 /* Enable filters */
1711 list_for_each_entry(runtime,
1712 &event->bytecode_runtime_head, node)
1713 lttng_filter_sync_state(runtime);
1714 }
1715 }
1716
1717 /*
1718 * Apply enablers to session events, adding events to session if need
1719 * be. It is required after each modification applied to an active
1720 * session, and right before session "start".
1721 * "lazy" sync means we only sync if required.
1722 * Should be called with sessions mutex held.
1723 */
1724 static
1725 void lttng_session_lazy_sync_event_enablers(struct lttng_session *session)
1726 {
1727 /* We can skip if session is not active */
1728 if (!session->active)
1729 return;
1730 lttng_session_sync_event_enablers(session);
1731 }
1732
1733 /*
1734 * Serialize at most one packet worth of metadata into a metadata
1735 * channel.
1736 * We grab the metadata cache mutex to get exclusive access to our metadata
1737 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1738 * allows us to do racy operations such as looking for remaining space left in
1739 * packet and write, since mutual exclusion protects us from concurrent writes.
1740 * Mutual exclusion on the metadata cache allow us to read the cache content
1741 * without racing against reallocation of the cache by updates.
1742 * Returns the number of bytes written in the channel, 0 if no data
1743 * was written and a negative value on error.
1744 */
1745 int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1746 struct channel *chan, bool *coherent)
1747 {
1748 struct lib_ring_buffer_ctx ctx;
1749 int ret = 0;
1750 size_t len, reserve_len;
1751
1752 /*
1753 * Ensure we support mutiple get_next / put sequences followed by
1754 * put_next. The metadata cache lock protects reading the metadata
1755 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1756 * "flush" operations on the buffer invoked by different processes.
1757 * Moreover, since the metadata cache memory can be reallocated, we
1758 * need to have exclusive access against updates even though we only
1759 * read it.
1760 */
1761 mutex_lock(&stream->metadata_cache->lock);
1762 WARN_ON(stream->metadata_in < stream->metadata_out);
1763 if (stream->metadata_in != stream->metadata_out)
1764 goto end;
1765
1766 /* Metadata regenerated, change the version. */
1767 if (stream->metadata_cache->version != stream->version)
1768 stream->version = stream->metadata_cache->version;
1769
1770 len = stream->metadata_cache->metadata_written -
1771 stream->metadata_in;
1772 if (!len)
1773 goto end;
1774 reserve_len = min_t(size_t,
1775 stream->transport->ops.packet_avail_size(chan),
1776 len);
1777 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
1778 sizeof(char), -1);
1779 /*
1780 * If reservation failed, return an error to the caller.
1781 */
1782 ret = stream->transport->ops.event_reserve(&ctx, 0);
1783 if (ret != 0) {
1784 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1785 stream->coherent = false;
1786 goto end;
1787 }
1788 stream->transport->ops.event_write(&ctx,
1789 stream->metadata_cache->data + stream->metadata_in,
1790 reserve_len);
1791 stream->transport->ops.event_commit(&ctx);
1792 stream->metadata_in += reserve_len;
1793 if (reserve_len < len)
1794 stream->coherent = false;
1795 else
1796 stream->coherent = true;
1797 ret = reserve_len;
1798
1799 end:
1800 if (coherent)
1801 *coherent = stream->coherent;
1802 mutex_unlock(&stream->metadata_cache->lock);
1803 return ret;
1804 }
1805
1806 static
1807 void lttng_metadata_begin(struct lttng_session *session)
1808 {
1809 if (atomic_inc_return(&session->metadata_cache->producing) == 1)
1810 mutex_lock(&session->metadata_cache->lock);
1811 }
1812
1813 static
1814 void lttng_metadata_end(struct lttng_session *session)
1815 {
1816 WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing));
1817 if (atomic_dec_return(&session->metadata_cache->producing) == 0) {
1818 struct lttng_metadata_stream *stream;
1819
1820 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1821 wake_up_interruptible(&stream->read_wait);
1822 mutex_unlock(&session->metadata_cache->lock);
1823 }
1824 }
1825
1826 /*
1827 * Write the metadata to the metadata cache.
1828 * Must be called with sessions_mutex held.
1829 * The metadata cache lock protects us from concurrent read access from
1830 * thread outputting metadata content to ring buffer.
1831 * The content of the printf is printed as a single atomic metadata
1832 * transaction.
1833 */
1834 int lttng_metadata_printf(struct lttng_session *session,
1835 const char *fmt, ...)
1836 {
1837 char *str;
1838 size_t len;
1839 va_list ap;
1840
1841 WARN_ON_ONCE(!LTTNG_READ_ONCE(session->active));
1842
1843 va_start(ap, fmt);
1844 str = kvasprintf(GFP_KERNEL, fmt, ap);
1845 va_end(ap);
1846 if (!str)
1847 return -ENOMEM;
1848
1849 len = strlen(str);
1850 WARN_ON_ONCE(!atomic_read(&session->metadata_cache->producing));
1851 if (session->metadata_cache->metadata_written + len >
1852 session->metadata_cache->cache_alloc) {
1853 char *tmp_cache_realloc;
1854 unsigned int tmp_cache_alloc_size;
1855
1856 tmp_cache_alloc_size = max_t(unsigned int,
1857 session->metadata_cache->cache_alloc + len,
1858 session->metadata_cache->cache_alloc << 1);
1859 tmp_cache_realloc = vzalloc(tmp_cache_alloc_size);
1860 if (!tmp_cache_realloc)
1861 goto err;
1862 if (session->metadata_cache->data) {
1863 memcpy(tmp_cache_realloc,
1864 session->metadata_cache->data,
1865 session->metadata_cache->cache_alloc);
1866 vfree(session->metadata_cache->data);
1867 }
1868
1869 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1870 session->metadata_cache->data = tmp_cache_realloc;
1871 }
1872 memcpy(session->metadata_cache->data +
1873 session->metadata_cache->metadata_written,
1874 str, len);
1875 session->metadata_cache->metadata_written += len;
1876 kfree(str);
1877
1878 return 0;
1879
1880 err:
1881 kfree(str);
1882 return -ENOMEM;
1883 }
1884
1885 static
1886 int print_tabs(struct lttng_session *session, size_t nesting)
1887 {
1888 size_t i;
1889
1890 for (i = 0; i < nesting; i++) {
1891 int ret;
1892
1893 ret = lttng_metadata_printf(session, " ");
1894 if (ret) {
1895 return ret;
1896 }
1897 }
1898 return 0;
1899 }
1900
1901 static
1902 int lttng_field_name_statedump(struct lttng_session *session,
1903 const struct lttng_event_field *field,
1904 size_t nesting)
1905 {
1906 return lttng_metadata_printf(session, " _%s;\n", field->name);
1907 }
1908
1909 static
1910 int _lttng_integer_type_statedump(struct lttng_session *session,
1911 const struct lttng_type *type,
1912 size_t nesting)
1913 {
1914 int ret;
1915
1916 WARN_ON_ONCE(type->atype != atype_integer);
1917 ret = print_tabs(session, nesting);
1918 if (ret)
1919 return ret;
1920 ret = lttng_metadata_printf(session,
1921 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s }",
1922 type->u.integer.size,
1923 type->u.integer.alignment,
1924 type->u.integer.signedness,
1925 (type->u.integer.encoding == lttng_encode_none)
1926 ? "none"
1927 : (type->u.integer.encoding == lttng_encode_UTF8)
1928 ? "UTF8"
1929 : "ASCII",
1930 type->u.integer.base,
1931 #if __BYTE_ORDER == __BIG_ENDIAN
1932 type->u.integer.reverse_byte_order ? " byte_order = le;" : ""
1933 #else
1934 type->u.integer.reverse_byte_order ? " byte_order = be;" : ""
1935 #endif
1936 );
1937 return ret;
1938 }
1939
1940 /*
1941 * Must be called with sessions_mutex held.
1942 */
1943 static
1944 int _lttng_struct_type_statedump(struct lttng_session *session,
1945 const struct lttng_type *type,
1946 size_t nesting)
1947 {
1948 int ret;
1949 uint32_t i, nr_fields;
1950 unsigned int alignment;
1951
1952 WARN_ON_ONCE(type->atype != atype_struct_nestable);
1953
1954 ret = print_tabs(session, nesting);
1955 if (ret)
1956 return ret;
1957 ret = lttng_metadata_printf(session,
1958 "struct {\n");
1959 if (ret)
1960 return ret;
1961 nr_fields = type->u.struct_nestable.nr_fields;
1962 for (i = 0; i < nr_fields; i++) {
1963 const struct lttng_event_field *iter_field;
1964
1965 iter_field = &type->u.struct_nestable.fields[i];
1966 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1967 if (ret)
1968 return ret;
1969 }
1970 ret = print_tabs(session, nesting);
1971 if (ret)
1972 return ret;
1973 alignment = type->u.struct_nestable.alignment;
1974 if (alignment) {
1975 ret = lttng_metadata_printf(session,
1976 "} align(%u)",
1977 alignment);
1978 } else {
1979 ret = lttng_metadata_printf(session,
1980 "}");
1981 }
1982 return ret;
1983 }
1984
1985 /*
1986 * Must be called with sessions_mutex held.
1987 */
1988 static
1989 int _lttng_struct_field_statedump(struct lttng_session *session,
1990 const struct lttng_event_field *field,
1991 size_t nesting)
1992 {
1993 int ret;
1994
1995 ret = _lttng_struct_type_statedump(session,
1996 &field->type, nesting);
1997 if (ret)
1998 return ret;
1999 return lttng_field_name_statedump(session, field, nesting);
2000 }
2001
2002 /*
2003 * Must be called with sessions_mutex held.
2004 */
2005 static
2006 int _lttng_variant_type_statedump(struct lttng_session *session,
2007 const struct lttng_type *type,
2008 size_t nesting)
2009 {
2010 int ret;
2011 uint32_t i, nr_choices;
2012
2013 WARN_ON_ONCE(type->atype != atype_variant_nestable);
2014 /*
2015 * CTF 1.8 does not allow expressing nonzero variant alignment in a nestable way.
2016 */
2017 if (type->u.variant_nestable.alignment != 0)
2018 return -EINVAL;
2019 ret = print_tabs(session, nesting);
2020 if (ret)
2021 return ret;
2022 ret = lttng_metadata_printf(session,
2023 "variant <_%s> {\n",
2024 type->u.variant_nestable.tag_name);
2025 if (ret)
2026 return ret;
2027 nr_choices = type->u.variant_nestable.nr_choices;
2028 for (i = 0; i < nr_choices; i++) {
2029 const struct lttng_event_field *iter_field;
2030
2031 iter_field = &type->u.variant_nestable.choices[i];
2032 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
2033 if (ret)
2034 return ret;
2035 }
2036 ret = print_tabs(session, nesting);
2037 if (ret)
2038 return ret;
2039 ret = lttng_metadata_printf(session,
2040 "}");
2041 return ret;
2042 }
2043
2044 /*
2045 * Must be called with sessions_mutex held.
2046 */
2047 static
2048 int _lttng_variant_field_statedump(struct lttng_session *session,
2049 const struct lttng_event_field *field,
2050 size_t nesting)
2051 {
2052 int ret;
2053
2054 ret = _lttng_variant_type_statedump(session,
2055 &field->type, nesting);
2056 if (ret)
2057 return ret;
2058 return lttng_field_name_statedump(session, field, nesting);
2059 }
2060
2061 /*
2062 * Must be called with sessions_mutex held.
2063 */
2064 static
2065 int _lttng_array_field_statedump(struct lttng_session *session,
2066 const struct lttng_event_field *field,
2067 size_t nesting)
2068 {
2069 int ret;
2070 const struct lttng_type *elem_type;
2071
2072 WARN_ON_ONCE(field->type.atype != atype_array_nestable);
2073
2074 if (field->type.u.array_nestable.alignment) {
2075 ret = print_tabs(session, nesting);
2076 if (ret)
2077 return ret;
2078 ret = lttng_metadata_printf(session,
2079 "struct { } align(%u) _%s_padding;\n",
2080 field->type.u.array_nestable.alignment * CHAR_BIT,
2081 field->name);
2082 if (ret)
2083 return ret;
2084 }
2085 /*
2086 * Nested compound types: Only array of structures and variants are
2087 * currently supported.
2088 */
2089 elem_type = field->type.u.array_nestable.elem_type;
2090 switch (elem_type->atype) {
2091 case atype_integer:
2092 case atype_struct_nestable:
2093 case atype_variant_nestable:
2094 ret = _lttng_type_statedump(session, elem_type, nesting);
2095 if (ret)
2096 return ret;
2097 break;
2098
2099 default:
2100 return -EINVAL;
2101 }
2102 ret = lttng_metadata_printf(session,
2103 " _%s[%u];\n",
2104 field->name,
2105 field->type.u.array_nestable.length);
2106 return ret;
2107 }
2108
2109 /*
2110 * Must be called with sessions_mutex held.
2111 */
2112 static
2113 int _lttng_sequence_field_statedump(struct lttng_session *session,
2114 const struct lttng_event_field *field,
2115 size_t nesting)
2116 {
2117 int ret;
2118 const char *length_name;
2119 const struct lttng_type *elem_type;
2120
2121 WARN_ON_ONCE(field->type.atype != atype_sequence_nestable);
2122
2123 length_name = field->type.u.sequence_nestable.length_name;
2124
2125 if (field->type.u.sequence_nestable.alignment) {
2126 ret = print_tabs(session, nesting);
2127 if (ret)
2128 return ret;
2129 ret = lttng_metadata_printf(session,
2130 "struct { } align(%u) _%s_padding;\n",
2131 field->type.u.sequence_nestable.alignment * CHAR_BIT,
2132 field->name);
2133 if (ret)
2134 return ret;
2135 }
2136
2137 /*
2138 * Nested compound types: Only array of structures and variants are
2139 * currently supported.
2140 */
2141 elem_type = field->type.u.sequence_nestable.elem_type;
2142 switch (elem_type->atype) {
2143 case atype_integer:
2144 case atype_struct_nestable:
2145 case atype_variant_nestable:
2146 ret = _lttng_type_statedump(session, elem_type, nesting);
2147 if (ret)
2148 return ret;
2149 break;
2150
2151 default:
2152 return -EINVAL;
2153 }
2154 ret = lttng_metadata_printf(session,
2155 " _%s[ _%s ];\n",
2156 field->name,
2157 field->type.u.sequence_nestable.length_name);
2158 return ret;
2159 }
2160
2161 /*
2162 * Must be called with sessions_mutex held.
2163 */
2164 static
2165 int _lttng_enum_type_statedump(struct lttng_session *session,
2166 const struct lttng_type *type,
2167 size_t nesting)
2168 {
2169 const struct lttng_enum_desc *enum_desc;
2170 const struct lttng_type *container_type;
2171 int ret;
2172 unsigned int i, nr_entries;
2173
2174 container_type = type->u.enum_nestable.container_type;
2175 if (container_type->atype != atype_integer) {
2176 ret = -EINVAL;
2177 goto end;
2178 }
2179 enum_desc = type->u.enum_nestable.desc;
2180 nr_entries = enum_desc->nr_entries;
2181
2182 ret = print_tabs(session, nesting);
2183 if (ret)
2184 goto end;
2185 ret = lttng_metadata_printf(session, "enum : ");
2186 if (ret)
2187 goto end;
2188 ret = _lttng_integer_type_statedump(session, container_type, 0);
2189 if (ret)
2190 goto end;
2191 ret = lttng_metadata_printf(session, " {\n");
2192 if (ret)
2193 goto end;
2194 /* Dump all entries */
2195 for (i = 0; i < nr_entries; i++) {
2196 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
2197 int j, len;
2198
2199 ret = print_tabs(session, nesting + 1);
2200 if (ret)
2201 goto end;
2202 ret = lttng_metadata_printf(session,
2203 "\"");
2204 if (ret)
2205 goto end;
2206 len = strlen(entry->string);
2207 /* Escape the character '"' */
2208 for (j = 0; j < len; j++) {
2209 char c = entry->string[j];
2210
2211 switch (c) {
2212 case '"':
2213 ret = lttng_metadata_printf(session,
2214 "\\\"");
2215 break;
2216 case '\\':
2217 ret = lttng_metadata_printf(session,
2218 "\\\\");
2219 break;
2220 default:
2221 ret = lttng_metadata_printf(session,
2222 "%c", c);
2223 break;
2224 }
2225 if (ret)
2226 goto end;
2227 }
2228 ret = lttng_metadata_printf(session, "\"");
2229 if (ret)
2230 goto end;
2231
2232 if (entry->options.is_auto) {
2233 ret = lttng_metadata_printf(session, ",\n");
2234 if (ret)
2235 goto end;
2236 } else {
2237 ret = lttng_metadata_printf(session,
2238 " = ");
2239 if (ret)
2240 goto end;
2241 if (entry->start.signedness)
2242 ret = lttng_metadata_printf(session,
2243 "%lld", (long long) entry->start.value);
2244 else
2245 ret = lttng_metadata_printf(session,
2246 "%llu", entry->start.value);
2247 if (ret)
2248 goto end;
2249 if (entry->start.signedness == entry->end.signedness &&
2250 entry->start.value
2251 == entry->end.value) {
2252 ret = lttng_metadata_printf(session,
2253 ",\n");
2254 } else {
2255 if (entry->end.signedness) {
2256 ret = lttng_metadata_printf(session,
2257 " ... %lld,\n",
2258 (long long) entry->end.value);
2259 } else {
2260 ret = lttng_metadata_printf(session,
2261 " ... %llu,\n",
2262 entry->end.value);
2263 }
2264 }
2265 if (ret)
2266 goto end;
2267 }
2268 }
2269 ret = print_tabs(session, nesting);
2270 if (ret)
2271 goto end;
2272 ret = lttng_metadata_printf(session, "}");
2273 end:
2274 return ret;
2275 }
2276
2277 /*
2278 * Must be called with sessions_mutex held.
2279 */
2280 static
2281 int _lttng_enum_field_statedump(struct lttng_session *session,
2282 const struct lttng_event_field *field,
2283 size_t nesting)
2284 {
2285 int ret;
2286
2287 ret = _lttng_enum_type_statedump(session, &field->type, nesting);
2288 if (ret)
2289 return ret;
2290 return lttng_field_name_statedump(session, field, nesting);
2291 }
2292
2293 static
2294 int _lttng_integer_field_statedump(struct lttng_session *session,
2295 const struct lttng_event_field *field,
2296 size_t nesting)
2297 {
2298 int ret;
2299
2300 ret = _lttng_integer_type_statedump(session, &field->type, nesting);
2301 if (ret)
2302 return ret;
2303 return lttng_field_name_statedump(session, field, nesting);
2304 }
2305
2306 static
2307 int _lttng_string_type_statedump(struct lttng_session *session,
2308 const struct lttng_type *type,
2309 size_t nesting)
2310 {
2311 int ret;
2312
2313 WARN_ON_ONCE(type->atype != atype_string);
2314 /* Default encoding is UTF8 */
2315 ret = print_tabs(session, nesting);
2316 if (ret)
2317 return ret;
2318 ret = lttng_metadata_printf(session,
2319 "string%s",
2320 type->u.string.encoding == lttng_encode_ASCII ?
2321 " { encoding = ASCII; }" : "");
2322 return ret;
2323 }
2324
2325 static
2326 int _lttng_string_field_statedump(struct lttng_session *session,
2327 const struct lttng_event_field *field,
2328 size_t nesting)
2329 {
2330 int ret;
2331
2332 WARN_ON_ONCE(field->type.atype != atype_string);
2333 ret = _lttng_string_type_statedump(session, &field->type, nesting);
2334 if (ret)
2335 return ret;
2336 return lttng_field_name_statedump(session, field, nesting);
2337 }
2338
2339 /*
2340 * Must be called with sessions_mutex held.
2341 */
2342 static
2343 int _lttng_type_statedump(struct lttng_session *session,
2344 const struct lttng_type *type,
2345 size_t nesting)
2346 {
2347 int ret = 0;
2348
2349 switch (type->atype) {
2350 case atype_integer:
2351 ret = _lttng_integer_type_statedump(session, type, nesting);
2352 break;
2353 case atype_enum_nestable:
2354 ret = _lttng_enum_type_statedump(session, type, nesting);
2355 break;
2356 case atype_string:
2357 ret = _lttng_string_type_statedump(session, type, nesting);
2358 break;
2359 case atype_struct_nestable:
2360 ret = _lttng_struct_type_statedump(session, type, nesting);
2361 break;
2362 case atype_variant_nestable:
2363 ret = _lttng_variant_type_statedump(session, type, nesting);
2364 break;
2365
2366 /* Nested arrays and sequences are not supported yet. */
2367 case atype_array_nestable:
2368 case atype_sequence_nestable:
2369 default:
2370 WARN_ON_ONCE(1);
2371 return -EINVAL;
2372 }
2373 return ret;
2374 }
2375
2376 /*
2377 * Must be called with sessions_mutex held.
2378 */
2379 static
2380 int _lttng_field_statedump(struct lttng_session *session,
2381 const struct lttng_event_field *field,
2382 size_t nesting)
2383 {
2384 int ret = 0;
2385
2386 switch (field->type.atype) {
2387 case atype_integer:
2388 ret = _lttng_integer_field_statedump(session, field, nesting);
2389 break;
2390 case atype_enum_nestable:
2391 ret = _lttng_enum_field_statedump(session, field, nesting);
2392 break;
2393 case atype_string:
2394 ret = _lttng_string_field_statedump(session, field, nesting);
2395 break;
2396 case atype_struct_nestable:
2397 ret = _lttng_struct_field_statedump(session, field, nesting);
2398 break;
2399 case atype_array_nestable:
2400 ret = _lttng_array_field_statedump(session, field, nesting);
2401 break;
2402 case atype_sequence_nestable:
2403 ret = _lttng_sequence_field_statedump(session, field, nesting);
2404 break;
2405 case atype_variant_nestable:
2406 ret = _lttng_variant_field_statedump(session, field, nesting);
2407 break;
2408
2409 default:
2410 WARN_ON_ONCE(1);
2411 return -EINVAL;
2412 }
2413 return ret;
2414 }
2415
2416 static
2417 int _lttng_context_metadata_statedump(struct lttng_session *session,
2418 struct lttng_ctx *ctx)
2419 {
2420 int ret = 0;
2421 int i;
2422
2423 if (!ctx)
2424 return 0;
2425 for (i = 0; i < ctx->nr_fields; i++) {
2426 const struct lttng_ctx_field *field = &ctx->fields[i];
2427
2428 ret = _lttng_field_statedump(session, &field->event_field, 2);
2429 if (ret)
2430 return ret;
2431 }
2432 return ret;
2433 }
2434
2435 static
2436 int _lttng_fields_metadata_statedump(struct lttng_session *session,
2437 struct lttng_event *event)
2438 {
2439 const struct lttng_event_desc *desc = event->desc;
2440 int ret = 0;
2441 int i;
2442
2443 for (i = 0; i < desc->nr_fields; i++) {
2444 const struct lttng_event_field *field = &desc->fields[i];
2445
2446 ret = _lttng_field_statedump(session, field, 2);
2447 if (ret)
2448 return ret;
2449 }
2450 return ret;
2451 }
2452
2453 /*
2454 * Must be called with sessions_mutex held.
2455 * The entire event metadata is printed as a single atomic metadata
2456 * transaction.
2457 */
2458 static
2459 int _lttng_event_metadata_statedump(struct lttng_session *session,
2460 struct lttng_channel *chan,
2461 struct lttng_event *event)
2462 {
2463 int ret = 0;
2464
2465 if (event->metadata_dumped || !LTTNG_READ_ONCE(session->active))
2466 return 0;
2467 if (chan->channel_type == METADATA_CHANNEL)
2468 return 0;
2469
2470 lttng_metadata_begin(session);
2471
2472 ret = lttng_metadata_printf(session,
2473 "event {\n"
2474 " name = \"%s\";\n"
2475 " id = %u;\n"
2476 " stream_id = %u;\n",
2477 event->desc->name,
2478 event->id,
2479 event->chan->id);
2480 if (ret)
2481 goto end;
2482
2483 if (event->ctx) {
2484 ret = lttng_metadata_printf(session,
2485 " context := struct {\n");
2486 if (ret)
2487 goto end;
2488 }
2489 ret = _lttng_context_metadata_statedump(session, event->ctx);
2490 if (ret)
2491 goto end;
2492 if (event->ctx) {
2493 ret = lttng_metadata_printf(session,
2494 " };\n");
2495 if (ret)
2496 goto end;
2497 }
2498
2499 ret = lttng_metadata_printf(session,
2500 " fields := struct {\n"
2501 );
2502 if (ret)
2503 goto end;
2504
2505 ret = _lttng_fields_metadata_statedump(session, event);
2506 if (ret)
2507 goto end;
2508
2509 /*
2510 * LTTng space reservation can only reserve multiples of the
2511 * byte size.
2512 */
2513 ret = lttng_metadata_printf(session,
2514 " };\n"
2515 "};\n\n");
2516 if (ret)
2517 goto end;
2518
2519 event->metadata_dumped = 1;
2520 end:
2521 lttng_metadata_end(session);
2522 return ret;
2523
2524 }
2525
2526 /*
2527 * Must be called with sessions_mutex held.
2528 * The entire channel metadata is printed as a single atomic metadata
2529 * transaction.
2530 */
2531 static
2532 int _lttng_channel_metadata_statedump(struct lttng_session *session,
2533 struct lttng_channel *chan)
2534 {
2535 int ret = 0;
2536
2537 if (chan->metadata_dumped || !LTTNG_READ_ONCE(session->active))
2538 return 0;
2539
2540 if (chan->channel_type == METADATA_CHANNEL)
2541 return 0;
2542
2543 lttng_metadata_begin(session);
2544
2545 WARN_ON_ONCE(!chan->header_type);
2546 ret = lttng_metadata_printf(session,
2547 "stream {\n"
2548 " id = %u;\n"
2549 " event.header := %s;\n"
2550 " packet.context := struct packet_context;\n",
2551 chan->id,
2552 chan->header_type == 1 ? "struct event_header_compact" :
2553 "struct event_header_large");
2554 if (ret)
2555 goto end;
2556
2557 if (chan->ctx) {
2558 ret = lttng_metadata_printf(session,
2559 " event.context := struct {\n");
2560 if (ret)
2561 goto end;
2562 }
2563 ret = _lttng_context_metadata_statedump(session, chan->ctx);
2564 if (ret)
2565 goto end;
2566 if (chan->ctx) {
2567 ret = lttng_metadata_printf(session,
2568 " };\n");
2569 if (ret)
2570 goto end;
2571 }
2572
2573 ret = lttng_metadata_printf(session,
2574 "};\n\n");
2575
2576 chan->metadata_dumped = 1;
2577 end:
2578 lttng_metadata_end(session);
2579 return ret;
2580 }
2581
2582 /*
2583 * Must be called with sessions_mutex held.
2584 */
2585 static
2586 int _lttng_stream_packet_context_declare(struct lttng_session *session)
2587 {
2588 return lttng_metadata_printf(session,
2589 "struct packet_context {\n"
2590 " uint64_clock_monotonic_t timestamp_begin;\n"
2591 " uint64_clock_monotonic_t timestamp_end;\n"
2592 " uint64_t content_size;\n"
2593 " uint64_t packet_size;\n"
2594 " uint64_t packet_seq_num;\n"
2595 " unsigned long events_discarded;\n"
2596 " uint32_t cpu_id;\n"
2597 "};\n\n"
2598 );
2599 }
2600
2601 /*
2602 * Compact header:
2603 * id: range: 0 - 30.
2604 * id 31 is reserved to indicate an extended header.
2605 *
2606 * Large header:
2607 * id: range: 0 - 65534.
2608 * id 65535 is reserved to indicate an extended header.
2609 *
2610 * Must be called with sessions_mutex held.
2611 */
2612 static
2613 int _lttng_event_header_declare(struct lttng_session *session)
2614 {
2615 return lttng_metadata_printf(session,
2616 "struct event_header_compact {\n"
2617 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2618 " variant <id> {\n"
2619 " struct {\n"
2620 " uint27_clock_monotonic_t timestamp;\n"
2621 " } compact;\n"
2622 " struct {\n"
2623 " uint32_t id;\n"
2624 " uint64_clock_monotonic_t timestamp;\n"
2625 " } extended;\n"
2626 " } v;\n"
2627 "} align(%u);\n"
2628 "\n"
2629 "struct event_header_large {\n"
2630 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2631 " variant <id> {\n"
2632 " struct {\n"
2633 " uint32_clock_monotonic_t timestamp;\n"
2634 " } compact;\n"
2635 " struct {\n"
2636 " uint32_t id;\n"
2637 " uint64_clock_monotonic_t timestamp;\n"
2638 " } extended;\n"
2639 " } v;\n"
2640 "} align(%u);\n\n",
2641 lttng_alignof(uint32_t) * CHAR_BIT,
2642 lttng_alignof(uint16_t) * CHAR_BIT
2643 );
2644 }
2645
2646 /*
2647 * Approximation of NTP time of day to clock monotonic correlation,
2648 * taken at start of trace.
2649 * Yes, this is only an approximation. Yes, we can (and will) do better
2650 * in future versions.
2651 * This function may return a negative offset. It may happen if the
2652 * system sets the REALTIME clock to 0 after boot.
2653 *
2654 * Use 64bit timespec on kernels that have it, this makes 32bit arch
2655 * y2038 compliant.
2656 */
2657 static
2658 int64_t measure_clock_offset(void)
2659 {
2660 uint64_t monotonic_avg, monotonic[2], realtime;
2661 uint64_t tcf = trace_clock_freq();
2662 int64_t offset;
2663 unsigned long flags;
2664 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2665 struct timespec64 rts = { 0, 0 };
2666 #else
2667 struct timespec rts = { 0, 0 };
2668 #endif
2669
2670 /* Disable interrupts to increase correlation precision. */
2671 local_irq_save(flags);
2672 monotonic[0] = trace_clock_read64();
2673 #ifdef LTTNG_KERNEL_HAS_TIMESPEC64
2674 ktime_get_real_ts64(&rts);
2675 #else
2676 getnstimeofday(&rts);
2677 #endif
2678 monotonic[1] = trace_clock_read64();
2679 local_irq_restore(flags);
2680
2681 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2682 realtime = (uint64_t) rts.tv_sec * tcf;
2683 if (tcf == NSEC_PER_SEC) {
2684 realtime += rts.tv_nsec;
2685 } else {
2686 uint64_t n = rts.tv_nsec * tcf;
2687
2688 do_div(n, NSEC_PER_SEC);
2689 realtime += n;
2690 }
2691 offset = (int64_t) realtime - monotonic_avg;
2692 return offset;
2693 }
2694
2695 static
2696 int print_escaped_ctf_string(struct lttng_session *session, const char *string)
2697 {
2698 int ret = 0;
2699 size_t i;
2700 char cur;
2701
2702 i = 0;
2703 cur = string[i];
2704 while (cur != '\0') {
2705 switch (cur) {
2706 case '\n':
2707 ret = lttng_metadata_printf(session, "%s", "\\n");
2708 break;
2709 case '\\':
2710 case '"':
2711 ret = lttng_metadata_printf(session, "%c", '\\');
2712 if (ret)
2713 goto error;
2714 /* We still print the current char */
2715 /* Fallthrough */
2716 default:
2717 ret = lttng_metadata_printf(session, "%c", cur);
2718 break;
2719 }
2720
2721 if (ret)
2722 goto error;
2723
2724 cur = string[++i];
2725 }
2726 error:
2727 return ret;
2728 }
2729
2730 static
2731 int print_metadata_escaped_field(struct lttng_session *session, const char *field,
2732 const char *field_value)
2733 {
2734 int ret;
2735
2736 ret = lttng_metadata_printf(session, " %s = \"", field);
2737 if (ret)
2738 goto error;
2739
2740 ret = print_escaped_ctf_string(session, field_value);
2741 if (ret)
2742 goto error;
2743
2744 ret = lttng_metadata_printf(session, "\";\n");
2745
2746 error:
2747 return ret;
2748 }
2749
2750 /*
2751 * Output metadata into this session's metadata buffers.
2752 * Must be called with sessions_mutex held.
2753 */
2754 static
2755 int _lttng_session_metadata_statedump(struct lttng_session *session)
2756 {
2757 unsigned char *uuid_c = session->uuid.b;
2758 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
2759 const char *product_uuid;
2760 struct lttng_channel *chan;
2761 struct lttng_event *event;
2762 int ret = 0;
2763
2764 if (!LTTNG_READ_ONCE(session->active))
2765 return 0;
2766
2767 lttng_metadata_begin(session);
2768
2769 if (session->metadata_dumped)
2770 goto skip_session;
2771
2772 snprintf(uuid_s, sizeof(uuid_s),
2773 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2774 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2775 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2776 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2777 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
2778
2779 ret = lttng_metadata_printf(session,
2780 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2781 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2782 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2783 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
2784 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
2785 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2786 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
2787 "\n"
2788 "trace {\n"
2789 " major = %u;\n"
2790 " minor = %u;\n"
2791 " uuid = \"%s\";\n"
2792 " byte_order = %s;\n"
2793 " packet.header := struct {\n"
2794 " uint32_t magic;\n"
2795 " uint8_t uuid[16];\n"
2796 " uint32_t stream_id;\n"
2797 " uint64_t stream_instance_id;\n"
2798 " };\n"
2799 "};\n\n",
2800 lttng_alignof(uint8_t) * CHAR_BIT,
2801 lttng_alignof(uint16_t) * CHAR_BIT,
2802 lttng_alignof(uint32_t) * CHAR_BIT,
2803 lttng_alignof(uint64_t) * CHAR_BIT,
2804 sizeof(unsigned long) * CHAR_BIT,
2805 lttng_alignof(unsigned long) * CHAR_BIT,
2806 CTF_SPEC_MAJOR,
2807 CTF_SPEC_MINOR,
2808 uuid_s,
2809 #if __BYTE_ORDER == __BIG_ENDIAN
2810 "be"
2811 #else
2812 "le"
2813 #endif
2814 );
2815 if (ret)
2816 goto end;
2817
2818 ret = lttng_metadata_printf(session,
2819 "env {\n"
2820 " hostname = \"%s\";\n"
2821 " domain = \"kernel\";\n"
2822 " sysname = \"%s\";\n"
2823 " kernel_release = \"%s\";\n"
2824 " kernel_version = \"%s\";\n"
2825 " tracer_name = \"lttng-modules\";\n"
2826 " tracer_major = %d;\n"
2827 " tracer_minor = %d;\n"
2828 " tracer_patchlevel = %d;\n"
2829 " trace_buffering_scheme = \"global\";\n",
2830 current->nsproxy->uts_ns->name.nodename,
2831 utsname()->sysname,
2832 utsname()->release,
2833 utsname()->version,
2834 LTTNG_MODULES_MAJOR_VERSION,
2835 LTTNG_MODULES_MINOR_VERSION,
2836 LTTNG_MODULES_PATCHLEVEL_VERSION
2837 );
2838 if (ret)
2839 goto end;
2840
2841 ret = print_metadata_escaped_field(session, "trace_name", session->name);
2842 if (ret)
2843 goto end;
2844 ret = print_metadata_escaped_field(session, "trace_creation_datetime",
2845 session->creation_time);
2846 if (ret)
2847 goto end;
2848
2849 /* Add the product UUID to the 'env' section */
2850 product_uuid = dmi_get_system_info(DMI_PRODUCT_UUID);
2851 if (product_uuid) {
2852 ret = lttng_metadata_printf(session,
2853 " product_uuid = \"%s\";\n",
2854 product_uuid
2855 );
2856 if (ret)
2857 goto end;
2858 }
2859
2860 /* Close the 'env' section */
2861 ret = lttng_metadata_printf(session, "};\n\n");
2862 if (ret)
2863 goto end;
2864
2865 ret = lttng_metadata_printf(session,
2866 "clock {\n"
2867 " name = \"%s\";\n",
2868 trace_clock_name()
2869 );
2870 if (ret)
2871 goto end;
2872
2873 if (!trace_clock_uuid(clock_uuid_s)) {
2874 ret = lttng_metadata_printf(session,
2875 " uuid = \"%s\";\n",
2876 clock_uuid_s
2877 );
2878 if (ret)
2879 goto end;
2880 }
2881
2882 ret = lttng_metadata_printf(session,
2883 " description = \"%s\";\n"
2884 " freq = %llu; /* Frequency, in Hz */\n"
2885 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
2886 " offset = %lld;\n"
2887 "};\n\n",
2888 trace_clock_description(),
2889 (unsigned long long) trace_clock_freq(),
2890 (long long) measure_clock_offset()
2891 );
2892 if (ret)
2893 goto end;
2894
2895 ret = lttng_metadata_printf(session,
2896 "typealias integer {\n"
2897 " size = 27; align = 1; signed = false;\n"
2898 " map = clock.%s.value;\n"
2899 "} := uint27_clock_monotonic_t;\n"
2900 "\n"
2901 "typealias integer {\n"
2902 " size = 32; align = %u; signed = false;\n"
2903 " map = clock.%s.value;\n"
2904 "} := uint32_clock_monotonic_t;\n"
2905 "\n"
2906 "typealias integer {\n"
2907 " size = 64; align = %u; signed = false;\n"
2908 " map = clock.%s.value;\n"
2909 "} := uint64_clock_monotonic_t;\n\n",
2910 trace_clock_name(),
2911 lttng_alignof(uint32_t) * CHAR_BIT,
2912 trace_clock_name(),
2913 lttng_alignof(uint64_t) * CHAR_BIT,
2914 trace_clock_name()
2915 );
2916 if (ret)
2917 goto end;
2918
2919 ret = _lttng_stream_packet_context_declare(session);
2920 if (ret)
2921 goto end;
2922
2923 ret = _lttng_event_header_declare(session);
2924 if (ret)
2925 goto end;
2926
2927 skip_session:
2928 list_for_each_entry(chan, &session->chan, list) {
2929 ret = _lttng_channel_metadata_statedump(session, chan);
2930 if (ret)
2931 goto end;
2932 }
2933
2934 list_for_each_entry(event, &session->events, list) {
2935 ret = _lttng_event_metadata_statedump(session, event->chan, event);
2936 if (ret)
2937 goto end;
2938 }
2939 session->metadata_dumped = 1;
2940 end:
2941 lttng_metadata_end(session);
2942 return ret;
2943 }
2944
2945 /**
2946 * lttng_transport_register - LTT transport registration
2947 * @transport: transport structure
2948 *
2949 * Registers a transport which can be used as output to extract the data out of
2950 * LTTng. The module calling this registration function must ensure that no
2951 * trap-inducing code will be executed by the transport functions. E.g.
2952 * vmalloc_sync_mappings() must be called between a vmalloc and the moment the memory
2953 * is made visible to the transport function. This registration acts as a
2954 * vmalloc_sync_mappings. Therefore, only if the module allocates virtual memory
2955 * after its registration must it synchronize the TLBs.
2956 */
2957 void lttng_transport_register(struct lttng_transport *transport)
2958 {
2959 /*
2960 * Make sure no page fault can be triggered by the module about to be
2961 * registered. We deal with this here so we don't have to call
2962 * vmalloc_sync_mappings() in each module's init.
2963 */
2964 wrapper_vmalloc_sync_mappings();
2965
2966 mutex_lock(&sessions_mutex);
2967 list_add_tail(&transport->node, &lttng_transport_list);
2968 mutex_unlock(&sessions_mutex);
2969 }
2970 EXPORT_SYMBOL_GPL(lttng_transport_register);
2971
2972 /**
2973 * lttng_transport_unregister - LTT transport unregistration
2974 * @transport: transport structure
2975 */
2976 void lttng_transport_unregister(struct lttng_transport *transport)
2977 {
2978 mutex_lock(&sessions_mutex);
2979 list_del(&transport->node);
2980 mutex_unlock(&sessions_mutex);
2981 }
2982 EXPORT_SYMBOL_GPL(lttng_transport_unregister);
2983
2984 #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))
2985
2986 enum cpuhp_state lttng_hp_prepare;
2987 enum cpuhp_state lttng_hp_online;
2988
2989 static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2990 {
2991 struct lttng_cpuhp_node *lttng_node;
2992
2993 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2994 switch (lttng_node->component) {
2995 case LTTNG_RING_BUFFER_FRONTEND:
2996 return 0;
2997 case LTTNG_RING_BUFFER_BACKEND:
2998 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2999 case LTTNG_RING_BUFFER_ITER:
3000 return 0;
3001 case LTTNG_CONTEXT_PERF_COUNTERS:
3002 return 0;
3003 default:
3004 return -EINVAL;
3005 }
3006 }
3007
3008 static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
3009 {
3010 struct lttng_cpuhp_node *lttng_node;
3011
3012 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
3013 switch (lttng_node->component) {
3014 case LTTNG_RING_BUFFER_FRONTEND:
3015 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
3016 case LTTNG_RING_BUFFER_BACKEND:
3017 return 0;
3018 case LTTNG_RING_BUFFER_ITER:
3019 return 0;
3020 case LTTNG_CONTEXT_PERF_COUNTERS:
3021 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
3022 default:
3023 return -EINVAL;
3024 }
3025 }
3026
3027 static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
3028 {
3029 struct lttng_cpuhp_node *lttng_node;
3030
3031 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
3032 switch (lttng_node->component) {
3033 case LTTNG_RING_BUFFER_FRONTEND:
3034 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
3035 case LTTNG_RING_BUFFER_BACKEND:
3036 return 0;
3037 case LTTNG_RING_BUFFER_ITER:
3038 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
3039 case LTTNG_CONTEXT_PERF_COUNTERS:
3040 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
3041 default:
3042 return -EINVAL;
3043 }
3044 }
3045
3046 static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
3047 {
3048 struct lttng_cpuhp_node *lttng_node;
3049
3050 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
3051 switch (lttng_node->component) {
3052 case LTTNG_RING_BUFFER_FRONTEND:
3053 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
3054 case LTTNG_RING_BUFFER_BACKEND:
3055 return 0;
3056 case LTTNG_RING_BUFFER_ITER:
3057 return 0;
3058 case LTTNG_CONTEXT_PERF_COUNTERS:
3059 return 0;
3060 default:
3061 return -EINVAL;
3062 }
3063 }
3064
3065 static int __init lttng_init_cpu_hotplug(void)
3066 {
3067 int ret;
3068
3069 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
3070 lttng_hotplug_prepare,
3071 lttng_hotplug_dead);
3072 if (ret < 0) {
3073 return ret;
3074 }
3075 lttng_hp_prepare = ret;
3076 lttng_rb_set_hp_prepare(ret);
3077
3078 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
3079 lttng_hotplug_online,
3080 lttng_hotplug_offline);
3081 if (ret < 0) {
3082 cpuhp_remove_multi_state(lttng_hp_prepare);
3083 lttng_hp_prepare = 0;
3084 return ret;
3085 }
3086 lttng_hp_online = ret;
3087 lttng_rb_set_hp_online(ret);
3088
3089 return 0;
3090 }
3091
3092 static void __exit lttng_exit_cpu_hotplug(void)
3093 {
3094 lttng_rb_set_hp_online(0);
3095 cpuhp_remove_multi_state(lttng_hp_online);
3096 lttng_rb_set_hp_prepare(0);
3097 cpuhp_remove_multi_state(lttng_hp_prepare);
3098 }
3099
3100 #else /* #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3101 static int lttng_init_cpu_hotplug(void)
3102 {
3103 return 0;
3104 }
3105 static void lttng_exit_cpu_hotplug(void)
3106 {
3107 }
3108 #endif /* #else #if (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)) */
3109
3110
3111 static int __init lttng_events_init(void)
3112 {
3113 int ret;
3114
3115 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
3116 if (ret)
3117 return ret;
3118 ret = wrapper_get_pfnblock_flags_mask_init();
3119 if (ret)
3120 return ret;
3121 ret = wrapper_get_pageblock_flags_mask_init();
3122 if (ret)
3123 return ret;
3124 ret = lttng_probes_init();
3125 if (ret)
3126 return ret;
3127 ret = lttng_context_init();
3128 if (ret)
3129 return ret;
3130 ret = lttng_tracepoint_init();
3131 if (ret)
3132 goto error_tp;
3133 event_cache = KMEM_CACHE(lttng_event, 0);
3134 if (!event_cache) {
3135 ret = -ENOMEM;
3136 goto error_kmem;
3137 }
3138 ret = lttng_abi_init();
3139 if (ret)
3140 goto error_abi;
3141 ret = lttng_logger_init();
3142 if (ret)
3143 goto error_logger;
3144 ret = lttng_init_cpu_hotplug();
3145 if (ret)
3146 goto error_hotplug;
3147 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)%s%s\n",
3148 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3149 __stringify(LTTNG_MODULES_MINOR_VERSION),
3150 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3151 LTTNG_MODULES_EXTRAVERSION,
3152 LTTNG_VERSION_NAME,
3153 #ifdef LTTNG_EXTRA_VERSION_GIT
3154 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3155 #else
3156 "",
3157 #endif
3158 #ifdef LTTNG_EXTRA_VERSION_NAME
3159 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3160 #else
3161 "");
3162 #endif
3163 return 0;
3164
3165 error_hotplug:
3166 lttng_logger_exit();
3167 error_logger:
3168 lttng_abi_exit();
3169 error_abi:
3170 kmem_cache_destroy(event_cache);
3171 error_kmem:
3172 lttng_tracepoint_exit();
3173 error_tp:
3174 lttng_context_exit();
3175 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)%s%s\n",
3176 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3177 __stringify(LTTNG_MODULES_MINOR_VERSION),
3178 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3179 LTTNG_MODULES_EXTRAVERSION,
3180 LTTNG_VERSION_NAME,
3181 #ifdef LTTNG_EXTRA_VERSION_GIT
3182 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3183 #else
3184 "",
3185 #endif
3186 #ifdef LTTNG_EXTRA_VERSION_NAME
3187 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3188 #else
3189 "");
3190 #endif
3191 return ret;
3192 }
3193
3194 module_init(lttng_events_init);
3195
3196 static void __exit lttng_events_exit(void)
3197 {
3198 struct lttng_session *session, *tmpsession;
3199
3200 lttng_exit_cpu_hotplug();
3201 lttng_logger_exit();
3202 lttng_abi_exit();
3203 list_for_each_entry_safe(session, tmpsession, &sessions, list)
3204 lttng_session_destroy(session);
3205 kmem_cache_destroy(event_cache);
3206 lttng_tracepoint_exit();
3207 lttng_context_exit();
3208 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)%s%s\n",
3209 __stringify(LTTNG_MODULES_MAJOR_VERSION),
3210 __stringify(LTTNG_MODULES_MINOR_VERSION),
3211 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
3212 LTTNG_MODULES_EXTRAVERSION,
3213 LTTNG_VERSION_NAME,
3214 #ifdef LTTNG_EXTRA_VERSION_GIT
3215 LTTNG_EXTRA_VERSION_GIT[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_GIT,
3216 #else
3217 "",
3218 #endif
3219 #ifdef LTTNG_EXTRA_VERSION_NAME
3220 LTTNG_EXTRA_VERSION_NAME[0] == '\0' ? "" : " - " LTTNG_EXTRA_VERSION_NAME);
3221 #else
3222 "");
3223 #endif
3224 }
3225
3226 module_exit(lttng_events_exit);
3227
3228 #include <generated/patches.h>
3229 #ifdef LTTNG_EXTRA_VERSION_GIT
3230 MODULE_INFO(extra_version_git, LTTNG_EXTRA_VERSION_GIT);
3231 #endif
3232 #ifdef LTTNG_EXTRA_VERSION_NAME
3233 MODULE_INFO(extra_version_name, LTTNG_EXTRA_VERSION_NAME);
3234 #endif
3235 MODULE_LICENSE("GPL and additional rights");
3236 MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
3237 MODULE_DESCRIPTION("LTTng tracer");
3238 MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
3239 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
3240 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
3241 LTTNG_MODULES_EXTRAVERSION);
This page took 0.098181 seconds and 4 git commands to generate.