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