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