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.
28 //ust// #include <linux/ctype.h>
29 //ust// #include <linux/string.h>
30 //ust// #include <linux/module.h>
31 //ust// #include <linux/ltt-tracer.h>
35 #include "kernelcompat.h"
38 #include <kcompat/rculist.h>
47 LTT_TYPE_UNSIGNED_INT
,
52 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
55 * Inspired from vsnprintf
57 * The serialization format string supports the basic printf format strings.
58 * In addition, it defines new formats that can be used to serialize more
59 * complex/non portable data structures.
64 * field_name #tracetype %ctype
65 * field_name #tracetype %ctype1 %ctype2 ...
67 * A conversion is performed between format string types supported by GCC and
68 * the trace type requested. GCC type is used to perform type checking on format
69 * strings. Trace type is used to specify the exact binary representation
70 * in the trace. A mapping is done between one or more GCC types to one trace
71 * type. Sign extension, if required by the conversion, is performed following
74 * If a gcc format is not declared with a trace format, the gcc format is
75 * also used as binary representation in the trace.
77 * Strings are supported with %s.
78 * A single tracetype (sequence) can take multiple c types as parameter.
84 * Note: to write a uint32_t in a trace, the following expression is recommended
85 * si it can be portable:
87 * ("#4u%lu", (unsigned long)var)
91 * Serialization specific formats :
103 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
107 * n: (for network byte order)
109 * is written in the trace in network byte order.
111 * i.e.: #bn4u%lu, #n%lu, #b%u
114 * Variable length sequence
115 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
117 * #a specifies that this is a sequence
118 * #tracetype1 is the type of elements in the sequence
119 * #tracetype2 is the type of the element count
121 * array_ptr is a pointer to an array that contains members of size
123 * num_elems is the number of elements in the array.
124 * i.e.: #a #lu #lu %p %lu %u
127 * #k callback (taken from the probe data)
128 * The following % arguments are exepected by the callback
130 * i.e.: #a #lu #lu #k %p
132 * Note: No conversion is done from floats to integers, nor from integers to
133 * floats between c types and trace types. float conversion from double to float
134 * or from float to double is also not supported.
137 * %*b expects sizeof(data), data
138 * where sizeof(data) is 1, 2, 4 or 8
140 * Fixed length struct, union or array.
141 * FIXME: unable to extract those sizes statically.
142 * %*r expects sizeof(*ptr), ptr
143 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
144 * struct and unions removed.
145 * Fixed length array:
146 * [%p]#a[len #tracetype]
147 * i.e.: [%p]#a[12 #lu]
149 * Variable length sequence
150 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
151 * where elem_num is the number of elements in the sequence
153 static inline const char *parse_trace_type(const char *fmt
,
154 char *trace_size
, enum ltt_type
*trace_type
,
155 unsigned long *attributes
)
157 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
158 /* 'z' support added 23/7/1999 S.H. */
159 /* 'z' changed to 'Z' --davidm 1/25/99 */
160 /* 't' added for ptrdiff_t */
162 /* parse attributes. */
166 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
171 /* get the conversion qualifier */
173 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
174 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
175 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
176 *fmt
== '4' || *fmt
== 8) {
179 if (qualifier
== 'l' && *fmt
== 'l') {
187 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
188 *trace_size
= sizeof(unsigned char);
191 *trace_type
= LTT_TYPE_STRING
;
194 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
195 *trace_size
= sizeof(void *);
199 *trace_type
= LTT_TYPE_SIGNED_INT
;
205 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
214 *trace_size
= sizeof(long long);
217 *trace_size
= sizeof(long);
221 *trace_size
= sizeof(size_t);
224 //ust// *trace_size = sizeof(ptrdiff_t);
227 *trace_size
= sizeof(short);
230 *trace_size
= sizeof(uint8_t);
233 *trace_size
= sizeof(uint16_t);
236 *trace_size
= sizeof(uint32_t);
239 *trace_size
= sizeof(uint64_t);
242 *trace_size
= sizeof(int);
251 * Field width and precision are *not* supported.
254 static inline const char *parse_c_type(const char *fmt
,
255 char *c_size
, enum ltt_type
*c_type
)
257 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
258 /* 'z' support added 23/7/1999 S.H. */
259 /* 'z' changed to 'Z' --davidm 1/25/99 */
260 /* 't' added for ptrdiff_t */
262 /* process flags : ignore standard print formats for now. */
274 /* get the conversion qualifier */
276 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
277 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
281 if (qualifier
== 'l' && *fmt
== 'l') {
289 *c_type
= LTT_TYPE_UNSIGNED_INT
;
290 *c_size
= sizeof(unsigned char);
293 *c_type
= LTT_TYPE_STRING
;
296 *c_type
= LTT_TYPE_UNSIGNED_INT
;
297 *c_size
= sizeof(void *);
301 *c_type
= LTT_TYPE_SIGNED_INT
;
307 *c_type
= LTT_TYPE_UNSIGNED_INT
;
316 *c_size
= sizeof(long long);
319 *c_size
= sizeof(long);
323 *c_size
= sizeof(size_t);
326 //ust// *c_size = sizeof(ptrdiff_t);
329 *c_size
= sizeof(short);
332 *c_size
= sizeof(int);
339 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
341 char trace_size
, enum ltt_type trace_type
,
342 char c_size
, enum ltt_type c_type
,
343 int *largest_align
, va_list *args
)
346 unsigned long v_ulong
;
355 * Be careful about sign extension here.
356 * Sign extension is done with the destination (trace) type.
358 switch (trace_type
) {
359 case LTT_TYPE_SIGNED_INT
:
362 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
365 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
368 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
371 tmp
.v_uint64
= va_arg(*args
, int64_t);
377 case LTT_TYPE_UNSIGNED_INT
:
380 tmp
.v_ulong
= (unsigned long)(uint8_t)
381 va_arg(*args
, unsigned int);
384 tmp
.v_ulong
= (unsigned long)(uint16_t)
385 va_arg(*args
, unsigned int);
388 tmp
.v_ulong
= (unsigned long)(uint32_t)
389 va_arg(*args
, unsigned int);
392 tmp
.v_uint64
= va_arg(*args
, uint64_t);
398 case LTT_TYPE_STRING
:
399 tmp
.v_string
.s
= va_arg(*args
, const char *);
400 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
401 tmp
.v_string
.s
= "<NULL>";
402 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
404 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
406 buf_offset
+= tmp
.v_string
.len
;
413 * If trace_size is lower or equal to 4 bytes, there is no sign
414 * extension to do because we are already encoded in a long. Therefore,
415 * we can combine signed and unsigned ops. 4 bytes float also works
416 * with this, because we do a simple copy of 4 bytes into 4 bytes
417 * without manipulation (and we do not support conversion from integers
419 * It is also the case if c_size is 8 bytes, which is the largest
422 if (ltt_get_alignment()) {
423 buf_offset
+= ltt_align(buf_offset
, trace_size
);
425 *largest_align
= max_t(int, *largest_align
, trace_size
);
427 if (trace_size
<= 4 || c_size
== 8) {
429 switch (trace_size
) {
432 ltt_relay_write(buf
, buf_offset
,
433 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
436 ltt_relay_write(buf
, buf_offset
,
437 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
442 ltt_relay_write(buf
, buf_offset
,
443 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
446 ltt_relay_write(buf
, buf_offset
,
447 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
452 ltt_relay_write(buf
, buf_offset
,
453 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
456 ltt_relay_write(buf
, buf_offset
,
457 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
462 * c_size cannot be other than 8 here because
465 ltt_relay_write(buf
, buf_offset
,
466 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
473 buf_offset
+= trace_size
;
477 * Perform sign extension.
480 switch (trace_type
) {
481 case LTT_TYPE_SIGNED_INT
:
482 ltt_relay_write(buf
, buf_offset
,
483 (int64_t[]){ (int64_t)tmp
.v_ulong
},
486 case LTT_TYPE_UNSIGNED_INT
:
487 ltt_relay_write(buf
, buf_offset
,
488 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
495 buf_offset
+= trace_size
;
503 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
504 struct ltt_serialize_closure
*closure
,
505 void *serialize_private
, int *largest_align
,
506 const char *fmt
, va_list *args
)
508 char trace_size
= 0, c_size
= 0; /*
509 * 0 (unset), 1, 2, 4, 8 bytes.
511 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
512 unsigned long attributes
= 0;
514 for (; *fmt
; ++fmt
) {
518 ++fmt
; /* skip first '#' */
519 if (*fmt
== '#') /* Escaped ## */
522 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
527 ++fmt
; /* skip first '%' */
528 if (*fmt
== '%') /* Escaped %% */
530 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
532 * Output c types if no trace types has been
537 if (trace_type
== LTT_TYPE_NONE
)
539 if (c_type
== LTT_TYPE_STRING
)
540 trace_type
= LTT_TYPE_STRING
;
541 /* perform trace write */
542 buf_offset
= serialize_trace_data(buf
,
543 buf_offset
, trace_size
,
544 trace_type
, c_size
, c_type
,
545 largest_align
, args
);
548 trace_type
= LTT_TYPE_NONE
;
549 c_size
= LTT_TYPE_NONE
;
552 /* default is to skip the text, doing nothing */
557 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
560 * Calculate data size
561 * Assume that the padding for alignment starts at a sizeof(void *) address.
563 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
564 void *serialize_private
, int *largest_align
,
565 const char *fmt
, va_list *args
)
567 ltt_serialize_cb cb
= closure
->callbacks
[0];
569 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
570 largest_align
, fmt
, args
);
574 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
575 struct ltt_serialize_closure
*closure
,
576 void *serialize_private
, int largest_align
,
577 const char *fmt
, va_list *args
)
579 ltt_serialize_cb cb
= closure
->callbacks
[0];
581 buf_offset
+= ltt_align(buf_offset
, largest_align
);
582 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
586 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
587 void *call_data
, const char *fmt
, va_list *args
)
589 int largest_align
, ret
;
590 struct ltt_active_marker
*pdata
;
592 size_t data_size
, slot_size
;
593 unsigned int chan_index
;
594 struct ltt_channel_struct
*channel
;
595 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
596 struct rchan_buf
*buf
;
597 void *transport_data
;
601 struct ltt_serialize_closure closure
;
602 struct ltt_probe_private_data
*private_data
= call_data
;
603 void *serialize_private
= NULL
;
608 * This test is useful for quickly exiting static tracing when no trace
609 * is active. We expect to have an active trace when we get here.
611 if (unlikely(ltt_traces
.num_active_traces
== 0))
614 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
615 //ust// cpu = smp_processor_id();
616 //ust// __get_cpu_var(ltt_nesting)++;
619 pdata
= (struct ltt_active_marker
*)probe_data
;
620 eID
= mdata
->event_id
;
621 chan_index
= mdata
->channel_id
;
622 closure
.callbacks
= pdata
->probe
->callbacks
;
624 if (unlikely(private_data
)) {
625 dest_trace
= private_data
->trace
;
626 if (private_data
->serializer
)
627 closure
.callbacks
= &private_data
->serializer
;
628 serialize_private
= private_data
->serialize_private
;
631 va_copy(args_copy
, *args
);
633 * Assumes event payload to start on largest_align alignment.
635 largest_align
= 1; /* must be non-zero for ltt_align */
636 data_size
= ltt_get_data_size(&closure
, serialize_private
,
637 &largest_align
, fmt
, &args_copy
);
638 largest_align
= min_t(int, largest_align
, sizeof(void *));
641 /* Iterate on each trace */
642 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
644 * Expect the filter to filter out events. If we get here,
645 * we went through tracepoint activation as a first step.
647 if (unlikely(dest_trace
&& trace
!= dest_trace
))
649 if (unlikely(!trace
->active
))
651 if (unlikely(!ltt_run_filter(trace
, eID
)))
653 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
654 rflags
= LTT_RFLAG_ID_SIZE
;
656 if (unlikely(eID
>= LTT_FREE_EVENTS
))
657 rflags
= LTT_RFLAG_ID
;
662 * Skip channels added after trace creation.
664 if (unlikely(chan_index
>= trace
->nr_channels
))
666 channel
= &trace
->channels
[chan_index
];
667 if (!channel
->active
)
670 /* reserve space : header and data */
671 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
672 data_size
, &slot_size
, &buf_offset
,
675 if (unlikely(ret
< 0))
676 continue; /* buffer full */
678 va_copy(args_copy
, *args
);
679 /* FIXME : could probably encapsulate transport better. */
680 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
681 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
682 /* Out-of-order write : header and data */
683 buf_offset
= ltt_write_event_header(trace
,
684 channel
, buf
, buf_offset
,
685 eID
, data_size
, tsc
, rflags
);
686 ltt_write_event_data(buf
, buf_offset
, &closure
,
688 largest_align
, fmt
, &args_copy
);
690 /* Out-of-order commit */
691 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
692 data_size
, slot_size
);
693 DBG("just commited event at offset %ld and size %zd\n", buf_offset
, slot_size
);
695 //ust// __get_cpu_var(ltt_nesting)--;
697 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
699 EXPORT_SYMBOL_GPL(ltt_vtrace
);
701 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
702 void *call_data
, const char *fmt
, ...)
707 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
710 EXPORT_SYMBOL_GPL(ltt_trace
);
712 //ust// MODULE_LICENSE("GPL");
713 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
714 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");