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