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