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