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