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.
29 #include <sys/syscall.h>
35 #include <ust/kernelcompat.h>
38 #include <urcu/rculist.h>
47 LTT_TYPE_UNSIGNED_INT
,
52 static int ust_get_cpu(void)
54 return sched_getcpu();
57 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
60 * Inspired from vsnprintf
62 * The serialization format string supports the basic printf format strings.
63 * In addition, it defines new formats that can be used to serialize more
64 * complex/non portable data structures.
69 * field_name #tracetype %ctype
70 * field_name #tracetype %ctype1 %ctype2 ...
72 * A conversion is performed between format string types supported by GCC and
73 * the trace type requested. GCC type is used to perform type checking on format
74 * strings. Trace type is used to specify the exact binary representation
75 * in the trace. A mapping is done between one or more GCC types to one trace
76 * type. Sign extension, if required by the conversion, is performed following
79 * If a gcc format is not declared with a trace format, the gcc format is
80 * also used as binary representation in the trace.
82 * Strings are supported with %s.
83 * A single tracetype (sequence) can take multiple c types as parameter.
89 * Note: to write a uint32_t in a trace, the following expression is recommended
90 * si it can be portable:
92 * ("#4u%lu", (unsigned long)var)
96 * Serialization specific formats :
100 * #2u writes uint16_t
101 * #4u writes uint32_t
102 * #8u writes uint64_t
108 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
112 * n: (for network byte order)
114 * is written in the trace in network byte order.
116 * i.e.: #bn4u%lu, #n%lu, #b%u
119 * Variable length sequence
120 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
122 * #a specifies that this is a sequence
123 * #tracetype1 is the type of elements in the sequence
124 * #tracetype2 is the type of the element count
126 * array_ptr is a pointer to an array that contains members of size
128 * num_elems is the number of elements in the array.
129 * i.e.: #a #lu #lu %p %lu %u
132 * #k callback (taken from the probe data)
133 * The following % arguments are exepected by the callback
135 * i.e.: #a #lu #lu #k %p
137 * Note: No conversion is done from floats to integers, nor from integers to
138 * floats between c types and trace types. float conversion from double to float
139 * or from float to double is also not supported.
142 * %*b expects sizeof(data), data
143 * where sizeof(data) is 1, 2, 4 or 8
145 * Fixed length struct, union or array.
146 * FIXME: unable to extract those sizes statically.
147 * %*r expects sizeof(*ptr), ptr
148 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
149 * struct and unions removed.
150 * Fixed length array:
151 * [%p]#a[len #tracetype]
152 * i.e.: [%p]#a[12 #lu]
154 * Variable length sequence
155 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
156 * where elem_num is the number of elements in the sequence
158 static inline const char *parse_trace_type(const char *fmt
,
159 char *trace_size
, enum ltt_type
*trace_type
,
160 unsigned long *attributes
)
162 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
163 /* 'z' support added 23/7/1999 S.H. */
164 /* 'z' changed to 'Z' --davidm 1/25/99 */
165 /* 't' added for ptrdiff_t */
167 /* parse attributes. */
171 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
176 /* get the conversion qualifier */
178 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
179 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
180 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
181 *fmt
== '4' || *fmt
== 8) {
184 if (qualifier
== 'l' && *fmt
== 'l') {
192 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
193 *trace_size
= sizeof(unsigned char);
196 *trace_type
= LTT_TYPE_STRING
;
199 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
200 *trace_size
= sizeof(void *);
204 *trace_type
= LTT_TYPE_SIGNED_INT
;
210 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
219 *trace_size
= sizeof(long long);
222 *trace_size
= sizeof(long);
226 *trace_size
= sizeof(size_t);
229 //ust// *trace_size = sizeof(ptrdiff_t);
232 *trace_size
= sizeof(short);
235 *trace_size
= sizeof(uint8_t);
238 *trace_size
= sizeof(uint16_t);
241 *trace_size
= sizeof(uint32_t);
244 *trace_size
= sizeof(uint64_t);
247 *trace_size
= sizeof(int);
256 * Field width and precision are *not* supported.
259 static inline const char *parse_c_type(const char *fmt
,
260 char *c_size
, enum ltt_type
*c_type
)
262 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
263 /* 'z' support added 23/7/1999 S.H. */
264 /* 'z' changed to 'Z' --davidm 1/25/99 */
265 /* 't' added for ptrdiff_t */
267 /* process flags : ignore standard print formats for now. */
279 /* get the conversion qualifier */
281 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
282 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
286 if (qualifier
== 'l' && *fmt
== 'l') {
294 *c_type
= LTT_TYPE_UNSIGNED_INT
;
295 *c_size
= sizeof(unsigned char);
298 *c_type
= LTT_TYPE_STRING
;
301 *c_type
= LTT_TYPE_UNSIGNED_INT
;
302 *c_size
= sizeof(void *);
306 *c_type
= LTT_TYPE_SIGNED_INT
;
312 *c_type
= LTT_TYPE_UNSIGNED_INT
;
321 *c_size
= sizeof(long long);
324 *c_size
= sizeof(long);
328 *c_size
= sizeof(size_t);
331 //ust// *c_size = sizeof(ptrdiff_t);
334 *c_size
= sizeof(short);
337 *c_size
= sizeof(int);
344 static inline size_t serialize_trace_data(struct ust_buffer
*buf
,
346 char trace_size
, enum ltt_type trace_type
,
347 char c_size
, enum ltt_type c_type
,
348 int *largest_align
, va_list *args
)
351 unsigned long v_ulong
;
360 * Be careful about sign extension here.
361 * Sign extension is done with the destination (trace) type.
363 switch (trace_type
) {
364 case LTT_TYPE_SIGNED_INT
:
367 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
370 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
373 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
376 tmp
.v_uint64
= va_arg(*args
, int64_t);
382 case LTT_TYPE_UNSIGNED_INT
:
385 tmp
.v_ulong
= (unsigned long)(uint8_t)
386 va_arg(*args
, unsigned int);
389 tmp
.v_ulong
= (unsigned long)(uint16_t)
390 va_arg(*args
, unsigned int);
393 tmp
.v_ulong
= (unsigned long)(uint32_t)
394 va_arg(*args
, unsigned int);
397 tmp
.v_uint64
= va_arg(*args
, uint64_t);
403 case LTT_TYPE_STRING
:
404 tmp
.v_string
.s
= va_arg(*args
, const char *);
405 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
406 tmp
.v_string
.s
= "<NULL>";
407 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
409 ust_buffers_write(buf
, buf_offset
, tmp
.v_string
.s
,
411 buf_offset
+= tmp
.v_string
.len
;
418 * If trace_size is lower or equal to 4 bytes, there is no sign
419 * extension to do because we are already encoded in a long. Therefore,
420 * we can combine signed and unsigned ops. 4 bytes float also works
421 * with this, because we do a simple copy of 4 bytes into 4 bytes
422 * without manipulation (and we do not support conversion from integers
424 * It is also the case if c_size is 8 bytes, which is the largest
427 if (ltt_get_alignment()) {
428 buf_offset
+= ltt_align(buf_offset
, trace_size
);
430 *largest_align
= max_t(int, *largest_align
, trace_size
);
432 if (trace_size
<= 4 || c_size
== 8) {
434 switch (trace_size
) {
437 ust_buffers_write(buf
, buf_offset
,
438 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
441 ust_buffers_write(buf
, buf_offset
,
442 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
447 ust_buffers_write(buf
, buf_offset
,
448 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
451 ust_buffers_write(buf
, buf_offset
,
452 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
457 ust_buffers_write(buf
, buf_offset
,
458 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
461 ust_buffers_write(buf
, buf_offset
,
462 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
467 * c_size cannot be other than 8 here because
470 ust_buffers_write(buf
, buf_offset
,
471 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
478 buf_offset
+= trace_size
;
482 * Perform sign extension.
485 switch (trace_type
) {
486 case LTT_TYPE_SIGNED_INT
:
487 ust_buffers_write(buf
, buf_offset
,
488 (int64_t[]){ (int64_t)tmp
.v_ulong
},
491 case LTT_TYPE_UNSIGNED_INT
:
492 ust_buffers_write(buf
, buf_offset
,
493 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
500 buf_offset
+= trace_size
;
508 static notrace
void skip_space(const char **ps
)
514 static notrace
void copy_token(char **out
, const char **in
)
516 while(**in
!= ' ' && **in
!= '\0') {
525 * Given a format string and a va_list of arguments, convert them to a
526 * human-readable string.
528 * @outbuf: the buffer to output the string to
529 * @bufsize: the max size that can be used in outbuf
530 * @fmt: the marker format string
531 * @ap: a va_list that contains the arguments corresponding to fmt
533 * Return value: the number of chars that have been put in outbuf, excluding
534 * the final \0, or, if the buffer was too small, the number of chars that
535 * would have been written in outbuf if it had been large enough.
537 * outbuf may be NULL. The return value may then be used be allocate an
538 * appropriate outbuf.
543 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
545 int fmt_len
= strlen(fmt
);
546 char *new_fmt
= alloca(fmt_len
+ 1);
547 const char *orig_fmt_p
= fmt
;
548 char *new_fmt_p
= new_fmt
;
551 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
553 while(*orig_fmt_p
!= '\0') {
554 if(*orig_fmt_p
== '%') {
556 copy_token(&new_fmt_p
, &orig_fmt_p
);
558 else if(*orig_fmt_p
== '#') {
559 prev_token
= tracefmt
;
562 } while(*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
564 else if(*orig_fmt_p
== ' ') {
565 if(prev_token
== argname
) {
569 else if(prev_token
== cfmt
) {
574 skip_space(&orig_fmt_p
);
577 prev_token
= argname
;
578 copy_token(&new_fmt_p
, &orig_fmt_p
);
585 /* use this false_buffer for compatibility with pre-C99 */
589 result
= vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);
594 notrace
size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
595 struct ltt_serialize_closure
*closure
,
596 void *serialize_private
, int *largest_align
,
597 const char *fmt
, va_list *args
)
599 char trace_size
= 0, c_size
= 0; /*
600 * 0 (unset), 1, 2, 4, 8 bytes.
602 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
603 unsigned long attributes
= 0;
605 for (; *fmt
; ++fmt
) {
609 ++fmt
; /* skip first '#' */
610 if (*fmt
== '#') /* Escaped ## */
613 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
618 ++fmt
; /* skip first '%' */
619 if (*fmt
== '%') /* Escaped %% */
621 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
623 * Output c types if no trace types has been
628 if (trace_type
== LTT_TYPE_NONE
)
630 if (c_type
== LTT_TYPE_STRING
)
631 trace_type
= LTT_TYPE_STRING
;
632 /* perform trace write */
633 buf_offset
= serialize_trace_data(buf
,
634 buf_offset
, trace_size
,
635 trace_type
, c_size
, c_type
,
636 largest_align
, args
);
639 trace_type
= LTT_TYPE_NONE
;
640 c_size
= LTT_TYPE_NONE
;
643 /* default is to skip the text, doing nothing */
650 * Calculate data size
651 * Assume that the padding for alignment starts at a sizeof(void *) address.
653 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
654 void *serialize_private
, int *largest_align
,
655 const char *fmt
, va_list *args
)
657 ltt_serialize_cb cb
= closure
->callbacks
[0];
659 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
660 largest_align
, fmt
, args
);
664 void ltt_write_event_data(struct ust_buffer
*buf
, size_t buf_offset
,
665 struct ltt_serialize_closure
*closure
,
666 void *serialize_private
, int largest_align
,
667 const char *fmt
, va_list *args
)
669 ltt_serialize_cb cb
= closure
->callbacks
[0];
671 buf_offset
+= ltt_align(buf_offset
, largest_align
);
672 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
676 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
677 struct registers
*regs
, void *call_data
,
678 const char *fmt
, va_list *args
)
680 int largest_align
, ret
;
681 struct ltt_active_marker
*pdata
;
683 size_t data_size
, slot_size
;
684 unsigned int chan_index
;
685 struct ust_channel
*channel
;
686 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
687 struct ust_buffer
*buf
;
688 void *transport_data
;
692 struct ltt_serialize_closure closure
;
693 struct ltt_probe_private_data
*private_data
= call_data
;
694 void *serialize_private
= NULL
;
699 * This test is useful for quickly exiting static tracing when no trace
700 * is active. We expect to have an active trace when we get here.
702 if (unlikely(ltt_traces
.num_active_traces
== 0))
705 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
706 //ust// cpu = smp_processor_id();
708 //ust// __get_cpu_var(ltt_nesting)++;
709 /* FIXME: should nesting be per-cpu? */
712 pdata
= (struct ltt_active_marker
*)probe_data
;
713 eID
= mdata
->event_id
;
714 chan_index
= mdata
->channel_id
;
715 closure
.callbacks
= pdata
->probe
->callbacks
;
717 if (unlikely(private_data
)) {
718 dest_trace
= private_data
->trace
;
719 if (private_data
->serializer
)
720 closure
.callbacks
= &private_data
->serializer
;
721 serialize_private
= private_data
->serialize_private
;
724 va_copy(args_copy
, *args
);
726 * Assumes event payload to start on largest_align alignment.
728 largest_align
= 1; /* must be non-zero for ltt_align */
729 data_size
= ltt_get_data_size(&closure
, serialize_private
,
730 &largest_align
, fmt
, &args_copy
);
731 largest_align
= min_t(int, largest_align
, sizeof(void *));
734 /* Iterate on each trace */
735 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
737 * Expect the filter to filter out events. If we get here,
738 * we went through tracepoint activation as a first step.
740 if (unlikely(dest_trace
&& trace
!= dest_trace
))
742 if (unlikely(!trace
->active
))
744 if (unlikely(!ltt_run_filter(trace
, eID
)))
746 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
747 rflags
= LTT_RFLAG_ID_SIZE
;
749 if (unlikely(eID
>= LTT_FREE_EVENTS
))
750 rflags
= LTT_RFLAG_ID
;
755 * Skip channels added after trace creation.
757 if (unlikely(chan_index
>= trace
->nr_channels
))
759 channel
= &trace
->channels
[chan_index
];
760 if (!channel
->active
)
763 /* If a new cpu was plugged since the trace was started, we did
764 * not add it to the trace, and therefore we write the event to
767 if(cpu
>= channel
->n_cpus
) {
771 /* reserve space : header and data */
772 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
773 data_size
, &slot_size
, &buf_offset
,
776 if (unlikely(ret
< 0))
777 continue; /* buffer full */
779 va_copy(args_copy
, *args
);
780 /* FIXME : could probably encapsulate transport better. */
781 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
782 buf
= channel
->buf
[cpu
];
783 /* Out-of-order write : header and data */
784 buf_offset
= ltt_write_event_header(trace
,
786 eID
, data_size
, tsc
, rflags
);
787 ltt_write_event_data(buf
, buf_offset
, &closure
,
789 largest_align
, fmt
, &args_copy
);
791 /* Out-of-order commit */
792 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
793 data_size
, slot_size
);
794 DBG("just commited event at offset %ld and size %zd", buf_offset
, slot_size
);
796 //ust// __get_cpu_var(ltt_nesting)--;
798 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
801 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
802 struct registers
*regs
, void *call_data
,
803 const char *fmt
, ...)
808 ltt_vtrace(mdata
, probe_data
, regs
, call_data
, fmt
, &args
);
812 //ust// MODULE_LICENSE("GPL");
813 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
814 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");