2 * LTTng serializing code.
4 * Copyright Mathieu Desnoyers, March 2007.
6 * Licensed under the GPLv2.
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.
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>
21 #include "kernelcompat.h"
29 LTT_TYPE_UNSIGNED_INT
,
34 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
37 * Inspired from vsnprintf
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.
46 * field_name #tracetype %ctype
47 * field_name #tracetype %ctype1 %ctype2 ...
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
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.
59 * Strings are supported with %s.
60 * A single tracetype (sequence) can take multiple c types as parameter.
66 * Note: to write a uint32_t in a trace, the following expression is recommended
67 * si it can be portable:
69 * ("#4u%lu", (unsigned long)var)
73 * Serialization specific formats :
85 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
89 * n: (for network byte order)
91 * is written in the trace in network byte order.
93 * i.e.: #bn4u%lu, #n%lu, #b%u
96 * Variable length sequence
97 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
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
103 * array_ptr is a pointer to an array that contains members of size
105 * num_elems is the number of elements in the array.
106 * i.e.: #a #lu #lu %p %lu %u
109 * #k callback (taken from the probe data)
110 * The following % arguments are exepected by the callback
112 * i.e.: #a #lu #lu #k %p
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.
119 * %*b expects sizeof(data), data
120 * where sizeof(data) is 1, 2, 4 or 8
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]
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
135 static inline const char *parse_trace_type(const char *fmt
,
136 char *trace_size
, enum ltt_type
*trace_type
,
137 unsigned long *attributes
)
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 */
144 /* parse attributes. */
148 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
153 /* get the conversion qualifier */
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) {
161 if (qualifier
== 'l' && *fmt
== 'l') {
169 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
170 *trace_size
= sizeof(unsigned char);
173 *trace_type
= LTT_TYPE_STRING
;
176 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
177 *trace_size
= sizeof(void *);
181 *trace_type
= LTT_TYPE_SIGNED_INT
;
187 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
196 *trace_size
= sizeof(long long);
199 *trace_size
= sizeof(long);
203 *trace_size
= sizeof(size_t);
206 //ust// *trace_size = sizeof(ptrdiff_t);
209 *trace_size
= sizeof(short);
212 *trace_size
= sizeof(uint8_t);
215 *trace_size
= sizeof(uint16_t);
218 *trace_size
= sizeof(uint32_t);
221 *trace_size
= sizeof(uint64_t);
224 *trace_size
= sizeof(int);
233 * Field width and precision are *not* supported.
236 static inline const char *parse_c_type(const char *fmt
,
237 char *c_size
, enum ltt_type
*c_type
)
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 */
244 /* process flags : ignore standard print formats for now. */
256 /* get the conversion qualifier */
258 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
259 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
263 if (qualifier
== 'l' && *fmt
== 'l') {
271 *c_type
= LTT_TYPE_UNSIGNED_INT
;
272 *c_size
= sizeof(unsigned char);
275 *c_type
= LTT_TYPE_STRING
;
278 *c_type
= LTT_TYPE_UNSIGNED_INT
;
279 *c_size
= sizeof(void *);
283 *c_type
= LTT_TYPE_SIGNED_INT
;
289 *c_type
= LTT_TYPE_UNSIGNED_INT
;
298 *c_size
= sizeof(long long);
301 *c_size
= sizeof(long);
305 *c_size
= sizeof(size_t);
308 //ust// *c_size = sizeof(ptrdiff_t);
311 *c_size
= sizeof(short);
314 *c_size
= sizeof(int);
321 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
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
)
328 unsigned long v_ulong
;
337 * Be careful about sign extension here.
338 * Sign extension is done with the destination (trace) type.
340 switch (trace_type
) {
341 case LTT_TYPE_SIGNED_INT
:
344 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
347 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
350 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
353 tmp
.v_uint64
= va_arg(*args
, int64_t);
359 case LTT_TYPE_UNSIGNED_INT
:
362 tmp
.v_ulong
= (unsigned long)(uint8_t)
363 va_arg(*args
, unsigned int);
366 tmp
.v_ulong
= (unsigned long)(uint16_t)
367 va_arg(*args
, unsigned int);
370 tmp
.v_ulong
= (unsigned long)(uint32_t)
371 va_arg(*args
, unsigned int);
374 tmp
.v_uint64
= va_arg(*args
, uint64_t);
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;
386 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
388 buf_offset
+= tmp
.v_string
.len
;
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
401 * It is also the case if c_size is 8 bytes, which is the largest
404 if (ltt_get_alignment()) {
405 buf_offset
+= ltt_align(buf_offset
, trace_size
);
407 *largest_align
= max_t(int, *largest_align
, trace_size
);
409 if (trace_size
<= 4 || c_size
== 8) {
411 switch (trace_size
) {
414 ltt_relay_write(buf
, buf_offset
,
415 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
418 ltt_relay_write(buf
, buf_offset
,
419 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
424 ltt_relay_write(buf
, buf_offset
,
425 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
428 ltt_relay_write(buf
, buf_offset
,
429 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
434 ltt_relay_write(buf
, buf_offset
,
435 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
438 ltt_relay_write(buf
, buf_offset
,
439 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
444 * c_size cannot be other than 8 here because
447 ltt_relay_write(buf
, buf_offset
,
448 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
455 buf_offset
+= trace_size
;
459 * Perform sign extension.
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
},
468 case LTT_TYPE_UNSIGNED_INT
:
469 ltt_relay_write(buf
, buf_offset
,
470 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
477 buf_offset
+= trace_size
;
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
)
490 char trace_size
= 0, c_size
= 0; /*
491 * 0 (unset), 1, 2, 4, 8 bytes.
493 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
494 unsigned long attributes
= 0;
496 for (; *fmt
; ++fmt
) {
500 ++fmt
; /* skip first '#' */
501 if (*fmt
== '#') /* Escaped ## */
504 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
509 ++fmt
; /* skip first '%' */
510 if (*fmt
== '%') /* Escaped %% */
512 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
514 * Output c types if no trace types has been
519 if (trace_type
== LTT_TYPE_NONE
)
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
);
530 trace_type
= LTT_TYPE_NONE
;
531 c_size
= LTT_TYPE_NONE
;
534 /* default is to skip the text, doing nothing */
539 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
542 * Calculate data size
543 * Assume that the padding for alignment starts at a sizeof(void *) address.
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
)
549 ltt_serialize_cb cb
= closure
->callbacks
[0];
551 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
552 largest_align
, fmt
, args
);
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
)
561 ltt_serialize_cb cb
= closure
->callbacks
[0];
563 buf_offset
+= ltt_align(buf_offset
, largest_align
);
564 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
568 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
569 void *call_data
, const char *fmt
, va_list *args
)
571 int largest_align
, ret
;
572 struct ltt_active_marker
*pdata
;
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
;
583 struct ltt_serialize_closure closure
;
584 struct ltt_probe_private_data
*private_data
= call_data
;
585 void *serialize_private
= NULL
;
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.
593 if (unlikely(ltt_traces
.num_active_traces
== 0))
596 rcu_read_lock_sched_notrace();
597 cpu
= smp_processor_id();
598 //ust// __get_cpu_var(ltt_nesting)++;
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
;
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
;
613 va_copy(args_copy
, *args
);
615 * Assumes event payload to start on largest_align alignment.
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 *));
623 /* Iterate on each trace */
624 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
626 * Expect the filter to filter out events. If we get here,
627 * we went through tracepoint activation as a first step.
629 if (unlikely(dest_trace
&& trace
!= dest_trace
))
631 if (unlikely(!trace
->active
))
633 if (unlikely(!ltt_run_filter(trace
, eID
)))
635 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
636 rflags
= LTT_RFLAG_ID_SIZE
;
638 if (unlikely(eID
>= LTT_FREE_EVENTS
))
639 rflags
= LTT_RFLAG_ID
;
644 * Skip channels added after trace creation.
646 if (unlikely(chan_index
>= trace
->nr_channels
))
648 channel
= &trace
->channels
[chan_index
];
649 if (!channel
->active
)
652 /* reserve space : header and data */
653 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
654 data_size
, &slot_size
, &buf_offset
,
657 if (unlikely(ret
< 0))
658 continue; /* buffer full */
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
,
670 largest_align
, fmt
, &args_copy
);
672 /* Out-of-order commit */
673 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
675 printf("just commited event at offset %d and size %d\n", buf_offset
, slot_size
);
677 //ust// __get_cpu_var(ltt_nesting)--;
679 rcu_read_unlock_sched_notrace();
681 EXPORT_SYMBOL_GPL(ltt_vtrace
);
683 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
684 void *call_data
, const char *fmt
, ...)
689 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
692 EXPORT_SYMBOL_GPL(ltt_trace
);
694 //ust// MODULE_LICENSE("GPL");
695 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
696 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");