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