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