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