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