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