c7ba8480e71de59e592bd8eaadb8b38bab62ed4a
[lttng-ust.git] / libust / serialize.c
1 /*
2 * LTTng serializing code.
3 *
4 * Copyright Mathieu Desnoyers, March 2007.
5 *
6 * Licensed under the GPLv2.
7 *
8 * See this discussion about weirdness about passing va_list and then va_list to
9 * functions. (related to array argument passing). va_list seems to be
10 * implemented as an array on x86_64, but not on i386... This is why we pass a
11 * va_list * to ltt_vtrace.
12 */
13
14 #include <stdarg.h>
15 //ust// #include <linux/ctype.h>
16 //ust// #include <linux/string.h>
17 //ust// #include <linux/module.h>
18 //ust// #include <linux/ltt-tracer.h>
19 #include <string.h>
20 #include <stdint.h>
21
22 #include "kernelcompat.h"
23 #define _LGPL_SOURCE
24 #include <urcu.h>
25 #include <kcompat/rculist.h>
26
27 #include "relay.h"
28 #include "tracer.h"
29 //#include "list.h"
30 #include "usterr.h"
31
32 enum ltt_type {
33 LTT_TYPE_SIGNED_INT,
34 LTT_TYPE_UNSIGNED_INT,
35 LTT_TYPE_STRING,
36 LTT_TYPE_NONE,
37 };
38
39 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
40
41 /*
42 * Inspired from vsnprintf
43 *
44 * The serialization format string supports the basic printf format strings.
45 * In addition, it defines new formats that can be used to serialize more
46 * complex/non portable data structures.
47 *
48 * Typical use:
49 *
50 * field_name %ctype
51 * field_name #tracetype %ctype
52 * field_name #tracetype %ctype1 %ctype2 ...
53 *
54 * A conversion is performed between format string types supported by GCC and
55 * the trace type requested. GCC type is used to perform type checking on format
56 * strings. Trace type is used to specify the exact binary representation
57 * in the trace. A mapping is done between one or more GCC types to one trace
58 * type. Sign extension, if required by the conversion, is performed following
59 * the trace type.
60 *
61 * If a gcc format is not declared with a trace format, the gcc format is
62 * also used as binary representation in the trace.
63 *
64 * Strings are supported with %s.
65 * A single tracetype (sequence) can take multiple c types as parameter.
66 *
67 * c types:
68 *
69 * see printf(3).
70 *
71 * Note: to write a uint32_t in a trace, the following expression is recommended
72 * si it can be portable:
73 *
74 * ("#4u%lu", (unsigned long)var)
75 *
76 * trace types:
77 *
78 * Serialization specific formats :
79 *
80 * Fixed size integers
81 * #1u writes uint8_t
82 * #2u writes uint16_t
83 * #4u writes uint32_t
84 * #8u writes uint64_t
85 * #1d writes int8_t
86 * #2d writes int16_t
87 * #4d writes int32_t
88 * #8d writes int64_t
89 * i.e.:
90 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
91 *
92 * * Attributes:
93 *
94 * n: (for network byte order)
95 * #ntracetype%ctype
96 * is written in the trace in network byte order.
97 *
98 * i.e.: #bn4u%lu, #n%lu, #b%u
99 *
100 * TODO (eventually)
101 * Variable length sequence
102 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
103 * In the trace:
104 * #a specifies that this is a sequence
105 * #tracetype1 is the type of elements in the sequence
106 * #tracetype2 is the type of the element count
107 * GCC input:
108 * array_ptr is a pointer to an array that contains members of size
109 * elem_size.
110 * num_elems is the number of elements in the array.
111 * i.e.: #a #lu #lu %p %lu %u
112 *
113 * Callback
114 * #k callback (taken from the probe data)
115 * The following % arguments are exepected by the callback
116 *
117 * i.e.: #a #lu #lu #k %p
118 *
119 * Note: No conversion is done from floats to integers, nor from integers to
120 * floats between c types and trace types. float conversion from double to float
121 * or from float to double is also not supported.
122 *
123 * REMOVE
124 * %*b expects sizeof(data), data
125 * where sizeof(data) is 1, 2, 4 or 8
126 *
127 * Fixed length struct, union or array.
128 * FIXME: unable to extract those sizes statically.
129 * %*r expects sizeof(*ptr), ptr
130 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
131 * struct and unions removed.
132 * Fixed length array:
133 * [%p]#a[len #tracetype]
134 * i.e.: [%p]#a[12 #lu]
135 *
136 * Variable length sequence
137 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
138 * where elem_num is the number of elements in the sequence
139 */
140 static inline const char *parse_trace_type(const char *fmt,
141 char *trace_size, enum ltt_type *trace_type,
142 unsigned long *attributes)
143 {
144 int qualifier; /* 'h', 'l', or 'L' for integer fields */
145 /* 'z' support added 23/7/1999 S.H. */
146 /* 'z' changed to 'Z' --davidm 1/25/99 */
147 /* 't' added for ptrdiff_t */
148
149 /* parse attributes. */
150 repeat:
151 switch (*fmt) {
152 case 'n':
153 *attributes |= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER;
154 ++fmt;
155 goto repeat;
156 }
157
158 /* get the conversion qualifier */
159 qualifier = -1;
160 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
161 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
162 *fmt == 'S' || *fmt == '1' || *fmt == '2' ||
163 *fmt == '4' || *fmt == 8) {
164 qualifier = *fmt;
165 ++fmt;
166 if (qualifier == 'l' && *fmt == 'l') {
167 qualifier = 'L';
168 ++fmt;
169 }
170 }
171
172 switch (*fmt) {
173 case 'c':
174 *trace_type = LTT_TYPE_UNSIGNED_INT;
175 *trace_size = sizeof(unsigned char);
176 goto parse_end;
177 case 's':
178 *trace_type = LTT_TYPE_STRING;
179 goto parse_end;
180 case 'p':
181 *trace_type = LTT_TYPE_UNSIGNED_INT;
182 *trace_size = sizeof(void *);
183 goto parse_end;
184 case 'd':
185 case 'i':
186 *trace_type = LTT_TYPE_SIGNED_INT;
187 break;
188 case 'o':
189 case 'u':
190 case 'x':
191 case 'X':
192 *trace_type = LTT_TYPE_UNSIGNED_INT;
193 break;
194 default:
195 if (!*fmt)
196 --fmt;
197 goto parse_end;
198 }
199 switch (qualifier) {
200 case 'L':
201 *trace_size = sizeof(long long);
202 break;
203 case 'l':
204 *trace_size = sizeof(long);
205 break;
206 case 'Z':
207 case 'z':
208 *trace_size = sizeof(size_t);
209 break;
210 //ust// case 't':
211 //ust// *trace_size = sizeof(ptrdiff_t);
212 //ust// break;
213 case 'h':
214 *trace_size = sizeof(short);
215 break;
216 case '1':
217 *trace_size = sizeof(uint8_t);
218 break;
219 case '2':
220 *trace_size = sizeof(uint16_t);
221 break;
222 case '4':
223 *trace_size = sizeof(uint32_t);
224 break;
225 case '8':
226 *trace_size = sizeof(uint64_t);
227 break;
228 default:
229 *trace_size = sizeof(int);
230 }
231
232 parse_end:
233 return fmt;
234 }
235
236 /*
237 * Restrictions:
238 * Field width and precision are *not* supported.
239 * %n not supported.
240 */
241 static inline const char *parse_c_type(const char *fmt,
242 char *c_size, enum ltt_type *c_type)
243 {
244 int qualifier; /* 'h', 'l', or 'L' for integer fields */
245 /* 'z' support added 23/7/1999 S.H. */
246 /* 'z' changed to 'Z' --davidm 1/25/99 */
247 /* 't' added for ptrdiff_t */
248
249 /* process flags : ignore standard print formats for now. */
250 repeat:
251 switch (*fmt) {
252 case '-':
253 case '+':
254 case ' ':
255 case '#':
256 case '0':
257 ++fmt;
258 goto repeat;
259 }
260
261 /* get the conversion qualifier */
262 qualifier = -1;
263 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
264 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
265 *fmt == 'S') {
266 qualifier = *fmt;
267 ++fmt;
268 if (qualifier == 'l' && *fmt == 'l') {
269 qualifier = 'L';
270 ++fmt;
271 }
272 }
273
274 switch (*fmt) {
275 case 'c':
276 *c_type = LTT_TYPE_UNSIGNED_INT;
277 *c_size = sizeof(unsigned char);
278 goto parse_end;
279 case 's':
280 *c_type = LTT_TYPE_STRING;
281 goto parse_end;
282 case 'p':
283 *c_type = LTT_TYPE_UNSIGNED_INT;
284 *c_size = sizeof(void *);
285 goto parse_end;
286 case 'd':
287 case 'i':
288 *c_type = LTT_TYPE_SIGNED_INT;
289 break;
290 case 'o':
291 case 'u':
292 case 'x':
293 case 'X':
294 *c_type = LTT_TYPE_UNSIGNED_INT;
295 break;
296 default:
297 if (!*fmt)
298 --fmt;
299 goto parse_end;
300 }
301 switch (qualifier) {
302 case 'L':
303 *c_size = sizeof(long long);
304 break;
305 case 'l':
306 *c_size = sizeof(long);
307 break;
308 case 'Z':
309 case 'z':
310 *c_size = sizeof(size_t);
311 break;
312 //ust// case 't':
313 //ust// *c_size = sizeof(ptrdiff_t);
314 //ust// break;
315 case 'h':
316 *c_size = sizeof(short);
317 break;
318 default:
319 *c_size = sizeof(int);
320 }
321
322 parse_end:
323 return fmt;
324 }
325
326 static inline size_t serialize_trace_data(struct rchan_buf *buf,
327 size_t buf_offset,
328 char trace_size, enum ltt_type trace_type,
329 char c_size, enum ltt_type c_type,
330 int *largest_align, va_list *args)
331 {
332 union {
333 unsigned long v_ulong;
334 uint64_t v_uint64;
335 struct {
336 const char *s;
337 size_t len;
338 } v_string;
339 } tmp;
340
341 /*
342 * Be careful about sign extension here.
343 * Sign extension is done with the destination (trace) type.
344 */
345 switch (trace_type) {
346 case LTT_TYPE_SIGNED_INT:
347 switch (c_size) {
348 case 1:
349 tmp.v_ulong = (long)(int8_t)va_arg(*args, int);
350 break;
351 case 2:
352 tmp.v_ulong = (long)(int16_t)va_arg(*args, int);
353 break;
354 case 4:
355 tmp.v_ulong = (long)(int32_t)va_arg(*args, int);
356 break;
357 case 8:
358 tmp.v_uint64 = va_arg(*args, int64_t);
359 break;
360 default:
361 BUG();
362 }
363 break;
364 case LTT_TYPE_UNSIGNED_INT:
365 switch (c_size) {
366 case 1:
367 tmp.v_ulong = (unsigned long)(uint8_t)
368 va_arg(*args, unsigned int);
369 break;
370 case 2:
371 tmp.v_ulong = (unsigned long)(uint16_t)
372 va_arg(*args, unsigned int);
373 break;
374 case 4:
375 tmp.v_ulong = (unsigned long)(uint32_t)
376 va_arg(*args, unsigned int);
377 break;
378 case 8:
379 tmp.v_uint64 = va_arg(*args, uint64_t);
380 break;
381 default:
382 BUG();
383 }
384 break;
385 case LTT_TYPE_STRING:
386 tmp.v_string.s = va_arg(*args, const char *);
387 if ((unsigned long)tmp.v_string.s < PAGE_SIZE)
388 tmp.v_string.s = "<NULL>";
389 tmp.v_string.len = strlen(tmp.v_string.s)+1;
390 if (buf)
391 ltt_relay_write(buf, buf_offset, tmp.v_string.s,
392 tmp.v_string.len);
393 buf_offset += tmp.v_string.len;
394 goto copydone;
395 default:
396 BUG();
397 }
398
399 /*
400 * If trace_size is lower or equal to 4 bytes, there is no sign
401 * extension to do because we are already encoded in a long. Therefore,
402 * we can combine signed and unsigned ops. 4 bytes float also works
403 * with this, because we do a simple copy of 4 bytes into 4 bytes
404 * without manipulation (and we do not support conversion from integers
405 * to floats).
406 * It is also the case if c_size is 8 bytes, which is the largest
407 * possible integer.
408 */
409 if (ltt_get_alignment()) {
410 buf_offset += ltt_align(buf_offset, trace_size);
411 if (largest_align)
412 *largest_align = max_t(int, *largest_align, trace_size);
413 }
414 if (trace_size <= 4 || c_size == 8) {
415 if (buf) {
416 switch (trace_size) {
417 case 1:
418 if (c_size == 8)
419 ltt_relay_write(buf, buf_offset,
420 (uint8_t[]){ (uint8_t)tmp.v_uint64 },
421 sizeof(uint8_t));
422 else
423 ltt_relay_write(buf, buf_offset,
424 (uint8_t[]){ (uint8_t)tmp.v_ulong },
425 sizeof(uint8_t));
426 break;
427 case 2:
428 if (c_size == 8)
429 ltt_relay_write(buf, buf_offset,
430 (uint16_t[]){ (uint16_t)tmp.v_uint64 },
431 sizeof(uint16_t));
432 else
433 ltt_relay_write(buf, buf_offset,
434 (uint16_t[]){ (uint16_t)tmp.v_ulong },
435 sizeof(uint16_t));
436 break;
437 case 4:
438 if (c_size == 8)
439 ltt_relay_write(buf, buf_offset,
440 (uint32_t[]){ (uint32_t)tmp.v_uint64 },
441 sizeof(uint32_t));
442 else
443 ltt_relay_write(buf, buf_offset,
444 (uint32_t[]){ (uint32_t)tmp.v_ulong },
445 sizeof(uint32_t));
446 break;
447 case 8:
448 /*
449 * c_size cannot be other than 8 here because
450 * trace_size > 4.
451 */
452 ltt_relay_write(buf, buf_offset,
453 (uint64_t[]){ (uint64_t)tmp.v_uint64 },
454 sizeof(uint64_t));
455 break;
456 default:
457 BUG();
458 }
459 }
460 buf_offset += trace_size;
461 goto copydone;
462 } else {
463 /*
464 * Perform sign extension.
465 */
466 if (buf) {
467 switch (trace_type) {
468 case LTT_TYPE_SIGNED_INT:
469 ltt_relay_write(buf, buf_offset,
470 (int64_t[]){ (int64_t)tmp.v_ulong },
471 sizeof(int64_t));
472 break;
473 case LTT_TYPE_UNSIGNED_INT:
474 ltt_relay_write(buf, buf_offset,
475 (uint64_t[]){ (uint64_t)tmp.v_ulong },
476 sizeof(uint64_t));
477 break;
478 default:
479 BUG();
480 }
481 }
482 buf_offset += trace_size;
483 goto copydone;
484 }
485
486 copydone:
487 return buf_offset;
488 }
489
490 notrace size_t ltt_serialize_data(struct rchan_buf *buf, size_t buf_offset,
491 struct ltt_serialize_closure *closure,
492 void *serialize_private, int *largest_align,
493 const char *fmt, va_list *args)
494 {
495 char trace_size = 0, c_size = 0; /*
496 * 0 (unset), 1, 2, 4, 8 bytes.
497 */
498 enum ltt_type trace_type = LTT_TYPE_NONE, c_type = LTT_TYPE_NONE;
499 unsigned long attributes = 0;
500
501 for (; *fmt ; ++fmt) {
502 switch (*fmt) {
503 case '#':
504 /* tracetypes (#) */
505 ++fmt; /* skip first '#' */
506 if (*fmt == '#') /* Escaped ## */
507 break;
508 attributes = 0;
509 fmt = parse_trace_type(fmt, &trace_size, &trace_type,
510 &attributes);
511 break;
512 case '%':
513 /* c types (%) */
514 ++fmt; /* skip first '%' */
515 if (*fmt == '%') /* Escaped %% */
516 break;
517 fmt = parse_c_type(fmt, &c_size, &c_type);
518 /*
519 * Output c types if no trace types has been
520 * specified.
521 */
522 if (!trace_size)
523 trace_size = c_size;
524 if (trace_type == LTT_TYPE_NONE)
525 trace_type = c_type;
526 if (c_type == LTT_TYPE_STRING)
527 trace_type = LTT_TYPE_STRING;
528 /* perform trace write */
529 buf_offset = serialize_trace_data(buf,
530 buf_offset, trace_size,
531 trace_type, c_size, c_type,
532 largest_align, args);
533 trace_size = 0;
534 c_size = 0;
535 trace_type = LTT_TYPE_NONE;
536 c_size = LTT_TYPE_NONE;
537 attributes = 0;
538 break;
539 /* default is to skip the text, doing nothing */
540 }
541 }
542 return buf_offset;
543 }
544 EXPORT_SYMBOL_GPL(ltt_serialize_data);
545
546 /*
547 * Calculate data size
548 * Assume that the padding for alignment starts at a sizeof(void *) address.
549 */
550 static notrace size_t ltt_get_data_size(struct ltt_serialize_closure *closure,
551 void *serialize_private, int *largest_align,
552 const char *fmt, va_list *args)
553 {
554 ltt_serialize_cb cb = closure->callbacks[0];
555 closure->cb_idx = 0;
556 return (size_t)cb(NULL, 0, closure, serialize_private,
557 largest_align, fmt, args);
558 }
559
560 static notrace
561 void ltt_write_event_data(struct rchan_buf *buf, size_t buf_offset,
562 struct ltt_serialize_closure *closure,
563 void *serialize_private, int largest_align,
564 const char *fmt, va_list *args)
565 {
566 ltt_serialize_cb cb = closure->callbacks[0];
567 closure->cb_idx = 0;
568 buf_offset += ltt_align(buf_offset, largest_align);
569 cb(buf, buf_offset, closure, serialize_private, NULL, fmt, args);
570 }
571
572
573 notrace void ltt_vtrace(const struct marker *mdata, void *probe_data,
574 void *call_data, const char *fmt, va_list *args)
575 {
576 int largest_align, ret;
577 struct ltt_active_marker *pdata;
578 uint16_t eID;
579 size_t data_size, slot_size;
580 unsigned int chan_index;
581 struct ltt_channel_struct *channel;
582 struct ltt_trace_struct *trace, *dest_trace = NULL;
583 struct rchan_buf *buf;
584 void *transport_data;
585 u64 tsc;
586 long buf_offset;
587 va_list args_copy;
588 struct ltt_serialize_closure closure;
589 struct ltt_probe_private_data *private_data = call_data;
590 void *serialize_private = NULL;
591 //ust// int cpu;
592 unsigned int rflags;
593
594 /*
595 * This test is useful for quickly exiting static tracing when no trace
596 * is active. We expect to have an active trace when we get here.
597 */
598 if (unlikely(ltt_traces.num_active_traces == 0))
599 return;
600
601 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
602 //ust// cpu = smp_processor_id();
603 //ust// __get_cpu_var(ltt_nesting)++;
604 ltt_nesting++;
605
606 pdata = (struct ltt_active_marker *)probe_data;
607 eID = mdata->event_id;
608 chan_index = mdata->channel_id;
609 closure.callbacks = pdata->probe->callbacks;
610
611 if (unlikely(private_data)) {
612 dest_trace = private_data->trace;
613 if (private_data->serializer)
614 closure.callbacks = &private_data->serializer;
615 serialize_private = private_data->serialize_private;
616 }
617
618 va_copy(args_copy, *args);
619 /*
620 * Assumes event payload to start on largest_align alignment.
621 */
622 largest_align = 1; /* must be non-zero for ltt_align */
623 data_size = ltt_get_data_size(&closure, serialize_private,
624 &largest_align, fmt, &args_copy);
625 largest_align = min_t(int, largest_align, sizeof(void *));
626 va_end(args_copy);
627
628 /* Iterate on each trace */
629 list_for_each_entry_rcu(trace, &ltt_traces.head, list) {
630 /*
631 * Expect the filter to filter out events. If we get here,
632 * we went through tracepoint activation as a first step.
633 */
634 if (unlikely(dest_trace && trace != dest_trace))
635 continue;
636 if (unlikely(!trace->active))
637 continue;
638 if (unlikely(!ltt_run_filter(trace, eID)))
639 continue;
640 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
641 rflags = LTT_RFLAG_ID_SIZE;
642 #else
643 if (unlikely(eID >= LTT_FREE_EVENTS))
644 rflags = LTT_RFLAG_ID;
645 else
646 rflags = 0;
647 #endif
648 /*
649 * Skip channels added after trace creation.
650 */
651 if (unlikely(chan_index >= trace->nr_channels))
652 continue;
653 channel = &trace->channels[chan_index];
654 if (!channel->active)
655 continue;
656
657 /* reserve space : header and data */
658 ret = ltt_reserve_slot(trace, channel, &transport_data,
659 data_size, &slot_size, &buf_offset,
660 &tsc, &rflags,
661 largest_align);
662 if (unlikely(ret < 0))
663 continue; /* buffer full */
664
665 va_copy(args_copy, *args);
666 /* FIXME : could probably encapsulate transport better. */
667 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
668 buf = ((struct rchan *)channel->trans_channel_data)->buf;
669 /* Out-of-order write : header and data */
670 buf_offset = ltt_write_event_header(trace,
671 channel, buf, buf_offset,
672 eID, data_size, tsc, rflags);
673 ltt_write_event_data(buf, buf_offset, &closure,
674 serialize_private,
675 largest_align, fmt, &args_copy);
676 va_end(args_copy);
677 /* Out-of-order commit */
678 ltt_commit_slot(channel, &transport_data, buf_offset,
679 data_size, slot_size);
680 DBG("just commited event at offset %ld and size %zd\n", buf_offset, slot_size);
681 }
682 //ust// __get_cpu_var(ltt_nesting)--;
683 ltt_nesting--;
684 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
685 }
686 EXPORT_SYMBOL_GPL(ltt_vtrace);
687
688 notrace void ltt_trace(const struct marker *mdata, void *probe_data,
689 void *call_data, const char *fmt, ...)
690 {
691 va_list args;
692
693 va_start(args, fmt);
694 ltt_vtrace(mdata, probe_data, call_data, fmt, &args);
695 va_end(args);
696 }
697 EXPORT_SYMBOL_GPL(ltt_trace);
698
699 //ust// MODULE_LICENSE("GPL");
700 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
701 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");
This page took 0.044751 seconds and 3 git commands to generate.