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