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