2 * LTTng serializing code.
4 * Copyright Mathieu Desnoyers, March 2007.
6 * Licensed under the GPLv2.
8 * See this discussion about weirdness about passing va_list and then va_list to
9 * functions. (related to array argument passing). va_list seems to be
10 * implemented as an array on x86_64, but not on i386... This is why we pass a
11 * va_list * to ltt_vtrace.
15 //ust// #include <linux/ctype.h>
16 //ust// #include <linux/string.h>
17 //ust// #include <linux/module.h>
18 //ust// #include <linux/ltt-tracer.h>
21 #include "kernelcompat.h"
28 LTT_TYPE_UNSIGNED_INT
,
33 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
36 * Inspired from vsnprintf
38 * The serialization format string supports the basic printf format strings.
39 * In addition, it defines new formats that can be used to serialize more
40 * complex/non portable data structures.
45 * field_name #tracetype %ctype
46 * field_name #tracetype %ctype1 %ctype2 ...
48 * A conversion is performed between format string types supported by GCC and
49 * the trace type requested. GCC type is used to perform type checking on format
50 * strings. Trace type is used to specify the exact binary representation
51 * in the trace. A mapping is done between one or more GCC types to one trace
52 * type. Sign extension, if required by the conversion, is performed following
55 * If a gcc format is not declared with a trace format, the gcc format is
56 * also used as binary representation in the trace.
58 * Strings are supported with %s.
59 * A single tracetype (sequence) can take multiple c types as parameter.
65 * Note: to write a uint32_t in a trace, the following expression is recommended
66 * si it can be portable:
68 * ("#4u%lu", (unsigned long)var)
72 * Serialization specific formats :
84 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
88 * n: (for network byte order)
90 * is written in the trace in network byte order.
92 * i.e.: #bn4u%lu, #n%lu, #b%u
95 * Variable length sequence
96 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
98 * #a specifies that this is a sequence
99 * #tracetype1 is the type of elements in the sequence
100 * #tracetype2 is the type of the element count
102 * array_ptr is a pointer to an array that contains members of size
104 * num_elems is the number of elements in the array.
105 * i.e.: #a #lu #lu %p %lu %u
108 * #k callback (taken from the probe data)
109 * The following % arguments are exepected by the callback
111 * i.e.: #a #lu #lu #k %p
113 * Note: No conversion is done from floats to integers, nor from integers to
114 * floats between c types and trace types. float conversion from double to float
115 * or from float to double is also not supported.
118 * %*b expects sizeof(data), data
119 * where sizeof(data) is 1, 2, 4 or 8
121 * Fixed length struct, union or array.
122 * FIXME: unable to extract those sizes statically.
123 * %*r expects sizeof(*ptr), ptr
124 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
125 * struct and unions removed.
126 * Fixed length array:
127 * [%p]#a[len #tracetype]
128 * i.e.: [%p]#a[12 #lu]
130 * Variable length sequence
131 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
132 * where elem_num is the number of elements in the sequence
134 static inline const char *parse_trace_type(const char *fmt
,
135 char *trace_size
, enum ltt_type
*trace_type
,
136 unsigned long *attributes
)
138 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
139 /* 'z' support added 23/7/1999 S.H. */
140 /* 'z' changed to 'Z' --davidm 1/25/99 */
141 /* 't' added for ptrdiff_t */
143 /* parse attributes. */
147 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
152 /* get the conversion qualifier */
154 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
155 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
156 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
157 *fmt
== '4' || *fmt
== 8) {
160 if (qualifier
== 'l' && *fmt
== 'l') {
168 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
169 *trace_size
= sizeof(unsigned char);
172 *trace_type
= LTT_TYPE_STRING
;
175 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
176 *trace_size
= sizeof(void *);
180 *trace_type
= LTT_TYPE_SIGNED_INT
;
186 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
195 *trace_size
= sizeof(long long);
198 *trace_size
= sizeof(long);
202 *trace_size
= sizeof(size_t);
205 //ust// *trace_size = sizeof(ptrdiff_t);
208 *trace_size
= sizeof(short);
211 *trace_size
= sizeof(uint8_t);
214 *trace_size
= sizeof(uint16_t);
217 *trace_size
= sizeof(uint32_t);
220 *trace_size
= sizeof(uint64_t);
223 *trace_size
= sizeof(int);
232 * Field width and precision are *not* supported.
235 static inline const char *parse_c_type(const char *fmt
,
236 char *c_size
, enum ltt_type
*c_type
)
238 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
239 /* 'z' support added 23/7/1999 S.H. */
240 /* 'z' changed to 'Z' --davidm 1/25/99 */
241 /* 't' added for ptrdiff_t */
243 /* process flags : ignore standard print formats for now. */
255 /* get the conversion qualifier */
257 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
258 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
262 if (qualifier
== 'l' && *fmt
== 'l') {
270 *c_type
= LTT_TYPE_UNSIGNED_INT
;
271 *c_size
= sizeof(unsigned char);
274 *c_type
= LTT_TYPE_STRING
;
277 *c_type
= LTT_TYPE_UNSIGNED_INT
;
278 *c_size
= sizeof(void *);
282 *c_type
= LTT_TYPE_SIGNED_INT
;
288 *c_type
= LTT_TYPE_UNSIGNED_INT
;
297 *c_size
= sizeof(long long);
300 *c_size
= sizeof(long);
304 *c_size
= sizeof(size_t);
307 //ust// *c_size = sizeof(ptrdiff_t);
310 *c_size
= sizeof(short);
313 *c_size
= sizeof(int);
320 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
322 char trace_size
, enum ltt_type trace_type
,
323 char c_size
, enum ltt_type c_type
,
324 int *largest_align
, va_list *args
)
327 unsigned long v_ulong
;
336 * Be careful about sign extension here.
337 * Sign extension is done with the destination (trace) type.
339 switch (trace_type
) {
340 case LTT_TYPE_SIGNED_INT
:
343 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
346 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
349 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
352 tmp
.v_uint64
= va_arg(*args
, int64_t);
358 case LTT_TYPE_UNSIGNED_INT
:
361 tmp
.v_ulong
= (unsigned long)(uint8_t)
362 va_arg(*args
, unsigned int);
365 tmp
.v_ulong
= (unsigned long)(uint16_t)
366 va_arg(*args
, unsigned int);
369 tmp
.v_ulong
= (unsigned long)(uint32_t)
370 va_arg(*args
, unsigned int);
373 tmp
.v_uint64
= va_arg(*args
, uint64_t);
379 case LTT_TYPE_STRING
:
380 tmp
.v_string
.s
= va_arg(*args
, const char *);
381 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
382 tmp
.v_string
.s
= "<NULL>";
383 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
385 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
387 buf_offset
+= tmp
.v_string
.len
;
394 * If trace_size is lower or equal to 4 bytes, there is no sign
395 * extension to do because we are already encoded in a long. Therefore,
396 * we can combine signed and unsigned ops. 4 bytes float also works
397 * with this, because we do a simple copy of 4 bytes into 4 bytes
398 * without manipulation (and we do not support conversion from integers
400 * It is also the case if c_size is 8 bytes, which is the largest
403 if (ltt_get_alignment()) {
404 buf_offset
+= ltt_align(buf_offset
, trace_size
);
406 *largest_align
= max_t(int, *largest_align
, trace_size
);
408 if (trace_size
<= 4 || c_size
== 8) {
410 switch (trace_size
) {
413 ltt_relay_write(buf
, buf_offset
,
414 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
417 ltt_relay_write(buf
, buf_offset
,
418 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
423 ltt_relay_write(buf
, buf_offset
,
424 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
427 ltt_relay_write(buf
, buf_offset
,
428 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
433 ltt_relay_write(buf
, buf_offset
,
434 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
437 ltt_relay_write(buf
, buf_offset
,
438 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
443 * c_size cannot be other than 8 here because
446 ltt_relay_write(buf
, buf_offset
,
447 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
454 buf_offset
+= trace_size
;
458 * Perform sign extension.
461 switch (trace_type
) {
462 case LTT_TYPE_SIGNED_INT
:
463 ltt_relay_write(buf
, buf_offset
,
464 (int64_t[]){ (int64_t)tmp
.v_ulong
},
467 case LTT_TYPE_UNSIGNED_INT
:
468 ltt_relay_write(buf
, buf_offset
,
469 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
476 buf_offset
+= trace_size
;
484 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
485 struct ltt_serialize_closure
*closure
,
486 void *serialize_private
, int *largest_align
,
487 const char *fmt
, va_list *args
)
489 char trace_size
= 0, c_size
= 0; /*
490 * 0 (unset), 1, 2, 4, 8 bytes.
492 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
493 unsigned long attributes
= 0;
495 for (; *fmt
; ++fmt
) {
499 ++fmt
; /* skip first '#' */
500 if (*fmt
== '#') /* Escaped ## */
503 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
508 ++fmt
; /* skip first '%' */
509 if (*fmt
== '%') /* Escaped %% */
511 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
513 * Output c types if no trace types has been
518 if (trace_type
== LTT_TYPE_NONE
)
520 if (c_type
== LTT_TYPE_STRING
)
521 trace_type
= LTT_TYPE_STRING
;
522 /* perform trace write */
523 buf_offset
= serialize_trace_data(buf
,
524 buf_offset
, trace_size
,
525 trace_type
, c_size
, c_type
,
526 largest_align
, args
);
529 trace_type
= LTT_TYPE_NONE
;
530 c_size
= LTT_TYPE_NONE
;
533 /* default is to skip the text, doing nothing */
538 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
541 * Calculate data size
542 * Assume that the padding for alignment starts at a sizeof(void *) address.
544 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
545 void *serialize_private
, int *largest_align
,
546 const char *fmt
, va_list *args
)
548 ltt_serialize_cb cb
= closure
->callbacks
[0];
550 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
551 largest_align
, fmt
, args
);
555 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
556 struct ltt_serialize_closure
*closure
,
557 void *serialize_private
, int largest_align
,
558 const char *fmt
, va_list *args
)
560 ltt_serialize_cb cb
= closure
->callbacks
[0];
562 buf_offset
+= ltt_align(buf_offset
, largest_align
);
563 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
567 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
568 void *call_data
, const char *fmt
, va_list *args
)
570 int largest_align
, ret
;
571 struct ltt_active_marker
*pdata
;
573 size_t data_size
, slot_size
;
574 unsigned int chan_index
;
575 struct ltt_channel_struct
*channel
;
576 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
577 struct rchan_buf
*buf
;
578 void *transport_data
;
582 struct ltt_serialize_closure closure
;
583 struct ltt_probe_private_data
*private_data
= call_data
;
584 void *serialize_private
= NULL
;
589 * This test is useful for quickly exiting static tracing when no trace
590 * is active. We expect to have an active trace when we get here.
592 if (unlikely(ltt_traces
.num_active_traces
== 0))
595 rcu_read_lock_sched_notrace();
596 cpu
= smp_processor_id();
597 //ust// __get_cpu_var(ltt_nesting)++;
600 pdata
= (struct ltt_active_marker
*)probe_data
;
601 eID
= mdata
->event_id
;
602 chan_index
= mdata
->channel_id
;
603 closure
.callbacks
= pdata
->probe
->callbacks
;
605 if (unlikely(private_data
)) {
606 dest_trace
= private_data
->trace
;
607 if (private_data
->serializer
)
608 closure
.callbacks
= &private_data
->serializer
;
609 serialize_private
= private_data
->serialize_private
;
612 va_copy(args_copy
, *args
);
614 * Assumes event payload to start on largest_align alignment.
616 largest_align
= 1; /* must be non-zero for ltt_align */
617 data_size
= ltt_get_data_size(&closure
, serialize_private
,
618 &largest_align
, fmt
, &args_copy
);
619 largest_align
= min_t(int, largest_align
, sizeof(void *));
622 /* Iterate on each trace */
623 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
625 * Expect the filter to filter out events. If we get here,
626 * we went through tracepoint activation as a first step.
628 if (unlikely(dest_trace
&& trace
!= dest_trace
))
630 if (unlikely(!trace
->active
))
632 if (unlikely(!ltt_run_filter(trace
, eID
)))
634 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
635 rflags
= LTT_RFLAG_ID_SIZE
;
637 if (unlikely(eID
>= LTT_FREE_EVENTS
))
638 rflags
= LTT_RFLAG_ID
;
643 * Skip channels added after trace creation.
645 if (unlikely(chan_index
>= trace
->nr_channels
))
647 channel
= &trace
->channels
[chan_index
];
648 if (!channel
->active
)
651 /* reserve space : header and data */
652 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
653 data_size
, &slot_size
, &buf_offset
,
656 if (unlikely(ret
< 0))
657 continue; /* buffer full */
659 va_copy(args_copy
, *args
);
660 /* FIXME : could probably encapsulate transport better. */
661 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
662 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
663 /* Out-of-order write : header and data */
664 buf_offset
= ltt_write_event_header(trace
,
665 channel
, buf
, buf_offset
,
666 eID
, data_size
, tsc
, rflags
);
667 ltt_write_event_data(buf
, buf_offset
, &closure
,
669 largest_align
, fmt
, &args_copy
);
671 /* Out-of-order commit */
672 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
675 //ust// __get_cpu_var(ltt_nesting)--;
677 rcu_read_unlock_sched_notrace();
679 EXPORT_SYMBOL_GPL(ltt_vtrace
);
681 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
682 void *call_data
, const char *fmt
, ...)
687 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
690 EXPORT_SYMBOL_GPL(ltt_trace
);
692 //ust// MODULE_LICENSE("GPL");
693 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
694 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");