lttng-abi.c: cleanup whitespaces
[deliverable/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
389d7070
MD
23/*
24 * This page_alloc.h wrapper needs to be included before gfpflags.h because it
25 * overrides a function with a define.
26 */
27#include "wrapper/page_alloc.h"
28
4e3c1b9b 29#include <linux/module.h>
c0e31d2e
MD
30#include <linux/mutex.h>
31#include <linux/sched.h>
11b5a3c2 32#include <linux/slab.h>
c099397a 33#include <linux/jiffies.h>
99dc9597 34#include <linux/utsname.h>
abc0446a 35#include <linux/err.h>
7e6f9ef6
MD
36#include <linux/seq_file.h>
37#include <linux/file.h>
38#include <linux/anon_inodes.h>
241ae9a8 39#include <wrapper/file.h>
3c997079 40#include <linux/jhash.h>
8c6e7f13 41#include <linux/uaccess.h>
a606b6e8 42#include <linux/vmalloc.h>
3c997079 43
241ae9a8
MD
44#include <wrapper/uuid.h>
45#include <wrapper/vmalloc.h> /* for wrapper_vmalloc_sync_all() */
46#include <wrapper/random.h>
47#include <wrapper/tracepoint.h>
48#include <wrapper/list.h>
49#include <lttng-kernel-version.h>
50#include <lttng-events.h>
51#include <lttng-tracer.h>
52#include <lttng-abi-old.h>
5214fa50 53#include <lttng-endian.h>
241ae9a8 54#include <wrapper/vzalloc.h>
64af2437
MD
55#include <wrapper/ringbuffer/backend.h>
56#include <wrapper/ringbuffer/frontend.h>
4e3c1b9b 57
d83004aa
JD
58#define METADATA_CACHE_DEFAULT_SIZE 4096
59
4e3c1b9b 60static LIST_HEAD(sessions);
a90917c3 61static LIST_HEAD(lttng_transport_list);
d83004aa
JD
62/*
63 * Protect the sessions and metadata caches.
64 */
4e3c1b9b
MD
65static DEFINE_MUTEX(sessions_mutex);
66static struct kmem_cache *event_cache;
67
3c997079
MD
68static void lttng_session_lazy_sync_enablers(struct lttng_session *session);
69static void lttng_session_sync_enablers(struct lttng_session *session);
70static void lttng_enabler_destroy(struct lttng_enabler *enabler);
71
a90917c3
MD
72static void _lttng_event_destroy(struct lttng_event *event);
73static void _lttng_channel_destroy(struct lttng_channel *chan);
74static int _lttng_event_unregister(struct lttng_event *event);
c099397a 75static
a90917c3
MD
76int _lttng_event_metadata_statedump(struct lttng_session *session,
77 struct lttng_channel *chan,
78 struct lttng_event *event);
c099397a 79static
a90917c3 80int _lttng_session_metadata_statedump(struct lttng_session *session);
d83004aa
JD
81static
82void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream);
f513b2bf
MD
83static
84int _lttng_field_statedump(struct lttng_session *session,
85 const struct lttng_event_field *field,
86 size_t nesting);
c099397a 87
c099397a 88void synchronize_trace(void)
abcca994
MD
89{
90 synchronize_sched();
b8590f40
MD
91#if (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0))
92#ifdef CONFIG_PREEMPT_RT_FULL
93 synchronize_rcu();
94#endif
95#else /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
96#ifdef CONFIG_PREEMPT_RT
97 synchronize_rcu();
98#endif
b8590f40 99#endif /* (LINUX_VERSION_CODE >= KERNEL_VERSION(3,4,0)) */
abcca994
MD
100}
101
3c997079
MD
102void lttng_lock_sessions(void)
103{
104 mutex_lock(&sessions_mutex);
105}
106
107void lttng_unlock_sessions(void)
108{
109 mutex_unlock(&sessions_mutex);
110}
111
112/*
113 * Called with sessions lock held.
114 */
115int lttng_session_active(void)
116{
117 struct lttng_session *iter;
118
119 list_for_each_entry(iter, &sessions, list) {
120 if (iter->active)
121 return 1;
122 }
123 return 0;
124}
125
a90917c3 126struct lttng_session *lttng_session_create(void)
4e3c1b9b 127{
a90917c3 128 struct lttng_session *session;
d83004aa 129 struct lttng_metadata_cache *metadata_cache;
3c997079 130 int i;
4e3c1b9b
MD
131
132 mutex_lock(&sessions_mutex);
a90917c3 133 session = kzalloc(sizeof(struct lttng_session), GFP_KERNEL);
4e3c1b9b 134 if (!session)
d83004aa 135 goto err;
4e3c1b9b 136 INIT_LIST_HEAD(&session->chan);
f3d01b96 137 INIT_LIST_HEAD(&session->events);
d793d5e1 138 uuid_le_gen(&session->uuid);
d83004aa
JD
139
140 metadata_cache = kzalloc(sizeof(struct lttng_metadata_cache),
141 GFP_KERNEL);
142 if (!metadata_cache)
143 goto err_free_session;
f6da044f 144 metadata_cache->data = lttng_vzalloc(METADATA_CACHE_DEFAULT_SIZE);
d83004aa
JD
145 if (!metadata_cache->data)
146 goto err_free_cache;
147 metadata_cache->cache_alloc = METADATA_CACHE_DEFAULT_SIZE;
148 kref_init(&metadata_cache->refcount);
92d9f5e6 149 mutex_init(&metadata_cache->lock);
d83004aa
JD
150 session->metadata_cache = metadata_cache;
151 INIT_LIST_HEAD(&metadata_cache->metadata_stream);
a36580d5
MD
152 memcpy(&metadata_cache->uuid, &session->uuid,
153 sizeof(metadata_cache->uuid));
3c997079
MD
154 INIT_LIST_HEAD(&session->enablers_head);
155 for (i = 0; i < LTTNG_EVENT_HT_SIZE; i++)
156 INIT_HLIST_HEAD(&session->events_ht.table[i]);
4e3c1b9b
MD
157 list_add(&session->list, &sessions);
158 mutex_unlock(&sessions_mutex);
159 return session;
d83004aa
JD
160
161err_free_cache:
162 kfree(metadata_cache);
163err_free_session:
164 kfree(session);
165err:
166 mutex_unlock(&sessions_mutex);
167 return NULL;
168}
169
170void metadata_cache_destroy(struct kref *kref)
171{
172 struct lttng_metadata_cache *cache =
173 container_of(kref, struct lttng_metadata_cache, refcount);
a606b6e8 174 vfree(cache->data);
d83004aa 175 kfree(cache);
4e3c1b9b
MD
176}
177
a90917c3 178void lttng_session_destroy(struct lttng_session *session)
4e3c1b9b 179{
a90917c3
MD
180 struct lttng_channel *chan, *tmpchan;
181 struct lttng_event *event, *tmpevent;
d83004aa 182 struct lttng_metadata_stream *metadata_stream;
3c997079 183 struct lttng_enabler *enabler, *tmpenabler;
dda6a249 184 int ret;
4e3c1b9b
MD
185
186 mutex_lock(&sessions_mutex);
52fc2e1f 187 ACCESS_ONCE(session->active) = 0;
1ec65de1
MD
188 list_for_each_entry(chan, &session->chan, list) {
189 ret = lttng_syscalls_unregister(chan);
190 WARN_ON(ret);
191 }
dda6a249 192 list_for_each_entry(event, &session->events, list) {
a90917c3 193 ret = _lttng_event_unregister(event);
dda6a249
MD
194 WARN_ON(ret);
195 }
abcca994 196 synchronize_trace(); /* Wait for in-flight events to complete */
3c997079
MD
197 list_for_each_entry_safe(enabler, tmpenabler,
198 &session->enablers_head, node)
199 lttng_enabler_destroy(enabler);
4e3c1b9b 200 list_for_each_entry_safe(event, tmpevent, &session->events, list)
a90917c3 201 _lttng_event_destroy(event);
d83004aa
JD
202 list_for_each_entry_safe(chan, tmpchan, &session->chan, list) {
203 BUG_ON(chan->channel_type == METADATA_CHANNEL);
a90917c3 204 _lttng_channel_destroy(chan);
d83004aa
JD
205 }
206 list_for_each_entry(metadata_stream, &session->metadata_cache->metadata_stream, list)
207 _lttng_metadata_channel_hangup(metadata_stream);
e0130fab
MD
208 if (session->pid_tracker)
209 lttng_pid_tracker_destroy(session->pid_tracker);
d83004aa 210 kref_put(&session->metadata_cache->refcount, metadata_cache_destroy);
4e3c1b9b
MD
211 list_del(&session->list);
212 mutex_unlock(&sessions_mutex);
213 kfree(session);
214}
215
601252cf
MD
216int lttng_session_statedump(struct lttng_session *session)
217{
218 int ret;
219
220 mutex_lock(&sessions_mutex);
221 ret = lttng_statedump_start(session);
222 mutex_unlock(&sessions_mutex);
223 return ret;
224}
225
a90917c3 226int lttng_session_enable(struct lttng_session *session)
c0e31d2e
MD
227{
228 int ret = 0;
a90917c3 229 struct lttng_channel *chan;
c0e31d2e
MD
230
231 mutex_lock(&sessions_mutex);
232 if (session->active) {
233 ret = -EBUSY;
234 goto end;
235 }
c099397a 236
3c997079
MD
237 /* Set transient enabler state to "enabled" */
238 session->tstate = 1;
239
c099397a
MD
240 /*
241 * Snapshot the number of events per channel to know the type of header
242 * we need to use.
243 */
244 list_for_each_entry(chan, &session->chan, list) {
245 if (chan->header_type)
246 continue; /* don't change it if session stop/restart */
247 if (chan->free_event_id < 31)
248 chan->header_type = 1; /* compact */
249 else
250 chan->header_type = 2; /* large */
251 }
252
3c997079
MD
253 /* We need to sync enablers with session before activation. */
254 lttng_session_sync_enablers(session);
255
64af2437 256 /* Clear each stream's quiescent state. */
09b93db9
MD
257 list_for_each_entry(chan, &session->chan, list) {
258 if (chan->channel_type != METADATA_CHANNEL)
259 lib_ring_buffer_clear_quiescent_channel(chan->chan);
260 }
64af2437 261
52fc2e1f 262 ACCESS_ONCE(session->active) = 1;
8070f5c0 263 ACCESS_ONCE(session->been_active) = 1;
a90917c3 264 ret = _lttng_session_metadata_statedump(session);
c337ddc2
MD
265 if (ret) {
266 ACCESS_ONCE(session->active) = 0;
267 goto end;
268 }
269 ret = lttng_statedump_start(session);
360f38ea 270 if (ret)
c099397a 271 ACCESS_ONCE(session->active) = 0;
c0e31d2e
MD
272end:
273 mutex_unlock(&sessions_mutex);
11b5a3c2 274 return ret;
c0e31d2e
MD
275}
276
a90917c3 277int lttng_session_disable(struct lttng_session *session)
c0e31d2e
MD
278{
279 int ret = 0;
64af2437 280 struct lttng_channel *chan;
c0e31d2e
MD
281
282 mutex_lock(&sessions_mutex);
283 if (!session->active) {
284 ret = -EBUSY;
285 goto end;
286 }
52fc2e1f 287 ACCESS_ONCE(session->active) = 0;
3c997079
MD
288
289 /* Set transient enabler state to "disabled" */
290 session->tstate = 0;
291 lttng_session_sync_enablers(session);
64af2437
MD
292
293 /* Set each stream's quiescent state. */
09b93db9
MD
294 list_for_each_entry(chan, &session->chan, list) {
295 if (chan->channel_type != METADATA_CHANNEL)
296 lib_ring_buffer_set_quiescent_channel(chan->chan);
297 }
c0e31d2e
MD
298end:
299 mutex_unlock(&sessions_mutex);
11b5a3c2 300 return ret;
c0e31d2e
MD
301}
302
9616f0bf
JD
303int lttng_session_metadata_regenerate(struct lttng_session *session)
304{
305 int ret = 0;
306 struct lttng_channel *chan;
307 struct lttng_event *event;
308 struct lttng_metadata_cache *cache = session->metadata_cache;
309 struct lttng_metadata_stream *stream;
310
311 mutex_lock(&sessions_mutex);
312 if (!session->active) {
313 ret = -EBUSY;
314 goto end;
315 }
316
317 mutex_lock(&cache->lock);
318 memset(cache->data, 0, cache->cache_alloc);
319 cache->metadata_written = 0;
320 cache->version++;
321 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list) {
322 stream->metadata_out = 0;
323 stream->metadata_in = 0;
324 }
325 mutex_unlock(&cache->lock);
326
327 session->metadata_dumped = 0;
328 list_for_each_entry(chan, &session->chan, list) {
329 chan->metadata_dumped = 0;
330 }
331
332 list_for_each_entry(event, &session->events, list) {
333 event->metadata_dumped = 0;
334 }
335
336 ret = _lttng_session_metadata_statedump(session);
337
338end:
339 mutex_unlock(&sessions_mutex);
340 return ret;
341}
342
343
344
a90917c3 345int lttng_channel_enable(struct lttng_channel *channel)
e64957da 346{
3c997079 347 int ret = 0;
e64957da 348
3c997079
MD
349 mutex_lock(&sessions_mutex);
350 if (channel->channel_type == METADATA_CHANNEL) {
351 ret = -EPERM;
352 goto end;
353 }
354 if (channel->enabled) {
355 ret = -EEXIST;
356 goto end;
357 }
358 /* Set transient enabler state to "enabled" */
359 channel->tstate = 1;
360 lttng_session_sync_enablers(channel->session);
361 /* Set atomically the state to "enabled" */
362 ACCESS_ONCE(channel->enabled) = 1;
363end:
364 mutex_unlock(&sessions_mutex);
365 return ret;
e64957da
MD
366}
367
a90917c3 368int lttng_channel_disable(struct lttng_channel *channel)
e64957da 369{
3c997079 370 int ret = 0;
e64957da 371
3c997079
MD
372 mutex_lock(&sessions_mutex);
373 if (channel->channel_type == METADATA_CHANNEL) {
374 ret = -EPERM;
375 goto end;
376 }
377 if (!channel->enabled) {
378 ret = -EEXIST;
379 goto end;
380 }
381 /* Set atomically the state to "disabled" */
382 ACCESS_ONCE(channel->enabled) = 0;
383 /* Set transient enabler state to "enabled" */
384 channel->tstate = 0;
385 lttng_session_sync_enablers(channel->session);
386end:
387 mutex_unlock(&sessions_mutex);
388 return ret;
e64957da
MD
389}
390
a90917c3 391int lttng_event_enable(struct lttng_event *event)
e64957da 392{
3c997079 393 int ret = 0;
e64957da 394
3c997079
MD
395 mutex_lock(&sessions_mutex);
396 if (event->chan->channel_type == METADATA_CHANNEL) {
397 ret = -EPERM;
398 goto end;
399 }
400 if (event->enabled) {
401 ret = -EEXIST;
402 goto end;
403 }
a0493bef
MD
404 switch (event->instrumentation) {
405 case LTTNG_KERNEL_TRACEPOINT:
406 case LTTNG_KERNEL_SYSCALL:
407 ret = -EINVAL;
408 break;
409 case LTTNG_KERNEL_KPROBE:
410 case LTTNG_KERNEL_FUNCTION:
411 case LTTNG_KERNEL_NOOP:
412 ACCESS_ONCE(event->enabled) = 1;
413 break;
414 case LTTNG_KERNEL_KRETPROBE:
415 ret = lttng_kretprobes_event_enable_state(event, 1);
416 break;
417 default:
418 WARN_ON_ONCE(1);
419 ret = -EINVAL;
420 }
3c997079
MD
421end:
422 mutex_unlock(&sessions_mutex);
423 return ret;
e64957da
MD
424}
425
a90917c3 426int lttng_event_disable(struct lttng_event *event)
e64957da 427{
3c997079 428 int ret = 0;
e64957da 429
3c997079
MD
430 mutex_lock(&sessions_mutex);
431 if (event->chan->channel_type == METADATA_CHANNEL) {
432 ret = -EPERM;
433 goto end;
434 }
435 if (!event->enabled) {
436 ret = -EEXIST;
437 goto end;
438 }
a0493bef
MD
439 switch (event->instrumentation) {
440 case LTTNG_KERNEL_TRACEPOINT:
441 case LTTNG_KERNEL_SYSCALL:
442 ret = -EINVAL;
443 break;
444 case LTTNG_KERNEL_KPROBE:
445 case LTTNG_KERNEL_FUNCTION:
446 case LTTNG_KERNEL_NOOP:
447 ACCESS_ONCE(event->enabled) = 0;
448 break;
449 case LTTNG_KERNEL_KRETPROBE:
450 ret = lttng_kretprobes_event_enable_state(event, 0);
451 break;
452 default:
453 WARN_ON_ONCE(1);
454 ret = -EINVAL;
455 }
3c997079
MD
456end:
457 mutex_unlock(&sessions_mutex);
458 return ret;
e64957da
MD
459}
460
a90917c3 461static struct lttng_transport *lttng_transport_find(const char *name)
f3d01b96 462{
a90917c3 463 struct lttng_transport *transport;
f3d01b96 464
a90917c3 465 list_for_each_entry(transport, &lttng_transport_list, node) {
f3d01b96
MD
466 if (!strcmp(transport->name, name))
467 return transport;
468 }
469 return NULL;
470}
471
a90917c3 472struct lttng_channel *lttng_channel_create(struct lttng_session *session,
5dbbdb43
MD
473 const char *transport_name,
474 void *buf_addr,
4e3c1b9b
MD
475 size_t subbuf_size, size_t num_subbuf,
476 unsigned int switch_timer_interval,
d83004aa
JD
477 unsigned int read_timer_interval,
478 enum channel_type channel_type)
4e3c1b9b 479{
a90917c3
MD
480 struct lttng_channel *chan;
481 struct lttng_transport *transport = NULL;
4e3c1b9b
MD
482
483 mutex_lock(&sessions_mutex);
d83004aa 484 if (session->been_active && channel_type != METADATA_CHANNEL)
e5382b6d 485 goto active; /* Refuse to add channel to active session */
a90917c3 486 transport = lttng_transport_find(transport_name);
f3d01b96
MD
487 if (!transport) {
488 printk(KERN_WARNING "LTTng transport %s not found\n",
489 transport_name);
c0e31d2e 490 goto notransport;
f3d01b96 491 }
a33c9927
MD
492 if (!try_module_get(transport->owner)) {
493 printk(KERN_WARNING "LTT : Can't lock transport module.\n");
494 goto notransport;
495 }
a90917c3 496 chan = kzalloc(sizeof(struct lttng_channel), GFP_KERNEL);
4e3c1b9b 497 if (!chan)
c0e31d2e 498 goto nomem;
4e3c1b9b 499 chan->session = session;
05d32c64 500 chan->id = session->free_chan_id++;
3b731ab1 501 chan->ops = &transport->ops;
125b4df4
MD
502 /*
503 * Note: the channel creation op already writes into the packet
504 * headers. Therefore the "chan" information used as input
505 * should be already accessible.
506 */
5f5ddf01
MD
507 chan->chan = transport->ops.channel_create(transport_name,
508 chan, buf_addr, subbuf_size, num_subbuf,
509 switch_timer_interval, read_timer_interval);
f3d01b96
MD
510 if (!chan->chan)
511 goto create_error;
3c997079 512 chan->tstate = 1;
e64957da 513 chan->enabled = 1;
a33c9927 514 chan->transport = transport;
d83004aa 515 chan->channel_type = channel_type;
4e3c1b9b
MD
516 list_add(&chan->list, &session->chan);
517 mutex_unlock(&sessions_mutex);
518 return chan;
519
f3d01b96
MD
520create_error:
521 kfree(chan);
c0e31d2e 522nomem:
a33c9927
MD
523 if (transport)
524 module_put(transport->owner);
c0e31d2e 525notransport:
e5382b6d 526active:
4e3c1b9b
MD
527 mutex_unlock(&sessions_mutex);
528 return NULL;
529}
530
531/*
d83004aa
JD
532 * Only used internally at session destruction for per-cpu channels, and
533 * when metadata channel is released.
534 * Needs to be called with sessions mutex held.
4e3c1b9b 535 */
aa7c23a9 536static
a90917c3 537void _lttng_channel_destroy(struct lttng_channel *chan)
4e3c1b9b 538{
11b5a3c2 539 chan->ops->channel_destroy(chan->chan);
a33c9927 540 module_put(chan->transport->owner);
4e3c1b9b 541 list_del(&chan->list);
8070f5c0 542 lttng_destroy_context(chan->ctx);
4e3c1b9b
MD
543 kfree(chan);
544}
545
d83004aa
JD
546void lttng_metadata_channel_destroy(struct lttng_channel *chan)
547{
548 BUG_ON(chan->channel_type != METADATA_CHANNEL);
549
550 /* Protect the metadata cache with the sessions_mutex. */
551 mutex_lock(&sessions_mutex);
552 _lttng_channel_destroy(chan);
553 mutex_unlock(&sessions_mutex);
554}
555EXPORT_SYMBOL_GPL(lttng_metadata_channel_destroy);
556
557static
558void _lttng_metadata_channel_hangup(struct lttng_metadata_stream *stream)
559{
560 stream->finalized = 1;
561 wake_up_interruptible(&stream->read_wait);
562}
563
e5382b6d
MD
564/*
565 * Supports event creation while tracing session is active.
3c997079 566 * Needs to be called with sessions mutex held.
e5382b6d 567 */
3c997079
MD
568struct lttng_event *_lttng_event_create(struct lttng_channel *chan,
569 struct lttng_kernel_event *event_param,
570 void *filter,
571 const struct lttng_event_desc *event_desc,
572 enum lttng_kernel_instrumentation itype)
4e3c1b9b 573{
3c997079 574 struct lttng_session *session = chan->session;
a90917c3 575 struct lttng_event *event;
3c997079
MD
576 const char *event_name;
577 struct hlist_head *head;
578 size_t name_len;
579 uint32_t hash;
3d084699 580 int ret;
4e3c1b9b 581
abc0446a 582 if (chan->free_event_id == -1U) {
4cf0bf51 583 ret = -EMFILE;
e5382b6d 584 goto full;
abc0446a 585 }
3c997079
MD
586
587 switch (itype) {
588 case LTTNG_KERNEL_TRACEPOINT:
589 event_name = event_desc->name;
590 break;
591 case LTTNG_KERNEL_KPROBE:
592 case LTTNG_KERNEL_KRETPROBE:
593 case LTTNG_KERNEL_FUNCTION:
594 case LTTNG_KERNEL_NOOP:
33a39a3c 595 case LTTNG_KERNEL_SYSCALL:
3c997079
MD
596 event_name = event_param->name;
597 break;
598 default:
599 WARN_ON_ONCE(1);
600 ret = -EINVAL;
601 goto type_error;
602 }
603 name_len = strlen(event_name);
604 hash = jhash(event_name, name_len, 0);
605 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
d216ecae 606 lttng_hlist_for_each_entry(event, head, hlist) {
3c997079
MD
607 WARN_ON_ONCE(!event->desc);
608 if (!strncmp(event->desc->name, event_name,
609 LTTNG_KERNEL_SYM_NAME_LEN - 1)
610 && chan == event->chan) {
611 ret = -EEXIST;
612 goto exist;
abc0446a
MD
613 }
614 }
3c997079 615
11b5a3c2 616 event = kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 617 if (!event) {
4cf0bf51 618 ret = -ENOMEM;
4e3c1b9b 619 goto cache_error;
abc0446a 620 }
4e3c1b9b
MD
621 event->chan = chan;
622 event->filter = filter;
e5382b6d 623 event->id = chan->free_event_id++;
3c997079
MD
624 event->instrumentation = itype;
625 event->evtype = LTTNG_TYPE_EVENT;
07dfc1d0 626 INIT_LIST_HEAD(&event->bytecode_runtime_head);
3c997079
MD
627 INIT_LIST_HEAD(&event->enablers_ref_head);
628
629 switch (itype) {
ab2277d6 630 case LTTNG_KERNEL_TRACEPOINT:
3c997079
MD
631 /* Event will be enabled by enabler sync. */
632 event->enabled = 0;
633 event->registered = 0;
634 event->desc = lttng_event_get(event_name);
abc0446a 635 if (!event->desc) {
4cf0bf51 636 ret = -ENOENT;
d3dbe23c 637 goto register_error;
abc0446a 638 }
3c997079
MD
639 /* Populate lttng_event structure before event registration. */
640 smp_wmb();
baf20995 641 break;
ab2277d6 642 case LTTNG_KERNEL_KPROBE:
a0493bef
MD
643 /*
644 * Needs to be explicitly enabled after creation, since
645 * we may want to apply filters.
646 */
647 event->enabled = 0;
3c997079
MD
648 event->registered = 1;
649 /*
650 * Populate lttng_event structure before event
651 * registration.
652 */
653 smp_wmb();
654 ret = lttng_kprobes_register(event_name,
f17701fb
MD
655 event_param->u.kprobe.symbol_name,
656 event_param->u.kprobe.offset,
657 event_param->u.kprobe.addr,
658 event);
abc0446a 659 if (ret) {
4cf0bf51 660 ret = -EINVAL;
d6d808f3 661 goto register_error;
abc0446a 662 }
edeb3137
MD
663 ret = try_module_get(event->desc->owner);
664 WARN_ON_ONCE(!ret);
d6d808f3 665 break;
7371f44c
MD
666 case LTTNG_KERNEL_KRETPROBE:
667 {
a90917c3 668 struct lttng_event *event_return;
7371f44c
MD
669
670 /* kretprobe defines 2 events */
a0493bef
MD
671 /*
672 * Needs to be explicitly enabled after creation, since
673 * we may want to apply filters.
674 */
675 event->enabled = 0;
3c997079 676 event->registered = 1;
7371f44c
MD
677 event_return =
678 kmem_cache_zalloc(event_cache, GFP_KERNEL);
abc0446a 679 if (!event_return) {
4cf0bf51 680 ret = -ENOMEM;
7371f44c 681 goto register_error;
abc0446a 682 }
7371f44c
MD
683 event_return->chan = chan;
684 event_return->filter = filter;
685 event_return->id = chan->free_event_id++;
a0493bef 686 event_return->enabled = 0;
3c997079
MD
687 event_return->registered = 1;
688 event_return->instrumentation = itype;
7371f44c 689 /*
a90917c3 690 * Populate lttng_event structure before kretprobe registration.
7371f44c
MD
691 */
692 smp_wmb();
3c997079 693 ret = lttng_kretprobes_register(event_name,
7371f44c
MD
694 event_param->u.kretprobe.symbol_name,
695 event_param->u.kretprobe.offset,
696 event_param->u.kretprobe.addr,
697 event, event_return);
698 if (ret) {
699 kmem_cache_free(event_cache, event_return);
4cf0bf51 700 ret = -EINVAL;
7371f44c
MD
701 goto register_error;
702 }
703 /* Take 2 refs on the module: one per event. */
704 ret = try_module_get(event->desc->owner);
705 WARN_ON_ONCE(!ret);
706 ret = try_module_get(event->desc->owner);
707 WARN_ON_ONCE(!ret);
a90917c3 708 ret = _lttng_event_metadata_statedump(chan->session, chan,
7371f44c 709 event_return);
abc0446a 710 WARN_ON_ONCE(ret > 0);
7371f44c
MD
711 if (ret) {
712 kmem_cache_free(event_cache, event_return);
713 module_put(event->desc->owner);
714 module_put(event->desc->owner);
715 goto statedump_error;
716 }
717 list_add(&event_return->list, &chan->session->events);
718 break;
719 }
ab2277d6 720 case LTTNG_KERNEL_FUNCTION:
a0493bef
MD
721 /*
722 * Needs to be explicitly enabled after creation, since
723 * we may want to apply filters.
724 */
725 event->enabled = 0;
3c997079
MD
726 event->registered = 1;
727 /*
728 * Populate lttng_event structure before event
729 * registration.
730 */
731 smp_wmb();
732 ret = lttng_ftrace_register(event_name,
e0a7a7c4
MD
733 event_param->u.ftrace.symbol_name,
734 event);
abc0446a 735 if (ret) {
e0a7a7c4 736 goto register_error;
abc0446a 737 }
edeb3137
MD
738 ret = try_module_get(event->desc->owner);
739 WARN_ON_ONCE(!ret);
e0a7a7c4 740 break;
1ec65de1 741 case LTTNG_KERNEL_NOOP:
33a39a3c 742 case LTTNG_KERNEL_SYSCALL:
a0493bef
MD
743 /*
744 * Needs to be explicitly enabled after creation, since
745 * we may want to apply filters.
746 */
747 event->enabled = 0;
3c997079
MD
748 event->registered = 0;
749 event->desc = event_desc;
abc0446a 750 if (!event->desc) {
4cf0bf51 751 ret = -EINVAL;
259b6cb3 752 goto register_error;
abc0446a 753 }
1ec65de1 754 break;
baf20995
MD
755 default:
756 WARN_ON_ONCE(1);
4cf0bf51 757 ret = -EINVAL;
df07930b 758 goto register_error;
baf20995 759 }
a90917c3 760 ret = _lttng_event_metadata_statedump(chan->session, chan, event);
abc0446a
MD
761 WARN_ON_ONCE(ret > 0);
762 if (ret) {
c099397a 763 goto statedump_error;
abc0446a 764 }
3c997079 765 hlist_add_head(&event->hlist, head);
dda6a249 766 list_add(&event->list, &chan->session->events);
4e3c1b9b
MD
767 return event;
768
c099397a 769statedump_error:
259b6cb3 770 /* If a statedump error occurs, events will not be readable. */
11b5a3c2 771register_error:
11b5a3c2 772 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
773cache_error:
774exist:
3c997079 775type_error:
e5382b6d 776full:
4cf0bf51 777 return ERR_PTR(ret);
4e3c1b9b
MD
778}
779
3c997079
MD
780struct lttng_event *lttng_event_create(struct lttng_channel *chan,
781 struct lttng_kernel_event *event_param,
782 void *filter,
783 const struct lttng_event_desc *event_desc,
784 enum lttng_kernel_instrumentation itype)
785{
786 struct lttng_event *event;
787
788 mutex_lock(&sessions_mutex);
789 event = _lttng_event_create(chan, event_param, filter, event_desc,
790 itype);
791 mutex_unlock(&sessions_mutex);
792 return event;
793}
794
795/* Only used for tracepoints for now. */
796static
797void register_event(struct lttng_event *event)
798{
799 const struct lttng_event_desc *desc;
f127e61e 800 int ret = -EINVAL;
3c997079 801
3c997079
MD
802 if (event->registered)
803 return;
4ecb5ad5 804
3c997079 805 desc = event->desc;
4ecb5ad5
MD
806 switch (event->instrumentation) {
807 case LTTNG_KERNEL_TRACEPOINT:
808 ret = lttng_wrapper_tracepoint_probe_register(desc->kname,
809 desc->probe_callback,
810 event);
811 break;
812 case LTTNG_KERNEL_SYSCALL:
813 ret = lttng_syscall_filter_enable(event->chan,
814 desc->name);
815 break;
816 case LTTNG_KERNEL_KPROBE:
817 case LTTNG_KERNEL_KRETPROBE:
818 case LTTNG_KERNEL_FUNCTION:
819 case LTTNG_KERNEL_NOOP:
820 ret = 0;
821 break;
822 default:
823 WARN_ON_ONCE(1);
824 }
3c997079
MD
825 if (!ret)
826 event->registered = 1;
827}
828
4e3c1b9b
MD
829/*
830 * Only used internally at session destruction.
831 */
a90917c3 832int _lttng_event_unregister(struct lttng_event *event)
4e3c1b9b 833{
4ecb5ad5 834 const struct lttng_event_desc *desc;
11b5a3c2
MD
835 int ret = -EINVAL;
836
3c997079
MD
837 if (!event->registered)
838 return 0;
839
4ecb5ad5 840 desc = event->desc;
38d024ae 841 switch (event->instrumentation) {
ab2277d6 842 case LTTNG_KERNEL_TRACEPOINT:
20591cf7 843 ret = lttng_wrapper_tracepoint_probe_unregister(event->desc->kname,
85a9ca7f 844 event->desc->probe_callback,
11b5a3c2 845 event);
baf20995 846 break;
ab2277d6 847 case LTTNG_KERNEL_KPROBE:
f17701fb 848 lttng_kprobes_unregister(event);
d6d808f3
MD
849 ret = 0;
850 break;
7371f44c
MD
851 case LTTNG_KERNEL_KRETPROBE:
852 lttng_kretprobes_unregister(event);
853 ret = 0;
854 break;
ab2277d6 855 case LTTNG_KERNEL_FUNCTION:
e0a7a7c4
MD
856 lttng_ftrace_unregister(event);
857 ret = 0;
858 break;
33a39a3c 859 case LTTNG_KERNEL_SYSCALL:
4ecb5ad5
MD
860 ret = lttng_syscall_filter_disable(event->chan,
861 desc->name);
862 break;
863 case LTTNG_KERNEL_NOOP:
259b6cb3
MD
864 ret = 0;
865 break;
baf20995
MD
866 default:
867 WARN_ON_ONCE(1);
868 }
3c997079
MD
869 if (!ret)
870 event->registered = 0;
dda6a249
MD
871 return ret;
872}
873
874/*
875 * Only used internally at session destruction.
876 */
be066e6c 877static
a90917c3 878void _lttng_event_destroy(struct lttng_event *event)
dda6a249 879{
edeb3137
MD
880 switch (event->instrumentation) {
881 case LTTNG_KERNEL_TRACEPOINT:
a90917c3 882 lttng_event_put(event->desc);
edeb3137
MD
883 break;
884 case LTTNG_KERNEL_KPROBE:
885 module_put(event->desc->owner);
886 lttng_kprobes_destroy_private(event);
887 break;
7371f44c
MD
888 case LTTNG_KERNEL_KRETPROBE:
889 module_put(event->desc->owner);
890 lttng_kretprobes_destroy_private(event);
891 break;
edeb3137
MD
892 case LTTNG_KERNEL_FUNCTION:
893 module_put(event->desc->owner);
894 lttng_ftrace_destroy_private(event);
895 break;
259b6cb3 896 case LTTNG_KERNEL_NOOP:
33a39a3c 897 case LTTNG_KERNEL_SYSCALL:
259b6cb3 898 break;
edeb3137
MD
899 default:
900 WARN_ON_ONCE(1);
901 }
dda6a249 902 list_del(&event->list);
8070f5c0 903 lttng_destroy_context(event->ctx);
11b5a3c2 904 kmem_cache_free(event_cache, event);
4e3c1b9b
MD
905}
906
e0130fab
MD
907int lttng_session_track_pid(struct lttng_session *session, int pid)
908{
909 int ret;
910
911 if (pid < -1)
912 return -EINVAL;
913 mutex_lock(&sessions_mutex);
914 if (pid == -1) {
915 /* track all pids: destroy tracker. */
916 if (session->pid_tracker) {
917 struct lttng_pid_tracker *lpf;
918
919 lpf = session->pid_tracker;
920 rcu_assign_pointer(session->pid_tracker, NULL);
921 synchronize_trace();
922 lttng_pid_tracker_destroy(lpf);
923 }
924 ret = 0;
925 } else {
926 if (!session->pid_tracker) {
927 struct lttng_pid_tracker *lpf;
928
929 lpf = lttng_pid_tracker_create();
930 if (!lpf) {
931 ret = -ENOMEM;
932 goto unlock;
933 }
934 ret = lttng_pid_tracker_add(lpf, pid);
935 rcu_assign_pointer(session->pid_tracker, lpf);
936 } else {
937 ret = lttng_pid_tracker_add(session->pid_tracker, pid);
938 }
939 }
940unlock:
941 mutex_unlock(&sessions_mutex);
942 return ret;
943}
944
945int lttng_session_untrack_pid(struct lttng_session *session, int pid)
946{
947 int ret;
948
949 if (pid < -1)
950 return -EINVAL;
951 mutex_lock(&sessions_mutex);
952 if (pid == -1) {
953 /* untrack all pids: replace by empty tracker. */
954 struct lttng_pid_tracker *old_lpf = session->pid_tracker;
955 struct lttng_pid_tracker *lpf;
956
957 lpf = lttng_pid_tracker_create();
958 if (!lpf) {
959 ret = -ENOMEM;
960 goto unlock;
961 }
962 rcu_assign_pointer(session->pid_tracker, lpf);
963 synchronize_trace();
964 if (old_lpf)
965 lttng_pid_tracker_destroy(old_lpf);
966 ret = 0;
967 } else {
968 if (!session->pid_tracker) {
969 ret = -ENOENT;
970 goto unlock;
971 }
972 ret = lttng_pid_tracker_del(session->pid_tracker, pid);
973 }
974unlock:
975 mutex_unlock(&sessions_mutex);
976 return ret;
977}
978
7e6f9ef6
MD
979static
980void *pid_list_start(struct seq_file *m, loff_t *pos)
981{
982 struct lttng_session *session = m->private;
983 struct lttng_pid_tracker *lpf;
984 struct lttng_pid_hash_node *e;
985 int iter = 0, i;
986
987 mutex_lock(&sessions_mutex);
988 lpf = session->pid_tracker;
989 if (lpf) {
990 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
991 struct hlist_head *head = &lpf->pid_hash[i];
992
f934e302 993 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
994 if (iter++ >= *pos)
995 return e;
996 }
997 }
998 } else {
999 /* PID tracker disabled. */
1000 if (iter >= *pos && iter == 0) {
1001 return session; /* empty tracker */
1002 }
1003 iter++;
1004 }
1005 /* End of list */
1006 return NULL;
1007}
1008
1009/* Called with sessions_mutex held. */
1010static
1011void *pid_list_next(struct seq_file *m, void *p, loff_t *ppos)
1012{
1013 struct lttng_session *session = m->private;
1014 struct lttng_pid_tracker *lpf;
1015 struct lttng_pid_hash_node *e;
1016 int iter = 0, i;
1017
1018 (*ppos)++;
1019 lpf = session->pid_tracker;
1020 if (lpf) {
1021 for (i = 0; i < LTTNG_PID_TABLE_SIZE; i++) {
1022 struct hlist_head *head = &lpf->pid_hash[i];
1023
f934e302 1024 lttng_hlist_for_each_entry(e, head, hlist) {
7e6f9ef6
MD
1025 if (iter++ >= *ppos)
1026 return e;
1027 }
1028 }
1029 } else {
1030 /* PID tracker disabled. */
1031 if (iter >= *ppos && iter == 0)
1032 return session; /* empty tracker */
1033 iter++;
1034 }
1035
1036 /* End of list */
1037 return NULL;
1038}
1039
1040static
1041void pid_list_stop(struct seq_file *m, void *p)
1042{
1043 mutex_unlock(&sessions_mutex);
1044}
1045
1046static
1047int pid_list_show(struct seq_file *m, void *p)
1048{
1049 int pid;
1050
1051 if (p == m->private) {
1052 /* Tracker disabled. */
1053 pid = -1;
1054 } else {
1055 const struct lttng_pid_hash_node *e = p;
1056
1057 pid = lttng_pid_tracker_get_node_pid(e);
1058 }
1059 seq_printf(m, "process { pid = %d; };\n", pid);
1060 return 0;
1061}
1062
1063static
1064const struct seq_operations lttng_tracker_pids_list_seq_ops = {
1065 .start = pid_list_start,
1066 .next = pid_list_next,
1067 .stop = pid_list_stop,
1068 .show = pid_list_show,
1069};
1070
1071static
1072int lttng_tracker_pids_list_open(struct inode *inode, struct file *file)
1073{
1074 return seq_open(file, &lttng_tracker_pids_list_seq_ops);
1075}
1076
1077static
1078int lttng_tracker_pids_list_release(struct inode *inode, struct file *file)
1079{
1080 struct seq_file *m = file->private_data;
1081 struct lttng_session *session = m->private;
1082 int ret;
1083
1084 WARN_ON_ONCE(!session);
1085 ret = seq_release(inode, file);
1086 if (!ret && session)
1087 fput(session->file);
1088 return ret;
1089}
1090
1091const struct file_operations lttng_tracker_pids_list_fops = {
1092 .owner = THIS_MODULE,
1093 .open = lttng_tracker_pids_list_open,
1094 .read = seq_read,
1095 .llseek = seq_lseek,
1096 .release = lttng_tracker_pids_list_release,
1097};
1098
1099int lttng_session_list_tracker_pids(struct lttng_session *session)
1100{
1101 struct file *tracker_pids_list_file;
1102 struct seq_file *m;
1103 int file_fd, ret;
1104
1105 file_fd = lttng_get_unused_fd();
1106 if (file_fd < 0) {
1107 ret = file_fd;
1108 goto fd_error;
1109 }
1110
1111 tracker_pids_list_file = anon_inode_getfile("[lttng_tracker_pids_list]",
1112 &lttng_tracker_pids_list_fops,
1113 NULL, O_RDWR);
1114 if (IS_ERR(tracker_pids_list_file)) {
1115 ret = PTR_ERR(tracker_pids_list_file);
1116 goto file_error;
1117 }
9c1f4643
MD
1118 if (atomic_long_add_unless(&session->file->f_count,
1119 1, INT_MAX) == INT_MAX) {
1120 goto refcount_error;
1121 }
7e6f9ef6
MD
1122 ret = lttng_tracker_pids_list_fops.open(NULL, tracker_pids_list_file);
1123 if (ret < 0)
1124 goto open_error;
1125 m = tracker_pids_list_file->private_data;
1126 m->private = session;
1127 fd_install(file_fd, tracker_pids_list_file);
7e6f9ef6
MD
1128
1129 return file_fd;
1130
1131open_error:
9c1f4643
MD
1132 atomic_long_dec(&session->file->f_count);
1133refcount_error:
7e6f9ef6
MD
1134 fput(tracker_pids_list_file);
1135file_error:
1136 put_unused_fd(file_fd);
1137fd_error:
1138 return ret;
1139}
1140
3c997079
MD
1141/*
1142 * Enabler management.
1143 */
1144static
4ecb5ad5
MD
1145int lttng_match_enabler_wildcard(const char *desc_name,
1146 const char *name)
3c997079 1147{
3c997079 1148 /* Compare excluding final '*' */
4ecb5ad5 1149 if (strncmp(desc_name, name, strlen(name) - 1))
3c997079
MD
1150 return 0;
1151 return 1;
1152}
1153
1154static
4ecb5ad5
MD
1155int lttng_match_enabler_name(const char *desc_name,
1156 const char *name)
3c997079 1157{
4ecb5ad5 1158 if (strcmp(desc_name, name))
3c997079
MD
1159 return 0;
1160 return 1;
1161}
1162
1163static
1164int lttng_desc_match_enabler(const struct lttng_event_desc *desc,
1165 struct lttng_enabler *enabler)
1166{
4ecb5ad5
MD
1167 const char *desc_name, *enabler_name;
1168
1169 enabler_name = enabler->event_param.name;
1170 switch (enabler->event_param.instrumentation) {
1171 case LTTNG_KERNEL_TRACEPOINT:
1172 desc_name = desc->name;
1173 break;
1174 case LTTNG_KERNEL_SYSCALL:
1175 desc_name = desc->name;
1176 if (!strncmp(desc_name, "compat_", strlen("compat_")))
1177 desc_name += strlen("compat_");
1178 if (!strncmp(desc_name, "syscall_exit_",
1179 strlen("syscall_exit_"))) {
1180 desc_name += strlen("syscall_exit_");
1181 } else if (!strncmp(desc_name, "syscall_entry_",
1182 strlen("syscall_entry_"))) {
1183 desc_name += strlen("syscall_entry_");
1184 } else {
1185 WARN_ON_ONCE(1);
1186 return -EINVAL;
1187 }
1188 break;
1189 default:
1190 WARN_ON_ONCE(1);
1191 return -EINVAL;
1192 }
3c997079
MD
1193 switch (enabler->type) {
1194 case LTTNG_ENABLER_WILDCARD:
4ecb5ad5 1195 return lttng_match_enabler_wildcard(desc_name, enabler_name);
3c997079 1196 case LTTNG_ENABLER_NAME:
4ecb5ad5 1197 return lttng_match_enabler_name(desc_name, enabler_name);
3c997079
MD
1198 default:
1199 return -EINVAL;
1200 }
1201}
1202
1203static
1204int lttng_event_match_enabler(struct lttng_event *event,
1205 struct lttng_enabler *enabler)
1206{
33a39a3c
MD
1207 if (enabler->event_param.instrumentation != event->instrumentation)
1208 return 0;
3c997079
MD
1209 if (lttng_desc_match_enabler(event->desc, enabler)
1210 && event->chan == enabler->chan)
1211 return 1;
1212 else
1213 return 0;
1214}
1215
1216static
1217struct lttng_enabler_ref *lttng_event_enabler_ref(struct lttng_event *event,
1218 struct lttng_enabler *enabler)
1219{
1220 struct lttng_enabler_ref *enabler_ref;
1221
1222 list_for_each_entry(enabler_ref,
1223 &event->enablers_ref_head, node) {
1224 if (enabler_ref->ref == enabler)
1225 return enabler_ref;
1226 }
1227 return NULL;
1228}
1229
3c997079 1230static
33a39a3c 1231void lttng_create_tracepoint_if_missing(struct lttng_enabler *enabler)
3c997079
MD
1232{
1233 struct lttng_session *session = enabler->chan->session;
1234 struct lttng_probe_desc *probe_desc;
1235 const struct lttng_event_desc *desc;
1236 int i;
1237 struct list_head *probe_list;
1238
1239 probe_list = lttng_get_probe_list_head();
1240 /*
1241 * For each probe event, if we find that a probe event matches
1242 * our enabler, create an associated lttng_event if not
1243 * already present.
1244 */
1245 list_for_each_entry(probe_desc, probe_list, head) {
1246 for (i = 0; i < probe_desc->nr_events; i++) {
1247 int found = 0;
1248 struct hlist_head *head;
1249 const char *event_name;
1250 size_t name_len;
1251 uint32_t hash;
1252 struct lttng_event *event;
1253
1254 desc = probe_desc->event_desc[i];
1255 if (!lttng_desc_match_enabler(desc, enabler))
1256 continue;
1257 event_name = desc->name;
1258 name_len = strlen(event_name);
1259
1260 /*
1261 * Check if already created.
1262 */
1263 hash = jhash(event_name, name_len, 0);
1264 head = &session->events_ht.table[hash & (LTTNG_EVENT_HT_SIZE - 1)];
d216ecae 1265 lttng_hlist_for_each_entry(event, head, hlist) {
3c997079
MD
1266 if (event->desc == desc
1267 && event->chan == enabler->chan)
1268 found = 1;
1269 }
1270 if (found)
1271 continue;
1272
1273 /*
1274 * We need to create an event for this
1275 * event probe.
1276 */
1277 event = _lttng_event_create(enabler->chan,
1278 NULL, NULL, desc,
1279 LTTNG_KERNEL_TRACEPOINT);
1280 if (!event) {
1281 printk(KERN_INFO "Unable to create event %s\n",
1282 probe_desc->event_desc[i]->name);
1283 }
1284 }
1285 }
1286}
1287
33a39a3c
MD
1288static
1289void lttng_create_syscall_if_missing(struct lttng_enabler *enabler)
1290{
1291 int ret;
1292
1293 ret = lttng_syscalls_register(enabler->chan, NULL);
1294 WARN_ON_ONCE(ret);
1295}
1296
1297/*
1298 * Create struct lttng_event if it is missing and present in the list of
1299 * tracepoint probes.
1300 * Should be called with sessions mutex held.
1301 */
1302static
1303void lttng_create_event_if_missing(struct lttng_enabler *enabler)
1304{
1305 switch (enabler->event_param.instrumentation) {
1306 case LTTNG_KERNEL_TRACEPOINT:
1307 lttng_create_tracepoint_if_missing(enabler);
1308 break;
1309 case LTTNG_KERNEL_SYSCALL:
1310 lttng_create_syscall_if_missing(enabler);
1311 break;
1312 default:
1313 WARN_ON_ONCE(1);
1314 break;
1315 }
1316}
1317
3c997079
MD
1318/*
1319 * Create events associated with an enabler (if not already present),
1320 * and add backward reference from the event to the enabler.
1321 * Should be called with sessions mutex held.
1322 */
1323static
1324int lttng_enabler_ref_events(struct lttng_enabler *enabler)
1325{
1326 struct lttng_session *session = enabler->chan->session;
1327 struct lttng_event *event;
1328
1329 /* First ensure that probe events are created for this enabler. */
1330 lttng_create_event_if_missing(enabler);
1331
1332 /* For each event matching enabler in session event list. */
1333 list_for_each_entry(event, &session->events, list) {
1334 struct lttng_enabler_ref *enabler_ref;
1335
1336 if (!lttng_event_match_enabler(event, enabler))
1337 continue;
1338 enabler_ref = lttng_event_enabler_ref(event, enabler);
1339 if (!enabler_ref) {
1340 /*
1341 * If no backward ref, create it.
1342 * Add backward ref from event to enabler.
1343 */
1344 enabler_ref = kzalloc(sizeof(*enabler_ref), GFP_KERNEL);
1345 if (!enabler_ref)
1346 return -ENOMEM;
1347 enabler_ref->ref = enabler;
1348 list_add(&enabler_ref->node,
1349 &event->enablers_ref_head);
1350 }
1351
f127e61e
MD
1352 /*
1353 * Link filter bytecodes if not linked yet.
1354 */
1355 lttng_enabler_event_link_bytecode(event, enabler);
1356
3c997079
MD
1357 /* TODO: merge event context. */
1358 }
1359 return 0;
1360}
1361
1362/*
1363 * Called at module load: connect the probe on all enablers matching
1364 * this event.
1365 * Called with sessions lock held.
1366 */
1367int lttng_fix_pending_events(void)
1368{
1369 struct lttng_session *session;
1370
1371 list_for_each_entry(session, &sessions, list)
1372 lttng_session_lazy_sync_enablers(session);
1373 return 0;
1374}
1375
1376struct lttng_enabler *lttng_enabler_create(enum lttng_enabler_type type,
1377 struct lttng_kernel_event *event_param,
1378 struct lttng_channel *chan)
1379{
1380 struct lttng_enabler *enabler;
1381
1382 enabler = kzalloc(sizeof(*enabler), GFP_KERNEL);
1383 if (!enabler)
1384 return NULL;
1385 enabler->type = type;
07dfc1d0 1386 INIT_LIST_HEAD(&enabler->filter_bytecode_head);
3c997079
MD
1387 memcpy(&enabler->event_param, event_param,
1388 sizeof(enabler->event_param));
1389 enabler->chan = chan;
1390 /* ctx left NULL */
4fd19d8a 1391 enabler->enabled = 0;
3c997079
MD
1392 enabler->evtype = LTTNG_TYPE_ENABLER;
1393 mutex_lock(&sessions_mutex);
1394 list_add(&enabler->node, &enabler->chan->session->enablers_head);
1395 lttng_session_lazy_sync_enablers(enabler->chan->session);
1396 mutex_unlock(&sessions_mutex);
1397 return enabler;
1398}
1399
1400int lttng_enabler_enable(struct lttng_enabler *enabler)
1401{
1402 mutex_lock(&sessions_mutex);
1403 enabler->enabled = 1;
1404 lttng_session_lazy_sync_enablers(enabler->chan->session);
1405 mutex_unlock(&sessions_mutex);
1406 return 0;
1407}
1408
1409int lttng_enabler_disable(struct lttng_enabler *enabler)
1410{
1411 mutex_lock(&sessions_mutex);
1412 enabler->enabled = 0;
1413 lttng_session_lazy_sync_enablers(enabler->chan->session);
1414 mutex_unlock(&sessions_mutex);
1415 return 0;
1416}
1417
07dfc1d0
MD
1418int lttng_enabler_attach_bytecode(struct lttng_enabler *enabler,
1419 struct lttng_kernel_filter_bytecode __user *bytecode)
1420{
1421 struct lttng_filter_bytecode_node *bytecode_node;
1422 uint32_t bytecode_len;
1423 int ret;
1424
1425 ret = get_user(bytecode_len, &bytecode->len);
1426 if (ret)
1427 return ret;
1428 bytecode_node = kzalloc(sizeof(*bytecode_node) + bytecode_len,
1429 GFP_KERNEL);
1430 if (!bytecode_node)
1431 return -ENOMEM;
1432 ret = copy_from_user(&bytecode_node->bc, bytecode,
1433 sizeof(*bytecode) + bytecode_len);
1434 if (ret)
1435 goto error_free;
1436 bytecode_node->enabler = enabler;
1437 /* Enforce length based on allocated size */
1438 bytecode_node->bc.len = bytecode_len;
1439 list_add_tail(&bytecode_node->node, &enabler->filter_bytecode_head);
1440 lttng_session_lazy_sync_enablers(enabler->chan->session);
1441 return 0;
1442
1443error_free:
1444 kfree(bytecode_node);
1445 return ret;
1446}
1447
3c997079
MD
1448int lttng_enabler_attach_context(struct lttng_enabler *enabler,
1449 struct lttng_kernel_context *context_param)
1450{
1451 return -ENOSYS;
1452}
1453
1454static
1455void lttng_enabler_destroy(struct lttng_enabler *enabler)
1456{
07dfc1d0
MD
1457 struct lttng_filter_bytecode_node *filter_node, *tmp_filter_node;
1458
1459 /* Destroy filter bytecode */
1460 list_for_each_entry_safe(filter_node, tmp_filter_node,
1461 &enabler->filter_bytecode_head, node) {
1462 kfree(filter_node);
1463 }
1464
3c997079
MD
1465 /* Destroy contexts */
1466 lttng_destroy_context(enabler->ctx);
1467
1468 list_del(&enabler->node);
1469 kfree(enabler);
1470}
1471
1472/*
1473 * lttng_session_sync_enablers should be called just before starting a
1474 * session.
1475 * Should be called with sessions mutex held.
1476 */
1477static
1478void lttng_session_sync_enablers(struct lttng_session *session)
1479{
1480 struct lttng_enabler *enabler;
1481 struct lttng_event *event;
1482
1483 list_for_each_entry(enabler, &session->enablers_head, node)
1484 lttng_enabler_ref_events(enabler);
1485 /*
1486 * For each event, if at least one of its enablers is enabled,
1487 * and its channel and session transient states are enabled, we
1488 * enable the event, else we disable it.
1489 */
1490 list_for_each_entry(event, &session->events, list) {
1491 struct lttng_enabler_ref *enabler_ref;
07dfc1d0
MD
1492 struct lttng_bytecode_runtime *runtime;
1493 int enabled = 0, has_enablers_without_bytecode = 0;
3c997079 1494
4ecb5ad5
MD
1495 switch (event->instrumentation) {
1496 case LTTNG_KERNEL_TRACEPOINT:
1497 case LTTNG_KERNEL_SYSCALL:
3c997079
MD
1498 /* Enable events */
1499 list_for_each_entry(enabler_ref,
1500 &event->enablers_ref_head, node) {
1501 if (enabler_ref->ref->enabled) {
1502 enabled = 1;
1503 break;
1504 }
1505 }
4ecb5ad5
MD
1506 break;
1507 default:
3c997079
MD
1508 /* Not handled with lazy sync. */
1509 continue;
1510 }
1511 /*
1512 * Enabled state is based on union of enablers, with
1513 * intesection of session and channel transient enable
1514 * states.
1515 */
1516 enabled = enabled && session->tstate && event->chan->tstate;
1517
1518 ACCESS_ONCE(event->enabled) = enabled;
1519 /*
1520 * Sync tracepoint registration with event enabled
1521 * state.
1522 */
1523 if (enabled) {
1524 register_event(event);
1525 } else {
1526 _lttng_event_unregister(event);
1527 }
07dfc1d0
MD
1528
1529 /* Check if has enablers without bytecode enabled */
1530 list_for_each_entry(enabler_ref,
1531 &event->enablers_ref_head, node) {
1532 if (enabler_ref->ref->enabled
1533 && list_empty(&enabler_ref->ref->filter_bytecode_head)) {
1534 has_enablers_without_bytecode = 1;
1535 break;
1536 }
1537 }
1538 event->has_enablers_without_bytecode =
1539 has_enablers_without_bytecode;
1540
1541 /* Enable filters */
1542 list_for_each_entry(runtime,
f127e61e 1543 &event->bytecode_runtime_head, node)
07dfc1d0 1544 lttng_filter_sync_state(runtime);
3c997079
MD
1545 }
1546}
1547
1548/*
1549 * Apply enablers to session events, adding events to session if need
1550 * be. It is required after each modification applied to an active
1551 * session, and right before session "start".
1552 * "lazy" sync means we only sync if required.
1553 * Should be called with sessions mutex held.
1554 */
1555static
1556void lttng_session_lazy_sync_enablers(struct lttng_session *session)
1557{
1558 /* We can skip if session is not active */
1559 if (!session->active)
1560 return;
1561 lttng_session_sync_enablers(session);
1562}
1563
1ec3f75a 1564/*
d83004aa
JD
1565 * Serialize at most one packet worth of metadata into a metadata
1566 * channel.
92d9f5e6
MD
1567 * We grab the metadata cache mutex to get exclusive access to our metadata
1568 * buffer and to the metadata cache. Exclusive access to the metadata buffer
1569 * allows us to do racy operations such as looking for remaining space left in
1570 * packet and write, since mutual exclusion protects us from concurrent writes.
1571 * Mutual exclusion on the metadata cache allow us to read the cache content
1572 * without racing against reallocation of the cache by updates.
35097f36
JD
1573 * Returns the number of bytes written in the channel, 0 if no data
1574 * was written and a negative value on error.
1ec3f75a 1575 */
b3b8072b
MD
1576int lttng_metadata_output_channel(struct lttng_metadata_stream *stream,
1577 struct channel *chan)
d83004aa
JD
1578{
1579 struct lib_ring_buffer_ctx ctx;
1580 int ret = 0;
1581 size_t len, reserve_len;
1582
f613e3e6 1583 /*
92d9f5e6
MD
1584 * Ensure we support mutiple get_next / put sequences followed by
1585 * put_next. The metadata cache lock protects reading the metadata
1586 * cache. It can indeed be read concurrently by "get_next_subbuf" and
1587 * "flush" operations on the buffer invoked by different processes.
1588 * Moreover, since the metadata cache memory can be reallocated, we
1589 * need to have exclusive access against updates even though we only
1590 * read it.
f613e3e6 1591 */
92d9f5e6 1592 mutex_lock(&stream->metadata_cache->lock);
f613e3e6
MD
1593 WARN_ON(stream->metadata_in < stream->metadata_out);
1594 if (stream->metadata_in != stream->metadata_out)
de23d59d 1595 goto end;
f613e3e6 1596
9616f0bf
JD
1597 /* Metadata regenerated, change the version. */
1598 if (stream->metadata_cache->version != stream->version)
1599 stream->version = stream->metadata_cache->version;
1600
d83004aa 1601 len = stream->metadata_cache->metadata_written -
f613e3e6 1602 stream->metadata_in;
9de2c215 1603 if (!len)
de23d59d 1604 goto end;
d83004aa 1605 reserve_len = min_t(size_t,
b3b8072b 1606 stream->transport->ops.packet_avail_size(chan),
d83004aa 1607 len);
b3b8072b 1608 lib_ring_buffer_ctx_init(&ctx, chan, NULL, reserve_len,
d83004aa
JD
1609 sizeof(char), -1);
1610 /*
1611 * If reservation failed, return an error to the caller.
1612 */
b3b8072b 1613 ret = stream->transport->ops.event_reserve(&ctx, 0);
d83004aa
JD
1614 if (ret != 0) {
1615 printk(KERN_WARNING "LTTng: Metadata event reservation failed\n");
1616 goto end;
1617 }
b3b8072b 1618 stream->transport->ops.event_write(&ctx,
f613e3e6 1619 stream->metadata_cache->data + stream->metadata_in,
d83004aa 1620 reserve_len);
b3b8072b 1621 stream->transport->ops.event_commit(&ctx);
f613e3e6 1622 stream->metadata_in += reserve_len;
d83004aa
JD
1623 ret = reserve_len;
1624
1625end:
92d9f5e6 1626 mutex_unlock(&stream->metadata_cache->lock);
d83004aa
JD
1627 return ret;
1628}
1629
1630/*
1631 * Write the metadata to the metadata cache.
1632 * Must be called with sessions_mutex held.
92d9f5e6
MD
1633 * The metadata cache lock protects us from concurrent read access from
1634 * thread outputting metadata content to ring buffer.
d83004aa 1635 */
a90917c3 1636int lttng_metadata_printf(struct lttng_session *session,
c099397a
MD
1637 const char *fmt, ...)
1638{
c099397a 1639 char *str;
d83004aa 1640 size_t len;
c099397a 1641 va_list ap;
d83004aa 1642 struct lttng_metadata_stream *stream;
c099397a
MD
1643
1644 WARN_ON_ONCE(!ACCESS_ONCE(session->active));
1645
1646 va_start(ap, fmt);
1647 str = kvasprintf(GFP_KERNEL, fmt, ap);
1648 va_end(ap);
1649 if (!str)
1650 return -ENOMEM;
1651
1ec3f75a 1652 len = strlen(str);
92d9f5e6 1653 mutex_lock(&session->metadata_cache->lock);
d83004aa
JD
1654 if (session->metadata_cache->metadata_written + len >
1655 session->metadata_cache->cache_alloc) {
1656 char *tmp_cache_realloc;
1657 unsigned int tmp_cache_alloc_size;
1658
1659 tmp_cache_alloc_size = max_t(unsigned int,
1660 session->metadata_cache->cache_alloc + len,
1661 session->metadata_cache->cache_alloc << 1);
f6da044f 1662 tmp_cache_realloc = lttng_vzalloc(tmp_cache_alloc_size);
d83004aa
JD
1663 if (!tmp_cache_realloc)
1664 goto err;
a606b6e8
ML
1665 if (session->metadata_cache->data) {
1666 memcpy(tmp_cache_realloc,
1667 session->metadata_cache->data,
1668 session->metadata_cache->cache_alloc);
1669 vfree(session->metadata_cache->data);
1670 }
1671
d83004aa
JD
1672 session->metadata_cache->cache_alloc = tmp_cache_alloc_size;
1673 session->metadata_cache->data = tmp_cache_realloc;
c099397a 1674 }
d83004aa
JD
1675 memcpy(session->metadata_cache->data +
1676 session->metadata_cache->metadata_written,
1677 str, len);
1678 session->metadata_cache->metadata_written += len;
92d9f5e6 1679 mutex_unlock(&session->metadata_cache->lock);
c099397a 1680 kfree(str);
d83004aa
JD
1681
1682 list_for_each_entry(stream, &session->metadata_cache->metadata_stream, list)
1683 wake_up_interruptible(&stream->read_wait);
1684
1685 return 0;
1686
1687err:
92d9f5e6 1688 mutex_unlock(&session->metadata_cache->lock);
d83004aa
JD
1689 kfree(str);
1690 return -ENOMEM;
c099397a
MD
1691}
1692
f513b2bf
MD
1693static
1694int print_tabs(struct lttng_session *session, size_t nesting)
1695{
1696 size_t i;
1697
1698 for (i = 0; i < nesting; i++) {
1699 int ret;
1700
1701 ret = lttng_metadata_printf(session, " ");
1702 if (ret) {
1703 return ret;
1704 }
1705 }
1706 return 0;
1707}
1708
1709/*
1710 * Must be called with sessions_mutex held.
1711 */
1712static
1713int _lttng_struct_type_statedump(struct lttng_session *session,
1714 const struct lttng_type *type,
1715 size_t nesting)
1716{
1717 int ret;
1718 uint32_t i, nr_fields;
1719
1720 ret = print_tabs(session, nesting);
1721 if (ret)
1722 return ret;
1723 ret = lttng_metadata_printf(session,
1724 "struct {\n");
1725 if (ret)
1726 return ret;
1727 nr_fields = type->u._struct.nr_fields;
1728 for (i = 0; i < nr_fields; i++) {
1729 const struct lttng_event_field *iter_field;
1730
1731 iter_field = &type->u._struct.fields[i];
1732 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1733 if (ret)
1734 return ret;
1735 }
1736 ret = print_tabs(session, nesting);
1737 if (ret)
1738 return ret;
1739 ret = lttng_metadata_printf(session,
1740 "}");
1741 return ret;
1742}
1743
1744/*
1745 * Must be called with sessions_mutex held.
1746 */
1747static
1748int _lttng_struct_statedump(struct lttng_session *session,
1749 const struct lttng_event_field *field,
1750 size_t nesting)
1751{
1752 int ret;
1753
1754 ret = _lttng_struct_type_statedump(session,
1755 &field->type, nesting);
1756 if (ret)
1757 return ret;
1758 ret = lttng_metadata_printf(session,
1759 "_%s;\n",
1760 field->name);
1761 return ret;
1762}
1763
65c85aa6
MD
1764/*
1765 * Must be called with sessions_mutex held.
1766 */
1767static
1768int _lttng_variant_type_statedump(struct lttng_session *session,
1769 const struct lttng_type *type,
1770 size_t nesting)
1771{
1772 int ret;
1773 uint32_t i, nr_choices;
1774
1775 ret = print_tabs(session, nesting);
1776 if (ret)
1777 return ret;
1778 ret = lttng_metadata_printf(session,
1779 "variant <_%s> {\n",
1780 type->u.variant.tag_name);
1781 if (ret)
1782 return ret;
1783 nr_choices = type->u.variant.nr_choices;
1784 for (i = 0; i < nr_choices; i++) {
1785 const struct lttng_event_field *iter_field;
1786
1787 iter_field = &type->u.variant.choices[i];
1788 ret = _lttng_field_statedump(session, iter_field, nesting + 1);
1789 if (ret)
1790 return ret;
1791 }
1792 ret = print_tabs(session, nesting);
1793 if (ret)
1794 return ret;
1795 ret = lttng_metadata_printf(session,
1796 "}");
1797 return ret;
1798}
1799
1800/*
1801 * Must be called with sessions_mutex held.
1802 */
1803static
1804int _lttng_variant_statedump(struct lttng_session *session,
1805 const struct lttng_event_field *field,
1806 size_t nesting)
1807{
1808 int ret;
1809
1810 ret = _lttng_variant_type_statedump(session,
1811 &field->type, nesting);
1812 if (ret)
1813 return ret;
1814 ret = lttng_metadata_printf(session,
1815 "_%s;\n",
1816 field->name);
1817 return ret;
1818}
1819
f513b2bf
MD
1820/*
1821 * Must be called with sessions_mutex held.
1822 */
1823static
1824int _lttng_array_compound_statedump(struct lttng_session *session,
1825 const struct lttng_event_field *field,
1826 size_t nesting)
1827{
1828 int ret;
1829 const struct lttng_type *elem_type;
1830
65c85aa6 1831 /* Only array of structures and variants are currently supported. */
f513b2bf
MD
1832 elem_type = field->type.u.array_compound.elem_type;
1833 switch (elem_type->atype) {
1834 case atype_struct:
1835 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1836 if (ret)
1837 return ret;
1838 break;
65c85aa6
MD
1839 case atype_variant:
1840 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1841 if (ret)
1842 return ret;
1843 break;
f513b2bf
MD
1844 default:
1845 return -EINVAL;
1846 }
1847 ret = lttng_metadata_printf(session,
1848 " _%s[%u];\n",
1849 field->name,
1850 field->type.u.array_compound.length);
1851 return ret;
1852}
1853
1854/*
1855 * Must be called with sessions_mutex held.
1856 */
1857static
1858int _lttng_sequence_compound_statedump(struct lttng_session *session,
1859 const struct lttng_event_field *field,
1860 size_t nesting)
1861{
1862 int ret;
1863 const char *length_name;
1864 const struct lttng_type *elem_type;
1865
1866 length_name = field->type.u.sequence_compound.length_name;
1867
65c85aa6 1868 /* Only array of structures and variants are currently supported. */
f513b2bf
MD
1869 elem_type = field->type.u.sequence_compound.elem_type;
1870 switch (elem_type->atype) {
1871 case atype_struct:
1872 ret = _lttng_struct_type_statedump(session, elem_type, nesting);
1873 if (ret)
1874 return ret;
1875 break;
65c85aa6
MD
1876 case atype_variant:
1877 ret = _lttng_variant_type_statedump(session, elem_type, nesting);
1878 if (ret)
1879 return ret;
1880 break;
f513b2bf
MD
1881 default:
1882 return -EINVAL;
1883 }
1884 ret = lttng_metadata_printf(session,
1885 " _%s[ _%s ];\n",
1886 field->name,
1887 length_name);
1888 return ret;
1889}
1890
141ddf28
MD
1891/*
1892 * Must be called with sessions_mutex held.
1893 */
1894static
1895int _lttng_enum_statedump(struct lttng_session *session,
1896 const struct lttng_event_field *field,
1897 size_t nesting)
1898{
1899 const struct lttng_enum_desc *enum_desc;
1900 const struct lttng_integer_type *container_type;
1901 int ret;
1902 unsigned int i, nr_entries;
1903
1904 enum_desc = field->type.u.basic.enumeration.desc;
1905 container_type = &field->type.u.basic.enumeration.container_type;
1906 nr_entries = enum_desc->nr_entries;
1907
1908 ret = print_tabs(session, nesting);
1909 if (ret)
1910 goto end;
1911 ret = lttng_metadata_printf(session,
578e68ec 1912 "enum : integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } {\n",
141ddf28
MD
1913 container_type->size,
1914 container_type->alignment,
1915 container_type->signedness,
1916 (container_type->encoding == lttng_encode_none)
1917 ? "none"
1918 : (container_type->encoding == lttng_encode_UTF8)
1919 ? "UTF8"
1920 : "ASCII",
578e68ec
GB
1921 container_type->base,
1922#if __BYTE_ORDER == __BIG_ENDIAN
1923 container_type->reverse_byte_order ? " byte_order = le;" : ""
1924#else
1925 container_type->reverse_byte_order ? " byte_order = be;" : ""
1926#endif
1927 );
141ddf28
MD
1928 if (ret)
1929 goto end;
1930 /* Dump all entries */
1931 for (i = 0; i < nr_entries; i++) {
1932 const struct lttng_enum_entry *entry = &enum_desc->entries[i];
1933 int j, len;
1934
1935 ret = print_tabs(session, nesting + 1);
1936 if (ret)
1937 goto end;
1938 ret = lttng_metadata_printf(session,
1939 "\"");
1940 if (ret)
1941 goto end;
1942 len = strlen(entry->string);
1943 /* Escape the character '"' */
1944 for (j = 0; j < len; j++) {
1945 char c = entry->string[j];
1946
1947 switch (c) {
1948 case '"':
1949 ret = lttng_metadata_printf(session,
1950 "\\\"");
1951 break;
1952 case '\\':
1953 ret = lttng_metadata_printf(session,
1954 "\\\\");
1955 break;
1956 default:
1957 ret = lttng_metadata_printf(session,
1958 "%c", c);
1959 break;
1960 }
1961 if (ret)
1962 goto end;
1963 }
08ad1061
PP
1964 ret = lttng_metadata_printf(session, "\"");
1965 if (ret)
1966 goto end;
1967
1968 if (entry->options.is_auto) {
1969 ret = lttng_metadata_printf(session, ",\n");
1970 if (ret)
1971 goto end;
141ddf28 1972 } else {
7c559490
MD
1973 ret = lttng_metadata_printf(session,
1974 " = ");
1975 if (ret)
1976 goto end;
1977 if (entry->start.signedness)
141ddf28 1978 ret = lttng_metadata_printf(session,
7c559490
MD
1979 "%lld", (long long) entry->start.value);
1980 else
1981 ret = lttng_metadata_printf(session,
1982 "%llu", entry->start.value);
1983 if (ret)
1984 goto end;
1985 if (entry->start.signedness == entry->end.signedness &&
1986 entry->start.value
1987 == entry->end.value) {
141ddf28 1988 ret = lttng_metadata_printf(session,
7c559490
MD
1989 ",\n");
1990 } else {
1991 if (entry->end.signedness) {
1992 ret = lttng_metadata_printf(session,
1993 " ... %lld,\n",
1994 (long long) entry->end.value);
1995 } else {
1996 ret = lttng_metadata_printf(session,
1997 " ... %llu,\n",
1998 entry->end.value);
1999 }
141ddf28 2000 }
7c559490
MD
2001 if (ret)
2002 goto end;
141ddf28 2003 }
141ddf28
MD
2004 }
2005 ret = print_tabs(session, nesting);
2006 if (ret)
2007 goto end;
2008 ret = lttng_metadata_printf(session, "} _%s;\n",
2009 field->name);
2010end:
2011 return ret;
2012}
2013
d83004aa
JD
2014/*
2015 * Must be called with sessions_mutex held.
2016 */
c099397a 2017static
a90917c3 2018int _lttng_field_statedump(struct lttng_session *session,
f513b2bf
MD
2019 const struct lttng_event_field *field,
2020 size_t nesting)
c099397a 2021{
c099397a 2022 int ret = 0;
c099397a 2023
8070f5c0
MD
2024 switch (field->type.atype) {
2025 case atype_integer:
f513b2bf
MD
2026 ret = print_tabs(session, nesting);
2027 if (ret)
2028 return ret;
8070f5c0 2029 ret = lttng_metadata_printf(session,
f513b2bf 2030 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s;\n",
8070f5c0
MD
2031 field->type.u.basic.integer.size,
2032 field->type.u.basic.integer.alignment,
2033 field->type.u.basic.integer.signedness,
2034 (field->type.u.basic.integer.encoding == lttng_encode_none)
2035 ? "none"
2036 : (field->type.u.basic.integer.encoding == lttng_encode_UTF8)
2037 ? "UTF8"
2038 : "ASCII",
2039 field->type.u.basic.integer.base,
5214fa50 2040#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2041 field->type.u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
c099397a 2042#else
8070f5c0 2043 field->type.u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
c099397a 2044#endif
8070f5c0
MD
2045 field->name);
2046 break;
2047 case atype_enum:
141ddf28 2048 ret = _lttng_enum_statedump(session, field, nesting);
8070f5c0
MD
2049 break;
2050 case atype_array:
2051 {
2052 const struct lttng_basic_type *elem_type;
2053
2054 elem_type = &field->type.u.array.elem_type;
43803cf2 2055 if (field->type.u.array.elem_alignment) {
f513b2bf
MD
2056 ret = print_tabs(session, nesting);
2057 if (ret)
2058 return ret;
43803cf2 2059 ret = lttng_metadata_printf(session,
f513b2bf 2060 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2061 field->type.u.array.elem_alignment * CHAR_BIT,
2062 field->name);
2063 if (ret)
2064 return ret;
2065 }
f513b2bf
MD
2066 ret = print_tabs(session, nesting);
2067 if (ret)
2068 return ret;
8070f5c0 2069 ret = lttng_metadata_printf(session,
f513b2bf 2070 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[%u];\n",
8070f5c0
MD
2071 elem_type->u.basic.integer.size,
2072 elem_type->u.basic.integer.alignment,
2073 elem_type->u.basic.integer.signedness,
2074 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2075 ? "none"
2076 : (elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
2077 ? "UTF8"
2078 : "ASCII",
2079 elem_type->u.basic.integer.base,
5214fa50 2080#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2081 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2082#else
8070f5c0 2083 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2084#endif
8070f5c0
MD
2085 field->name, field->type.u.array.length);
2086 break;
2087 }
2088 case atype_sequence:
2089 {
2090 const struct lttng_basic_type *elem_type;
2091 const struct lttng_basic_type *length_type;
2092
2093 elem_type = &field->type.u.sequence.elem_type;
2094 length_type = &field->type.u.sequence.length_type;
f513b2bf
MD
2095 ret = print_tabs(session, nesting);
2096 if (ret)
2097 return ret;
8070f5c0 2098 ret = lttng_metadata_printf(session,
f513b2bf 2099 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } __%s_length;\n",
8070f5c0 2100 length_type->u.basic.integer.size,
32257272 2101 (unsigned int) length_type->u.basic.integer.alignment,
8070f5c0
MD
2102 length_type->u.basic.integer.signedness,
2103 (length_type->u.basic.integer.encoding == lttng_encode_none)
2104 ? "none"
0635cdd4 2105 : ((length_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2106 ? "UTF8"
0635cdd4 2107 : "ASCII"),
8070f5c0 2108 length_type->u.basic.integer.base,
5214fa50 2109#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2110 length_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
e0a7a7c4 2111#else
0635cdd4 2112 length_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
e0a7a7c4 2113#endif
27d2368e
MD
2114 field->name);
2115 if (ret)
2116 return ret;
2117
43803cf2 2118 if (field->type.u.sequence.elem_alignment) {
f513b2bf
MD
2119 ret = print_tabs(session, nesting);
2120 if (ret)
2121 return ret;
43803cf2 2122 ret = lttng_metadata_printf(session,
f513b2bf 2123 "struct { } align(%u) _%s_padding;\n",
43803cf2
MD
2124 field->type.u.sequence.elem_alignment * CHAR_BIT,
2125 field->name);
2126 if (ret)
2127 return ret;
2128 }
f513b2bf
MD
2129 ret = print_tabs(session, nesting);
2130 if (ret)
2131 return ret;
27d2368e 2132 ret = lttng_metadata_printf(session,
f513b2bf 2133 "integer { size = %u; align = %u; signed = %u; encoding = %s; base = %u;%s } _%s[ __%s_length ];\n",
8070f5c0 2134 elem_type->u.basic.integer.size,
32257272 2135 (unsigned int) elem_type->u.basic.integer.alignment,
8070f5c0
MD
2136 elem_type->u.basic.integer.signedness,
2137 (elem_type->u.basic.integer.encoding == lttng_encode_none)
2138 ? "none"
0635cdd4 2139 : ((elem_type->u.basic.integer.encoding == lttng_encode_UTF8)
8070f5c0 2140 ? "UTF8"
0635cdd4 2141 : "ASCII"),
8070f5c0 2142 elem_type->u.basic.integer.base,
5214fa50 2143#if __BYTE_ORDER == __BIG_ENDIAN
8070f5c0 2144 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = le;" : "",
8fa75f34 2145#else
8070f5c0 2146 elem_type->u.basic.integer.reverse_byte_order ? " byte_order = be;" : "",
8fa75f34 2147#endif
8070f5c0 2148 field->name,
27d2368e 2149 field->name);
8070f5c0
MD
2150 break;
2151 }
c099397a 2152
8070f5c0
MD
2153 case atype_string:
2154 /* Default encoding is UTF8 */
f513b2bf
MD
2155 ret = print_tabs(session, nesting);
2156 if (ret)
2157 return ret;
8070f5c0 2158 ret = lttng_metadata_printf(session,
f513b2bf 2159 "string%s _%s;\n",
8070f5c0
MD
2160 field->type.u.basic.string.encoding == lttng_encode_ASCII ?
2161 " { encoding = ASCII; }" : "",
2162 field->name);
2163 break;
f513b2bf
MD
2164 case atype_struct:
2165 ret = _lttng_struct_statedump(session, field, nesting);
2166 break;
2167 case atype_array_compound:
2168 ret = _lttng_array_compound_statedump(session, field, nesting);
2169 break;
2170 case atype_sequence_compound:
2171 ret = _lttng_sequence_compound_statedump(session, field, nesting);
2172 break;
65c85aa6
MD
2173 case atype_variant:
2174 ret = _lttng_variant_statedump(session, field, nesting);
2175 break;
f513b2bf 2176
8070f5c0
MD
2177 default:
2178 WARN_ON_ONCE(1);
2179 return -EINVAL;
2180 }
2181 return ret;
2182}
2183
2184static
a90917c3 2185int _lttng_context_metadata_statedump(struct lttng_session *session,
8070f5c0
MD
2186 struct lttng_ctx *ctx)
2187{
2188 int ret = 0;
2189 int i;
2190
2191 if (!ctx)
2192 return 0;
2193 for (i = 0; i < ctx->nr_fields; i++) {
2194 const struct lttng_ctx_field *field = &ctx->fields[i];
2195
f513b2bf 2196 ret = _lttng_field_statedump(session, &field->event_field, 2);
8070f5c0
MD
2197 if (ret)
2198 return ret;
2199 }
2200 return ret;
2201}
2202
2203static
a90917c3
MD
2204int _lttng_fields_metadata_statedump(struct lttng_session *session,
2205 struct lttng_event *event)
8070f5c0
MD
2206{
2207 const struct lttng_event_desc *desc = event->desc;
2208 int ret = 0;
2209 int i;
2210
2211 for (i = 0; i < desc->nr_fields; i++) {
2212 const struct lttng_event_field *field = &desc->fields[i];
2213
f513b2bf 2214 ret = _lttng_field_statedump(session, field, 2);
8070f5c0
MD
2215 if (ret)
2216 return ret;
c099397a
MD
2217 }
2218 return ret;
2219}
2220
d83004aa
JD
2221/*
2222 * Must be called with sessions_mutex held.
2223 */
c099397a 2224static
a90917c3
MD
2225int _lttng_event_metadata_statedump(struct lttng_session *session,
2226 struct lttng_channel *chan,
2227 struct lttng_event *event)
c099397a
MD
2228{
2229 int ret = 0;
2230
2231 if (event->metadata_dumped || !ACCESS_ONCE(session->active))
2232 return 0;
d83004aa 2233 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2234 return 0;
2235
2236 ret = lttng_metadata_printf(session,
2237 "event {\n"
ae734547 2238 " name = \"%s\";\n"
c099397a 2239 " id = %u;\n"
b9074a1b 2240 " stream_id = %u;\n",
c099397a
MD
2241 event->desc->name,
2242 event->id,
2243 event->chan->id);
2244 if (ret)
2245 goto end;
2246
b9074a1b
MD
2247 if (event->ctx) {
2248 ret = lttng_metadata_printf(session,
2249 " context := struct {\n");
2250 if (ret)
2251 goto end;
2252 }
a90917c3 2253 ret = _lttng_context_metadata_statedump(session, event->ctx);
8070f5c0
MD
2254 if (ret)
2255 goto end;
b9074a1b
MD
2256 if (event->ctx) {
2257 ret = lttng_metadata_printf(session,
2258 " };\n");
2259 if (ret)
2260 goto end;
2261 }
8070f5c0
MD
2262
2263 ret = lttng_metadata_printf(session,
8070f5c0
MD
2264 " fields := struct {\n"
2265 );
2266 if (ret)
2267 goto end;
2268
a90917c3 2269 ret = _lttng_fields_metadata_statedump(session, event);
c099397a
MD
2270 if (ret)
2271 goto end;
2272
2273 /*
2274 * LTTng space reservation can only reserve multiples of the
2275 * byte size.
2276 */
2277 ret = lttng_metadata_printf(session,
9115fbdc
MD
2278 " };\n"
2279 "};\n\n");
c099397a
MD
2280 if (ret)
2281 goto end;
2282
c099397a
MD
2283 event->metadata_dumped = 1;
2284end:
2285 return ret;
2286
2287}
2288
d83004aa
JD
2289/*
2290 * Must be called with sessions_mutex held.
2291 */
c099397a 2292static
a90917c3
MD
2293int _lttng_channel_metadata_statedump(struct lttng_session *session,
2294 struct lttng_channel *chan)
c099397a
MD
2295{
2296 int ret = 0;
2297
2298 if (chan->metadata_dumped || !ACCESS_ONCE(session->active))
2299 return 0;
d83004aa
JD
2300
2301 if (chan->channel_type == METADATA_CHANNEL)
c099397a
MD
2302 return 0;
2303
2304 WARN_ON_ONCE(!chan->header_type);
2305 ret = lttng_metadata_printf(session,
2306 "stream {\n"
2307 " id = %u;\n"
9115fbdc 2308 " event.header := %s;\n"
b9074a1b 2309 " packet.context := struct packet_context;\n",
c099397a
MD
2310 chan->id,
2311 chan->header_type == 1 ? "struct event_header_compact" :
2312 "struct event_header_large");
2313 if (ret)
2314 goto end;
2315
b9074a1b
MD
2316 if (chan->ctx) {
2317 ret = lttng_metadata_printf(session,
2318 " event.context := struct {\n");
2319 if (ret)
2320 goto end;
2321 }
a90917c3 2322 ret = _lttng_context_metadata_statedump(session, chan->ctx);
8070f5c0
MD
2323 if (ret)
2324 goto end;
b9074a1b
MD
2325 if (chan->ctx) {
2326 ret = lttng_metadata_printf(session,
2327 " };\n");
2328 if (ret)
2329 goto end;
2330 }
8070f5c0
MD
2331
2332 ret = lttng_metadata_printf(session,
b9074a1b 2333 "};\n\n");
8070f5c0 2334
c099397a
MD
2335 chan->metadata_dumped = 1;
2336end:
2337 return ret;
2338}
2339
d83004aa
JD
2340/*
2341 * Must be called with sessions_mutex held.
2342 */
9115fbdc 2343static
a90917c3 2344int _lttng_stream_packet_context_declare(struct lttng_session *session)
9115fbdc
MD
2345{
2346 return lttng_metadata_printf(session,
2347 "struct packet_context {\n"
a3ccff4f
MD
2348 " uint64_clock_monotonic_t timestamp_begin;\n"
2349 " uint64_clock_monotonic_t timestamp_end;\n"
576ca06a
MD
2350 " uint64_t content_size;\n"
2351 " uint64_t packet_size;\n"
5b3cf4f9 2352 " uint64_t packet_seq_num;\n"
a9afe705 2353 " unsigned long events_discarded;\n"
9115fbdc 2354 " uint32_t cpu_id;\n"
c6dfdf6f 2355 "};\n\n"
9115fbdc
MD
2356 );
2357}
2358
2359/*
2360 * Compact header:
2361 * id: range: 0 - 30.
2362 * id 31 is reserved to indicate an extended header.
2363 *
2364 * Large header:
2365 * id: range: 0 - 65534.
2366 * id 65535 is reserved to indicate an extended header.
d83004aa
JD
2367 *
2368 * Must be called with sessions_mutex held.
9115fbdc
MD
2369 */
2370static
a90917c3 2371int _lttng_event_header_declare(struct lttng_session *session)
9115fbdc
MD
2372{
2373 return lttng_metadata_printf(session,
2374 "struct event_header_compact {\n"
2375 " enum : uint5_t { compact = 0 ... 30, extended = 31 } id;\n"
2376 " variant <id> {\n"
2377 " struct {\n"
a3ccff4f 2378 " uint27_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2379 " } compact;\n"
2380 " struct {\n"
2381 " uint32_t id;\n"
a3ccff4f 2382 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2383 " } extended;\n"
2384 " } v;\n"
2385 "} align(%u);\n"
2386 "\n"
2387 "struct event_header_large {\n"
2388 " enum : uint16_t { compact = 0 ... 65534, extended = 65535 } id;\n"
2389 " variant <id> {\n"
2390 " struct {\n"
a3ccff4f 2391 " uint32_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2392 " } compact;\n"
2393 " struct {\n"
2394 " uint32_t id;\n"
a3ccff4f 2395 " uint64_clock_monotonic_t timestamp;\n"
9115fbdc
MD
2396 " } extended;\n"
2397 " } v;\n"
2398 "} align(%u);\n\n",
a90917c3
MD
2399 lttng_alignof(uint32_t) * CHAR_BIT,
2400 lttng_alignof(uint16_t) * CHAR_BIT
9115fbdc
MD
2401 );
2402}
2403
a3ccff4f
MD
2404 /*
2405 * Approximation of NTP time of day to clock monotonic correlation,
2406 * taken at start of trace.
2407 * Yes, this is only an approximation. Yes, we can (and will) do better
2408 * in future versions.
5283350b
MD
2409 * This function may return a negative offset. It may happen if the
2410 * system sets the REALTIME clock to 0 after boot.
a3ccff4f
MD
2411 */
2412static
8967b2a3 2413int64_t measure_clock_offset(void)
a3ccff4f 2414{
dcb93448 2415 uint64_t monotonic_avg, monotonic[2], realtime;
2754583e 2416 uint64_t tcf = trace_clock_freq();
dcb93448 2417 int64_t offset;
a3ccff4f
MD
2418 struct timespec rts = { 0, 0 };
2419 unsigned long flags;
2420
2421 /* Disable interrupts to increase correlation precision. */
2422 local_irq_save(flags);
2423 monotonic[0] = trace_clock_read64();
216b6baa 2424 getnstimeofday(&rts);
a3ccff4f
MD
2425 monotonic[1] = trace_clock_read64();
2426 local_irq_restore(flags);
2427
dcb93448 2428 monotonic_avg = (monotonic[0] + monotonic[1]) >> 1;
2754583e
MD
2429 realtime = (uint64_t) rts.tv_sec * tcf;
2430 if (tcf == NSEC_PER_SEC) {
2431 realtime += rts.tv_nsec;
2432 } else {
2433 uint64_t n = rts.tv_nsec * tcf;
2434
2435 do_div(n, NSEC_PER_SEC);
2436 realtime += n;
2437 }
dcb93448 2438 offset = (int64_t) realtime - monotonic_avg;
a3ccff4f
MD
2439 return offset;
2440}
2441
c099397a
MD
2442/*
2443 * Output metadata into this session's metadata buffers.
d83004aa 2444 * Must be called with sessions_mutex held.
c099397a
MD
2445 */
2446static
a90917c3 2447int _lttng_session_metadata_statedump(struct lttng_session *session)
c099397a 2448{
30bdb6e4 2449 unsigned char *uuid_c = session->uuid.b;
a82c63f1 2450 unsigned char uuid_s[37], clock_uuid_s[BOOT_ID_LEN];
a90917c3
MD
2451 struct lttng_channel *chan;
2452 struct lttng_event *event;
c099397a
MD
2453 int ret = 0;
2454
2455 if (!ACCESS_ONCE(session->active))
2456 return 0;
2457 if (session->metadata_dumped)
2458 goto skip_session;
c099397a 2459
d793d5e1 2460 snprintf(uuid_s, sizeof(uuid_s),
30bdb6e4
MD
2461 "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",
2462 uuid_c[0], uuid_c[1], uuid_c[2], uuid_c[3],
2463 uuid_c[4], uuid_c[5], uuid_c[6], uuid_c[7],
2464 uuid_c[8], uuid_c[9], uuid_c[10], uuid_c[11],
2465 uuid_c[12], uuid_c[13], uuid_c[14], uuid_c[15]);
d793d5e1
MD
2466
2467 ret = lttng_metadata_printf(session,
9115fbdc
MD
2468 "typealias integer { size = 8; align = %u; signed = false; } := uint8_t;\n"
2469 "typealias integer { size = 16; align = %u; signed = false; } := uint16_t;\n"
2470 "typealias integer { size = 32; align = %u; signed = false; } := uint32_t;\n"
2471 "typealias integer { size = 64; align = %u; signed = false; } := uint64_t;\n"
a9afe705 2472 "typealias integer { size = %u; align = %u; signed = false; } := unsigned long;\n"
9115fbdc
MD
2473 "typealias integer { size = 5; align = 1; signed = false; } := uint5_t;\n"
2474 "typealias integer { size = 27; align = 1; signed = false; } := uint27_t;\n"
d793d5e1
MD
2475 "\n"
2476 "trace {\n"
2477 " major = %u;\n"
2478 " minor = %u;\n"
30bdb6e4 2479 " uuid = \"%s\";\n"
d793d5e1
MD
2480 " byte_order = %s;\n"
2481 " packet.header := struct {\n"
2482 " uint32_t magic;\n"
1ec3f75a 2483 " uint8_t uuid[16];\n"
d793d5e1 2484 " uint32_t stream_id;\n"
5594698f 2485 " uint64_t stream_instance_id;\n"
0eb25f58 2486 " };\n"
d793d5e1 2487 "};\n\n",
a90917c3
MD
2488 lttng_alignof(uint8_t) * CHAR_BIT,
2489 lttng_alignof(uint16_t) * CHAR_BIT,
2490 lttng_alignof(uint32_t) * CHAR_BIT,
2491 lttng_alignof(uint64_t) * CHAR_BIT,
a9afe705
MD
2492 sizeof(unsigned long) * CHAR_BIT,
2493 lttng_alignof(unsigned long) * CHAR_BIT,
c6c9e10f
MD
2494 CTF_SPEC_MAJOR,
2495 CTF_SPEC_MINOR,
d793d5e1 2496 uuid_s,
5214fa50 2497#if __BYTE_ORDER == __BIG_ENDIAN
d793d5e1
MD
2498 "be"
2499#else
2500 "le"
2501#endif
2502 );
2503 if (ret)
2504 goto end;
2505
99dc9597
MD
2506 ret = lttng_metadata_printf(session,
2507 "env {\n"
a6058143 2508 " hostname = \"%s\";\n"
c6c9e10f 2509 " domain = \"kernel\";\n"
99dc9597 2510 " sysname = \"%s\";\n"
c6c9e10f
MD
2511 " kernel_release = \"%s\";\n"
2512 " kernel_version = \"%s\";\n"
2513 " tracer_name = \"lttng-modules\";\n"
2514 " tracer_major = %d;\n"
2515 " tracer_minor = %d;\n"
2516 " tracer_patchlevel = %d;\n"
99dc9597 2517 "};\n\n",
3d0d43db 2518 current->nsproxy->uts_ns->name.nodename,
99dc9597
MD
2519 utsname()->sysname,
2520 utsname()->release,
c6c9e10f
MD
2521 utsname()->version,
2522 LTTNG_MODULES_MAJOR_VERSION,
2523 LTTNG_MODULES_MINOR_VERSION,
2524 LTTNG_MODULES_PATCHLEVEL_VERSION
99dc9597
MD
2525 );
2526 if (ret)
2527 goto end;
2528
a3ccff4f
MD
2529 ret = lttng_metadata_printf(session,
2530 "clock {\n"
2754583e
MD
2531 " name = \"%s\";\n",
2532 trace_clock_name()
a82c63f1
MD
2533 );
2534 if (ret)
2535 goto end;
2536
2537 if (!trace_clock_uuid(clock_uuid_s)) {
2538 ret = lttng_metadata_printf(session,
7c27cb17 2539 " uuid = \"%s\";\n",
a82c63f1
MD
2540 clock_uuid_s
2541 );
2542 if (ret)
2543 goto end;
2544 }
2545
2546 ret = lttng_metadata_printf(session,
2754583e 2547 " description = \"%s\";\n"
a3ccff4f
MD
2548 " freq = %llu; /* Frequency, in Hz */\n"
2549 " /* clock value offset from Epoch is: offset * (1/freq) */\n"
8967b2a3 2550 " offset = %lld;\n"
a3ccff4f 2551 "};\n\n",
2754583e 2552 trace_clock_description(),
a3ccff4f 2553 (unsigned long long) trace_clock_freq(),
8967b2a3 2554 (long long) measure_clock_offset()
a3ccff4f
MD
2555 );
2556 if (ret)
2557 goto end;
2558
2559 ret = lttng_metadata_printf(session,
2560 "typealias integer {\n"
2561 " size = 27; align = 1; signed = false;\n"
2754583e 2562 " map = clock.%s.value;\n"
a3ccff4f
MD
2563 "} := uint27_clock_monotonic_t;\n"
2564 "\n"
2565 "typealias integer {\n"
2566 " size = 32; align = %u; signed = false;\n"
2754583e 2567 " map = clock.%s.value;\n"
a3ccff4f
MD
2568 "} := uint32_clock_monotonic_t;\n"
2569 "\n"
2570 "typealias integer {\n"
2571 " size = 64; align = %u; signed = false;\n"
2754583e 2572 " map = clock.%s.value;\n"
a3ccff4f 2573 "} := uint64_clock_monotonic_t;\n\n",
2754583e 2574 trace_clock_name(),
a3ccff4f 2575 lttng_alignof(uint32_t) * CHAR_BIT,
2754583e
MD
2576 trace_clock_name(),
2577 lttng_alignof(uint64_t) * CHAR_BIT,
2578 trace_clock_name()
a3ccff4f
MD
2579 );
2580 if (ret)
2581 goto end;
2582
a90917c3 2583 ret = _lttng_stream_packet_context_declare(session);
9115fbdc
MD
2584 if (ret)
2585 goto end;
2586
a90917c3 2587 ret = _lttng_event_header_declare(session);
9115fbdc
MD
2588 if (ret)
2589 goto end;
2590
c099397a
MD
2591skip_session:
2592 list_for_each_entry(chan, &session->chan, list) {
a90917c3 2593 ret = _lttng_channel_metadata_statedump(session, chan);
c099397a
MD
2594 if (ret)
2595 goto end;
2596 }
2597
2598 list_for_each_entry(event, &session->events, list) {
a90917c3 2599 ret = _lttng_event_metadata_statedump(session, event->chan, event);
c099397a
MD
2600 if (ret)
2601 goto end;
2602 }
2603 session->metadata_dumped = 1;
2604end:
2605 return ret;
2606}
2607
c0e31d2e 2608/**
a90917c3 2609 * lttng_transport_register - LTT transport registration
c0e31d2e
MD
2610 * @transport: transport structure
2611 *
2612 * Registers a transport which can be used as output to extract the data out of
2613 * LTTng. The module calling this registration function must ensure that no
2614 * trap-inducing code will be executed by the transport functions. E.g.
2615 * vmalloc_sync_all() must be called between a vmalloc and the moment the memory
2616 * is made visible to the transport function. This registration acts as a
2617 * vmalloc_sync_all. Therefore, only if the module allocates virtual memory
2618 * after its registration must it synchronize the TLBs.
2619 */
a90917c3 2620void lttng_transport_register(struct lttng_transport *transport)
c0e31d2e
MD
2621{
2622 /*
2623 * Make sure no page fault can be triggered by the module about to be
2624 * registered. We deal with this here so we don't have to call
2625 * vmalloc_sync_all() in each module's init.
2626 */
6d2a620c 2627 wrapper_vmalloc_sync_all();
c0e31d2e
MD
2628
2629 mutex_lock(&sessions_mutex);
a90917c3 2630 list_add_tail(&transport->node, &lttng_transport_list);
c0e31d2e
MD
2631 mutex_unlock(&sessions_mutex);
2632}
a90917c3 2633EXPORT_SYMBOL_GPL(lttng_transport_register);
c0e31d2e
MD
2634
2635/**
a90917c3 2636 * lttng_transport_unregister - LTT transport unregistration
c0e31d2e
MD
2637 * @transport: transport structure
2638 */
a90917c3 2639void lttng_transport_unregister(struct lttng_transport *transport)
c0e31d2e
MD
2640{
2641 mutex_lock(&sessions_mutex);
2642 list_del(&transport->node);
2643 mutex_unlock(&sessions_mutex);
2644}
a90917c3 2645EXPORT_SYMBOL_GPL(lttng_transport_unregister);
c0e31d2e 2646
1e367326
MD
2647#if (defined(CONFIG_HOTPLUG_CPU) && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0)))
2648
2649enum cpuhp_state lttng_hp_prepare;
2650enum cpuhp_state lttng_hp_online;
2651
2652static int lttng_hotplug_prepare(unsigned int cpu, struct hlist_node *node)
2653{
2654 struct lttng_cpuhp_node *lttng_node;
2655
2656 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2657 switch (lttng_node->component) {
2658 case LTTNG_RING_BUFFER_FRONTEND:
2659 return 0;
2660 case LTTNG_RING_BUFFER_BACKEND:
2661 return lttng_cpuhp_rb_backend_prepare(cpu, lttng_node);
2662 case LTTNG_RING_BUFFER_ITER:
2663 return 0;
2664 case LTTNG_CONTEXT_PERF_COUNTERS:
2665 return 0;
2666 default:
2667 return -EINVAL;
2668 }
2669}
2670
2671static int lttng_hotplug_dead(unsigned int cpu, struct hlist_node *node)
2672{
2673 struct lttng_cpuhp_node *lttng_node;
2674
2675 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2676 switch (lttng_node->component) {
2677 case LTTNG_RING_BUFFER_FRONTEND:
2678 return lttng_cpuhp_rb_frontend_dead(cpu, lttng_node);
2679 case LTTNG_RING_BUFFER_BACKEND:
2680 return 0;
2681 case LTTNG_RING_BUFFER_ITER:
2682 return 0;
2683 case LTTNG_CONTEXT_PERF_COUNTERS:
2684 return lttng_cpuhp_perf_counter_dead(cpu, lttng_node);
2685 default:
2686 return -EINVAL;
2687 }
2688}
2689
2690static int lttng_hotplug_online(unsigned int cpu, struct hlist_node *node)
2691{
2692 struct lttng_cpuhp_node *lttng_node;
2693
2694 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2695 switch (lttng_node->component) {
2696 case LTTNG_RING_BUFFER_FRONTEND:
2697 return lttng_cpuhp_rb_frontend_online(cpu, lttng_node);
2698 case LTTNG_RING_BUFFER_BACKEND:
2699 return 0;
2700 case LTTNG_RING_BUFFER_ITER:
2701 return lttng_cpuhp_rb_iter_online(cpu, lttng_node);
2702 case LTTNG_CONTEXT_PERF_COUNTERS:
2703 return lttng_cpuhp_perf_counter_online(cpu, lttng_node);
2704 default:
2705 return -EINVAL;
2706 }
2707}
2708
2709static int lttng_hotplug_offline(unsigned int cpu, struct hlist_node *node)
2710{
2711 struct lttng_cpuhp_node *lttng_node;
2712
2713 lttng_node = container_of(node, struct lttng_cpuhp_node, node);
2714 switch (lttng_node->component) {
2715 case LTTNG_RING_BUFFER_FRONTEND:
2716 return lttng_cpuhp_rb_frontend_offline(cpu, lttng_node);
2717 case LTTNG_RING_BUFFER_BACKEND:
2718 return 0;
2719 case LTTNG_RING_BUFFER_ITER:
2720 return 0;
2721 case LTTNG_CONTEXT_PERF_COUNTERS:
2722 return 0;
2723 default:
2724 return -EINVAL;
2725 }
2726}
2727
2728static int __init lttng_init_cpu_hotplug(void)
2729{
2730 int ret;
2731
2732 ret = cpuhp_setup_state_multi(CPUHP_BP_PREPARE_DYN, "lttng:prepare",
2733 lttng_hotplug_prepare,
2734 lttng_hotplug_dead);
2735 if (ret < 0) {
2736 return ret;
2737 }
2738 lttng_hp_prepare = ret;
2739 lttng_rb_set_hp_prepare(ret);
2740
2741 ret = cpuhp_setup_state_multi(CPUHP_AP_ONLINE_DYN, "lttng:online",
2742 lttng_hotplug_online,
2743 lttng_hotplug_offline);
2744 if (ret < 0) {
2745 cpuhp_remove_multi_state(lttng_hp_prepare);
2746 lttng_hp_prepare = 0;
2747 return ret;
2748 }
2749 lttng_hp_online = ret;
2750 lttng_rb_set_hp_online(ret);
2751
2752 return 0;
2753}
2754
2755static void __exit lttng_exit_cpu_hotplug(void)
2756{
2757 lttng_rb_set_hp_online(0);
2758 cpuhp_remove_multi_state(lttng_hp_online);
2759 lttng_rb_set_hp_prepare(0);
2760 cpuhp_remove_multi_state(lttng_hp_prepare);
2761}
2762
2763#else /* #if (CONFIG_HOTPLUG_CPU && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))) */
2764static int lttng_init_cpu_hotplug(void)
2765{
2766 return 0;
2767}
2768static void lttng_exit_cpu_hotplug(void)
2769{
2770}
2771#endif /* #else #if (CONFIG_HOTPLUG_CPU && (LINUX_VERSION_CODE >= KERNEL_VERSION(4,10,0))) */
2772
2773
a90917c3 2774static int __init lttng_events_init(void)
4e3c1b9b 2775{
1c25284c
MD
2776 int ret;
2777
453b2495
JD
2778 ret = wrapper_lttng_fixup_sig(THIS_MODULE);
2779 if (ret)
2780 return ret;
389d7070 2781 ret = wrapper_get_pfnblock_flags_mask_init();
36561775
MD
2782 if (ret)
2783 return ret;
2784 ret = wrapper_get_pageblock_flags_mask_init();
114667d5
MD
2785 if (ret)
2786 return ret;
2787 ret = lttng_probes_init();
389d7070
MD
2788 if (ret)
2789 return ret;
07dfc1d0 2790 ret = lttng_context_init();
20591cf7
MD
2791 if (ret)
2792 return ret;
07dfc1d0
MD
2793 ret = lttng_tracepoint_init();
2794 if (ret)
2795 goto error_tp;
a90917c3 2796 event_cache = KMEM_CACHE(lttng_event, 0);
20591cf7
MD
2797 if (!event_cache) {
2798 ret = -ENOMEM;
2799 goto error_kmem;
2800 }
80996790 2801 ret = lttng_abi_init();
02119ee5
MD
2802 if (ret)
2803 goto error_abi;
0c956676
MD
2804 ret = lttng_logger_init();
2805 if (ret)
2806 goto error_logger;
1e367326
MD
2807 ret = lttng_init_cpu_hotplug();
2808 if (ret)
2809 goto error_hotplug;
489de24b
MJ
2810 printk(KERN_NOTICE "LTTng: Loaded modules v%s.%s.%s%s (%s)\n",
2811 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2812 __stringify(LTTNG_MODULES_MINOR_VERSION),
2813 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2814 LTTNG_MODULES_EXTRAVERSION,
2815 LTTNG_VERSION_NAME);
4e3c1b9b 2816 return 0;
0c956676 2817
1e367326
MD
2818error_hotplug:
2819 lttng_logger_exit();
0c956676
MD
2820error_logger:
2821 lttng_abi_exit();
02119ee5 2822error_abi:
1c25284c 2823 kmem_cache_destroy(event_cache);
20591cf7
MD
2824error_kmem:
2825 lttng_tracepoint_exit();
07dfc1d0
MD
2826error_tp:
2827 lttng_context_exit();
489de24b
MJ
2828 printk(KERN_NOTICE "LTTng: Failed to load modules v%s.%s.%s%s (%s)\n",
2829 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2830 __stringify(LTTNG_MODULES_MINOR_VERSION),
2831 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2832 LTTNG_MODULES_EXTRAVERSION,
2833 LTTNG_VERSION_NAME);
1c25284c 2834 return ret;
4e3c1b9b
MD
2835}
2836
a90917c3 2837module_init(lttng_events_init);
11b5a3c2 2838
a90917c3 2839static void __exit lttng_events_exit(void)
4e3c1b9b 2840{
a90917c3 2841 struct lttng_session *session, *tmpsession;
92e94819 2842
1e367326 2843 lttng_exit_cpu_hotplug();
0c956676 2844 lttng_logger_exit();
80996790 2845 lttng_abi_exit();
92e94819 2846 list_for_each_entry_safe(session, tmpsession, &sessions, list)
a90917c3 2847 lttng_session_destroy(session);
11b5a3c2 2848 kmem_cache_destroy(event_cache);
20591cf7 2849 lttng_tracepoint_exit();
07dfc1d0 2850 lttng_context_exit();
489de24b
MJ
2851 printk(KERN_NOTICE "LTTng: Unloaded modules v%s.%s.%s%s (%s)\n",
2852 __stringify(LTTNG_MODULES_MAJOR_VERSION),
2853 __stringify(LTTNG_MODULES_MINOR_VERSION),
2854 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION),
2855 LTTNG_MODULES_EXTRAVERSION,
2856 LTTNG_VERSION_NAME);
4e3c1b9b 2857}
92e94819 2858
a90917c3 2859module_exit(lttng_events_exit);
11b5a3c2 2860
92e94819
MD
2861MODULE_LICENSE("GPL and additional rights");
2862MODULE_AUTHOR("Mathieu Desnoyers <mathieu.desnoyers@efficios.com>");
2863MODULE_DESCRIPTION("LTTng Events");
9a9973ef
MD
2864MODULE_VERSION(__stringify(LTTNG_MODULES_MAJOR_VERSION) "."
2865 __stringify(LTTNG_MODULES_MINOR_VERSION) "."
309370e2
MD
2866 __stringify(LTTNG_MODULES_PATCHLEVEL_VERSION)
2867 LTTNG_MODULES_EXTRAVERSION);
This page took 0.203082 seconds and 5 git commands to generate.