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