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>
22 #include "kernelcompat.h"
25 #include <kcompat/rculist.h>
34 LTT_TYPE_UNSIGNED_INT
,
39 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
42 * Inspired from vsnprintf
44 * The serialization format string supports the basic printf format strings.
45 * In addition, it defines new formats that can be used to serialize more
46 * complex/non portable data structures.
51 * field_name #tracetype %ctype
52 * field_name #tracetype %ctype1 %ctype2 ...
54 * A conversion is performed between format string types supported by GCC and
55 * the trace type requested. GCC type is used to perform type checking on format
56 * strings. Trace type is used to specify the exact binary representation
57 * in the trace. A mapping is done between one or more GCC types to one trace
58 * type. Sign extension, if required by the conversion, is performed following
61 * If a gcc format is not declared with a trace format, the gcc format is
62 * also used as binary representation in the trace.
64 * Strings are supported with %s.
65 * A single tracetype (sequence) can take multiple c types as parameter.
71 * Note: to write a uint32_t in a trace, the following expression is recommended
72 * si it can be portable:
74 * ("#4u%lu", (unsigned long)var)
78 * Serialization specific formats :
90 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
94 * n: (for network byte order)
96 * is written in the trace in network byte order.
98 * i.e.: #bn4u%lu, #n%lu, #b%u
101 * Variable length sequence
102 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
104 * #a specifies that this is a sequence
105 * #tracetype1 is the type of elements in the sequence
106 * #tracetype2 is the type of the element count
108 * array_ptr is a pointer to an array that contains members of size
110 * num_elems is the number of elements in the array.
111 * i.e.: #a #lu #lu %p %lu %u
114 * #k callback (taken from the probe data)
115 * The following % arguments are exepected by the callback
117 * i.e.: #a #lu #lu #k %p
119 * Note: No conversion is done from floats to integers, nor from integers to
120 * floats between c types and trace types. float conversion from double to float
121 * or from float to double is also not supported.
124 * %*b expects sizeof(data), data
125 * where sizeof(data) is 1, 2, 4 or 8
127 * Fixed length struct, union or array.
128 * FIXME: unable to extract those sizes statically.
129 * %*r expects sizeof(*ptr), ptr
130 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
131 * struct and unions removed.
132 * Fixed length array:
133 * [%p]#a[len #tracetype]
134 * i.e.: [%p]#a[12 #lu]
136 * Variable length sequence
137 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
138 * where elem_num is the number of elements in the sequence
140 static inline const char *parse_trace_type(const char *fmt
,
141 char *trace_size
, enum ltt_type
*trace_type
,
142 unsigned long *attributes
)
144 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
145 /* 'z' support added 23/7/1999 S.H. */
146 /* 'z' changed to 'Z' --davidm 1/25/99 */
147 /* 't' added for ptrdiff_t */
149 /* parse attributes. */
153 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
158 /* get the conversion qualifier */
160 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
161 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
162 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
163 *fmt
== '4' || *fmt
== 8) {
166 if (qualifier
== 'l' && *fmt
== 'l') {
174 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
175 *trace_size
= sizeof(unsigned char);
178 *trace_type
= LTT_TYPE_STRING
;
181 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
182 *trace_size
= sizeof(void *);
186 *trace_type
= LTT_TYPE_SIGNED_INT
;
192 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
201 *trace_size
= sizeof(long long);
204 *trace_size
= sizeof(long);
208 *trace_size
= sizeof(size_t);
211 //ust// *trace_size = sizeof(ptrdiff_t);
214 *trace_size
= sizeof(short);
217 *trace_size
= sizeof(uint8_t);
220 *trace_size
= sizeof(uint16_t);
223 *trace_size
= sizeof(uint32_t);
226 *trace_size
= sizeof(uint64_t);
229 *trace_size
= sizeof(int);
238 * Field width and precision are *not* supported.
241 static inline const char *parse_c_type(const char *fmt
,
242 char *c_size
, enum ltt_type
*c_type
)
244 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
245 /* 'z' support added 23/7/1999 S.H. */
246 /* 'z' changed to 'Z' --davidm 1/25/99 */
247 /* 't' added for ptrdiff_t */
249 /* process flags : ignore standard print formats for now. */
261 /* get the conversion qualifier */
263 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
264 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
268 if (qualifier
== 'l' && *fmt
== 'l') {
276 *c_type
= LTT_TYPE_UNSIGNED_INT
;
277 *c_size
= sizeof(unsigned char);
280 *c_type
= LTT_TYPE_STRING
;
283 *c_type
= LTT_TYPE_UNSIGNED_INT
;
284 *c_size
= sizeof(void *);
288 *c_type
= LTT_TYPE_SIGNED_INT
;
294 *c_type
= LTT_TYPE_UNSIGNED_INT
;
303 *c_size
= sizeof(long long);
306 *c_size
= sizeof(long);
310 *c_size
= sizeof(size_t);
313 //ust// *c_size = sizeof(ptrdiff_t);
316 *c_size
= sizeof(short);
319 *c_size
= sizeof(int);
326 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
328 char trace_size
, enum ltt_type trace_type
,
329 char c_size
, enum ltt_type c_type
,
330 int *largest_align
, va_list *args
)
333 unsigned long v_ulong
;
342 * Be careful about sign extension here.
343 * Sign extension is done with the destination (trace) type.
345 switch (trace_type
) {
346 case LTT_TYPE_SIGNED_INT
:
349 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
352 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
355 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
358 tmp
.v_uint64
= va_arg(*args
, int64_t);
364 case LTT_TYPE_UNSIGNED_INT
:
367 tmp
.v_ulong
= (unsigned long)(uint8_t)
368 va_arg(*args
, unsigned int);
371 tmp
.v_ulong
= (unsigned long)(uint16_t)
372 va_arg(*args
, unsigned int);
375 tmp
.v_ulong
= (unsigned long)(uint32_t)
376 va_arg(*args
, unsigned int);
379 tmp
.v_uint64
= va_arg(*args
, uint64_t);
385 case LTT_TYPE_STRING
:
386 tmp
.v_string
.s
= va_arg(*args
, const char *);
387 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
388 tmp
.v_string
.s
= "<NULL>";
389 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
391 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
393 buf_offset
+= tmp
.v_string
.len
;
400 * If trace_size is lower or equal to 4 bytes, there is no sign
401 * extension to do because we are already encoded in a long. Therefore,
402 * we can combine signed and unsigned ops. 4 bytes float also works
403 * with this, because we do a simple copy of 4 bytes into 4 bytes
404 * without manipulation (and we do not support conversion from integers
406 * It is also the case if c_size is 8 bytes, which is the largest
409 if (ltt_get_alignment()) {
410 buf_offset
+= ltt_align(buf_offset
, trace_size
);
412 *largest_align
= max_t(int, *largest_align
, trace_size
);
414 if (trace_size
<= 4 || c_size
== 8) {
416 switch (trace_size
) {
419 ltt_relay_write(buf
, buf_offset
,
420 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
423 ltt_relay_write(buf
, buf_offset
,
424 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
429 ltt_relay_write(buf
, buf_offset
,
430 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
433 ltt_relay_write(buf
, buf_offset
,
434 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
439 ltt_relay_write(buf
, buf_offset
,
440 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
443 ltt_relay_write(buf
, buf_offset
,
444 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
449 * c_size cannot be other than 8 here because
452 ltt_relay_write(buf
, buf_offset
,
453 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
460 buf_offset
+= trace_size
;
464 * Perform sign extension.
467 switch (trace_type
) {
468 case LTT_TYPE_SIGNED_INT
:
469 ltt_relay_write(buf
, buf_offset
,
470 (int64_t[]){ (int64_t)tmp
.v_ulong
},
473 case LTT_TYPE_UNSIGNED_INT
:
474 ltt_relay_write(buf
, buf_offset
,
475 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
482 buf_offset
+= trace_size
;
490 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
491 struct ltt_serialize_closure
*closure
,
492 void *serialize_private
, int *largest_align
,
493 const char *fmt
, va_list *args
)
495 char trace_size
= 0, c_size
= 0; /*
496 * 0 (unset), 1, 2, 4, 8 bytes.
498 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
499 unsigned long attributes
= 0;
501 for (; *fmt
; ++fmt
) {
505 ++fmt
; /* skip first '#' */
506 if (*fmt
== '#') /* Escaped ## */
509 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
514 ++fmt
; /* skip first '%' */
515 if (*fmt
== '%') /* Escaped %% */
517 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
519 * Output c types if no trace types has been
524 if (trace_type
== LTT_TYPE_NONE
)
526 if (c_type
== LTT_TYPE_STRING
)
527 trace_type
= LTT_TYPE_STRING
;
528 /* perform trace write */
529 buf_offset
= serialize_trace_data(buf
,
530 buf_offset
, trace_size
,
531 trace_type
, c_size
, c_type
,
532 largest_align
, args
);
535 trace_type
= LTT_TYPE_NONE
;
536 c_size
= LTT_TYPE_NONE
;
539 /* default is to skip the text, doing nothing */
544 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
547 * Calculate data size
548 * Assume that the padding for alignment starts at a sizeof(void *) address.
550 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
551 void *serialize_private
, int *largest_align
,
552 const char *fmt
, va_list *args
)
554 ltt_serialize_cb cb
= closure
->callbacks
[0];
556 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
557 largest_align
, fmt
, args
);
561 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
562 struct ltt_serialize_closure
*closure
,
563 void *serialize_private
, int largest_align
,
564 const char *fmt
, va_list *args
)
566 ltt_serialize_cb cb
= closure
->callbacks
[0];
568 buf_offset
+= ltt_align(buf_offset
, largest_align
);
569 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
573 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
574 void *call_data
, const char *fmt
, va_list *args
)
576 int largest_align
, ret
;
577 struct ltt_active_marker
*pdata
;
579 size_t data_size
, slot_size
;
580 unsigned int chan_index
;
581 struct ltt_channel_struct
*channel
;
582 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
583 struct rchan_buf
*buf
;
584 void *transport_data
;
588 struct ltt_serialize_closure closure
;
589 struct ltt_probe_private_data
*private_data
= call_data
;
590 void *serialize_private
= NULL
;
595 * This test is useful for quickly exiting static tracing when no trace
596 * is active. We expect to have an active trace when we get here.
598 if (unlikely(ltt_traces
.num_active_traces
== 0))
601 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
602 //ust// cpu = smp_processor_id();
603 //ust// __get_cpu_var(ltt_nesting)++;
606 pdata
= (struct ltt_active_marker
*)probe_data
;
607 eID
= mdata
->event_id
;
608 chan_index
= mdata
->channel_id
;
609 closure
.callbacks
= pdata
->probe
->callbacks
;
611 if (unlikely(private_data
)) {
612 dest_trace
= private_data
->trace
;
613 if (private_data
->serializer
)
614 closure
.callbacks
= &private_data
->serializer
;
615 serialize_private
= private_data
->serialize_private
;
618 va_copy(args_copy
, *args
);
620 * Assumes event payload to start on largest_align alignment.
622 largest_align
= 1; /* must be non-zero for ltt_align */
623 data_size
= ltt_get_data_size(&closure
, serialize_private
,
624 &largest_align
, fmt
, &args_copy
);
625 largest_align
= min_t(int, largest_align
, sizeof(void *));
628 /* Iterate on each trace */
629 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
631 * Expect the filter to filter out events. If we get here,
632 * we went through tracepoint activation as a first step.
634 if (unlikely(dest_trace
&& trace
!= dest_trace
))
636 if (unlikely(!trace
->active
))
638 if (unlikely(!ltt_run_filter(trace
, eID
)))
640 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
641 rflags
= LTT_RFLAG_ID_SIZE
;
643 if (unlikely(eID
>= LTT_FREE_EVENTS
))
644 rflags
= LTT_RFLAG_ID
;
649 * Skip channels added after trace creation.
651 if (unlikely(chan_index
>= trace
->nr_channels
))
653 channel
= &trace
->channels
[chan_index
];
654 if (!channel
->active
)
657 /* reserve space : header and data */
658 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
659 data_size
, &slot_size
, &buf_offset
,
662 if (unlikely(ret
< 0))
663 continue; /* buffer full */
665 va_copy(args_copy
, *args
);
666 /* FIXME : could probably encapsulate transport better. */
667 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
668 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
669 /* Out-of-order write : header and data */
670 buf_offset
= ltt_write_event_header(trace
,
671 channel
, buf
, buf_offset
,
672 eID
, data_size
, tsc
, rflags
);
673 ltt_write_event_data(buf
, buf_offset
, &closure
,
675 largest_align
, fmt
, &args_copy
);
677 /* Out-of-order commit */
678 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
679 data_size
, slot_size
);
680 DBG("just commited event at offset %ld and size %zd\n", buf_offset
, slot_size
);
682 //ust// __get_cpu_var(ltt_nesting)--;
684 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
686 EXPORT_SYMBOL_GPL(ltt_vtrace
);
688 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
689 void *call_data
, const char *fmt
, ...)
694 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
697 EXPORT_SYMBOL_GPL(ltt_trace
);
699 //ust// MODULE_LICENSE("GPL");
700 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
701 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");