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