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.
32 #include <ust/kernelcompat.h>
35 #include <urcu/rculist.h>
44 LTT_TYPE_UNSIGNED_INT
,
49 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
52 * Inspired from vsnprintf
54 * The serialization format string supports the basic printf format strings.
55 * In addition, it defines new formats that can be used to serialize more
56 * complex/non portable data structures.
61 * field_name #tracetype %ctype
62 * field_name #tracetype %ctype1 %ctype2 ...
64 * A conversion is performed between format string types supported by GCC and
65 * the trace type requested. GCC type is used to perform type checking on format
66 * strings. Trace type is used to specify the exact binary representation
67 * in the trace. A mapping is done between one or more GCC types to one trace
68 * type. Sign extension, if required by the conversion, is performed following
71 * If a gcc format is not declared with a trace format, the gcc format is
72 * also used as binary representation in the trace.
74 * Strings are supported with %s.
75 * A single tracetype (sequence) can take multiple c types as parameter.
81 * Note: to write a uint32_t in a trace, the following expression is recommended
82 * si it can be portable:
84 * ("#4u%lu", (unsigned long)var)
88 * Serialization specific formats :
100 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
104 * n: (for network byte order)
106 * is written in the trace in network byte order.
108 * i.e.: #bn4u%lu, #n%lu, #b%u
111 * Variable length sequence
112 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
114 * #a specifies that this is a sequence
115 * #tracetype1 is the type of elements in the sequence
116 * #tracetype2 is the type of the element count
118 * array_ptr is a pointer to an array that contains members of size
120 * num_elems is the number of elements in the array.
121 * i.e.: #a #lu #lu %p %lu %u
124 * #k callback (taken from the probe data)
125 * The following % arguments are exepected by the callback
127 * i.e.: #a #lu #lu #k %p
129 * Note: No conversion is done from floats to integers, nor from integers to
130 * floats between c types and trace types. float conversion from double to float
131 * or from float to double is also not supported.
134 * %*b expects sizeof(data), data
135 * where sizeof(data) is 1, 2, 4 or 8
137 * Fixed length struct, union or array.
138 * FIXME: unable to extract those sizes statically.
139 * %*r expects sizeof(*ptr), ptr
140 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
141 * struct and unions removed.
142 * Fixed length array:
143 * [%p]#a[len #tracetype]
144 * i.e.: [%p]#a[12 #lu]
146 * Variable length sequence
147 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
148 * where elem_num is the number of elements in the sequence
150 static inline const char *parse_trace_type(const char *fmt
,
151 char *trace_size
, enum ltt_type
*trace_type
,
152 unsigned long *attributes
)
154 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
155 /* 'z' support added 23/7/1999 S.H. */
156 /* 'z' changed to 'Z' --davidm 1/25/99 */
157 /* 't' added for ptrdiff_t */
159 /* parse attributes. */
163 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
168 /* get the conversion qualifier */
170 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
171 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
172 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
173 *fmt
== '4' || *fmt
== 8) {
176 if (qualifier
== 'l' && *fmt
== 'l') {
184 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
185 *trace_size
= sizeof(unsigned char);
188 *trace_type
= LTT_TYPE_STRING
;
191 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
192 *trace_size
= sizeof(void *);
196 *trace_type
= LTT_TYPE_SIGNED_INT
;
202 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
211 *trace_size
= sizeof(long long);
214 *trace_size
= sizeof(long);
218 *trace_size
= sizeof(size_t);
221 //ust// *trace_size = sizeof(ptrdiff_t);
224 *trace_size
= sizeof(short);
227 *trace_size
= sizeof(uint8_t);
230 *trace_size
= sizeof(uint16_t);
233 *trace_size
= sizeof(uint32_t);
236 *trace_size
= sizeof(uint64_t);
239 *trace_size
= sizeof(int);
248 * Field width and precision are *not* supported.
251 static inline const char *parse_c_type(const char *fmt
,
252 char *c_size
, enum ltt_type
*c_type
)
254 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
255 /* 'z' support added 23/7/1999 S.H. */
256 /* 'z' changed to 'Z' --davidm 1/25/99 */
257 /* 't' added for ptrdiff_t */
259 /* process flags : ignore standard print formats for now. */
271 /* get the conversion qualifier */
273 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
274 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
278 if (qualifier
== 'l' && *fmt
== 'l') {
286 *c_type
= LTT_TYPE_UNSIGNED_INT
;
287 *c_size
= sizeof(unsigned char);
290 *c_type
= LTT_TYPE_STRING
;
293 *c_type
= LTT_TYPE_UNSIGNED_INT
;
294 *c_size
= sizeof(void *);
298 *c_type
= LTT_TYPE_SIGNED_INT
;
304 *c_type
= LTT_TYPE_UNSIGNED_INT
;
313 *c_size
= sizeof(long long);
316 *c_size
= sizeof(long);
320 *c_size
= sizeof(size_t);
323 //ust// *c_size = sizeof(ptrdiff_t);
326 *c_size
= sizeof(short);
329 *c_size
= sizeof(int);
336 static inline size_t serialize_trace_data(struct ust_buffer
*buf
,
338 char trace_size
, enum ltt_type trace_type
,
339 char c_size
, enum ltt_type c_type
,
340 int *largest_align
, va_list *args
)
343 unsigned long v_ulong
;
352 * Be careful about sign extension here.
353 * Sign extension is done with the destination (trace) type.
355 switch (trace_type
) {
356 case LTT_TYPE_SIGNED_INT
:
359 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
362 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
365 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
368 tmp
.v_uint64
= va_arg(*args
, int64_t);
374 case LTT_TYPE_UNSIGNED_INT
:
377 tmp
.v_ulong
= (unsigned long)(uint8_t)
378 va_arg(*args
, unsigned int);
381 tmp
.v_ulong
= (unsigned long)(uint16_t)
382 va_arg(*args
, unsigned int);
385 tmp
.v_ulong
= (unsigned long)(uint32_t)
386 va_arg(*args
, unsigned int);
389 tmp
.v_uint64
= va_arg(*args
, uint64_t);
395 case LTT_TYPE_STRING
:
396 tmp
.v_string
.s
= va_arg(*args
, const char *);
397 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
398 tmp
.v_string
.s
= "<NULL>";
399 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
401 ust_buffers_write(buf
, buf_offset
, tmp
.v_string
.s
,
403 buf_offset
+= tmp
.v_string
.len
;
410 * If trace_size is lower or equal to 4 bytes, there is no sign
411 * extension to do because we are already encoded in a long. Therefore,
412 * we can combine signed and unsigned ops. 4 bytes float also works
413 * with this, because we do a simple copy of 4 bytes into 4 bytes
414 * without manipulation (and we do not support conversion from integers
416 * It is also the case if c_size is 8 bytes, which is the largest
419 if (ltt_get_alignment()) {
420 buf_offset
+= ltt_align(buf_offset
, trace_size
);
422 *largest_align
= max_t(int, *largest_align
, trace_size
);
424 if (trace_size
<= 4 || c_size
== 8) {
426 switch (trace_size
) {
429 ust_buffers_write(buf
, buf_offset
,
430 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
433 ust_buffers_write(buf
, buf_offset
,
434 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
439 ust_buffers_write(buf
, buf_offset
,
440 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
443 ust_buffers_write(buf
, buf_offset
,
444 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
449 ust_buffers_write(buf
, buf_offset
,
450 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
453 ust_buffers_write(buf
, buf_offset
,
454 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
459 * c_size cannot be other than 8 here because
462 ust_buffers_write(buf
, buf_offset
,
463 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
470 buf_offset
+= trace_size
;
474 * Perform sign extension.
477 switch (trace_type
) {
478 case LTT_TYPE_SIGNED_INT
:
479 ust_buffers_write(buf
, buf_offset
,
480 (int64_t[]){ (int64_t)tmp
.v_ulong
},
483 case LTT_TYPE_UNSIGNED_INT
:
484 ust_buffers_write(buf
, buf_offset
,
485 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
492 buf_offset
+= trace_size
;
500 static notrace
void skip_space(const char **ps
)
506 static notrace
void copy_token(char **out
, const char **in
)
508 while(**in
!= ' ' && **in
!= '\0') {
517 * Given a format string and a va_list of arguments, convert them to a
518 * human-readable string.
520 * @outbuf: the buffer to output the string to
521 * @bufsize: the max size that can be used in outbuf
522 * @fmt: the marker format string
523 * @ap: a va_list that contains the arguments corresponding to fmt
525 * Return value: the number of chars that have been put in outbuf, excluding
526 * the final \0, or, if the buffer was too small, the number of chars that
527 * would have been written in outbuf if it had been large enough.
529 * outbuf may be NULL. The return value may then be used be allocate an
530 * appropriate outbuf.
535 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
537 int fmt_len
= strlen(fmt
);
538 char *new_fmt
= alloca(fmt_len
+ 1);
539 const char *orig_fmt_p
= fmt
;
540 char *new_fmt_p
= new_fmt
;
543 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
545 while(*orig_fmt_p
!= '\0') {
546 if(*orig_fmt_p
== '%') {
548 copy_token(&new_fmt_p
, &orig_fmt_p
);
550 else if(*orig_fmt_p
== '#') {
551 prev_token
= tracefmt
;
554 } while(*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
556 else if(*orig_fmt_p
== ' ') {
557 if(prev_token
== argname
) {
561 else if(prev_token
== cfmt
) {
566 skip_space(&orig_fmt_p
);
569 prev_token
= argname
;
570 copy_token(&new_fmt_p
, &orig_fmt_p
);
577 /* use this false_buffer for compatibility with pre-C99 */
581 result
= vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);
586 notrace
size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
587 struct ltt_serialize_closure
*closure
,
588 void *serialize_private
, int *largest_align
,
589 const char *fmt
, va_list *args
)
591 char trace_size
= 0, c_size
= 0; /*
592 * 0 (unset), 1, 2, 4, 8 bytes.
594 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
595 unsigned long attributes
= 0;
597 for (; *fmt
; ++fmt
) {
601 ++fmt
; /* skip first '#' */
602 if (*fmt
== '#') /* Escaped ## */
605 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
610 ++fmt
; /* skip first '%' */
611 if (*fmt
== '%') /* Escaped %% */
613 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
615 * Output c types if no trace types has been
620 if (trace_type
== LTT_TYPE_NONE
)
622 if (c_type
== LTT_TYPE_STRING
)
623 trace_type
= LTT_TYPE_STRING
;
624 /* perform trace write */
625 buf_offset
= serialize_trace_data(buf
,
626 buf_offset
, trace_size
,
627 trace_type
, c_size
, c_type
,
628 largest_align
, args
);
631 trace_type
= LTT_TYPE_NONE
;
632 c_size
= LTT_TYPE_NONE
;
635 /* default is to skip the text, doing nothing */
642 * Calculate data size
643 * Assume that the padding for alignment starts at a sizeof(void *) address.
645 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
646 void *serialize_private
, int *largest_align
,
647 const char *fmt
, va_list *args
)
649 ltt_serialize_cb cb
= closure
->callbacks
[0];
651 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
652 largest_align
, fmt
, args
);
656 void ltt_write_event_data(struct ust_buffer
*buf
, size_t buf_offset
,
657 struct ltt_serialize_closure
*closure
,
658 void *serialize_private
, int largest_align
,
659 const char *fmt
, va_list *args
)
661 ltt_serialize_cb cb
= closure
->callbacks
[0];
663 buf_offset
+= ltt_align(buf_offset
, largest_align
);
664 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
668 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
669 struct registers
*regs
, void *call_data
,
670 const char *fmt
, va_list *args
)
672 int largest_align
, ret
;
673 struct ltt_active_marker
*pdata
;
675 size_t data_size
, slot_size
;
676 unsigned int chan_index
;
677 struct ust_channel
*channel
;
678 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
679 struct ust_buffer
*buf
;
680 void *transport_data
;
684 struct ltt_serialize_closure closure
;
685 struct ltt_probe_private_data
*private_data
= call_data
;
686 void *serialize_private
= NULL
;
691 * This test is useful for quickly exiting static tracing when no trace
692 * is active. We expect to have an active trace when we get here.
694 if (unlikely(ltt_traces
.num_active_traces
== 0))
697 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
698 //ust// cpu = smp_processor_id();
699 //ust// __get_cpu_var(ltt_nesting)++;
702 pdata
= (struct ltt_active_marker
*)probe_data
;
703 eID
= mdata
->event_id
;
704 chan_index
= mdata
->channel_id
;
705 closure
.callbacks
= pdata
->probe
->callbacks
;
707 if (unlikely(private_data
)) {
708 dest_trace
= private_data
->trace
;
709 if (private_data
->serializer
)
710 closure
.callbacks
= &private_data
->serializer
;
711 serialize_private
= private_data
->serialize_private
;
714 va_copy(args_copy
, *args
);
716 * Assumes event payload to start on largest_align alignment.
718 largest_align
= 1; /* must be non-zero for ltt_align */
719 data_size
= ltt_get_data_size(&closure
, serialize_private
,
720 &largest_align
, fmt
, &args_copy
);
721 largest_align
= min_t(int, largest_align
, sizeof(void *));
724 /* Iterate on each trace */
725 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
727 * Expect the filter to filter out events. If we get here,
728 * we went through tracepoint activation as a first step.
730 if (unlikely(dest_trace
&& trace
!= dest_trace
))
732 if (unlikely(!trace
->active
))
734 if (unlikely(!ltt_run_filter(trace
, eID
)))
736 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
737 rflags
= LTT_RFLAG_ID_SIZE
;
739 if (unlikely(eID
>= LTT_FREE_EVENTS
))
740 rflags
= LTT_RFLAG_ID
;
745 * Skip channels added after trace creation.
747 if (unlikely(chan_index
>= trace
->nr_channels
))
749 channel
= &trace
->channels
[chan_index
];
750 if (!channel
->active
)
753 /* reserve space : header and data */
754 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
755 data_size
, &slot_size
, &buf_offset
,
758 if (unlikely(ret
< 0))
759 continue; /* buffer full */
761 va_copy(args_copy
, *args
);
762 /* FIXME : could probably encapsulate transport better. */
763 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
765 /* Out-of-order write : header and data */
766 buf_offset
= ltt_write_event_header(trace
,
768 eID
, data_size
, tsc
, rflags
);
769 ltt_write_event_data(buf
, buf_offset
, &closure
,
771 largest_align
, fmt
, &args_copy
);
773 /* Out-of-order commit */
774 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
775 data_size
, slot_size
);
776 DBG("just commited event at offset %ld and size %zd", buf_offset
, slot_size
);
778 //ust// __get_cpu_var(ltt_nesting)--;
780 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
783 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
784 struct registers
*regs
, void *call_data
,
785 const char *fmt
, ...)
790 ltt_vtrace(mdata
, probe_data
, regs
, call_data
, fmt
, &args
);
794 //ust// MODULE_LICENSE("GPL");
795 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
796 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");