2 * LTTng serializing code.
4 * Copyright Mathieu Desnoyers, March 2007.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * See this discussion about weirdness about passing va_list and then va_list to
22 * functions. (related to array argument passing). va_list seems to be
23 * implemented as an array on x86_64, but not on i386... This is why we pass a
24 * va_list * to ltt_vtrace.
28 //ust// #include <linux/ctype.h>
29 //ust// #include <linux/string.h>
30 //ust// #include <linux/module.h>
31 //ust// #include <linux/ltt-tracer.h>
36 #include <ust/kernelcompat.h>
39 #include <urcu/rculist.h>
48 LTT_TYPE_UNSIGNED_INT
,
53 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
56 * Inspired from vsnprintf
58 * The serialization format string supports the basic printf format strings.
59 * In addition, it defines new formats that can be used to serialize more
60 * complex/non portable data structures.
65 * field_name #tracetype %ctype
66 * field_name #tracetype %ctype1 %ctype2 ...
68 * A conversion is performed between format string types supported by GCC and
69 * the trace type requested. GCC type is used to perform type checking on format
70 * strings. Trace type is used to specify the exact binary representation
71 * in the trace. A mapping is done between one or more GCC types to one trace
72 * type. Sign extension, if required by the conversion, is performed following
75 * If a gcc format is not declared with a trace format, the gcc format is
76 * also used as binary representation in the trace.
78 * Strings are supported with %s.
79 * A single tracetype (sequence) can take multiple c types as parameter.
85 * Note: to write a uint32_t in a trace, the following expression is recommended
86 * si it can be portable:
88 * ("#4u%lu", (unsigned long)var)
92 * Serialization specific formats :
104 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
108 * n: (for network byte order)
110 * is written in the trace in network byte order.
112 * i.e.: #bn4u%lu, #n%lu, #b%u
115 * Variable length sequence
116 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
118 * #a specifies that this is a sequence
119 * #tracetype1 is the type of elements in the sequence
120 * #tracetype2 is the type of the element count
122 * array_ptr is a pointer to an array that contains members of size
124 * num_elems is the number of elements in the array.
125 * i.e.: #a #lu #lu %p %lu %u
128 * #k callback (taken from the probe data)
129 * The following % arguments are exepected by the callback
131 * i.e.: #a #lu #lu #k %p
133 * Note: No conversion is done from floats to integers, nor from integers to
134 * floats between c types and trace types. float conversion from double to float
135 * or from float to double is also not supported.
138 * %*b expects sizeof(data), data
139 * where sizeof(data) is 1, 2, 4 or 8
141 * Fixed length struct, union or array.
142 * FIXME: unable to extract those sizes statically.
143 * %*r expects sizeof(*ptr), ptr
144 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
145 * struct and unions removed.
146 * Fixed length array:
147 * [%p]#a[len #tracetype]
148 * i.e.: [%p]#a[12 #lu]
150 * Variable length sequence
151 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
152 * where elem_num is the number of elements in the sequence
154 static inline const char *parse_trace_type(const char *fmt
,
155 char *trace_size
, enum ltt_type
*trace_type
,
156 unsigned long *attributes
)
158 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
159 /* 'z' support added 23/7/1999 S.H. */
160 /* 'z' changed to 'Z' --davidm 1/25/99 */
161 /* 't' added for ptrdiff_t */
163 /* parse attributes. */
167 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
172 /* get the conversion qualifier */
174 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
175 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
176 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
177 *fmt
== '4' || *fmt
== 8) {
180 if (qualifier
== 'l' && *fmt
== 'l') {
188 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
189 *trace_size
= sizeof(unsigned char);
192 *trace_type
= LTT_TYPE_STRING
;
195 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
196 *trace_size
= sizeof(void *);
200 *trace_type
= LTT_TYPE_SIGNED_INT
;
206 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
215 *trace_size
= sizeof(long long);
218 *trace_size
= sizeof(long);
222 *trace_size
= sizeof(size_t);
225 //ust// *trace_size = sizeof(ptrdiff_t);
228 *trace_size
= sizeof(short);
231 *trace_size
= sizeof(uint8_t);
234 *trace_size
= sizeof(uint16_t);
237 *trace_size
= sizeof(uint32_t);
240 *trace_size
= sizeof(uint64_t);
243 *trace_size
= sizeof(int);
252 * Field width and precision are *not* supported.
255 static inline const char *parse_c_type(const char *fmt
,
256 char *c_size
, enum ltt_type
*c_type
)
258 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
259 /* 'z' support added 23/7/1999 S.H. */
260 /* 'z' changed to 'Z' --davidm 1/25/99 */
261 /* 't' added for ptrdiff_t */
263 /* process flags : ignore standard print formats for now. */
275 /* get the conversion qualifier */
277 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
278 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
282 if (qualifier
== 'l' && *fmt
== 'l') {
290 *c_type
= LTT_TYPE_UNSIGNED_INT
;
291 *c_size
= sizeof(unsigned char);
294 *c_type
= LTT_TYPE_STRING
;
297 *c_type
= LTT_TYPE_UNSIGNED_INT
;
298 *c_size
= sizeof(void *);
302 *c_type
= LTT_TYPE_SIGNED_INT
;
308 *c_type
= LTT_TYPE_UNSIGNED_INT
;
317 *c_size
= sizeof(long long);
320 *c_size
= sizeof(long);
324 *c_size
= sizeof(size_t);
327 //ust// *c_size = sizeof(ptrdiff_t);
330 *c_size
= sizeof(short);
333 *c_size
= sizeof(int);
340 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
342 char trace_size
, enum ltt_type trace_type
,
343 char c_size
, enum ltt_type c_type
,
344 int *largest_align
, va_list *args
)
347 unsigned long v_ulong
;
356 * Be careful about sign extension here.
357 * Sign extension is done with the destination (trace) type.
359 switch (trace_type
) {
360 case LTT_TYPE_SIGNED_INT
:
363 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
366 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
369 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
372 tmp
.v_uint64
= va_arg(*args
, int64_t);
378 case LTT_TYPE_UNSIGNED_INT
:
381 tmp
.v_ulong
= (unsigned long)(uint8_t)
382 va_arg(*args
, unsigned int);
385 tmp
.v_ulong
= (unsigned long)(uint16_t)
386 va_arg(*args
, unsigned int);
389 tmp
.v_ulong
= (unsigned long)(uint32_t)
390 va_arg(*args
, unsigned int);
393 tmp
.v_uint64
= va_arg(*args
, uint64_t);
399 case LTT_TYPE_STRING
:
400 tmp
.v_string
.s
= va_arg(*args
, const char *);
401 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
402 tmp
.v_string
.s
= "<NULL>";
403 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
405 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
407 buf_offset
+= tmp
.v_string
.len
;
414 * If trace_size is lower or equal to 4 bytes, there is no sign
415 * extension to do because we are already encoded in a long. Therefore,
416 * we can combine signed and unsigned ops. 4 bytes float also works
417 * with this, because we do a simple copy of 4 bytes into 4 bytes
418 * without manipulation (and we do not support conversion from integers
420 * It is also the case if c_size is 8 bytes, which is the largest
423 if (ltt_get_alignment()) {
424 buf_offset
+= ltt_align(buf_offset
, trace_size
);
426 *largest_align
= max_t(int, *largest_align
, trace_size
);
428 if (trace_size
<= 4 || c_size
== 8) {
430 switch (trace_size
) {
433 ltt_relay_write(buf
, buf_offset
,
434 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
437 ltt_relay_write(buf
, buf_offset
,
438 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
443 ltt_relay_write(buf
, buf_offset
,
444 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
447 ltt_relay_write(buf
, buf_offset
,
448 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
453 ltt_relay_write(buf
, buf_offset
,
454 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
457 ltt_relay_write(buf
, buf_offset
,
458 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
463 * c_size cannot be other than 8 here because
466 ltt_relay_write(buf
, buf_offset
,
467 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
474 buf_offset
+= trace_size
;
478 * Perform sign extension.
481 switch (trace_type
) {
482 case LTT_TYPE_SIGNED_INT
:
483 ltt_relay_write(buf
, buf_offset
,
484 (int64_t[]){ (int64_t)tmp
.v_ulong
},
487 case LTT_TYPE_UNSIGNED_INT
:
488 ltt_relay_write(buf
, buf_offset
,
489 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
496 buf_offset
+= trace_size
;
504 static notrace
void skip_space(const char **ps
)
510 static notrace
void copy_token(char **out
, const char **in
)
512 while(**in
!= ' ' && **in
!= '\0') {
521 * Given a format string and a va_list of arguments, convert them to a
522 * human-readable string.
524 * @outbuf: the buffer to output the string to
525 * @bufsize: the max size that can be used in outbuf
526 * @fmt: the marker format string
527 * @ap: a va_list that contains the arguments corresponding to fmt
529 * Return value: the number of chars that have been put in outbuf, excluding
530 * the final \0, or, if the buffer was too small, the number of chars that
531 * would have been written in outbuf if it had been large enough.
533 * outbuf may be NULL. The return value may then be used be allocate an
534 * appropriate outbuf.
539 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
541 int fmt_len
= strlen(fmt
);
542 char *new_fmt
= alloca(fmt_len
+ 1);
543 const char *orig_fmt_p
= fmt
;
544 char *new_fmt_p
= new_fmt
;
547 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
549 while(*orig_fmt_p
!= '\0') {
550 if(*orig_fmt_p
== '%') {
552 copy_token(&new_fmt_p
, &orig_fmt_p
);
554 else if(*orig_fmt_p
== '#') {
555 prev_token
= tracefmt
;
558 } while(*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
560 else if(*orig_fmt_p
== ' ') {
561 if(prev_token
== argname
) {
565 else if(prev_token
== cfmt
) {
570 skip_space(&orig_fmt_p
);
573 prev_token
= argname
;
574 copy_token(&new_fmt_p
, &orig_fmt_p
);
581 /* use this false_buffer for compatibility with pre-C99 */
585 result
= vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);
590 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
591 struct ltt_serialize_closure
*closure
,
592 void *serialize_private
, int *largest_align
,
593 const char *fmt
, va_list *args
)
595 char trace_size
= 0, c_size
= 0; /*
596 * 0 (unset), 1, 2, 4, 8 bytes.
598 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
599 unsigned long attributes
= 0;
601 for (; *fmt
; ++fmt
) {
605 ++fmt
; /* skip first '#' */
606 if (*fmt
== '#') /* Escaped ## */
609 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
614 ++fmt
; /* skip first '%' */
615 if (*fmt
== '%') /* Escaped %% */
617 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
619 * Output c types if no trace types has been
624 if (trace_type
== LTT_TYPE_NONE
)
626 if (c_type
== LTT_TYPE_STRING
)
627 trace_type
= LTT_TYPE_STRING
;
628 /* perform trace write */
629 buf_offset
= serialize_trace_data(buf
,
630 buf_offset
, trace_size
,
631 trace_type
, c_size
, c_type
,
632 largest_align
, args
);
635 trace_type
= LTT_TYPE_NONE
;
636 c_size
= LTT_TYPE_NONE
;
639 /* default is to skip the text, doing nothing */
646 * Calculate data size
647 * Assume that the padding for alignment starts at a sizeof(void *) address.
649 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
650 void *serialize_private
, int *largest_align
,
651 const char *fmt
, va_list *args
)
653 ltt_serialize_cb cb
= closure
->callbacks
[0];
655 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
656 largest_align
, fmt
, args
);
660 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
661 struct ltt_serialize_closure
*closure
,
662 void *serialize_private
, int largest_align
,
663 const char *fmt
, va_list *args
)
665 ltt_serialize_cb cb
= closure
->callbacks
[0];
667 buf_offset
+= ltt_align(buf_offset
, largest_align
);
668 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
672 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
673 struct registers
*regs
, void *call_data
,
674 const char *fmt
, va_list *args
)
676 int largest_align
, ret
;
677 struct ltt_active_marker
*pdata
;
679 size_t data_size
, slot_size
;
680 unsigned int chan_index
;
681 struct ltt_channel_struct
*channel
;
682 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
683 struct rchan_buf
*buf
;
684 void *transport_data
;
688 struct ltt_serialize_closure closure
;
689 struct ltt_probe_private_data
*private_data
= call_data
;
690 void *serialize_private
= NULL
;
695 * This test is useful for quickly exiting static tracing when no trace
696 * is active. We expect to have an active trace when we get here.
698 if (unlikely(ltt_traces
.num_active_traces
== 0))
701 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
702 //ust// cpu = smp_processor_id();
703 //ust// __get_cpu_var(ltt_nesting)++;
706 pdata
= (struct ltt_active_marker
*)probe_data
;
707 eID
= mdata
->event_id
;
708 chan_index
= mdata
->channel_id
;
709 closure
.callbacks
= pdata
->probe
->callbacks
;
711 if (unlikely(private_data
)) {
712 dest_trace
= private_data
->trace
;
713 if (private_data
->serializer
)
714 closure
.callbacks
= &private_data
->serializer
;
715 serialize_private
= private_data
->serialize_private
;
718 va_copy(args_copy
, *args
);
720 * Assumes event payload to start on largest_align alignment.
722 largest_align
= 1; /* must be non-zero for ltt_align */
723 data_size
= ltt_get_data_size(&closure
, serialize_private
,
724 &largest_align
, fmt
, &args_copy
);
725 largest_align
= min_t(int, largest_align
, sizeof(void *));
728 /* Iterate on each trace */
729 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
731 * Expect the filter to filter out events. If we get here,
732 * we went through tracepoint activation as a first step.
734 if (unlikely(dest_trace
&& trace
!= dest_trace
))
736 if (unlikely(!trace
->active
))
738 if (unlikely(!ltt_run_filter(trace
, eID
)))
740 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
741 rflags
= LTT_RFLAG_ID_SIZE
;
743 if (unlikely(eID
>= LTT_FREE_EVENTS
))
744 rflags
= LTT_RFLAG_ID
;
749 * Skip channels added after trace creation.
751 if (unlikely(chan_index
>= trace
->nr_channels
))
753 channel
= &trace
->channels
[chan_index
];
754 if (!channel
->active
)
757 /* reserve space : header and data */
758 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
759 data_size
, &slot_size
, &buf_offset
,
762 if (unlikely(ret
< 0))
763 continue; /* buffer full */
765 va_copy(args_copy
, *args
);
766 /* FIXME : could probably encapsulate transport better. */
767 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
768 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
769 /* Out-of-order write : header and data */
770 buf_offset
= ltt_write_event_header(trace
,
771 channel
, buf
, buf_offset
,
772 eID
, data_size
, tsc
, rflags
);
773 ltt_write_event_data(buf
, buf_offset
, &closure
,
775 largest_align
, fmt
, &args_copy
);
777 /* Out-of-order commit */
778 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
779 data_size
, slot_size
);
780 DBG("just commited event at offset %ld and size %zd", buf_offset
, slot_size
);
782 //ust// __get_cpu_var(ltt_nesting)--;
784 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
787 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
788 struct registers
*regs
, void *call_data
,
789 const char *fmt
, ...)
794 ltt_vtrace(mdata
, probe_data
, regs
, call_data
, fmt
, &args
);
798 //ust// MODULE_LICENSE("GPL");
799 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
800 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");