Add version info to lttng-tracer.ko modinfo
[lttng-modules.git] / lttng-events.c
CommitLineData
4e3c1b9b 1/*
a90917c3 2 * lttng-events.c
4e3c1b9b 3 *
4e3c1b9b 4 * Holds LTTng per-session event registry.
17baffe2 5 *
886d51a3
MD
6 * Copyright (C) 2010-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; only
11 * version 2.1 of the License.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4e3c1b9b
MD
21 */
22
23#include <linux/module.h>
c0e31d2e
MD
24#include <linux/list.h>
25#include <linux/mutex.h>
26#include <linux/sched.h>
11b5a3c2 27#include <linux/slab.h>
c099397a 28#include <linux/jiffies.h>
99dc9597 29#include <linux/utsname.h>
a864fb02 30#include "wrapper/uuid.h"
b13f3ebe 31#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
a82c63f1 32#include "wrapper/random.h"
3a523f5b 33#include "wrapper/tracepoint.h"
b8590f40 34#include "lttng-kernel-version.h"
a90917c3
MD
35#include "lttng-events.h"
36#include "lttng-tracer.h"
6dccd6c1 37#include "lttng-abi-old.h"
4e3c1b9b 38
d83004aa
JD
39#define METADATA_CACHE_DEFAULT_SIZE 4096
40
4e3c1b9b 41static LIST_HEAD(sessions);
a90917c3 42static LIST_HEAD(lttng_transport_list);
d83004aa
JD
43/*
44 * Protect the sessions and metadata caches.
45 */
4e3c1b9b
MD
46static DEFINE_MUTEX(sessions_mutex);
47static struct kmem_cache *event_cache;
48
a90917c3
MD
49static void _lttng_event_destroy(struct lttng_event *event);
50static void _lttng_channel_destroy(struct lttng_channel *chan);
51static int _lttng_event_unregister(struct lttng_event *event);
c099397a 52static
a90917c3
MD
53int _lttng_event_metadata_statedump(struct lttng_session *session,
54 struct lttng_channel *chan,
55 struct lttng_event *event);
c099397a 56static
a90917c3 57int _lttng_session_metadata_statedump(struct lttng_session *session);
d83004aa
JD
58static
59void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
c099397a 60
c099397a 61void synchronize_trace(void)
abcca994
MD
62{
63 synchronize_sched();
b8590f40
MD
64#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
65#ifdef CONFIG_PREEMPT_RT_FULL
66 synchronize_rcu();
67#endif
68#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
69#ifdef CONFIG_PREEMPT_RT
70 synchronize_rcu();
71#endif
b8590f40 72#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
73}
74
a90917c3 75struct lttng_session *lttng_session_create(void)
4e3c1b9b 76{
a90917c3 77 struct lttng_session *session;
d83004aa 78 struct lttng_metadata_cache *metadata_cache;
4e3c1b9b
MD
79
80 mutex_lock(&sessions_mutex);
a90917c3 81 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
4e3c1b9b 82 if (!session)
d83004aa 83 goto err;
4e3c1b9b 84 INIT_LIST_HEAD(&session->chan);
f3d01b96 85 INIT_LIST_HEAD(&session->events);
d793d5e1 86 uuid_le_gen(&session->uuid);
d83004aa
JD
87
88 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
89 GFP_KERNEL);
90 if (!metadata_cache)
91 goto err_free_session;
92 metadata_cache->data = kzalloc(METADATA_CACHE_DEFAULT_SIZE,
93 GFP_KERNEL);
94 if (!metadata_cache->data)
95 goto err_free_cache;
96 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
97 kref_init(&metadata_cache->refcount);
98 session->metadata_cache = metadata_cache;
99 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
4e3c1b9b
MD
100 list_add(&session->list, &sessions);
101 mutex_unlock(&sessions_mutex);
102 return session;
d83004aa
JD
103
104err_free_cache:
105 kfree(metadata_cache);
106err_free_session:
107 kfree(session);
108err:
109 mutex_unlock(&sessions_mutex);
110 return NULL;
111}
112
113void metadata_cache_destroy(struct kref *kref)
114{
115 struct lttng_metadata_cache *cache =
116 container_of(kref, struct lttng_metadata_cache, refcount);
117 kfree(cache->data);
118 kfree(cache);
4e3c1b9b
MD
119}
120
a90917c3 121void lttng_session_destroy(struct lttng_session *session)
4e3c1b9b 122{
a90917c3
MD
123 struct lttng_channel *chan, *tmpchan;
124 struct lttng_event *event, *tmpevent;
d83004aa 125 struct lttng_metadata_stream *metadata_stream;
dda6a249 126 int ret;
4e3c1b9b
MD
127
128 mutex_lock(&sessions_mutex);
52fc2e1f 129 ACCESS_ONCE(session->active) = 0;
1ec65de1
MD
130 list_for_each_entry(chan, &session->chan, list) {
131 ret = lttng_syscalls_unregister(chan);
132 WARN_ON(ret);
133 }
dda6a249 134 list_for_each_entry(event, &session->events, list) {
a90917c3 135 ret = _lttng_event_unregister(event);
dda6a249
MD
136 WARN_ON(ret);
137 }
abcca994 138 synchronize_trace(); /* Wait for in-flight events to complete */
4e3c1b9b 139 list_for_each_entry_safe(event, tmpevent, &session->events, list)
a90917c3 140 _lttng_event_destroy(event);
d83004aa
JD
141 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
142 BUG_ON(chan->channel_type == METADATA_CHANNEL);
a90917c3 143 _lttng_channel_destroy(chan);
d83004aa
JD
144 }
145 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
146 _lttng_metadata_channel_hangup(metadata_stream);
147 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
4e3c1b9b
MD
148 list_del(&session->list);
149 mutex_unlock(&sessions_mutex);
150 kfree(session);
151}
152
a90917c3 153int lttng_session_enable(struct lttng_session *session)
c0e31d2e
MD
154{
155 int ret = 0;
a90917c3 156 struct lttng_channel *chan;
c0e31d2e
MD
157
158 mutex_lock(&sessions_mutex);
159 if (session->active) {
160 ret = -EBUSY;
161 goto end;
162 }
c099397a
MD
163
164 /*
165 * Snapshot the number of events per channel to know the type of header
166 * we need to use.
167 */
168 list_for_each_entry(chan, &session->chan, list) {
169 if (chan->header_type)
170 continue; /* don't change it if session stop/restart */
171 if (chan->free_event_id < 31)
172 chan->header_type = 1; /* compact */
173 else
174 chan->header_type = 2; /* large */
175 }
176
52fc2e1f 177 ACCESS_ONCE(session->active) = 1;
8070f5c0 178 ACCESS_ONCE(session->been_active) = 1;
a90917c3 179 ret = _lttng_session_metadata_statedump(session);
c337ddc2
MD
180 if (ret) {
181 ACCESS_ONCE(session->active) = 0;
182 goto end;
183 }
184 ret = lttng_statedump_start(session);
360f38ea 185 if (ret)
c099397a 186 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
187end:
188 mutex_unlock(&sessions_mutex);
11b5a3c2 189 return ret;
c0e31d2e
MD
190}
191
a90917c3 192int lttng_session_disable(struct lttng_session *session)
c0e31d2e
MD
193{
194 int ret = 0;
195
196 mutex_lock(&sessions_mutex);
197 if (!session->active) {
198 ret = -EBUSY;
199 goto end;
200 }
52fc2e1f 201 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
202end:
203 mutex_unlock(&sessions_mutex);
11b5a3c2 204 return ret;
c0e31d2e
MD
205}
206
a90917c3 207int lttng_channel_enable(struct lttng_channel *channel)
e64957da
MD
208{
209 int old;
210
d83004aa 211 if (channel->channel_type == METADATA_CHANNEL)
c6485006 212 return -EPERM;
e64957da
MD
213 old = xchg(&channel->enabled, 1);
214 if (old)
215 return -EEXIST;
216 return 0;
217}
218
a90917c3 219int lttng_channel_disable(struct lttng_channel *channel)
e64957da
MD
220{
221 int old;
222
d83004aa 223 if (channel->channel_type == METADATA_CHANNEL)
c6485006 224 return -EPERM;
e64957da
MD
225 old = xchg(&channel->enabled, 0);
226 if (!old)
227 return -EEXIST;
228 return 0;
229}
230
a90917c3 231int lttng_event_enable(struct lttng_event *event)
e64957da
MD
232{
233 int old;
234
d83004aa 235 if (event->chan->channel_type == METADATA_CHANNEL)
c6485006 236 return -EPERM;
e64957da
MD
237 old = xchg(&event->enabled, 1);
238 if (old)
239 return -EEXIST;
240 return 0;
241}
242
a90917c3 243int lttng_event_disable(struct lttng_event *event)
e64957da
MD
244{
245 int old;
246
d83004aa 247 if (event->chan->channel_type == METADATA_CHANNEL)
c6485006 248 return -EPERM;
e64957da
MD
249 old = xchg(&event->enabled, 0);
250 if (!old)
251 return -EEXIST;
252 return 0;
253}
254
a90917c3 255static struct lttng_transport *lttng_transport_find(const char *name)
f3d01b96 256{
a90917c3 257 struct lttng_transport *transport;
f3d01b96 258
a90917c3 259 list_for_each_entry(transport, &lttng_transport_list, node) {
f3d01b96
MD
260 if (!strcmp(transport->name, name))
261 return transport;
262 }
263 return NULL;
264}
265
a90917c3 266struct lttng_channel *lttng_channel_create(struct lttng_session *session,
5dbbdb43
MD
267 const char *transport_name,
268 void *buf_addr,
4e3c1b9b
MD
269 size_t subbuf_size, size_t num_subbuf,
270 unsigned int switch_timer_interval,
d83004aa
JD
271 unsigned int read_timer_interval,
272 enum channel_type channel_type)
4e3c1b9b 273{
a90917c3
MD
274 struct lttng_channel *chan;
275 struct lttng_transport *transport = NULL;
4e3c1b9b
MD
276
277 mutex_lock(&sessions_mutex);
d83004aa 278 if (session->been_active && channel_type != METADATA_CHANNEL)
e5382b6d 279 goto active; /* Refuse to add channel to active session */
a90917c3 280 transport = lttng_transport_find(transport_name);
f3d01b96
MD
281 if (!transport) {
282 printk(KERN_WARNING "LTTng transport %s not found\n",
283 transport_name);
c0e31d2e 284 goto notransport;
f3d01b96 285 }
a33c9927
MD
286 if (!try_module_get(transport->owner)) {
287 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
288 goto notransport;
289 }
a90917c3 290 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
4e3c1b9b 291 if (!chan)
c0e31d2e 292 goto nomem;
4e3c1b9b 293 chan->session = session;
05d32c64 294 chan->id = session->free_chan_id++;
3b731ab1 295 chan->ops = &transport->ops;
125b4df4
MD
296 /*
297 * Note: the channel creation op already writes into the packet
298 * headers. Therefore the "chan" information used as input
299 * should be already accessible.
300 */
5f5ddf01
MD
301 chan->chan = transport->ops.channel_create(transport_name,
302 chan, buf_addr, subbuf_size, num_subbuf,
303 switch_timer_interval, read_timer_interval);
f3d01b96
MD
304 if (!chan->chan)
305 goto create_error;
e64957da 306 chan->enabled = 1;
a33c9927 307 chan->transport = transport;
d83004aa 308 chan->channel_type = channel_type;
4e3c1b9b
MD
309 list_add(&chan->list, &session->chan);
310 mutex_unlock(&sessions_mutex);
311 return chan;
312
f3d01b96
MD
313create_error:
314 kfree(chan);
c0e31d2e 315nomem:
a33c9927
MD
316 if (transport)
317 module_put(transport->owner);
c0e31d2e 318notransport:
e5382b6d 319active:
4e3c1b9b
MD
320 mutex_unlock(&sessions_mutex);
321 return NULL;
322}
323
324/*
d83004aa
JD
325 * Only used internally at session destruction for per-cpu channels, and
326 * when metadata channel is released.
327 * Needs to be called with sessions mutex held.
4e3c1b9b 328 */
aa7c23a9 329static
a90917c3 330void _lttng_channel_destroy(struct lttng_channel *chan)
4e3c1b9b 331{
11b5a3c2 332 chan->ops->channel_destroy(chan->chan);
a33c9927 333 module_put(chan->transport->owner);
4e3c1b9b 334 list_del(&chan->list);
8070f5c0 335 lttng_destroy_context(chan->ctx);
4e3c1b9b
MD
336 kfree(chan);
337}
338
d83004aa
JD
339void lttng_metadata_channel_destroy(struct lttng_channel *chan)
340{
341 BUG_ON(chan->channel_type != METADATA_CHANNEL);
342
343 /* Protect the metadata cache with the sessions_mutex. */
344 mutex_lock(&sessions_mutex);
345 _lttng_channel_destroy(chan);
346 mutex_unlock(&sessions_mutex);
347}
348EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
349
350static
351void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
352{
353 stream->finalized = 1;
354 wake_up_interruptible(&stream->read_wait);
355}
356
e5382b6d
MD
357/*
358 * Supports event creation while tracing session is active.
359 */
a90917c3 360struct lttng_event *lttng_event_create(struct lttng_channel *chan,
d6d808f3 361 struct lttng_kernel_event *event_param,
259b6cb3
MD
362 void *filter,
363 const struct lttng_event_desc *internal_desc)
4e3c1b9b 364{
a90917c3 365 struct lttng_event *event;
3d084699 366 int ret;
4e3c1b9b
MD
367
368 mutex_lock(&sessions_mutex);
37303c75 369 if (chan->free_event_id == -1U)
e5382b6d 370 goto full;
4e3c1b9b 371 /*
dda6a249
MD
372 * This is O(n^2) (for each event, the loop is called at event
373 * creation). Might require a hash if we have lots of events.
4e3c1b9b
MD
374 */
375 list_for_each_entry(event, &chan->session->events, list)
30bdb6e4 376 if (!strcmp(event->desc->name, event_param->name))
4e3c1b9b 377 goto exist;
11b5a3c2 378 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
4e3c1b9b
MD
379 if (!event)
380 goto cache_error;
4e3c1b9b
MD
381 event->chan = chan;
382 event->filter = filter;
e5382b6d 383 event->id = chan->free_event_id++;
e64957da 384 event->enabled = 1;
d6d808f3 385 event->instrumentation = event_param->instrumentation;
a90917c3 386 /* Populate lttng_event structure before tracepoint registration. */
e5382b6d 387 smp_wmb();
d6d808f3 388 switch (event_param->instrumentation) {
ab2277d6 389 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 390 event->desc = lttng_event_get(event_param->name);
d3dbe23c
MD
391 if (!event->desc)
392 goto register_error;
3a523f5b 393 ret = kabi_2635_tracepoint_probe_register(event_param->name,
d3dbe23c
MD
394 event->desc->probe_callback,
395 event);
11b5a3c2
MD
396 if (ret)
397 goto register_error;
baf20995 398 break;
ab2277d6 399 case LTTNG_KERNEL_KPROBE:
30bdb6e4 400 ret = lttng_kprobes_register(event_param->name,
f17701fb
MD
401 event_param->u.kprobe.symbol_name,
402 event_param->u.kprobe.offset,
403 event_param->u.kprobe.addr,
404 event);
405 if (ret)
d6d808f3 406 goto register_error;
edeb3137
MD
407 ret = try_module_get(event->desc->owner);
408 WARN_ON_ONCE(!ret);
d6d808f3 409 break;
7371f44c
MD
410 case LTTNG_KERNEL_KRETPROBE:
411 {
a90917c3 412 struct lttng_event *event_return;
7371f44c
MD
413
414 /* kretprobe defines 2 events */
415 event_return =
416 kmem_cache_zalloc(event_cache, GFP_KERNEL);
417 if (!event_return)
418 goto register_error;
419 event_return->chan = chan;
420 event_return->filter = filter;
421 event_return->id = chan->free_event_id++;
422 event_return->enabled = 1;
423 event_return->instrumentation = event_param->instrumentation;
424 /*
a90917c3 425 * Populate lttng_event structure before kretprobe registration.
7371f44c
MD
426 */
427 smp_wmb();
428 ret = lttng_kretprobes_register(event_param->name,
429 event_param->u.kretprobe.symbol_name,
430 event_param->u.kretprobe.offset,
431 event_param->u.kretprobe.addr,
432 event, event_return);
433 if (ret) {
434 kmem_cache_free(event_cache, event_return);
435 goto register_error;
436 }
437 /* Take 2 refs on the module: one per event. */
438 ret = try_module_get(event->desc->owner);
439 WARN_ON_ONCE(!ret);
440 ret = try_module_get(event->desc->owner);
441 WARN_ON_ONCE(!ret);
a90917c3 442 ret = _lttng_event_metadata_statedump(chan->session, chan,
7371f44c
MD
443 event_return);
444 if (ret) {
445 kmem_cache_free(event_cache, event_return);
446 module_put(event->desc->owner);
447 module_put(event->desc->owner);
448 goto statedump_error;
449 }
450 list_add(&event_return->list, &chan->session->events);
451 break;
452 }
ab2277d6 453 case LTTNG_KERNEL_FUNCTION:
30bdb6e4 454 ret = lttng_ftrace_register(event_param->name,
e0a7a7c4
MD
455 event_param->u.ftrace.symbol_name,
456 event);
457 if (ret)
458 goto register_error;
edeb3137
MD
459 ret = try_module_get(event->desc->owner);
460 WARN_ON_ONCE(!ret);
e0a7a7c4 461 break;
1ec65de1 462 case LTTNG_KERNEL_NOOP:
259b6cb3
MD
463 event->desc = internal_desc;
464 if (!event->desc)
465 goto register_error;
1ec65de1 466 break;
baf20995
MD
467 default:
468 WARN_ON_ONCE(1);
df07930b 469 goto register_error;
baf20995 470 }
a90917c3 471 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
c099397a
MD
472 if (ret)
473 goto statedump_error;
dda6a249 474 list_add(&event->list, &chan->session->events);
11b5a3c2 475 mutex_unlock(&sessions_mutex);
4e3c1b9b
MD
476 return event;
477
c099397a 478statedump_error:
259b6cb3 479 /* If a statedump error occurs, events will not be readable. */
11b5a3c2 480register_error:
11b5a3c2 481 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
482cache_error:
483exist:
e5382b6d 484full:
4e3c1b9b
MD
485 mutex_unlock(&sessions_mutex);
486 return NULL;
487}
488
489/*
490 * Only used internally at session destruction.
491 */
a90917c3 492int _lttng_event_unregister(struct lttng_event *event)
4e3c1b9b 493{
11b5a3c2
MD
494 int ret = -EINVAL;
495
38d024ae 496 switch (event->instrumentation) {
ab2277d6 497 case LTTNG_KERNEL_TRACEPOINT:
3a523f5b 498 ret = kabi_2635_tracepoint_probe_unregister(event->desc->name,
85a9ca7f 499 event->desc->probe_callback,
11b5a3c2
MD
500 event);
501 if (ret)
502 return ret;
baf20995 503 break;
ab2277d6 504 case LTTNG_KERNEL_KPROBE:
f17701fb 505 lttng_kprobes_unregister(event);
d6d808f3
MD
506 ret = 0;
507 break;
7371f44c
MD
508 case LTTNG_KERNEL_KRETPROBE:
509 lttng_kretprobes_unregister(event);
510 ret = 0;
511 break;
ab2277d6 512 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
513 lttng_ftrace_unregister(event);
514 ret = 0;
515 break;
259b6cb3
MD
516 case LTTNG_KERNEL_NOOP:
517 ret = 0;
518 break;
baf20995
MD
519 default:
520 WARN_ON_ONCE(1);
521 }
dda6a249
MD
522 return ret;
523}
524
525/*
526 * Only used internally at session destruction.
527 */
be066e6c 528static
a90917c3 529void _lttng_event_destroy(struct lttng_event *event)
dda6a249 530{
edeb3137
MD
531 switch (event->instrumentation) {
532 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 533 lttng_event_put(event->desc);
edeb3137
MD
534 break;
535 case LTTNG_KERNEL_KPROBE:
536 module_put(event->desc->owner);
537 lttng_kprobes_destroy_private(event);
538 break;
7371f44c
MD
539 case LTTNG_KERNEL_KRETPROBE:
540 module_put(event->desc->owner);
541 lttng_kretprobes_destroy_private(event);
542 break;
edeb3137
MD
543 case LTTNG_KERNEL_FUNCTION:
544 module_put(event->desc->owner);
545 lttng_ftrace_destroy_private(event);
546 break;
259b6cb3
MD
547 case LTTNG_KERNEL_NOOP:
548 break;
edeb3137
MD
549 default:
550 WARN_ON_ONCE(1);
551 }
dda6a249 552 list_del(&event->list);
8070f5c0 553 lttng_destroy_context(event->ctx);
11b5a3c2 554 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
555}
556
1ec3f75a 557/*
d83004aa
JD
558 * Serialize at most one packet worth of metadata into a metadata
559 * channel.
1ec3f75a
MD
560 * We have exclusive access to our metadata buffer (protected by the
561 * sessions_mutex), so we can do racy operations such as looking for
562 * remaining space left in packet and write, since mutual exclusion
563 * protects us from concurrent writes.
35097f36
JD
564 * Returns the number of bytes written in the channel, 0 if no data
565 * was written and a negative value on error.
1ec3f75a 566 */
b3b8072b
MD
567int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
568 struct channel *chan)
d83004aa
JD
569{
570 struct lib_ring_buffer_ctx ctx;
571 int ret = 0;
572 size_t len, reserve_len;
573
f613e3e6
MD
574 /*
575 * Ensure we support mutiple get_next / put sequences followed
576 * by put_next.
577 */
578 WARN_ON(stream->metadata_in < stream->metadata_out);
579 if (stream->metadata_in != stream->metadata_out)
580 return 0;
581
d83004aa 582 len = stream->metadata_cache->metadata_written -
f613e3e6 583 stream->metadata_in;
9de2c215
MD
584 if (!len)
585 return 0;
d83004aa 586 reserve_len = min_t(size_t,
b3b8072b 587 stream->transport->ops.packet_avail_size(chan),
d83004aa 588 len);
b3b8072b 589 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
d83004aa
JD
590 sizeof(char), -1);
591 /*
592 * If reservation failed, return an error to the caller.
593 */
b3b8072b 594 ret = stream->transport->ops.event_reserve(&ctx, 0);
d83004aa
JD
595 if (ret != 0) {
596 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
597 goto end;
598 }
b3b8072b 599 stream->transport->ops.event_write(&ctx,
f613e3e6 600 stream->metadata_cache->data + stream->metadata_in,
d83004aa 601 reserve_len);
b3b8072b 602 stream->transport->ops.event_commit(&ctx);
f613e3e6 603 stream->metadata_in += reserve_len;
d83004aa
JD
604 ret = reserve_len;
605
606end:
607 return ret;
608}
609
610/*
611 * Write the metadata to the metadata cache.
612 * Must be called with sessions_mutex held.
613 */
a90917c3 614int lttng_metadata_printf(struct lttng_session *session,
c099397a
MD
615 const char *fmt, ...)
616{
c099397a 617 char *str;
d83004aa 618 size_t len;
c099397a 619 va_list ap;
d83004aa 620 struct lttng_metadata_stream *stream;
c099397a
MD
621
622 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
623
624 va_start(ap, fmt);
625 str = kvasprintf(GFP_KERNEL, fmt, ap);
626 va_end(ap);
627 if (!str)
628 return -ENOMEM;
629
1ec3f75a 630 len = strlen(str);
d83004aa
JD
631 if (session->metadata_cache->metadata_written + len >
632 session->metadata_cache->cache_alloc) {
633 char *tmp_cache_realloc;
634 unsigned int tmp_cache_alloc_size;
635
636 tmp_cache_alloc_size = max_t(unsigned int,
637 session->metadata_cache->cache_alloc + len,
638 session->metadata_cache->cache_alloc << 1);
639 tmp_cache_realloc = krealloc(session->metadata_cache->data,
640 tmp_cache_alloc_size, GFP_KERNEL);
641 if (!tmp_cache_realloc)
642 goto err;
643 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
644 session->metadata_cache->data = tmp_cache_realloc;
c099397a 645 }
d83004aa
JD
646 memcpy(session->metadata_cache->data +
647 session->metadata_cache->metadata_written,
648 str, len);
649 session->metadata_cache->metadata_written += len;
c099397a 650 kfree(str);
d83004aa
JD
651
652 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
653 wake_up_interruptible(&stream->read_wait);
654
655 return 0;
656
657err:
658 kfree(str);
659 return -ENOMEM;
c099397a
MD
660}
661
d83004aa
JD
662/*
663 * Must be called with sessions_mutex held.
664 */
c099397a 665static
a90917c3 666int _lttng_field_statedump(struct lttng_session *session,
8070f5c0 667 const struct lttng_event_field *field)
c099397a 668{
c099397a 669 int ret = 0;
c099397a 670
8070f5c0
MD
671 switch (field->type.atype) {
672 case atype_integer:
673 ret = lttng_metadata_printf(session,
244465ab 674 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8070f5c0
MD
675 field->type.u.basic.integer.size,
676 field->type.u.basic.integer.alignment,
677 field->type.u.basic.integer.signedness,
678 (field->type.u.basic.integer.encoding == lttng_encode_none)
679 ? "none"
680 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
681 ? "UTF8"
682 : "ASCII",
683 field->type.u.basic.integer.base,
c099397a 684#ifdef __BIG_ENDIAN
8070f5c0 685 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
c099397a 686#else
8070f5c0 687 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
c099397a 688#endif
8070f5c0
MD
689 field->name);
690 break;
691 case atype_enum:
692 ret = lttng_metadata_printf(session,
244465ab 693 " %s _%s;\n",
8070f5c0
MD
694 field->type.u.basic.enumeration.name,
695 field->name);
696 break;
697 case atype_array:
698 {
699 const struct lttng_basic_type *elem_type;
700
701 elem_type = &field->type.u.array.elem_type;
702 ret = lttng_metadata_printf(session,
244465ab 703 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8070f5c0
MD
704 elem_type->u.basic.integer.size,
705 elem_type->u.basic.integer.alignment,
706 elem_type->u.basic.integer.signedness,
707 (elem_type->u.basic.integer.encoding == lttng_encode_none)
708 ? "none"
709 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
710 ? "UTF8"
711 : "ASCII",
712 elem_type->u.basic.integer.base,
8fa75f34 713#ifdef __BIG_ENDIAN
8070f5c0 714 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 715#else
8070f5c0 716 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 717#endif
8070f5c0
MD
718 field->name, field->type.u.array.length);
719 break;
720 }
721 case atype_sequence:
722 {
723 const struct lttng_basic_type *elem_type;
724 const struct lttng_basic_type *length_type;
725
726 elem_type = &field->type.u.sequence.elem_type;
727 length_type = &field->type.u.sequence.length_type;
728 ret = lttng_metadata_printf(session,
729 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
8070f5c0 730 length_type->u.basic.integer.size,
32257272 731 (unsigned int) length_type->u.basic.integer.alignment,
8070f5c0
MD
732 length_type->u.basic.integer.signedness,
733 (length_type->u.basic.integer.encoding == lttng_encode_none)
734 ? "none"
0635cdd4 735 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 736 ? "UTF8"
0635cdd4 737 : "ASCII"),
8070f5c0 738 length_type->u.basic.integer.base,
e0a7a7c4 739#ifdef __BIG_ENDIAN
8070f5c0 740 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
e0a7a7c4 741#else
0635cdd4 742 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
e0a7a7c4 743#endif
27d2368e
MD
744 field->name);
745 if (ret)
746 return ret;
747
748 ret = lttng_metadata_printf(session,
244465ab 749 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8070f5c0 750 elem_type->u.basic.integer.size,
32257272 751 (unsigned int) elem_type->u.basic.integer.alignment,
8070f5c0
MD
752 elem_type->u.basic.integer.signedness,
753 (elem_type->u.basic.integer.encoding == lttng_encode_none)
754 ? "none"
0635cdd4 755 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 756 ? "UTF8"
0635cdd4 757 : "ASCII"),
8070f5c0 758 elem_type->u.basic.integer.base,
8fa75f34 759#ifdef __BIG_ENDIAN
8070f5c0 760 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 761#else
8070f5c0 762 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 763#endif
8070f5c0 764 field->name,
27d2368e 765 field->name);
8070f5c0
MD
766 break;
767 }
c099397a 768
8070f5c0
MD
769 case atype_string:
770 /* Default encoding is UTF8 */
771 ret = lttng_metadata_printf(session,
244465ab 772 " string%s _%s;\n",
8070f5c0
MD
773 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
774 " { encoding = ASCII; }" : "",
775 field->name);
776 break;
777 default:
778 WARN_ON_ONCE(1);
779 return -EINVAL;
780 }
781 return ret;
782}
783
784static
a90917c3 785int _lttng_context_metadata_statedump(struct lttng_session *session,
8070f5c0
MD
786 struct lttng_ctx *ctx)
787{
788 int ret = 0;
789 int i;
790
791 if (!ctx)
792 return 0;
793 for (i = 0; i < ctx->nr_fields; i++) {
794 const struct lttng_ctx_field *field = &ctx->fields[i];
795
a90917c3 796 ret = _lttng_field_statedump(session, &field->event_field);
8070f5c0
MD
797 if (ret)
798 return ret;
799 }
800 return ret;
801}
802
803static
a90917c3
MD
804int _lttng_fields_metadata_statedump(struct lttng_session *session,
805 struct lttng_event *event)
8070f5c0
MD
806{
807 const struct lttng_event_desc *desc = event->desc;
808 int ret = 0;
809 int i;
810
811 for (i = 0; i < desc->nr_fields; i++) {
812 const struct lttng_event_field *field = &desc->fields[i];
813
a90917c3 814 ret = _lttng_field_statedump(session, field);
8070f5c0
MD
815 if (ret)
816 return ret;
c099397a
MD
817 }
818 return ret;
819}
820
d83004aa
JD
821/*
822 * Must be called with sessions_mutex held.
823 */
c099397a 824static
a90917c3
MD
825int _lttng_event_metadata_statedump(struct lttng_session *session,
826 struct lttng_channel *chan,
827 struct lttng_event *event)
c099397a
MD
828{
829 int ret = 0;
830
831 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
832 return 0;
d83004aa 833 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
834 return 0;
835
836 ret = lttng_metadata_printf(session,
837 "event {\n"
838 " name = %s;\n"
839 " id = %u;\n"
b9074a1b 840 " stream_id = %u;\n",
c099397a
MD
841 event->desc->name,
842 event->id,
843 event->chan->id);
844 if (ret)
845 goto end;
846
b9074a1b
MD
847 if (event->ctx) {
848 ret = lttng_metadata_printf(session,
849 " context := struct {\n");
850 if (ret)
851 goto end;
852 }
a90917c3 853 ret = _lttng_context_metadata_statedump(session, event->ctx);
8070f5c0
MD
854 if (ret)
855 goto end;
b9074a1b
MD
856 if (event->ctx) {
857 ret = lttng_metadata_printf(session,
858 " };\n");
859 if (ret)
860 goto end;
861 }
8070f5c0
MD
862
863 ret = lttng_metadata_printf(session,
8070f5c0
MD
864 " fields := struct {\n"
865 );
866 if (ret)
867 goto end;
868
a90917c3 869 ret = _lttng_fields_metadata_statedump(session, event);
c099397a
MD
870 if (ret)
871 goto end;
872
873 /*
874 * LTTng space reservation can only reserve multiples of the
875 * byte size.
876 */
877 ret = lttng_metadata_printf(session,
9115fbdc
MD
878 " };\n"
879 "};\n\n");
c099397a
MD
880 if (ret)
881 goto end;
882
c099397a
MD
883 event->metadata_dumped = 1;
884end:
885 return ret;
886
887}
888
d83004aa
JD
889/*
890 * Must be called with sessions_mutex held.
891 */
c099397a 892static
a90917c3
MD
893int _lttng_channel_metadata_statedump(struct lttng_session *session,
894 struct lttng_channel *chan)
c099397a
MD
895{
896 int ret = 0;
897
898 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
899 return 0;
d83004aa
JD
900
901 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
902 return 0;
903
904 WARN_ON_ONCE(!chan->header_type);
905 ret = lttng_metadata_printf(session,
906 "stream {\n"
907 " id = %u;\n"
9115fbdc 908 " event.header := %s;\n"
b9074a1b 909 " packet.context := struct packet_context;\n",
c099397a
MD
910 chan->id,
911 chan->header_type == 1 ? "struct event_header_compact" :
912 "struct event_header_large");
913 if (ret)
914 goto end;
915
b9074a1b
MD
916 if (chan->ctx) {
917 ret = lttng_metadata_printf(session,
918 " event.context := struct {\n");
919 if (ret)
920 goto end;
921 }
a90917c3 922 ret = _lttng_context_metadata_statedump(session, chan->ctx);
8070f5c0
MD
923 if (ret)
924 goto end;
b9074a1b
MD
925 if (chan->ctx) {
926 ret = lttng_metadata_printf(session,
927 " };\n");
928 if (ret)
929 goto end;
930 }
8070f5c0
MD
931
932 ret = lttng_metadata_printf(session,
b9074a1b 933 "};\n\n");
8070f5c0 934
c099397a
MD
935 chan->metadata_dumped = 1;
936end:
937 return ret;
938}
939
d83004aa
JD
940/*
941 * Must be called with sessions_mutex held.
942 */
9115fbdc 943static
a90917c3 944int _lttng_stream_packet_context_declare(struct lttng_session *session)
9115fbdc
MD
945{
946 return lttng_metadata_printf(session,
947 "struct packet_context {\n"
a3ccff4f
MD
948 " uint64_clock_monotonic_t timestamp_begin;\n"
949 " uint64_clock_monotonic_t timestamp_end;\n"
576ca06a
MD
950 " uint64_t content_size;\n"
951 " uint64_t packet_size;\n"
a9afe705 952 " unsigned long events_discarded;\n"
9115fbdc 953 " uint32_t cpu_id;\n"
c6dfdf6f 954 "};\n\n"
9115fbdc
MD
955 );
956}
957
958/*
959 * Compact header:
960 * id: range: 0 - 30.
961 * id 31 is reserved to indicate an extended header.
962 *
963 * Large header:
964 * id: range: 0 - 65534.
965 * id 65535 is reserved to indicate an extended header.
d83004aa
JD
966 *
967 * Must be called with sessions_mutex held.
9115fbdc
MD
968 */
969static
a90917c3 970int _lttng_event_header_declare(struct lttng_session *session)
9115fbdc
MD
971{
972 return lttng_metadata_printf(session,
973 "struct event_header_compact {\n"
974 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
975 " variant <id> {\n"
976 " struct {\n"
a3ccff4f 977 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
978 " } compact;\n"
979 " struct {\n"
980 " uint32_t id;\n"
a3ccff4f 981 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
982 " } extended;\n"
983 " } v;\n"
984 "} align(%u);\n"
985 "\n"
986 "struct event_header_large {\n"
987 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
988 " variant <id> {\n"
989 " struct {\n"
a3ccff4f 990 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
991 " } compact;\n"
992 " struct {\n"
993 " uint32_t id;\n"
a3ccff4f 994 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
995 " } extended;\n"
996 " } v;\n"
997 "} align(%u);\n\n",
a90917c3
MD
998 lttng_alignof(uint32_t) * CHAR_BIT,
999 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
1000 );
1001}
1002
a3ccff4f
MD
1003 /*
1004 * Approximation of NTP time of day to clock monotonic correlation,
1005 * taken at start of trace.
1006 * Yes, this is only an approximation. Yes, we can (and will) do better
1007 * in future versions.
1008 */
1009static
1010uint64_t measure_clock_offset(void)
1011{
1012 uint64_t offset, monotonic[2], realtime;
1013 struct timespec rts = { 0, 0 };
1014 unsigned long flags;
1015
1016 /* Disable interrupts to increase correlation precision. */
1017 local_irq_save(flags);
1018 monotonic[0] = trace_clock_read64();
1019 getnstimeofday(&rts);
1020 monotonic[1] = trace_clock_read64();
1021 local_irq_restore(flags);
1022
1023 offset = (monotonic[0] + monotonic[1]) >> 1;
57be518d 1024 realtime = (uint64_t) rts.tv_sec * NSEC_PER_SEC;
a3ccff4f
MD
1025 realtime += rts.tv_nsec;
1026 offset = realtime - offset;
1027 return offset;
1028}
1029
c099397a
MD
1030/*
1031 * Output metadata into this session's metadata buffers.
d83004aa 1032 * Must be called with sessions_mutex held.
c099397a
MD
1033 */
1034static
a90917c3 1035int _lttng_session_metadata_statedump(struct lttng_session *session)
c099397a 1036{
30bdb6e4 1037 unsigned char *uuid_c = session->uuid.b;
a82c63f1 1038 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
a90917c3
MD
1039 struct lttng_channel *chan;
1040 struct lttng_event *event;
c099397a
MD
1041 int ret = 0;
1042
1043 if (!ACCESS_ONCE(session->active))
1044 return 0;
1045 if (session->metadata_dumped)
1046 goto skip_session;
c099397a 1047
d793d5e1 1048 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
1049 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
1050 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
1051 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
1052 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
1053 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
1054
1055 ret = lttng_metadata_printf(session,
9115fbdc
MD
1056 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
1057 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
1058 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
1059 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
a9afe705 1060 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
9115fbdc
MD
1061 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
1062 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
1063 "\n"
1064 "trace {\n"
1065 " major = %u;\n"
1066 " minor = %u;\n"
30bdb6e4 1067 " uuid = \"%s\";\n"
d793d5e1
MD
1068 " byte_order = %s;\n"
1069 " packet.header := struct {\n"
1070 " uint32_t magic;\n"
1ec3f75a 1071 " uint8_t uuid[16];\n"
d793d5e1 1072 " uint32_t stream_id;\n"
0eb25f58 1073 " };\n"
d793d5e1 1074 "};\n\n",
a90917c3
MD
1075 lttng_alignof(uint8_t) * CHAR_BIT,
1076 lttng_alignof(uint16_t) * CHAR_BIT,
1077 lttng_alignof(uint32_t) * CHAR_BIT,
1078 lttng_alignof(uint64_t) * CHAR_BIT,
a9afe705
MD
1079 sizeof(unsigned long) * CHAR_BIT,
1080 lttng_alignof(unsigned long) * CHAR_BIT,
c6c9e10f
MD
1081 CTF_SPEC_MAJOR,
1082 CTF_SPEC_MINOR,
d793d5e1
MD
1083 uuid_s,
1084#ifdef __BIG_ENDIAN
1085 "be"
1086#else
1087 "le"
1088#endif
1089 );
1090 if (ret)
1091 goto end;
1092
99dc9597
MD
1093 ret = lttng_metadata_printf(session,
1094 "env {\n"
a6058143 1095 " hostname = \"%s\";\n"
c6c9e10f 1096 " domain = \"kernel\";\n"
99dc9597 1097 " sysname = \"%s\";\n"
c6c9e10f
MD
1098 " kernel_release = \"%s\";\n"
1099 " kernel_version = \"%s\";\n"
1100 " tracer_name = \"lttng-modules\";\n"
1101 " tracer_major = %d;\n"
1102 " tracer_minor = %d;\n"
1103 " tracer_patchlevel = %d;\n"
99dc9597 1104 "};\n\n",
3d0d43db 1105 current->nsproxy->uts_ns->name.nodename,
99dc9597
MD
1106 utsname()->sysname,
1107 utsname()->release,
c6c9e10f
MD
1108 utsname()->version,
1109 LTTNG_MODULES_MAJOR_VERSION,
1110 LTTNG_MODULES_MINOR_VERSION,
1111 LTTNG_MODULES_PATCHLEVEL_VERSION
99dc9597
MD
1112 );
1113 if (ret)
1114 goto end;
1115
a3ccff4f
MD
1116 ret = lttng_metadata_printf(session,
1117 "clock {\n"
a82c63f1
MD
1118 " name = %s;\n",
1119 "monotonic"
1120 );
1121 if (ret)
1122 goto end;
1123
1124 if (!trace_clock_uuid(clock_uuid_s)) {
1125 ret = lttng_metadata_printf(session,
7c27cb17 1126 " uuid = \"%s\";\n",
a82c63f1
MD
1127 clock_uuid_s
1128 );
1129 if (ret)
1130 goto end;
1131 }
1132
1133 ret = lttng_metadata_printf(session,
a3ccff4f
MD
1134 " description = \"Monotonic Clock\";\n"
1135 " freq = %llu; /* Frequency, in Hz */\n"
1136 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
1137 " offset = %llu;\n"
1138 "};\n\n",
a3ccff4f
MD
1139 (unsigned long long) trace_clock_freq(),
1140 (unsigned long long) measure_clock_offset()
1141 );
1142 if (ret)
1143 goto end;
1144
1145 ret = lttng_metadata_printf(session,
1146 "typealias integer {\n"
1147 " size = 27; align = 1; signed = false;\n"
1148 " map = clock.monotonic.value;\n"
1149 "} := uint27_clock_monotonic_t;\n"
1150 "\n"
1151 "typealias integer {\n"
1152 " size = 32; align = %u; signed = false;\n"
1153 " map = clock.monotonic.value;\n"
1154 "} := uint32_clock_monotonic_t;\n"
1155 "\n"
1156 "typealias integer {\n"
1157 " size = 64; align = %u; signed = false;\n"
1158 " map = clock.monotonic.value;\n"
1159 "} := uint64_clock_monotonic_t;\n\n",
1160 lttng_alignof(uint32_t) * CHAR_BIT,
1161 lttng_alignof(uint64_t) * CHAR_BIT
1162 );
1163 if (ret)
1164 goto end;
1165
a90917c3 1166 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
1167 if (ret)
1168 goto end;
1169
a90917c3 1170 ret = _lttng_event_header_declare(session);
9115fbdc
MD
1171 if (ret)
1172 goto end;
1173
c099397a
MD
1174skip_session:
1175 list_for_each_entry(chan, &session->chan, list) {
a90917c3 1176 ret = _lttng_channel_metadata_statedump(session, chan);
c099397a
MD
1177 if (ret)
1178 goto end;
1179 }
1180
1181 list_for_each_entry(event, &session->events, list) {
a90917c3 1182 ret = _lttng_event_metadata_statedump(session, event->chan, event);
c099397a
MD
1183 if (ret)
1184 goto end;
1185 }
1186 session->metadata_dumped = 1;
1187end:
1188 return ret;
1189}
1190
c0e31d2e 1191/**
a90917c3 1192 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
1193 * @transport: transport structure
1194 *
1195 * Registers a transport which can be used as output to extract the data out of
1196 * LTTng. The module calling this registration function must ensure that no
1197 * trap-inducing code will be executed by the transport functions. E.g.
1198 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1199 * is made visible to the transport function. This registration acts as a
1200 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1201 * after its registration must it synchronize the TLBs.
1202 */
a90917c3 1203void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
1204{
1205 /*
1206 * Make sure no page fault can be triggered by the module about to be
1207 * registered. We deal with this here so we don't have to call
1208 * vmalloc_sync_all() in each module's init.
1209 */
6d2a620c 1210 wrapper_vmalloc_sync_all();
c0e31d2e
MD
1211
1212 mutex_lock(&sessions_mutex);
a90917c3 1213 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
1214 mutex_unlock(&sessions_mutex);
1215}
a90917c3 1216EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
1217
1218/**
a90917c3 1219 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
1220 * @transport: transport structure
1221 */
a90917c3 1222void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
1223{
1224 mutex_lock(&sessions_mutex);
1225 list_del(&transport->node);
1226 mutex_unlock(&sessions_mutex);
1227}
a90917c3 1228EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 1229
a90917c3 1230static int __init lttng_events_init(void)
4e3c1b9b 1231{
1c25284c
MD
1232 int ret;
1233
a90917c3 1234 event_cache = KMEM_CACHE(lttng_event, 0);
11b5a3c2 1235 if (!event_cache)
4e3c1b9b 1236 return -ENOMEM;
80996790 1237 ret = lttng_abi_init();
02119ee5
MD
1238 if (ret)
1239 goto error_abi;
4e3c1b9b 1240 return 0;
02119ee5 1241error_abi:
1c25284c
MD
1242 kmem_cache_destroy(event_cache);
1243 return ret;
4e3c1b9b
MD
1244}
1245
a90917c3 1246module_init(lttng_events_init);
11b5a3c2 1247
a90917c3 1248static void __exit lttng_events_exit(void)
4e3c1b9b 1249{
a90917c3 1250 struct lttng_session *session, *tmpsession;
92e94819 1251
80996790 1252 lttng_abi_exit();
92e94819 1253 list_for_each_entry_safe(session, tmpsession, &sessions, list)
a90917c3 1254 lttng_session_destroy(session);
11b5a3c2 1255 kmem_cache_destroy(event_cache);
4e3c1b9b 1256}
92e94819 1257
a90917c3 1258module_exit(lttng_events_exit);
11b5a3c2 1259
92e94819
MD
1260MODULE_LICENSE("GPL and additional rights");
1261MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1262MODULE_DESCRIPTION("LTTng Events");
9a9973ef
MD
1263MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
1264 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
1265 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION));
This page took 0.090744 seconds and 4 git commands to generate.