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