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