Allow open /proc/lttng for read & write
[lttng-modules.git] / lttng-events.c
CommitLineData
4e3c1b9b 1/*
a90917c3 2 * lttng-events.c
4e3c1b9b 3 *
a90917c3 4 * Copyright 2010-2011 (c) - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
4e3c1b9b
MD
5 *
6 * Holds LTTng per-session event registry.
17baffe2
MD
7 *
8 * Dual LGPL v2.1/GPL v2 license.
4e3c1b9b
MD
9 */
10
11#include <linux/module.h>
c0e31d2e
MD
12#include <linux/list.h>
13#include <linux/mutex.h>
14#include <linux/sched.h>
11b5a3c2 15#include <linux/slab.h>
c099397a 16#include <linux/jiffies.h>
a864fb02 17#include "wrapper/uuid.h"
b13f3ebe 18#include "wrapper/vmalloc.h" /* for wrapper_vmalloc_sync_all() */
a82c63f1 19#include "wrapper/random.h"
a90917c3
MD
20#include "lttng-events.h"
21#include "lttng-tracer.h"
4e3c1b9b
MD
22
23static LIST_HEAD(sessions);
a90917c3 24static LIST_HEAD(lttng_transport_list);
4e3c1b9b
MD
25static DEFINE_MUTEX(sessions_mutex);
26static struct kmem_cache *event_cache;
27
a90917c3
MD
28static void _lttng_event_destroy(struct lttng_event *event);
29static void _lttng_channel_destroy(struct lttng_channel *chan);
30static int _lttng_event_unregister(struct lttng_event *event);
c099397a 31static
a90917c3
MD
32int _lttng_event_metadata_statedump(struct lttng_session *session,
33 struct lttng_channel *chan,
34 struct lttng_event *event);
c099397a 35static
a90917c3 36int _lttng_session_metadata_statedump(struct lttng_session *session);
c099397a 37
c099397a 38void synchronize_trace(void)
abcca994
MD
39{
40 synchronize_sched();
41#ifdef CONFIG_PREEMPT_RT
42 synchronize_rcu();
43#endif
44}
45
a90917c3 46struct lttng_session *lttng_session_create(void)
4e3c1b9b 47{
a90917c3 48 struct lttng_session *session;
4e3c1b9b
MD
49
50 mutex_lock(&sessions_mutex);
a90917c3 51 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
4e3c1b9b
MD
52 if (!session)
53 return NULL;
4e3c1b9b 54 INIT_LIST_HEAD(&session->chan);
f3d01b96 55 INIT_LIST_HEAD(&session->events);
d793d5e1 56 uuid_le_gen(&session->uuid);
4e3c1b9b
MD
57 list_add(&session->list, &sessions);
58 mutex_unlock(&sessions_mutex);
59 return session;
4e3c1b9b
MD
60}
61
a90917c3 62void lttng_session_destroy(struct lttng_session *session)
4e3c1b9b 63{
a90917c3
MD
64 struct lttng_channel *chan, *tmpchan;
65 struct lttng_event *event, *tmpevent;
dda6a249 66 int ret;
4e3c1b9b
MD
67
68 mutex_lock(&sessions_mutex);
52fc2e1f 69 ACCESS_ONCE(session->active) = 0;
1ec65de1
MD
70 list_for_each_entry(chan, &session->chan, list) {
71 ret = lttng_syscalls_unregister(chan);
72 WARN_ON(ret);
73 }
dda6a249 74 list_for_each_entry(event, &session->events, list) {
a90917c3 75 ret = _lttng_event_unregister(event);
dda6a249
MD
76 WARN_ON(ret);
77 }
abcca994 78 synchronize_trace(); /* Wait for in-flight events to complete */
4e3c1b9b 79 list_for_each_entry_safe(event, tmpevent, &session->events, list)
a90917c3 80 _lttng_event_destroy(event);
4e3c1b9b 81 list_for_each_entry_safe(chan, tmpchan, &session->chan, list)
a90917c3 82 _lttng_channel_destroy(chan);
4e3c1b9b
MD
83 list_del(&session->list);
84 mutex_unlock(&sessions_mutex);
85 kfree(session);
86}
87
a90917c3 88int lttng_session_enable(struct lttng_session *session)
c0e31d2e
MD
89{
90 int ret = 0;
a90917c3 91 struct lttng_channel *chan;
c0e31d2e
MD
92
93 mutex_lock(&sessions_mutex);
94 if (session->active) {
95 ret = -EBUSY;
96 goto end;
97 }
c099397a
MD
98
99 /*
100 * Snapshot the number of events per channel to know the type of header
101 * we need to use.
102 */
103 list_for_each_entry(chan, &session->chan, list) {
104 if (chan->header_type)
105 continue; /* don't change it if session stop/restart */
106 if (chan->free_event_id < 31)
107 chan->header_type = 1; /* compact */
108 else
109 chan->header_type = 2; /* large */
110 }
111
52fc2e1f 112 ACCESS_ONCE(session->active) = 1;
8070f5c0 113 ACCESS_ONCE(session->been_active) = 1;
a90917c3 114 ret = _lttng_session_metadata_statedump(session);
360f38ea 115 if (ret)
c099397a 116 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
117end:
118 mutex_unlock(&sessions_mutex);
11b5a3c2 119 return ret;
c0e31d2e
MD
120}
121
a90917c3 122int lttng_session_disable(struct lttng_session *session)
c0e31d2e
MD
123{
124 int ret = 0;
125
126 mutex_lock(&sessions_mutex);
127 if (!session->active) {
128 ret = -EBUSY;
129 goto end;
130 }
52fc2e1f 131 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
132end:
133 mutex_unlock(&sessions_mutex);
11b5a3c2 134 return ret;
c0e31d2e
MD
135}
136
a90917c3 137int lttng_channel_enable(struct lttng_channel *channel)
e64957da
MD
138{
139 int old;
140
c6485006
MD
141 if (channel == channel->session->metadata)
142 return -EPERM;
e64957da
MD
143 old = xchg(&channel->enabled, 1);
144 if (old)
145 return -EEXIST;
146 return 0;
147}
148
a90917c3 149int lttng_channel_disable(struct lttng_channel *channel)
e64957da
MD
150{
151 int old;
152
c6485006
MD
153 if (channel == channel->session->metadata)
154 return -EPERM;
e64957da
MD
155 old = xchg(&channel->enabled, 0);
156 if (!old)
157 return -EEXIST;
158 return 0;
159}
160
a90917c3 161int lttng_event_enable(struct lttng_event *event)
e64957da
MD
162{
163 int old;
164
c6485006
MD
165 if (event->chan == event->chan->session->metadata)
166 return -EPERM;
e64957da
MD
167 old = xchg(&event->enabled, 1);
168 if (old)
169 return -EEXIST;
170 return 0;
171}
172
a90917c3 173int lttng_event_disable(struct lttng_event *event)
e64957da
MD
174{
175 int old;
176
c6485006
MD
177 if (event->chan == event->chan->session->metadata)
178 return -EPERM;
e64957da
MD
179 old = xchg(&event->enabled, 0);
180 if (!old)
181 return -EEXIST;
182 return 0;
183}
184
a90917c3 185static struct lttng_transport *lttng_transport_find(const char *name)
f3d01b96 186{
a90917c3 187 struct lttng_transport *transport;
f3d01b96 188
a90917c3 189 list_for_each_entry(transport, &lttng_transport_list, node) {
f3d01b96
MD
190 if (!strcmp(transport->name, name))
191 return transport;
192 }
193 return NULL;
194}
195
a90917c3 196struct lttng_channel *lttng_channel_create(struct lttng_session *session,
5dbbdb43
MD
197 const char *transport_name,
198 void *buf_addr,
4e3c1b9b
MD
199 size_t subbuf_size, size_t num_subbuf,
200 unsigned int switch_timer_interval,
201 unsigned int read_timer_interval)
202{
a90917c3
MD
203 struct lttng_channel *chan;
204 struct lttng_transport *transport = NULL;
4e3c1b9b
MD
205
206 mutex_lock(&sessions_mutex);
8070f5c0 207 if (session->been_active)
e5382b6d 208 goto active; /* Refuse to add channel to active session */
a90917c3 209 transport = lttng_transport_find(transport_name);
f3d01b96
MD
210 if (!transport) {
211 printk(KERN_WARNING "LTTng transport %s not found\n",
212 transport_name);
c0e31d2e 213 goto notransport;
f3d01b96 214 }
a33c9927
MD
215 if (!try_module_get(transport->owner)) {
216 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
217 goto notransport;
218 }
a90917c3 219 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
4e3c1b9b 220 if (!chan)
c0e31d2e 221 goto nomem;
4e3c1b9b 222 chan->session = session;
05d32c64 223 chan->id = session->free_chan_id++;
125b4df4
MD
224 /*
225 * Note: the channel creation op already writes into the packet
226 * headers. Therefore the "chan" information used as input
227 * should be already accessible.
228 */
9115fbdc 229 chan->chan = transport->ops.channel_create("[lttng]", chan, buf_addr,
11b5a3c2
MD
230 subbuf_size, num_subbuf, switch_timer_interval,
231 read_timer_interval);
f3d01b96
MD
232 if (!chan->chan)
233 goto create_error;
e64957da 234 chan->enabled = 1;
11b5a3c2 235 chan->ops = &transport->ops;
a33c9927 236 chan->transport = transport;
4e3c1b9b
MD
237 list_add(&chan->list, &session->chan);
238 mutex_unlock(&sessions_mutex);
239 return chan;
240
f3d01b96
MD
241create_error:
242 kfree(chan);
c0e31d2e 243nomem:
a33c9927
MD
244 if (transport)
245 module_put(transport->owner);
c0e31d2e 246notransport:
e5382b6d 247active:
4e3c1b9b
MD
248 mutex_unlock(&sessions_mutex);
249 return NULL;
250}
251
252/*
253 * Only used internally at session destruction.
254 */
aa7c23a9 255static
a90917c3 256void _lttng_channel_destroy(struct lttng_channel *chan)
4e3c1b9b 257{
11b5a3c2 258 chan->ops->channel_destroy(chan->chan);
a33c9927 259 module_put(chan->transport->owner);
4e3c1b9b 260 list_del(&chan->list);
8070f5c0 261 lttng_destroy_context(chan->ctx);
4e3c1b9b
MD
262 kfree(chan);
263}
264
e5382b6d
MD
265/*
266 * Supports event creation while tracing session is active.
267 */
a90917c3 268struct lttng_event *lttng_event_create(struct lttng_channel *chan,
d6d808f3 269 struct lttng_kernel_event *event_param,
259b6cb3
MD
270 void *filter,
271 const struct lttng_event_desc *internal_desc)
4e3c1b9b 272{
a90917c3 273 struct lttng_event *event;
3d084699 274 int ret;
4e3c1b9b
MD
275
276 mutex_lock(&sessions_mutex);
e5382b6d
MD
277 if (chan->free_event_id == -1UL)
278 goto full;
4e3c1b9b 279 /*
dda6a249
MD
280 * This is O(n^2) (for each event, the loop is called at event
281 * creation). Might require a hash if we have lots of events.
4e3c1b9b
MD
282 */
283 list_for_each_entry(event, &chan->session->events, list)
30bdb6e4 284 if (!strcmp(event->desc->name, event_param->name))
4e3c1b9b 285 goto exist;
11b5a3c2 286 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
4e3c1b9b
MD
287 if (!event)
288 goto cache_error;
4e3c1b9b
MD
289 event->chan = chan;
290 event->filter = filter;
e5382b6d 291 event->id = chan->free_event_id++;
e64957da 292 event->enabled = 1;
d6d808f3 293 event->instrumentation = event_param->instrumentation;
a90917c3 294 /* Populate lttng_event structure before tracepoint registration. */
e5382b6d 295 smp_wmb();
d6d808f3 296 switch (event_param->instrumentation) {
ab2277d6 297 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 298 event->desc = lttng_event_get(event_param->name);
d3dbe23c
MD
299 if (!event->desc)
300 goto register_error;
30bdb6e4 301 ret = tracepoint_probe_register(event_param->name,
d3dbe23c
MD
302 event->desc->probe_callback,
303 event);
11b5a3c2
MD
304 if (ret)
305 goto register_error;
baf20995 306 break;
ab2277d6 307 case LTTNG_KERNEL_KPROBE:
30bdb6e4 308 ret = lttng_kprobes_register(event_param->name,
f17701fb
MD
309 event_param->u.kprobe.symbol_name,
310 event_param->u.kprobe.offset,
311 event_param->u.kprobe.addr,
312 event);
313 if (ret)
d6d808f3 314 goto register_error;
edeb3137
MD
315 ret = try_module_get(event->desc->owner);
316 WARN_ON_ONCE(!ret);
d6d808f3 317 break;
7371f44c
MD
318 case LTTNG_KERNEL_KRETPROBE:
319 {
a90917c3 320 struct lttng_event *event_return;
7371f44c
MD
321
322 /* kretprobe defines 2 events */
323 event_return =
324 kmem_cache_zalloc(event_cache, GFP_KERNEL);
325 if (!event_return)
326 goto register_error;
327 event_return->chan = chan;
328 event_return->filter = filter;
329 event_return->id = chan->free_event_id++;
330 event_return->enabled = 1;
331 event_return->instrumentation = event_param->instrumentation;
332 /*
a90917c3 333 * Populate lttng_event structure before kretprobe registration.
7371f44c
MD
334 */
335 smp_wmb();
336 ret = lttng_kretprobes_register(event_param->name,
337 event_param->u.kretprobe.symbol_name,
338 event_param->u.kretprobe.offset,
339 event_param->u.kretprobe.addr,
340 event, event_return);
341 if (ret) {
342 kmem_cache_free(event_cache, event_return);
343 goto register_error;
344 }
345 /* Take 2 refs on the module: one per event. */
346 ret = try_module_get(event->desc->owner);
347 WARN_ON_ONCE(!ret);
348 ret = try_module_get(event->desc->owner);
349 WARN_ON_ONCE(!ret);
a90917c3 350 ret = _lttng_event_metadata_statedump(chan->session, chan,
7371f44c
MD
351 event_return);
352 if (ret) {
353 kmem_cache_free(event_cache, event_return);
354 module_put(event->desc->owner);
355 module_put(event->desc->owner);
356 goto statedump_error;
357 }
358 list_add(&event_return->list, &chan->session->events);
359 break;
360 }
ab2277d6 361 case LTTNG_KERNEL_FUNCTION:
30bdb6e4 362 ret = lttng_ftrace_register(event_param->name,
e0a7a7c4
MD
363 event_param->u.ftrace.symbol_name,
364 event);
365 if (ret)
366 goto register_error;
edeb3137
MD
367 ret = try_module_get(event->desc->owner);
368 WARN_ON_ONCE(!ret);
e0a7a7c4 369 break;
1ec65de1 370 case LTTNG_KERNEL_NOOP:
259b6cb3
MD
371 event->desc = internal_desc;
372 if (!event->desc)
373 goto register_error;
1ec65de1 374 break;
baf20995
MD
375 default:
376 WARN_ON_ONCE(1);
377 }
a90917c3 378 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
c099397a
MD
379 if (ret)
380 goto statedump_error;
dda6a249 381 list_add(&event->list, &chan->session->events);
11b5a3c2 382 mutex_unlock(&sessions_mutex);
4e3c1b9b
MD
383 return event;
384
c099397a 385statedump_error:
259b6cb3 386 /* If a statedump error occurs, events will not be readable. */
11b5a3c2 387register_error:
11b5a3c2 388 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
389cache_error:
390exist:
e5382b6d 391full:
4e3c1b9b
MD
392 mutex_unlock(&sessions_mutex);
393 return NULL;
394}
395
396/*
397 * Only used internally at session destruction.
398 */
a90917c3 399int _lttng_event_unregister(struct lttng_event *event)
4e3c1b9b 400{
11b5a3c2
MD
401 int ret = -EINVAL;
402
38d024ae 403 switch (event->instrumentation) {
ab2277d6 404 case LTTNG_KERNEL_TRACEPOINT:
85a9ca7f
MD
405 ret = tracepoint_probe_unregister(event->desc->name,
406 event->desc->probe_callback,
11b5a3c2
MD
407 event);
408 if (ret)
409 return ret;
baf20995 410 break;
ab2277d6 411 case LTTNG_KERNEL_KPROBE:
f17701fb 412 lttng_kprobes_unregister(event);
d6d808f3
MD
413 ret = 0;
414 break;
7371f44c
MD
415 case LTTNG_KERNEL_KRETPROBE:
416 lttng_kretprobes_unregister(event);
417 ret = 0;
418 break;
ab2277d6 419 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
420 lttng_ftrace_unregister(event);
421 ret = 0;
422 break;
259b6cb3
MD
423 case LTTNG_KERNEL_NOOP:
424 ret = 0;
425 break;
baf20995
MD
426 default:
427 WARN_ON_ONCE(1);
428 }
dda6a249
MD
429 return ret;
430}
431
432/*
433 * Only used internally at session destruction.
434 */
be066e6c 435static
a90917c3 436void _lttng_event_destroy(struct lttng_event *event)
dda6a249 437{
edeb3137
MD
438 switch (event->instrumentation) {
439 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 440 lttng_event_put(event->desc);
edeb3137
MD
441 break;
442 case LTTNG_KERNEL_KPROBE:
443 module_put(event->desc->owner);
444 lttng_kprobes_destroy_private(event);
445 break;
7371f44c
MD
446 case LTTNG_KERNEL_KRETPROBE:
447 module_put(event->desc->owner);
448 lttng_kretprobes_destroy_private(event);
449 break;
edeb3137
MD
450 case LTTNG_KERNEL_FUNCTION:
451 module_put(event->desc->owner);
452 lttng_ftrace_destroy_private(event);
453 break;
259b6cb3
MD
454 case LTTNG_KERNEL_NOOP:
455 break;
edeb3137
MD
456 default:
457 WARN_ON_ONCE(1);
458 }
dda6a249 459 list_del(&event->list);
8070f5c0 460 lttng_destroy_context(event->ctx);
11b5a3c2 461 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
462}
463
1ec3f75a
MD
464/*
465 * We have exclusive access to our metadata buffer (protected by the
466 * sessions_mutex), so we can do racy operations such as looking for
467 * remaining space left in packet and write, since mutual exclusion
468 * protects us from concurrent writes.
469 */
a90917c3 470int lttng_metadata_printf(struct lttng_session *session,
c099397a
MD
471 const char *fmt, ...)
472{
473 struct lib_ring_buffer_ctx ctx;
a90917c3 474 struct lttng_channel *chan = session->metadata;
c099397a
MD
475 char *str;
476 int ret = 0, waitret;
1ec3f75a 477 size_t len, reserve_len, pos;
c099397a
MD
478 va_list ap;
479
480 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
481
482 va_start(ap, fmt);
483 str = kvasprintf(GFP_KERNEL, fmt, ap);
484 va_end(ap);
485 if (!str)
486 return -ENOMEM;
487
1ec3f75a
MD
488 len = strlen(str);
489 pos = 0;
490
491 for (pos = 0; pos < len; pos += reserve_len) {
492 reserve_len = min_t(size_t,
493 chan->ops->packet_avail_size(chan->chan),
494 len - pos);
495 lib_ring_buffer_ctx_init(&ctx, chan->chan, NULL, reserve_len,
496 sizeof(char), -1);
497 /*
498 * We don't care about metadata buffer's records lost
499 * count, because we always retry here. Report error if
500 * we need to bail out after timeout or being
501 * interrupted.
502 */
71c1d843 503 waitret = wait_event_interruptible_timeout(*chan->ops->get_writer_buf_wait_queue(chan->chan, -1),
1ec3f75a 504 ({
4e1f08f4 505 ret = chan->ops->event_reserve(&ctx, 0);
1ec3f75a
MD
506 ret != -ENOBUFS || !ret;
507 }),
508 msecs_to_jiffies(LTTNG_METADATA_TIMEOUT_MSEC));
509 if (!waitret || waitret == -ERESTARTSYS || ret) {
510 printk(KERN_WARNING "LTTng: Failure to write metadata to buffers (%s)\n",
511 waitret == -ERESTARTSYS ? "interrupted" :
512 (ret == -ENOBUFS ? "timeout" : "I/O error"));
513 if (waitret == -ERESTARTSYS)
514 ret = waitret;
515 goto end;
516 }
f5cf8eff 517 chan->ops->event_write(&ctx, &str[pos], reserve_len);
1ec3f75a 518 chan->ops->event_commit(&ctx);
c099397a 519 }
c099397a
MD
520end:
521 kfree(str);
522 return ret;
523}
524
525static
a90917c3 526int _lttng_field_statedump(struct lttng_session *session,
8070f5c0 527 const struct lttng_event_field *field)
c099397a 528{
c099397a 529 int ret = 0;
c099397a 530
8070f5c0
MD
531 switch (field->type.atype) {
532 case atype_integer:
533 ret = lttng_metadata_printf(session,
244465ab 534 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8070f5c0
MD
535 field->type.u.basic.integer.size,
536 field->type.u.basic.integer.alignment,
537 field->type.u.basic.integer.signedness,
538 (field->type.u.basic.integer.encoding == lttng_encode_none)
539 ? "none"
540 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
541 ? "UTF8"
542 : "ASCII",
543 field->type.u.basic.integer.base,
c099397a 544#ifdef __BIG_ENDIAN
8070f5c0 545 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
c099397a 546#else
8070f5c0 547 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
c099397a 548#endif
8070f5c0
MD
549 field->name);
550 break;
551 case atype_enum:
552 ret = lttng_metadata_printf(session,
244465ab 553 " %s _%s;\n",
8070f5c0
MD
554 field->type.u.basic.enumeration.name,
555 field->name);
556 break;
557 case atype_array:
558 {
559 const struct lttng_basic_type *elem_type;
560
561 elem_type = &field->type.u.array.elem_type;
562 ret = lttng_metadata_printf(session,
244465ab 563 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8070f5c0
MD
564 elem_type->u.basic.integer.size,
565 elem_type->u.basic.integer.alignment,
566 elem_type->u.basic.integer.signedness,
567 (elem_type->u.basic.integer.encoding == lttng_encode_none)
568 ? "none"
569 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
570 ? "UTF8"
571 : "ASCII",
572 elem_type->u.basic.integer.base,
8fa75f34 573#ifdef __BIG_ENDIAN
8070f5c0 574 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 575#else
8070f5c0 576 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 577#endif
8070f5c0
MD
578 field->name, field->type.u.array.length);
579 break;
580 }
581 case atype_sequence:
582 {
583 const struct lttng_basic_type *elem_type;
584 const struct lttng_basic_type *length_type;
585
586 elem_type = &field->type.u.sequence.elem_type;
587 length_type = &field->type.u.sequence.length_type;
588 ret = lttng_metadata_printf(session,
589 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
8070f5c0 590 length_type->u.basic.integer.size,
32257272 591 (unsigned int) length_type->u.basic.integer.alignment,
8070f5c0
MD
592 length_type->u.basic.integer.signedness,
593 (length_type->u.basic.integer.encoding == lttng_encode_none)
594 ? "none"
0635cdd4 595 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 596 ? "UTF8"
0635cdd4 597 : "ASCII"),
8070f5c0 598 length_type->u.basic.integer.base,
e0a7a7c4 599#ifdef __BIG_ENDIAN
8070f5c0 600 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
e0a7a7c4 601#else
0635cdd4 602 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
e0a7a7c4 603#endif
27d2368e
MD
604 field->name);
605 if (ret)
606 return ret;
607
608 ret = lttng_metadata_printf(session,
244465ab 609 " integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8070f5c0 610 elem_type->u.basic.integer.size,
32257272 611 (unsigned int) elem_type->u.basic.integer.alignment,
8070f5c0
MD
612 elem_type->u.basic.integer.signedness,
613 (elem_type->u.basic.integer.encoding == lttng_encode_none)
614 ? "none"
0635cdd4 615 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 616 ? "UTF8"
0635cdd4 617 : "ASCII"),
8070f5c0 618 elem_type->u.basic.integer.base,
8fa75f34 619#ifdef __BIG_ENDIAN
8070f5c0 620 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 621#else
8070f5c0 622 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 623#endif
8070f5c0 624 field->name,
27d2368e 625 field->name);
8070f5c0
MD
626 break;
627 }
c099397a 628
8070f5c0
MD
629 case atype_string:
630 /* Default encoding is UTF8 */
631 ret = lttng_metadata_printf(session,
244465ab 632 " string%s _%s;\n",
8070f5c0
MD
633 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
634 " { encoding = ASCII; }" : "",
635 field->name);
636 break;
637 default:
638 WARN_ON_ONCE(1);
639 return -EINVAL;
640 }
641 return ret;
642}
643
644static
a90917c3 645int _lttng_context_metadata_statedump(struct lttng_session *session,
8070f5c0
MD
646 struct lttng_ctx *ctx)
647{
648 int ret = 0;
649 int i;
650
651 if (!ctx)
652 return 0;
653 for (i = 0; i < ctx->nr_fields; i++) {
654 const struct lttng_ctx_field *field = &ctx->fields[i];
655
a90917c3 656 ret = _lttng_field_statedump(session, &field->event_field);
8070f5c0
MD
657 if (ret)
658 return ret;
659 }
660 return ret;
661}
662
663static
a90917c3
MD
664int _lttng_fields_metadata_statedump(struct lttng_session *session,
665 struct lttng_event *event)
8070f5c0
MD
666{
667 const struct lttng_event_desc *desc = event->desc;
668 int ret = 0;
669 int i;
670
671 for (i = 0; i < desc->nr_fields; i++) {
672 const struct lttng_event_field *field = &desc->fields[i];
673
a90917c3 674 ret = _lttng_field_statedump(session, field);
8070f5c0
MD
675 if (ret)
676 return ret;
c099397a
MD
677 }
678 return ret;
679}
680
681static
a90917c3
MD
682int _lttng_event_metadata_statedump(struct lttng_session *session,
683 struct lttng_channel *chan,
684 struct lttng_event *event)
c099397a
MD
685{
686 int ret = 0;
687
688 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
689 return 0;
690 if (chan == session->metadata)
691 return 0;
692
693 ret = lttng_metadata_printf(session,
694 "event {\n"
695 " name = %s;\n"
696 " id = %u;\n"
b9074a1b 697 " stream_id = %u;\n",
c099397a
MD
698 event->desc->name,
699 event->id,
700 event->chan->id);
701 if (ret)
702 goto end;
703
b9074a1b
MD
704 if (event->ctx) {
705 ret = lttng_metadata_printf(session,
706 " context := struct {\n");
707 if (ret)
708 goto end;
709 }
a90917c3 710 ret = _lttng_context_metadata_statedump(session, event->ctx);
8070f5c0
MD
711 if (ret)
712 goto end;
b9074a1b
MD
713 if (event->ctx) {
714 ret = lttng_metadata_printf(session,
715 " };\n");
716 if (ret)
717 goto end;
718 }
8070f5c0
MD
719
720 ret = lttng_metadata_printf(session,
8070f5c0
MD
721 " fields := struct {\n"
722 );
723 if (ret)
724 goto end;
725
a90917c3 726 ret = _lttng_fields_metadata_statedump(session, event);
c099397a
MD
727 if (ret)
728 goto end;
729
730 /*
731 * LTTng space reservation can only reserve multiples of the
732 * byte size.
733 */
734 ret = lttng_metadata_printf(session,
9115fbdc
MD
735 " };\n"
736 "};\n\n");
c099397a
MD
737 if (ret)
738 goto end;
739
c099397a
MD
740 event->metadata_dumped = 1;
741end:
742 return ret;
743
744}
745
746static
a90917c3
MD
747int _lttng_channel_metadata_statedump(struct lttng_session *session,
748 struct lttng_channel *chan)
c099397a
MD
749{
750 int ret = 0;
751
752 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
753 return 0;
754 if (chan == session->metadata)
755 return 0;
756
757 WARN_ON_ONCE(!chan->header_type);
758 ret = lttng_metadata_printf(session,
759 "stream {\n"
760 " id = %u;\n"
9115fbdc 761 " event.header := %s;\n"
b9074a1b 762 " packet.context := struct packet_context;\n",
c099397a
MD
763 chan->id,
764 chan->header_type == 1 ? "struct event_header_compact" :
765 "struct event_header_large");
766 if (ret)
767 goto end;
768
b9074a1b
MD
769 if (chan->ctx) {
770 ret = lttng_metadata_printf(session,
771 " event.context := struct {\n");
772 if (ret)
773 goto end;
774 }
a90917c3 775 ret = _lttng_context_metadata_statedump(session, chan->ctx);
8070f5c0
MD
776 if (ret)
777 goto end;
b9074a1b
MD
778 if (chan->ctx) {
779 ret = lttng_metadata_printf(session,
780 " };\n");
781 if (ret)
782 goto end;
783 }
8070f5c0
MD
784
785 ret = lttng_metadata_printf(session,
b9074a1b 786 "};\n\n");
8070f5c0 787
c099397a
MD
788 chan->metadata_dumped = 1;
789end:
790 return ret;
791}
792
9115fbdc 793static
a90917c3 794int _lttng_stream_packet_context_declare(struct lttng_session *session)
9115fbdc
MD
795{
796 return lttng_metadata_printf(session,
797 "struct packet_context {\n"
a3ccff4f
MD
798 " uint64_clock_monotonic_t timestamp_begin;\n"
799 " uint64_clock_monotonic_t timestamp_end;\n"
9115fbdc
MD
800 " uint32_t events_discarded;\n"
801 " uint32_t content_size;\n"
802 " uint32_t packet_size;\n"
803 " uint32_t cpu_id;\n"
c6dfdf6f 804 "};\n\n"
9115fbdc
MD
805 );
806}
807
808/*
809 * Compact header:
810 * id: range: 0 - 30.
811 * id 31 is reserved to indicate an extended header.
812 *
813 * Large header:
814 * id: range: 0 - 65534.
815 * id 65535 is reserved to indicate an extended header.
816 */
817static
a90917c3 818int _lttng_event_header_declare(struct lttng_session *session)
9115fbdc
MD
819{
820 return lttng_metadata_printf(session,
821 "struct event_header_compact {\n"
822 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
823 " variant <id> {\n"
824 " struct {\n"
a3ccff4f 825 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
826 " } compact;\n"
827 " struct {\n"
828 " uint32_t id;\n"
a3ccff4f 829 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
830 " } extended;\n"
831 " } v;\n"
832 "} align(%u);\n"
833 "\n"
834 "struct event_header_large {\n"
835 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
836 " variant <id> {\n"
837 " struct {\n"
a3ccff4f 838 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
839 " } compact;\n"
840 " struct {\n"
841 " uint32_t id;\n"
a3ccff4f 842 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
843 " } extended;\n"
844 " } v;\n"
845 "} align(%u);\n\n",
a90917c3
MD
846 lttng_alignof(uint32_t) * CHAR_BIT,
847 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
848 );
849}
850
a3ccff4f
MD
851 /*
852 * Approximation of NTP time of day to clock monotonic correlation,
853 * taken at start of trace.
854 * Yes, this is only an approximation. Yes, we can (and will) do better
855 * in future versions.
856 */
857static
858uint64_t measure_clock_offset(void)
859{
860 uint64_t offset, monotonic[2], realtime;
861 struct timespec rts = { 0, 0 };
862 unsigned long flags;
863
864 /* Disable interrupts to increase correlation precision. */
865 local_irq_save(flags);
866 monotonic[0] = trace_clock_read64();
867 getnstimeofday(&rts);
868 monotonic[1] = trace_clock_read64();
869 local_irq_restore(flags);
870
871 offset = (monotonic[0] + monotonic[1]) >> 1;
872 realtime = rts.tv_sec * NSEC_PER_SEC;
873 realtime += rts.tv_nsec;
874 offset = realtime - offset;
875 return offset;
876}
877
c099397a
MD
878/*
879 * Output metadata into this session's metadata buffers.
880 */
881static
a90917c3 882int _lttng_session_metadata_statedump(struct lttng_session *session)
c099397a 883{
30bdb6e4 884 unsigned char *uuid_c = session->uuid.b;
a82c63f1 885 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
a90917c3
MD
886 struct lttng_channel *chan;
887 struct lttng_event *event;
c099397a
MD
888 int ret = 0;
889
890 if (!ACCESS_ONCE(session->active))
891 return 0;
892 if (session->metadata_dumped)
893 goto skip_session;
f40270ad 894 if (!session->metadata) {
cd4bd11f 895 printk(KERN_WARNING "LTTng: attempt to start tracing, but metadata channel is not found. Operation abort.\n");
f40270ad
MD
896 return -EPERM;
897 }
c099397a 898
d793d5e1 899 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
900 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
901 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
902 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
903 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
904 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
905
906 ret = lttng_metadata_printf(session,
9115fbdc
MD
907 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
908 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
909 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
910 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
911 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
912 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
913 "\n"
914 "trace {\n"
915 " major = %u;\n"
916 " minor = %u;\n"
30bdb6e4 917 " uuid = \"%s\";\n"
d793d5e1
MD
918 " byte_order = %s;\n"
919 " packet.header := struct {\n"
920 " uint32_t magic;\n"
1ec3f75a 921 " uint8_t uuid[16];\n"
d793d5e1 922 " uint32_t stream_id;\n"
0eb25f58 923 " };\n"
d793d5e1 924 "};\n\n",
a90917c3
MD
925 lttng_alignof(uint8_t) * CHAR_BIT,
926 lttng_alignof(uint16_t) * CHAR_BIT,
927 lttng_alignof(uint32_t) * CHAR_BIT,
928 lttng_alignof(uint64_t) * CHAR_BIT,
d793d5e1
MD
929 CTF_VERSION_MAJOR,
930 CTF_VERSION_MINOR,
931 uuid_s,
932#ifdef __BIG_ENDIAN
933 "be"
934#else
935 "le"
936#endif
937 );
938 if (ret)
939 goto end;
940
a3ccff4f
MD
941 ret = lttng_metadata_printf(session,
942 "clock {\n"
a82c63f1
MD
943 " name = %s;\n",
944 "monotonic"
945 );
946 if (ret)
947 goto end;
948
949 if (!trace_clock_uuid(clock_uuid_s)) {
950 ret = lttng_metadata_printf(session,
7c27cb17 951 " uuid = \"%s\";\n",
a82c63f1
MD
952 clock_uuid_s
953 );
954 if (ret)
955 goto end;
956 }
957
958 ret = lttng_metadata_printf(session,
a3ccff4f
MD
959 " description = \"Monotonic Clock\";\n"
960 " freq = %llu; /* Frequency, in Hz */\n"
961 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
962 " offset = %llu;\n"
963 "};\n\n",
a3ccff4f
MD
964 (unsigned long long) trace_clock_freq(),
965 (unsigned long long) measure_clock_offset()
966 );
967 if (ret)
968 goto end;
969
970 ret = lttng_metadata_printf(session,
971 "typealias integer {\n"
972 " size = 27; align = 1; signed = false;\n"
973 " map = clock.monotonic.value;\n"
974 "} := uint27_clock_monotonic_t;\n"
975 "\n"
976 "typealias integer {\n"
977 " size = 32; align = %u; signed = false;\n"
978 " map = clock.monotonic.value;\n"
979 "} := uint32_clock_monotonic_t;\n"
980 "\n"
981 "typealias integer {\n"
982 " size = 64; align = %u; signed = false;\n"
983 " map = clock.monotonic.value;\n"
984 "} := uint64_clock_monotonic_t;\n\n",
985 lttng_alignof(uint32_t) * CHAR_BIT,
986 lttng_alignof(uint64_t) * CHAR_BIT
987 );
988 if (ret)
989 goto end;
990
a90917c3 991 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
992 if (ret)
993 goto end;
994
a90917c3 995 ret = _lttng_event_header_declare(session);
9115fbdc
MD
996 if (ret)
997 goto end;
998
c099397a
MD
999skip_session:
1000 list_for_each_entry(chan, &session->chan, list) {
a90917c3 1001 ret = _lttng_channel_metadata_statedump(session, chan);
c099397a
MD
1002 if (ret)
1003 goto end;
1004 }
1005
1006 list_for_each_entry(event, &session->events, list) {
a90917c3 1007 ret = _lttng_event_metadata_statedump(session, event->chan, event);
c099397a
MD
1008 if (ret)
1009 goto end;
1010 }
1011 session->metadata_dumped = 1;
1012end:
1013 return ret;
1014}
1015
c0e31d2e 1016/**
a90917c3 1017 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
1018 * @transport: transport structure
1019 *
1020 * Registers a transport which can be used as output to extract the data out of
1021 * LTTng. The module calling this registration function must ensure that no
1022 * trap-inducing code will be executed by the transport functions. E.g.
1023 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
1024 * is made visible to the transport function. This registration acts as a
1025 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
1026 * after its registration must it synchronize the TLBs.
1027 */
a90917c3 1028void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
1029{
1030 /*
1031 * Make sure no page fault can be triggered by the module about to be
1032 * registered. We deal with this here so we don't have to call
1033 * vmalloc_sync_all() in each module's init.
1034 */
6d2a620c 1035 wrapper_vmalloc_sync_all();
c0e31d2e
MD
1036
1037 mutex_lock(&sessions_mutex);
a90917c3 1038 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
1039 mutex_unlock(&sessions_mutex);
1040}
a90917c3 1041EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
1042
1043/**
a90917c3 1044 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
1045 * @transport: transport structure
1046 */
a90917c3 1047void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
1048{
1049 mutex_lock(&sessions_mutex);
1050 list_del(&transport->node);
1051 mutex_unlock(&sessions_mutex);
1052}
a90917c3 1053EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 1054
a90917c3 1055static int __init lttng_events_init(void)
4e3c1b9b 1056{
1c25284c
MD
1057 int ret;
1058
a90917c3 1059 event_cache = KMEM_CACHE(lttng_event, 0);
11b5a3c2 1060 if (!event_cache)
4e3c1b9b 1061 return -ENOMEM;
80996790 1062 ret = lttng_abi_init();
02119ee5
MD
1063 if (ret)
1064 goto error_abi;
4e3c1b9b 1065 return 0;
02119ee5 1066error_abi:
1c25284c
MD
1067 kmem_cache_destroy(event_cache);
1068 return ret;
4e3c1b9b
MD
1069}
1070
a90917c3 1071module_init(lttng_events_init);
11b5a3c2 1072
a90917c3 1073static void __exit lttng_events_exit(void)
4e3c1b9b 1074{
a90917c3 1075 struct lttng_session *session, *tmpsession;
92e94819 1076
80996790 1077 lttng_abi_exit();
92e94819 1078 list_for_each_entry_safe(session, tmpsession, &sessions, list)
a90917c3 1079 lttng_session_destroy(session);
11b5a3c2 1080 kmem_cache_destroy(event_cache);
4e3c1b9b 1081}
92e94819 1082
a90917c3 1083module_exit(lttng_events_exit);
11b5a3c2 1084
92e94819
MD
1085MODULE_LICENSE("GPL and additional rights");
1086MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
1087MODULE_DESCRIPTION("LTTng Events");
This page took 0.080366 seconds and 4 git commands to generate.