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