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