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