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