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