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