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.
30 #include <sys/syscall.h>
37 #include <urcu/rculist.h>
40 #include <ust/clock.h>
43 #include "usterr_signal_safe.h"
44 #include "ust_snprintf.h"
47 * Because UST core defines a non-const PAGE_SIZE, define PAGE_SIZE_STATIC here.
48 * It is just an approximation for the tracer stack.
50 #define PAGE_SIZE_STATIC 4096
54 LTT_TYPE_UNSIGNED_INT
,
60 * Special stack for the tracer. Keeps serialization offsets for each field.
61 * Per-thread. Deals with reentrancy from signals by simply ensuring that
62 * interrupting signals put the stack back to its original position.
64 #define TRACER_STACK_LEN (PAGE_SIZE_STATIC / sizeof(unsigned long))
65 static unsigned long __thread tracer_stack
[TRACER_STACK_LEN
];
67 static unsigned int __thread tracer_stack_pos
;
69 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
72 * Inspired from vsnprintf
74 * The serialization format string supports the basic printf format strings.
75 * In addition, it defines new formats that can be used to serialize more
76 * complex/non portable data structures.
81 * field_name #tracetype %ctype
82 * field_name #tracetype %ctype1 %ctype2 ...
84 * A conversion is performed between format string types supported by GCC and
85 * the trace type requested. GCC type is used to perform type checking on format
86 * strings. Trace type is used to specify the exact binary representation
87 * in the trace. A mapping is done between one or more GCC types to one trace
88 * type. Sign extension, if required by the conversion, is performed following
91 * If a gcc format is not declared with a trace format, the gcc format is
92 * also used as binary representation in the trace.
94 * Strings are supported with %s.
95 * A single tracetype (sequence) can take multiple c types as parameter.
101 * Note: to write a uint32_t in a trace, the following expression is recommended
102 * si it can be portable:
104 * ("#4u%lu", (unsigned long)var)
108 * Serialization specific formats :
110 * Fixed size integers
112 * #2u writes uint16_t
113 * #4u writes uint32_t
114 * #8u writes uint64_t
120 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
124 * n: (for network byte order)
126 * is written in the trace in network byte order.
128 * i.e.: #bn4u%lu, #n%lu, #b%u
131 * Variable length sequence
132 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
134 * #a specifies that this is a sequence
135 * #tracetype1 is the type of elements in the sequence
136 * #tracetype2 is the type of the element count
138 * array_ptr is a pointer to an array that contains members of size
140 * num_elems is the number of elements in the array.
141 * i.e.: #a #lu #lu %p %lu %u
144 * #k callback (taken from the probe data)
145 * The following % arguments are exepected by the callback
147 * i.e.: #a #lu #lu #k %p
149 * Note: No conversion is done from floats to integers, nor from integers to
150 * floats between c types and trace types. float conversion from double to float
151 * or from float to double is also not supported.
154 * %*b expects sizeof(data), data
155 * where sizeof(data) is 1, 2, 4 or 8
157 * Fixed length struct, union or array.
158 * FIXME: unable to extract those sizes statically.
159 * %*r expects sizeof(*ptr), ptr
160 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
161 * struct and unions removed.
162 * Fixed length array:
163 * [%p]#a[len #tracetype]
164 * i.e.: [%p]#a[12 #lu]
166 * Variable length sequence
167 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
168 * where elem_num is the number of elements in the sequence
170 static inline const char *parse_trace_type(const char *fmt
,
171 char *trace_size
, enum ltt_type
*trace_type
,
172 unsigned long *attributes
)
174 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
175 /* 'z' support added 23/7/1999 S.H. */
176 /* 'z' changed to 'Z' --davidm 1/25/99 */
177 /* 't' added for ptrdiff_t */
179 /* parse attributes. */
183 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
188 /* get the conversion qualifier */
190 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
191 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
192 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
193 *fmt
== '4' || *fmt
== 8) {
196 if (qualifier
== 'l' && *fmt
== 'l') {
204 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
205 *trace_size
= sizeof(unsigned char);
208 *trace_type
= LTT_TYPE_STRING
;
211 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
212 *trace_size
= sizeof(void *);
216 *trace_type
= LTT_TYPE_SIGNED_INT
;
222 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
231 *trace_size
= sizeof(long long);
234 *trace_size
= sizeof(long);
238 *trace_size
= sizeof(size_t);
241 //ust// *trace_size = sizeof(ptrdiff_t);
244 *trace_size
= sizeof(short);
247 *trace_size
= sizeof(uint8_t);
250 *trace_size
= sizeof(uint16_t);
253 *trace_size
= sizeof(uint32_t);
256 *trace_size
= sizeof(uint64_t);
259 *trace_size
= sizeof(int);
268 * Field width and precision are *not* supported.
272 const char *parse_c_type(const char *fmt
, char *c_size
, enum ltt_type
*c_type
,
275 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
276 /* 'z' support added 23/7/1999 S.H. */
277 /* 'z' changed to 'Z' --davidm 1/25/99 */
278 /* 't' added for ptrdiff_t */
280 /* process flags : ignore standard print formats for now. */
292 /* get the conversion qualifier */
294 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
295 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
299 if (qualifier
== 'l' && *fmt
== 'l') {
307 *outfmt
++ = (char)qualifier
;
314 *c_type
= LTT_TYPE_UNSIGNED_INT
;
315 *c_size
= sizeof(unsigned char);
318 *c_type
= LTT_TYPE_STRING
;
321 *c_type
= LTT_TYPE_UNSIGNED_INT
;
322 *c_size
= sizeof(void *);
326 *c_type
= LTT_TYPE_SIGNED_INT
;
332 *c_type
= LTT_TYPE_UNSIGNED_INT
;
341 *c_size
= sizeof(long long);
344 *c_size
= sizeof(long);
348 *c_size
= sizeof(size_t);
351 //ust// *c_size = sizeof(ptrdiff_t);
354 *c_size
= sizeof(short);
357 *c_size
= sizeof(int);
364 static inline size_t serialize_trace_data(struct ust_buffer
*buf
,
366 char trace_size
, enum ltt_type trace_type
,
367 char c_size
, enum ltt_type c_type
,
368 unsigned int *stack_pos_ctx
,
373 unsigned long v_ulong
;
382 * Be careful about sign extension here.
383 * Sign extension is done with the destination (trace) type.
385 switch (trace_type
) {
386 case LTT_TYPE_SIGNED_INT
:
389 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
392 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
395 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
398 tmp
.v_uint64
= va_arg(*args
, int64_t);
404 case LTT_TYPE_UNSIGNED_INT
:
407 tmp
.v_ulong
= (unsigned long)(uint8_t)va_arg(*args
, unsigned int);
410 tmp
.v_ulong
= (unsigned long)(uint16_t)va_arg(*args
, unsigned int);
413 tmp
.v_ulong
= (unsigned long)(uint32_t)va_arg(*args
, unsigned int);
416 tmp
.v_uint64
= va_arg(*args
, uint64_t);
422 case LTT_TYPE_STRING
:
423 tmp
.v_string
.s
= va_arg(*args
, const char *);
424 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
425 tmp
.v_string
.s
= "<NULL>";
428 * Reserve tracer stack entry.
431 assert(tracer_stack_pos
<= TRACER_STACK_LEN
);
433 tracer_stack
[*stack_pos_ctx
] =
434 strlen(tmp
.v_string
.s
) + 1;
436 tmp
.v_string
.len
= tracer_stack
[(*stack_pos_ctx
)++];
438 ust_buffers_strncpy(buf
, buf_offset
, tmp
.v_string
.s
,
440 buf_offset
+= tmp
.v_string
.len
;
447 * If trace_size is lower or equal to 4 bytes, there is no sign
448 * extension to do because we are already encoded in a long. Therefore,
449 * we can combine signed and unsigned ops. 4 bytes float also works
450 * with this, because we do a simple copy of 4 bytes into 4 bytes
451 * without manipulation (and we do not support conversion from integers
453 * It is also the case if c_size is 8 bytes, which is the largest
456 if (ltt_get_alignment()) {
457 buf_offset
+= ltt_align(buf_offset
, trace_size
);
459 *largest_align
= max_t(int, *largest_align
, trace_size
);
461 if (trace_size
<= 4 || c_size
== 8) {
463 switch (trace_size
) {
466 ust_buffers_write(buf
, buf_offset
,
467 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
470 ust_buffers_write(buf
, buf_offset
,
471 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
476 ust_buffers_write(buf
, buf_offset
,
477 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
480 ust_buffers_write(buf
, buf_offset
,
481 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
486 ust_buffers_write(buf
, buf_offset
,
487 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
490 ust_buffers_write(buf
, buf_offset
,
491 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
496 * c_size cannot be other than 8 here because
499 ust_buffers_write(buf
, buf_offset
,
500 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
507 buf_offset
+= trace_size
;
511 * Perform sign extension.
514 switch (trace_type
) {
515 case LTT_TYPE_SIGNED_INT
:
516 ust_buffers_write(buf
, buf_offset
,
517 (int64_t[]){ (int64_t)tmp
.v_ulong
},
520 case LTT_TYPE_UNSIGNED_INT
:
521 ust_buffers_write(buf
, buf_offset
,
522 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
529 buf_offset
+= trace_size
;
537 notrace
size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
538 struct ltt_serialize_closure
*closure
,
539 void *serialize_private
,
540 unsigned int stack_pos_ctx
,
542 const char *fmt
, va_list *args
)
544 char trace_size
= 0, c_size
= 0; /*
545 * 0 (unset), 1, 2, 4, 8 bytes.
547 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
548 unsigned long attributes
= 0;
550 for (; *fmt
; ++fmt
) {
554 ++fmt
; /* skip first '#' */
555 if (*fmt
== '#') /* Escaped ## */
558 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
563 ++fmt
; /* skip first '%' */
564 if (*fmt
== '%') /* Escaped %% */
566 fmt
= parse_c_type(fmt
, &c_size
, &c_type
, NULL
);
568 * Output c types if no trace types has been
573 if (trace_type
== LTT_TYPE_NONE
)
575 if (c_type
== LTT_TYPE_STRING
)
576 trace_type
= LTT_TYPE_STRING
;
577 /* perform trace write */
578 buf_offset
= serialize_trace_data(buf
,
579 buf_offset
, trace_size
,
580 trace_type
, c_size
, c_type
,
586 trace_type
= LTT_TYPE_NONE
;
587 c_size
= LTT_TYPE_NONE
;
590 /* default is to skip the text, doing nothing */
597 * Calculate data size
598 * Assume that the padding for alignment starts at a sizeof(void *) address.
600 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
601 void *serialize_private
,
602 unsigned int stack_pos_ctx
, int *largest_align
,
603 const char *fmt
, va_list *args
)
605 ltt_serialize_cb cb
= closure
->callbacks
[0];
607 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
608 stack_pos_ctx
, largest_align
, fmt
, args
);
612 void ltt_write_event_data(struct ust_buffer
*buf
, size_t buf_offset
,
613 struct ltt_serialize_closure
*closure
,
614 void *serialize_private
,
615 unsigned int stack_pos_ctx
,
617 const char *fmt
, va_list *args
)
619 ltt_serialize_cb cb
= closure
->callbacks
[0];
621 buf_offset
+= ltt_align(buf_offset
, largest_align
);
622 cb(buf
, buf_offset
, closure
, serialize_private
, stack_pos_ctx
, NULL
,
627 notrace
void ltt_vtrace(const struct ust_marker
*mdata
, void *probe_data
,
629 const char *fmt
, va_list *args
)
631 int largest_align
, ret
;
632 struct ltt_active_ust_marker
*pdata
;
634 size_t data_size
, slot_size
;
635 unsigned int chan_index
;
636 struct ust_channel
*channel
;
637 struct ust_trace
*trace
, *dest_trace
= NULL
;
638 struct ust_buffer
*buf
;
642 struct ltt_serialize_closure closure
;
643 struct ltt_probe_private_data
*private_data
= call_data
;
644 void *serialize_private
= NULL
;
647 unsigned int stack_pos_ctx
;
650 * This test is useful for quickly exiting static tracing when no trace
651 * is active. We expect to have an active trace when we get here.
653 if (unlikely(ltt_traces
.num_active_traces
== 0))
659 /* Force volatile access. */
660 CMM_STORE_SHARED(ltt_nesting
, CMM_LOAD_SHARED(ltt_nesting
) + 1);
661 stack_pos_ctx
= tracer_stack_pos
;
664 pdata
= (struct ltt_active_ust_marker
*)probe_data
;
665 eID
= mdata
->event_id
;
666 chan_index
= mdata
->channel_id
;
667 closure
.callbacks
= pdata
->probe
->callbacks
;
669 if (unlikely(private_data
)) {
670 dest_trace
= private_data
->trace
;
671 if (private_data
->serializer
)
672 closure
.callbacks
= &private_data
->serializer
;
673 serialize_private
= private_data
->serialize_private
;
676 va_copy(args_copy
, *args
);
678 * Assumes event payload to start on largest_align alignment.
680 largest_align
= 1; /* must be non-zero for ltt_align */
681 data_size
= ltt_get_data_size(&closure
, serialize_private
,
682 stack_pos_ctx
, &largest_align
,
684 largest_align
= min_t(int, largest_align
, sizeof(void *));
687 /* Iterate on each trace */
688 cds_list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
690 * Expect the filter to filter out events. If we get here,
691 * we went through tracepoint activation as a first step.
693 if (unlikely(dest_trace
&& trace
!= dest_trace
))
695 if (unlikely(!trace
->active
))
697 if (unlikely(!ltt_run_filter(trace
, eID
)))
699 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
700 rflags
= LTT_RFLAG_ID_SIZE
;
702 if (unlikely(eID
>= LTT_FREE_EVENTS
))
703 rflags
= LTT_RFLAG_ID
;
708 * Skip channels added after trace creation.
710 if (unlikely(chan_index
>= trace
->nr_channels
))
712 channel
= &trace
->channels
[chan_index
];
713 if (!channel
->active
)
717 * If a new cpu was plugged since the trace was started, we did
718 * not add it to the trace, and therefore we write the event to
721 if (cpu
>= channel
->n_cpus
) {
725 /* reserve space : header and data */
726 ret
= ltt_reserve_slot(channel
, trace
, data_size
, largest_align
,
727 cpu
, &buf
, &slot_size
, &buf_offset
,
729 if (unlikely(ret
< 0))
730 continue; /* buffer full */
732 va_copy(args_copy
, *args
);
733 /* FIXME : could probably encapsulate transport better. */
734 buf
= channel
->buf
[cpu
];
735 /* Out-of-order write : header and data */
736 buf_offset
= ltt_write_event_header(channel
, buf
, buf_offset
,
737 eID
, data_size
, tsc
, rflags
);
738 ltt_write_event_data(buf
, buf_offset
, &closure
,
740 stack_pos_ctx
, largest_align
,
743 /* Out-of-order commit */
744 ltt_commit_slot(channel
, buf
, buf_offset
, data_size
, slot_size
);
745 DBG("just commited event (%s/%s) at offset %ld and size %zd", mdata
->channel
, mdata
->name
, buf_offset
, slot_size
);
749 tracer_stack_pos
= stack_pos_ctx
;
750 CMM_STORE_SHARED(ltt_nesting
, CMM_LOAD_SHARED(ltt_nesting
) - 1);
755 notrace
void ltt_trace(const struct ust_marker
*mdata
, void *probe_data
,
757 const char *fmt
, ...)
762 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
766 static notrace
void skip_space(const char **ps
)
772 static notrace
void copy_token(char **out
, const char **in
)
774 while (**in
!= ' ' && **in
!= '\0') {
783 * Given a format string and a va_list of arguments, convert them to a
784 * human-readable string.
786 * @outbuf: the buffer to output the string to
787 * @bufsize: the max size that can be used in outbuf
788 * @fmt: the marker format string
789 * @ap: a va_list that contains the arguments corresponding to fmt
791 * Return value: the number of chars that have been put in outbuf, excluding
792 * the final \0, or, if the buffer was too small, the number of chars that
793 * would have been written in outbuf if it had been large enough.
795 * outbuf may be NULL. The return value may then be used be allocate an
796 * appropriate outbuf.
801 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
803 int fmt_len
= strlen(fmt
);
804 char *new_fmt
= alloca(fmt_len
+ 1);
805 const char *orig_fmt_p
= fmt
;
806 char *new_fmt_p
= new_fmt
;
809 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
811 while (*orig_fmt_p
!= '\0') {
812 if (*orig_fmt_p
== '%') {
814 copy_token(&new_fmt_p
, &orig_fmt_p
);
816 else if (*orig_fmt_p
== '#') {
817 prev_token
= tracefmt
;
820 } while (*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
822 else if (*orig_fmt_p
== ' ') {
823 if (prev_token
== argname
) {
827 else if (prev_token
== cfmt
) {
832 skip_space(&orig_fmt_p
);
835 prev_token
= argname
;
836 copy_token(&new_fmt_p
, &orig_fmt_p
);
842 if (outbuf
== NULL
) {
843 /* use this false_buffer for compatibility with pre-C99 */
847 result
= ust_safe_vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);