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