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