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