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>
44 #include "ust_snprintf.h"
48 LTT_TYPE_UNSIGNED_INT
,
53 static int ust_get_cpu(void)
55 return sched_getcpu();
58 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
61 * Inspired from vsnprintf
63 * The serialization format string supports the basic printf format strings.
64 * In addition, it defines new formats that can be used to serialize more
65 * complex/non portable data structures.
70 * field_name #tracetype %ctype
71 * field_name #tracetype %ctype1 %ctype2 ...
73 * A conversion is performed between format string types supported by GCC and
74 * the trace type requested. GCC type is used to perform type checking on format
75 * strings. Trace type is used to specify the exact binary representation
76 * in the trace. A mapping is done between one or more GCC types to one trace
77 * type. Sign extension, if required by the conversion, is performed following
80 * If a gcc format is not declared with a trace format, the gcc format is
81 * also used as binary representation in the trace.
83 * Strings are supported with %s.
84 * A single tracetype (sequence) can take multiple c types as parameter.
90 * Note: to write a uint32_t in a trace, the following expression is recommended
91 * si it can be portable:
93 * ("#4u%lu", (unsigned long)var)
97 * Serialization specific formats :
101 * #2u writes uint16_t
102 * #4u writes uint32_t
103 * #8u writes uint64_t
109 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
113 * n: (for network byte order)
115 * is written in the trace in network byte order.
117 * i.e.: #bn4u%lu, #n%lu, #b%u
120 * Variable length sequence
121 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
123 * #a specifies that this is a sequence
124 * #tracetype1 is the type of elements in the sequence
125 * #tracetype2 is the type of the element count
127 * array_ptr is a pointer to an array that contains members of size
129 * num_elems is the number of elements in the array.
130 * i.e.: #a #lu #lu %p %lu %u
133 * #k callback (taken from the probe data)
134 * The following % arguments are exepected by the callback
136 * i.e.: #a #lu #lu #k %p
138 * Note: No conversion is done from floats to integers, nor from integers to
139 * floats between c types and trace types. float conversion from double to float
140 * or from float to double is also not supported.
143 * %*b expects sizeof(data), data
144 * where sizeof(data) is 1, 2, 4 or 8
146 * Fixed length struct, union or array.
147 * FIXME: unable to extract those sizes statically.
148 * %*r expects sizeof(*ptr), ptr
149 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
150 * struct and unions removed.
151 * Fixed length array:
152 * [%p]#a[len #tracetype]
153 * i.e.: [%p]#a[12 #lu]
155 * Variable length sequence
156 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
157 * where elem_num is the number of elements in the sequence
159 static inline const char *parse_trace_type(const char *fmt
,
160 char *trace_size
, enum ltt_type
*trace_type
,
161 unsigned long *attributes
)
163 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
164 /* 'z' support added 23/7/1999 S.H. */
165 /* 'z' changed to 'Z' --davidm 1/25/99 */
166 /* 't' added for ptrdiff_t */
168 /* parse attributes. */
172 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
177 /* get the conversion qualifier */
179 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
180 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
181 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
182 *fmt
== '4' || *fmt
== 8) {
185 if (qualifier
== 'l' && *fmt
== 'l') {
193 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
194 *trace_size
= sizeof(unsigned char);
197 *trace_type
= LTT_TYPE_STRING
;
200 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
201 *trace_size
= sizeof(void *);
205 *trace_type
= LTT_TYPE_SIGNED_INT
;
211 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
220 *trace_size
= sizeof(long long);
223 *trace_size
= sizeof(long);
227 *trace_size
= sizeof(size_t);
230 //ust// *trace_size = sizeof(ptrdiff_t);
233 *trace_size
= sizeof(short);
236 *trace_size
= sizeof(uint8_t);
239 *trace_size
= sizeof(uint16_t);
242 *trace_size
= sizeof(uint32_t);
245 *trace_size
= sizeof(uint64_t);
248 *trace_size
= sizeof(int);
257 * Field width and precision are *not* supported.
261 const char *parse_c_type(const char *fmt
, char *c_size
, enum ltt_type
*c_type
,
264 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
265 /* 'z' support added 23/7/1999 S.H. */
266 /* 'z' changed to 'Z' --davidm 1/25/99 */
267 /* 't' added for ptrdiff_t */
269 /* process flags : ignore standard print formats for now. */
281 /* get the conversion qualifier */
283 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
284 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
288 if (qualifier
== 'l' && *fmt
== 'l') {
296 *outfmt
++ = (char)qualifier
;
303 *c_type
= LTT_TYPE_UNSIGNED_INT
;
304 *c_size
= sizeof(unsigned char);
307 *c_type
= LTT_TYPE_STRING
;
310 *c_type
= LTT_TYPE_UNSIGNED_INT
;
311 *c_size
= sizeof(void *);
315 *c_type
= LTT_TYPE_SIGNED_INT
;
321 *c_type
= LTT_TYPE_UNSIGNED_INT
;
330 *c_size
= sizeof(long long);
333 *c_size
= sizeof(long);
337 *c_size
= sizeof(size_t);
340 //ust// *c_size = sizeof(ptrdiff_t);
343 *c_size
= sizeof(short);
346 *c_size
= sizeof(int);
353 static inline size_t serialize_trace_data(struct ust_buffer
*buf
,
355 char trace_size
, enum ltt_type trace_type
,
356 char c_size
, enum ltt_type c_type
,
357 int *largest_align
, va_list *args
)
360 unsigned long v_ulong
;
369 * Be careful about sign extension here.
370 * Sign extension is done with the destination (trace) type.
372 switch (trace_type
) {
373 case LTT_TYPE_SIGNED_INT
:
376 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
379 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
382 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
385 tmp
.v_uint64
= va_arg(*args
, int64_t);
391 case LTT_TYPE_UNSIGNED_INT
:
394 tmp
.v_ulong
= (unsigned long)(uint8_t)va_arg(*args
, unsigned int);
397 tmp
.v_ulong
= (unsigned long)(uint16_t)va_arg(*args
, unsigned int);
400 tmp
.v_ulong
= (unsigned long)(uint32_t)va_arg(*args
, unsigned int);
403 tmp
.v_uint64
= va_arg(*args
, uint64_t);
409 case LTT_TYPE_STRING
:
410 tmp
.v_string
.s
= va_arg(*args
, const char *);
411 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
412 tmp
.v_string
.s
= "<NULL>";
413 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
415 ust_buffers_write(buf
, buf_offset
, tmp
.v_string
.s
,
417 buf_offset
+= tmp
.v_string
.len
;
424 * If trace_size is lower or equal to 4 bytes, there is no sign
425 * extension to do because we are already encoded in a long. Therefore,
426 * we can combine signed and unsigned ops. 4 bytes float also works
427 * with this, because we do a simple copy of 4 bytes into 4 bytes
428 * without manipulation (and we do not support conversion from integers
430 * It is also the case if c_size is 8 bytes, which is the largest
433 if (ltt_get_alignment()) {
434 buf_offset
+= ltt_align(buf_offset
, trace_size
);
436 *largest_align
= max_t(int, *largest_align
, trace_size
);
438 if (trace_size
<= 4 || c_size
== 8) {
440 switch (trace_size
) {
443 ust_buffers_write(buf
, buf_offset
,
444 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
447 ust_buffers_write(buf
, buf_offset
,
448 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
453 ust_buffers_write(buf
, buf_offset
,
454 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
457 ust_buffers_write(buf
, buf_offset
,
458 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
463 ust_buffers_write(buf
, buf_offset
,
464 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
467 ust_buffers_write(buf
, buf_offset
,
468 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
473 * c_size cannot be other than 8 here because
476 ust_buffers_write(buf
, buf_offset
,
477 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
484 buf_offset
+= trace_size
;
488 * Perform sign extension.
491 switch (trace_type
) {
492 case LTT_TYPE_SIGNED_INT
:
493 ust_buffers_write(buf
, buf_offset
,
494 (int64_t[]){ (int64_t)tmp
.v_ulong
},
497 case LTT_TYPE_UNSIGNED_INT
:
498 ust_buffers_write(buf
, buf_offset
,
499 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
506 buf_offset
+= trace_size
;
514 notrace
size_t ltt_serialize_data(struct ust_buffer
*buf
, size_t buf_offset
,
515 struct ltt_serialize_closure
*closure
,
516 void *serialize_private
, int *largest_align
,
517 const char *fmt
, va_list *args
)
519 char trace_size
= 0, c_size
= 0; /*
520 * 0 (unset), 1, 2, 4, 8 bytes.
522 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
523 unsigned long attributes
= 0;
525 for (; *fmt
; ++fmt
) {
529 ++fmt
; /* skip first '#' */
530 if (*fmt
== '#') /* Escaped ## */
533 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
538 ++fmt
; /* skip first '%' */
539 if (*fmt
== '%') /* Escaped %% */
541 fmt
= parse_c_type(fmt
, &c_size
, &c_type
, NULL
);
543 * Output c types if no trace types has been
548 if (trace_type
== LTT_TYPE_NONE
)
550 if (c_type
== LTT_TYPE_STRING
)
551 trace_type
= LTT_TYPE_STRING
;
552 /* perform trace write */
553 buf_offset
= serialize_trace_data(buf
,
554 buf_offset
, trace_size
,
555 trace_type
, c_size
, c_type
,
556 largest_align
, args
);
559 trace_type
= LTT_TYPE_NONE
;
560 c_size
= LTT_TYPE_NONE
;
563 /* default is to skip the text, doing nothing */
570 * Calculate data size
571 * Assume that the padding for alignment starts at a sizeof(void *) address.
573 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
574 void *serialize_private
, int *largest_align
,
575 const char *fmt
, va_list *args
)
577 ltt_serialize_cb cb
= closure
->callbacks
[0];
579 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
580 largest_align
, fmt
, args
);
584 void ltt_write_event_data(struct ust_buffer
*buf
, size_t buf_offset
,
585 struct ltt_serialize_closure
*closure
,
586 void *serialize_private
, int largest_align
,
587 const char *fmt
, va_list *args
)
589 ltt_serialize_cb cb
= closure
->callbacks
[0];
591 buf_offset
+= ltt_align(buf_offset
, largest_align
);
592 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
596 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
597 struct registers
*regs
, void *call_data
,
598 const char *fmt
, va_list *args
)
600 int largest_align
, ret
;
601 struct ltt_active_marker
*pdata
;
603 size_t data_size
, slot_size
;
604 unsigned int chan_index
;
605 struct ust_channel
*channel
;
606 struct ust_trace
*trace
, *dest_trace
= NULL
;
607 struct ust_buffer
*buf
;
608 void *transport_data
;
612 struct ltt_serialize_closure closure
;
613 struct ltt_probe_private_data
*private_data
= call_data
;
614 void *serialize_private
= NULL
;
619 * This test is useful for quickly exiting static tracing when no trace
620 * is active. We expect to have an active trace when we get here.
622 if (unlikely(ltt_traces
.num_active_traces
== 0))
625 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
626 //ust// cpu = smp_processor_id();
628 //ust// __get_cpu_var(ltt_nesting)++;
629 /* FIXME: should nesting be per-cpu? */
632 pdata
= (struct ltt_active_marker
*)probe_data
;
633 eID
= mdata
->event_id
;
634 chan_index
= mdata
->channel_id
;
635 closure
.callbacks
= pdata
->probe
->callbacks
;
637 if (unlikely(private_data
)) {
638 dest_trace
= private_data
->trace
;
639 if (private_data
->serializer
)
640 closure
.callbacks
= &private_data
->serializer
;
641 serialize_private
= private_data
->serialize_private
;
644 va_copy(args_copy
, *args
);
646 * Assumes event payload to start on largest_align alignment.
648 largest_align
= 1; /* must be non-zero for ltt_align */
649 data_size
= ltt_get_data_size(&closure
, serialize_private
,
650 &largest_align
, fmt
, &args_copy
);
651 largest_align
= min_t(int, largest_align
, sizeof(void *));
654 /* Iterate on each trace */
655 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
657 * Expect the filter to filter out events. If we get here,
658 * we went through tracepoint activation as a first step.
660 if (unlikely(dest_trace
&& trace
!= dest_trace
))
662 if (unlikely(!trace
->active
))
664 if (unlikely(!ltt_run_filter(trace
, eID
)))
666 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
667 rflags
= LTT_RFLAG_ID_SIZE
;
669 if (unlikely(eID
>= LTT_FREE_EVENTS
))
670 rflags
= LTT_RFLAG_ID
;
675 * Skip channels added after trace creation.
677 if (unlikely(chan_index
>= trace
->nr_channels
))
679 channel
= &trace
->channels
[chan_index
];
680 if (!channel
->active
)
683 /* If a new cpu was plugged since the trace was started, we did
684 * not add it to the trace, and therefore we write the event to
687 if(cpu
>= channel
->n_cpus
) {
691 /* reserve space : header and data */
692 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
693 data_size
, &slot_size
, &buf_offset
,
696 if (unlikely(ret
< 0))
697 continue; /* buffer full */
699 va_copy(args_copy
, *args
);
700 /* FIXME : could probably encapsulate transport better. */
701 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
702 buf
= channel
->buf
[cpu
];
703 /* Out-of-order write : header and data */
704 buf_offset
= ltt_write_event_header(trace
,
705 channel
, buf
, buf_offset
,
706 eID
, data_size
, tsc
, rflags
);
707 ltt_write_event_data(buf
, buf_offset
, &closure
,
709 largest_align
, fmt
, &args_copy
);
711 /* Out-of-order commit */
712 ltt_commit_slot(channel
, buf
, buf_offset
, data_size
, slot_size
);
713 DBG("just commited event (%s/%s) at offset %ld and size %zd", mdata
->channel
, mdata
->name
, buf_offset
, slot_size
);
715 //ust// __get_cpu_var(ltt_nesting)--;
717 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
720 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
721 struct registers
*regs
, void *call_data
,
722 const char *fmt
, ...)
727 ltt_vtrace(mdata
, probe_data
, regs
, call_data
, fmt
, &args
);
731 static notrace
void skip_space(const char **ps
)
737 static notrace
void copy_token(char **out
, const char **in
)
739 while(**in
!= ' ' && **in
!= '\0') {
748 * Given a format string and a va_list of arguments, convert them to a
749 * human-readable string.
751 * @outbuf: the buffer to output the string to
752 * @bufsize: the max size that can be used in outbuf
753 * @fmt: the marker format string
754 * @ap: a va_list that contains the arguments corresponding to fmt
756 * Return value: the number of chars that have been put in outbuf, excluding
757 * the final \0, or, if the buffer was too small, the number of chars that
758 * would have been written in outbuf if it had been large enough.
760 * outbuf may be NULL. The return value may then be used be allocate an
761 * appropriate outbuf.
766 int serialize_to_text(char *outbuf
, int bufsize
, const char *fmt
, va_list ap
)
768 int fmt_len
= strlen(fmt
);
769 char *new_fmt
= alloca(fmt_len
+ 1);
770 const char *orig_fmt_p
= fmt
;
771 char *new_fmt_p
= new_fmt
;
774 enum { none
, cfmt
, tracefmt
, argname
} prev_token
= none
;
776 while(*orig_fmt_p
!= '\0') {
777 if(*orig_fmt_p
== '%') {
779 copy_token(&new_fmt_p
, &orig_fmt_p
);
781 else if(*orig_fmt_p
== '#') {
782 prev_token
= tracefmt
;
785 } while(*orig_fmt_p
!= ' ' && *orig_fmt_p
!= '\0');
787 else if(*orig_fmt_p
== ' ') {
788 if(prev_token
== argname
) {
792 else if(prev_token
== cfmt
) {
797 skip_space(&orig_fmt_p
);
800 prev_token
= argname
;
801 copy_token(&new_fmt_p
, &orig_fmt_p
);
808 /* use this false_buffer for compatibility with pre-C99 */
812 result
= ust_safe_vsnprintf(outbuf
, bufsize
, new_fmt
, ap
);