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>
37 #include <urcu/rculist.h>
39 #include <ust/kernelcompat.h>
45 #include "ust_snprintf.h"
49 LTT_TYPE_UNSIGNED_INT
,
54 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
57 * Inspired from vsnprintf
59 * The serialization format string supports the basic printf format strings.
60 * In addition, it defines new formats that can be used to serialize more
61 * complex/non portable data structures.
66 * field_name #tracetype %ctype
67 * field_name #tracetype %ctype1 %ctype2 ...
69 * A conversion is performed between format string types supported by GCC and
70 * the trace type requested. GCC type is used to perform type checking on format
71 * strings. Trace type is used to specify the exact binary representation
72 * in the trace. A mapping is done between one or more GCC types to one trace
73 * type. Sign extension, if required by the conversion, is performed following
76 * If a gcc format is not declared with a trace format, the gcc format is
77 * also used as binary representation in the trace.
79 * Strings are supported with %s.
80 * A single tracetype (sequence) can take multiple c types as parameter.
86 * Note: to write a uint32_t in a trace, the following expression is recommended
87 * si it can be portable:
89 * ("#4u%lu", (unsigned long)var)
93 * Serialization specific formats :
105 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
109 * n: (for network byte order)
111 * is written in the trace in network byte order.
113 * i.e.: #bn4u%lu, #n%lu, #b%u
116 * Variable length sequence
117 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
119 * #a specifies that this is a sequence
120 * #tracetype1 is the type of elements in the sequence
121 * #tracetype2 is the type of the element count
123 * array_ptr is a pointer to an array that contains members of size
125 * num_elems is the number of elements in the array.
126 * i.e.: #a #lu #lu %p %lu %u
129 * #k callback (taken from the probe data)
130 * The following % arguments are exepected by the callback
132 * i.e.: #a #lu #lu #k %p
134 * Note: No conversion is done from floats to integers, nor from integers to
135 * floats between c types and trace types. float conversion from double to float
136 * or from float to double is also not supported.
139 * %*b expects sizeof(data), data
140 * where sizeof(data) is 1, 2, 4 or 8
142 * Fixed length struct, union or array.
143 * FIXME: unable to extract those sizes statically.
144 * %*r expects sizeof(*ptr), ptr
145 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
146 * struct and unions removed.
147 * Fixed length array:
148 * [%p]#a[len #tracetype]
149 * i.e.: [%p]#a[12 #lu]
151 * Variable length sequence
152 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
153 * where elem_num is the number of elements in the sequence
155 static inline const char *parse_trace_type(const char *fmt
,
156 char *trace_size
, enum ltt_type
*trace_type
,
157 unsigned long *attributes
)
159 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
160 /* 'z' support added 23/7/1999 S.H. */
161 /* 'z' changed to 'Z' --davidm 1/25/99 */
162 /* 't' added for ptrdiff_t */
164 /* parse attributes. */
168 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
173 /* get the conversion qualifier */
175 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
176 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
177 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
178 *fmt
== '4' || *fmt
== 8) {
181 if (qualifier
== 'l' && *fmt
== 'l') {
189 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
190 *trace_size
= sizeof(unsigned char);
193 *trace_type
= LTT_TYPE_STRING
;
196 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
197 *trace_size
= sizeof(void *);
201 *trace_type
= LTT_TYPE_SIGNED_INT
;
207 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
216 *trace_size
= sizeof(long long);
219 *trace_size
= sizeof(long);
223 *trace_size
= sizeof(size_t);
226 //ust// *trace_size = sizeof(ptrdiff_t);
229 *trace_size
= sizeof(short);
232 *trace_size
= sizeof(uint8_t);
235 *trace_size
= sizeof(uint16_t);
238 *trace_size
= sizeof(uint32_t);
241 *trace_size
= sizeof(uint64_t);
244 *trace_size
= sizeof(int);
253 * Field width and precision are *not* supported.
257 const char *parse_c_type(const char *fmt
, char *c_size
, enum ltt_type
*c_type
,
260 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
261 /* 'z' support added 23/7/1999 S.H. */
262 /* 'z' changed to 'Z' --davidm 1/25/99 */
263 /* 't' added for ptrdiff_t */
265 /* process flags : ignore standard print formats for now. */
277 /* get the conversion qualifier */
279 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
280 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
284 if (qualifier
== 'l' && *fmt
== 'l') {
292 *outfmt
++ = (char)qualifier
;
299 *c_type
= LTT_TYPE_UNSIGNED_INT
;
300 *c_size
= sizeof(unsigned char);
303 *c_type
= LTT_TYPE_STRING
;
306 *c_type
= LTT_TYPE_UNSIGNED_INT
;
307 *c_size
= sizeof(void *);
311 *c_type
= LTT_TYPE_SIGNED_INT
;
317 *c_type
= LTT_TYPE_UNSIGNED_INT
;
326 *c_size
= sizeof(long long);
329 *c_size
= sizeof(long);
333 *c_size
= sizeof(size_t);
336 //ust// *c_size = sizeof(ptrdiff_t);
339 *c_size
= sizeof(short);
342 *c_size
= sizeof(int);
349 static inline size_t serialize_trace_data(struct ust_buffer
*buf
,
351 char trace_size
, enum ltt_type trace_type
,
352 char c_size
, enum ltt_type c_type
,
353 int *largest_align
, va_list *args
)
356 unsigned long v_ulong
;
365 * Be careful about sign extension here.
366 * Sign extension is done with the destination (trace) type.
368 switch (trace_type
) {
369 case LTT_TYPE_SIGNED_INT
:
372 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
375 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
378 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
381 tmp
.v_uint64
= va_arg(*args
, int64_t);
387 case LTT_TYPE_UNSIGNED_INT
:
390 tmp
.v_ulong
= (unsigned long)(uint8_t)va_arg(*args
, unsigned int);
393 tmp
.v_ulong
= (unsigned long)(uint16_t)va_arg(*args
, unsigned int);
396 tmp
.v_ulong
= (unsigned long)(uint32_t)va_arg(*args
, unsigned int);
399 tmp
.v_uint64
= va_arg(*args
, uint64_t);
405 case LTT_TYPE_STRING
:
406 tmp
.v_string
.s
= va_arg(*args
, const char *);
407 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
408 tmp
.v_string
.s
= "<NULL>";
409 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
411 ust_buffers_write(buf
, buf_offset
, tmp
.v_string
.s
,
413 buf_offset
+= tmp
.v_string
.len
;
420 * If trace_size is lower or equal to 4 bytes, there is no sign
421 * extension to do because we are already encoded in a long. Therefore,
422 * we can combine signed and unsigned ops. 4 bytes float also works
423 * with this, because we do a simple copy of 4 bytes into 4 bytes
424 * without manipulation (and we do not support conversion from integers
426 * It is also the case if c_size is 8 bytes, which is the largest
429 if (ltt_get_alignment()) {
430 buf_offset
+= ltt_align(buf_offset
, trace_size
);
432 *largest_align
= max_t(int, *largest_align
, trace_size
);
434 if (trace_size
<= 4 || c_size
== 8) {
436 switch (trace_size
) {
439 ust_buffers_write(buf
, buf_offset
,
440 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
443 ust_buffers_write(buf
, buf_offset
,
444 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
449 ust_buffers_write(buf
, buf_offset
,
450 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
453 ust_buffers_write(buf
, buf_offset
,
454 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
459 ust_buffers_write(buf
, buf_offset
,
460 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
463 ust_buffers_write(buf
, buf_offset
,
464 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
469 * c_size cannot be other than 8 here because
472 ust_buffers_write(buf
, buf_offset
,
473 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
480 buf_offset
+= trace_size
;
484 * Perform sign extension.
487 switch (trace_type
) {
488 case LTT_TYPE_SIGNED_INT
:
489 ust_buffers_write(buf
, buf_offset
,
490 (int64_t[]){ (int64_t)tmp
.v_ulong
},
493 case LTT_TYPE_UNSIGNED_INT
:
494 ust_buffers_write(buf
, buf_offset
,
495 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
502 buf_offset
+= trace_size
;
510 notrace
size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
511 struct ltt_serialize_closure
*closure
,
512 void *serialize_private
, int *largest_align
,
513 const char *fmt
, va_list *args
)
515 char trace_size
= 0, c_size
= 0; /*
516 * 0 (unset), 1, 2, 4, 8 bytes.
518 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
519 unsigned long attributes
= 0;
521 for (; *fmt
; ++fmt
) {
525 ++fmt
; /* skip first '#' */
526 if (*fmt
== '#') /* Escaped ## */
529 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
534 ++fmt
; /* skip first '%' */
535 if (*fmt
== '%') /* Escaped %% */
537 fmt
= parse_c_type(fmt
, &c_size
, &c_type
, NULL
);
539 * Output c types if no trace types has been
544 if (trace_type
== LTT_TYPE_NONE
)
546 if (c_type
== LTT_TYPE_STRING
)
547 trace_type
= LTT_TYPE_STRING
;
548 /* perform trace write */
549 buf_offset
= serialize_trace_data(buf
,
550 buf_offset
, trace_size
,
551 trace_type
, c_size
, c_type
,
552 largest_align
, args
);
555 trace_type
= LTT_TYPE_NONE
;
556 c_size
= LTT_TYPE_NONE
;
559 /* default is to skip the text, doing nothing */
566 * Calculate data size
567 * Assume that the padding for alignment starts at a sizeof(void *) address.
569 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
570 void *serialize_private
, int *largest_align
,
571 const char *fmt
, va_list *args
)
573 ltt_serialize_cb cb
= closure
->callbacks
[0];
575 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
576 largest_align
, fmt
, args
);
580 void ltt_write_event_data(struct ust_buffer
*buf
, size_t buf_offset
,
581 struct ltt_serialize_closure
*closure
,
582 void *serialize_private
, int largest_align
,
583 const char *fmt
, va_list *args
)
585 ltt_serialize_cb cb
= closure
->callbacks
[0];
587 buf_offset
+= ltt_align(buf_offset
, largest_align
);
588 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
592 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
593 struct registers
*regs
, void *call_data
,
594 const char *fmt
, va_list *args
)
596 int largest_align
, ret
;
597 struct ltt_active_marker
*pdata
;
599 size_t data_size
, slot_size
;
600 unsigned int chan_index
;
601 struct ust_channel
*channel
;
602 struct ust_trace
*trace
, *dest_trace
= NULL
;
603 struct ust_buffer
*buf
;
604 void *transport_data
;
608 struct ltt_serialize_closure closure
;
609 struct ltt_probe_private_data
*private_data
= call_data
;
610 void *serialize_private
= NULL
;
615 * This test is useful for quickly exiting static tracing when no trace
616 * is active. We expect to have an active trace when we get here.
618 if (unlikely(ltt_traces
.num_active_traces
== 0))
621 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
624 /* Force volatile access. */
625 STORE_SHARED(ltt_nesting
, LOAD_SHARED(ltt_nesting
) + 1);
628 pdata
= (struct ltt_active_marker
*)probe_data
;
629 eID
= mdata
->event_id
;
630 chan_index
= mdata
->channel_id
;
631 closure
.callbacks
= pdata
->probe
->callbacks
;
633 if (unlikely(private_data
)) {
634 dest_trace
= private_data
->trace
;
635 if (private_data
->serializer
)
636 closure
.callbacks
= &private_data
->serializer
;
637 serialize_private
= private_data
->serialize_private
;
640 va_copy(args_copy
, *args
);
642 * Assumes event payload to start on largest_align alignment.
644 largest_align
= 1; /* must be non-zero for ltt_align */
645 data_size
= ltt_get_data_size(&closure
, serialize_private
,
646 &largest_align
, fmt
, &args_copy
);
647 largest_align
= min_t(int, largest_align
, sizeof(void *));
650 /* Iterate on each trace */
651 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
653 * Expect the filter to filter out events. If we get here,
654 * we went through tracepoint activation as a first step.
656 if (unlikely(dest_trace
&& trace
!= dest_trace
))
658 if (unlikely(!trace
->active
))
660 if (unlikely(!ltt_run_filter(trace
, eID
)))
662 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
663 rflags
= LTT_RFLAG_ID_SIZE
;
665 if (unlikely(eID
>= LTT_FREE_EVENTS
))
666 rflags
= LTT_RFLAG_ID
;
671 * Skip channels added after trace creation.
673 if (unlikely(chan_index
>= trace
->nr_channels
))
675 channel
= &trace
->channels
[chan_index
];
676 if (!channel
->active
)
679 /* If a new cpu was plugged since the trace was started, we did
680 * not add it to the trace, and therefore we write the event to
683 if(cpu
>= channel
->n_cpus
) {
687 /* reserve space : header and data */
688 ret
= ltt_reserve_slot(channel
, trace
, data_size
, largest_align
,
689 cpu
, &buf
, &slot_size
, &buf_offset
,
691 if (unlikely(ret
< 0))
692 continue; /* buffer full */
694 va_copy(args_copy
, *args
);
695 /* FIXME : could probably encapsulate transport better. */
696 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
697 buf
= channel
->buf
[cpu
];
698 /* Out-of-order write : header and data */
699 buf_offset
= ltt_write_event_header(channel
, buf
, buf_offset
,
700 eID
, data_size
, tsc
, rflags
);
701 ltt_write_event_data(buf
, buf_offset
, &closure
,
703 largest_align
, fmt
, &args_copy
);
705 /* Out-of-order commit */
706 ltt_commit_slot(channel
, buf
, buf_offset
, data_size
, slot_size
);
707 DBG("just commited event (%s/%s) at offset %ld and size %zd", mdata
->channel
, mdata
->name
, buf_offset
, slot_size
);
711 STORE_SHARED(ltt_nesting
, LOAD_SHARED(ltt_nesting
) - 1);
713 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
716 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
717 struct registers
*regs
, void *call_data
,
718 const char *fmt
, ...)
723 ltt_vtrace(mdata
, probe_data
, regs
, call_data
, fmt
, &args
);
727 static notrace
void skip_space(const char **ps
)
733 static notrace
void copy_token(char **out
, const char **in
)
735 while(**in
!= ' ' && **in
!= '\0') {
744 * Given a format string and a va_list of arguments, convert them to a
745 * human-readable string.
747 * @outbuf: the buffer to output the string to
748 * @bufsize: the max size that can be used in outbuf
749 * @fmt: the marker format string
750 * @ap: a va_list that contains the arguments corresponding to fmt
752 * Return value: the number of chars that have been put in outbuf, excluding
753 * the final \0, or, if the buffer was too small, the number of chars that
754 * would have been written in outbuf if it had been large enough.
756 * outbuf may be NULL. The return value may then be used be allocate an
757 * appropriate outbuf.
762 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
764 int fmt_len
= strlen(fmt
);
765 char *new_fmt
= alloca(fmt_len
+ 1);
766 const char *orig_fmt_p
= fmt
;
767 char *new_fmt_p
= new_fmt
;
770 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
772 while(*orig_fmt_p
!= '\0') {
773 if(*orig_fmt_p
== '%') {
775 copy_token(&new_fmt_p
, &orig_fmt_p
);
777 else if(*orig_fmt_p
== '#') {
778 prev_token
= tracefmt
;
781 } while(*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
783 else if(*orig_fmt_p
== ' ') {
784 if(prev_token
== argname
) {
788 else if(prev_token
== cfmt
) {
793 skip_space(&orig_fmt_p
);
796 prev_token
= argname
;
797 copy_token(&new_fmt_p
, &orig_fmt_p
);
804 /* use this false_buffer for compatibility with pre-C99 */
808 result
= ust_safe_vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);