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