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"
30 LTT_TYPE_UNSIGNED_INT
,
35 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
38 * Inspired from vsnprintf
40 * The serialization format string supports the basic printf format strings.
41 * In addition, it defines new formats that can be used to serialize more
42 * complex/non portable data structures.
47 * field_name #tracetype %ctype
48 * field_name #tracetype %ctype1 %ctype2 ...
50 * A conversion is performed between format string types supported by GCC and
51 * the trace type requested. GCC type is used to perform type checking on format
52 * strings. Trace type is used to specify the exact binary representation
53 * in the trace. A mapping is done between one or more GCC types to one trace
54 * type. Sign extension, if required by the conversion, is performed following
57 * If a gcc format is not declared with a trace format, the gcc format is
58 * also used as binary representation in the trace.
60 * Strings are supported with %s.
61 * A single tracetype (sequence) can take multiple c types as parameter.
67 * Note: to write a uint32_t in a trace, the following expression is recommended
68 * si it can be portable:
70 * ("#4u%lu", (unsigned long)var)
74 * Serialization specific formats :
86 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
90 * n: (for network byte order)
92 * is written in the trace in network byte order.
94 * i.e.: #bn4u%lu, #n%lu, #b%u
97 * Variable length sequence
98 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
100 * #a specifies that this is a sequence
101 * #tracetype1 is the type of elements in the sequence
102 * #tracetype2 is the type of the element count
104 * array_ptr is a pointer to an array that contains members of size
106 * num_elems is the number of elements in the array.
107 * i.e.: #a #lu #lu %p %lu %u
110 * #k callback (taken from the probe data)
111 * The following % arguments are exepected by the callback
113 * i.e.: #a #lu #lu #k %p
115 * Note: No conversion is done from floats to integers, nor from integers to
116 * floats between c types and trace types. float conversion from double to float
117 * or from float to double is also not supported.
120 * %*b expects sizeof(data), data
121 * where sizeof(data) is 1, 2, 4 or 8
123 * Fixed length struct, union or array.
124 * FIXME: unable to extract those sizes statically.
125 * %*r expects sizeof(*ptr), ptr
126 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
127 * struct and unions removed.
128 * Fixed length array:
129 * [%p]#a[len #tracetype]
130 * i.e.: [%p]#a[12 #lu]
132 * Variable length sequence
133 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
134 * where elem_num is the number of elements in the sequence
136 static inline const char *parse_trace_type(const char *fmt
,
137 char *trace_size
, enum ltt_type
*trace_type
,
138 unsigned long *attributes
)
140 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
141 /* 'z' support added 23/7/1999 S.H. */
142 /* 'z' changed to 'Z' --davidm 1/25/99 */
143 /* 't' added for ptrdiff_t */
145 /* parse attributes. */
149 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
154 /* get the conversion qualifier */
156 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
157 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
158 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
159 *fmt
== '4' || *fmt
== 8) {
162 if (qualifier
== 'l' && *fmt
== 'l') {
170 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
171 *trace_size
= sizeof(unsigned char);
174 *trace_type
= LTT_TYPE_STRING
;
177 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
178 *trace_size
= sizeof(void *);
182 *trace_type
= LTT_TYPE_SIGNED_INT
;
188 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
197 *trace_size
= sizeof(long long);
200 *trace_size
= sizeof(long);
204 *trace_size
= sizeof(size_t);
207 //ust// *trace_size = sizeof(ptrdiff_t);
210 *trace_size
= sizeof(short);
213 *trace_size
= sizeof(uint8_t);
216 *trace_size
= sizeof(uint16_t);
219 *trace_size
= sizeof(uint32_t);
222 *trace_size
= sizeof(uint64_t);
225 *trace_size
= sizeof(int);
234 * Field width and precision are *not* supported.
237 static inline const char *parse_c_type(const char *fmt
,
238 char *c_size
, enum ltt_type
*c_type
)
240 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
241 /* 'z' support added 23/7/1999 S.H. */
242 /* 'z' changed to 'Z' --davidm 1/25/99 */
243 /* 't' added for ptrdiff_t */
245 /* process flags : ignore standard print formats for now. */
257 /* get the conversion qualifier */
259 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
260 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
264 if (qualifier
== 'l' && *fmt
== 'l') {
272 *c_type
= LTT_TYPE_UNSIGNED_INT
;
273 *c_size
= sizeof(unsigned char);
276 *c_type
= LTT_TYPE_STRING
;
279 *c_type
= LTT_TYPE_UNSIGNED_INT
;
280 *c_size
= sizeof(void *);
284 *c_type
= LTT_TYPE_SIGNED_INT
;
290 *c_type
= LTT_TYPE_UNSIGNED_INT
;
299 *c_size
= sizeof(long long);
302 *c_size
= sizeof(long);
306 *c_size
= sizeof(size_t);
309 //ust// *c_size = sizeof(ptrdiff_t);
312 *c_size
= sizeof(short);
315 *c_size
= sizeof(int);
322 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
324 char trace_size
, enum ltt_type trace_type
,
325 char c_size
, enum ltt_type c_type
,
326 int *largest_align
, va_list *args
)
329 unsigned long v_ulong
;
338 * Be careful about sign extension here.
339 * Sign extension is done with the destination (trace) type.
341 switch (trace_type
) {
342 case LTT_TYPE_SIGNED_INT
:
345 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
348 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
351 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
354 tmp
.v_uint64
= va_arg(*args
, int64_t);
360 case LTT_TYPE_UNSIGNED_INT
:
363 tmp
.v_ulong
= (unsigned long)(uint8_t)
364 va_arg(*args
, unsigned int);
367 tmp
.v_ulong
= (unsigned long)(uint16_t)
368 va_arg(*args
, unsigned int);
371 tmp
.v_ulong
= (unsigned long)(uint32_t)
372 va_arg(*args
, unsigned int);
375 tmp
.v_uint64
= va_arg(*args
, uint64_t);
381 case LTT_TYPE_STRING
:
382 tmp
.v_string
.s
= va_arg(*args
, const char *);
383 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
384 tmp
.v_string
.s
= "<NULL>";
385 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
387 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
389 buf_offset
+= tmp
.v_string
.len
;
396 * If trace_size is lower or equal to 4 bytes, there is no sign
397 * extension to do because we are already encoded in a long. Therefore,
398 * we can combine signed and unsigned ops. 4 bytes float also works
399 * with this, because we do a simple copy of 4 bytes into 4 bytes
400 * without manipulation (and we do not support conversion from integers
402 * It is also the case if c_size is 8 bytes, which is the largest
405 if (ltt_get_alignment()) {
406 buf_offset
+= ltt_align(buf_offset
, trace_size
);
408 *largest_align
= max_t(int, *largest_align
, trace_size
);
410 if (trace_size
<= 4 || c_size
== 8) {
412 switch (trace_size
) {
415 ltt_relay_write(buf
, buf_offset
,
416 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
419 ltt_relay_write(buf
, buf_offset
,
420 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
425 ltt_relay_write(buf
, buf_offset
,
426 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
429 ltt_relay_write(buf
, buf_offset
,
430 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
435 ltt_relay_write(buf
, buf_offset
,
436 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
439 ltt_relay_write(buf
, buf_offset
,
440 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
445 * c_size cannot be other than 8 here because
448 ltt_relay_write(buf
, buf_offset
,
449 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
456 buf_offset
+= trace_size
;
460 * Perform sign extension.
463 switch (trace_type
) {
464 case LTT_TYPE_SIGNED_INT
:
465 ltt_relay_write(buf
, buf_offset
,
466 (int64_t[]){ (int64_t)tmp
.v_ulong
},
469 case LTT_TYPE_UNSIGNED_INT
:
470 ltt_relay_write(buf
, buf_offset
,
471 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
478 buf_offset
+= trace_size
;
486 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
487 struct ltt_serialize_closure
*closure
,
488 void *serialize_private
, int *largest_align
,
489 const char *fmt
, va_list *args
)
491 char trace_size
= 0, c_size
= 0; /*
492 * 0 (unset), 1, 2, 4, 8 bytes.
494 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
495 unsigned long attributes
= 0;
497 for (; *fmt
; ++fmt
) {
501 ++fmt
; /* skip first '#' */
502 if (*fmt
== '#') /* Escaped ## */
505 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
510 ++fmt
; /* skip first '%' */
511 if (*fmt
== '%') /* Escaped %% */
513 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
515 * Output c types if no trace types has been
520 if (trace_type
== LTT_TYPE_NONE
)
522 if (c_type
== LTT_TYPE_STRING
)
523 trace_type
= LTT_TYPE_STRING
;
524 /* perform trace write */
525 buf_offset
= serialize_trace_data(buf
,
526 buf_offset
, trace_size
,
527 trace_type
, c_size
, c_type
,
528 largest_align
, args
);
531 trace_type
= LTT_TYPE_NONE
;
532 c_size
= LTT_TYPE_NONE
;
535 /* default is to skip the text, doing nothing */
540 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
543 * Calculate data size
544 * Assume that the padding for alignment starts at a sizeof(void *) address.
546 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
547 void *serialize_private
, int *largest_align
,
548 const char *fmt
, va_list *args
)
550 ltt_serialize_cb cb
= closure
->callbacks
[0];
552 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
553 largest_align
, fmt
, args
);
557 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
558 struct ltt_serialize_closure
*closure
,
559 void *serialize_private
, int largest_align
,
560 const char *fmt
, va_list *args
)
562 ltt_serialize_cb cb
= closure
->callbacks
[0];
564 buf_offset
+= ltt_align(buf_offset
, largest_align
);
565 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
569 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
570 void *call_data
, const char *fmt
, va_list *args
)
572 int largest_align
, ret
;
573 struct ltt_active_marker
*pdata
;
575 size_t data_size
, slot_size
;
576 unsigned int chan_index
;
577 struct ltt_channel_struct
*channel
;
578 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
579 struct rchan_buf
*buf
;
580 void *transport_data
;
584 struct ltt_serialize_closure closure
;
585 struct ltt_probe_private_data
*private_data
= call_data
;
586 void *serialize_private
= NULL
;
591 * This test is useful for quickly exiting static tracing when no trace
592 * is active. We expect to have an active trace when we get here.
594 if (unlikely(ltt_traces
.num_active_traces
== 0))
597 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
598 cpu
= smp_processor_id();
599 //ust// __get_cpu_var(ltt_nesting)++;
602 pdata
= (struct ltt_active_marker
*)probe_data
;
603 eID
= mdata
->event_id
;
604 chan_index
= mdata
->channel_id
;
605 closure
.callbacks
= pdata
->probe
->callbacks
;
607 if (unlikely(private_data
)) {
608 dest_trace
= private_data
->trace
;
609 if (private_data
->serializer
)
610 closure
.callbacks
= &private_data
->serializer
;
611 serialize_private
= private_data
->serialize_private
;
614 va_copy(args_copy
, *args
);
616 * Assumes event payload to start on largest_align alignment.
618 largest_align
= 1; /* must be non-zero for ltt_align */
619 data_size
= ltt_get_data_size(&closure
, serialize_private
,
620 &largest_align
, fmt
, &args_copy
);
621 largest_align
= min_t(int, largest_align
, sizeof(void *));
624 /* Iterate on each trace */
625 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
627 * Expect the filter to filter out events. If we get here,
628 * we went through tracepoint activation as a first step.
630 if (unlikely(dest_trace
&& trace
!= dest_trace
))
632 if (unlikely(!trace
->active
))
634 if (unlikely(!ltt_run_filter(trace
, eID
)))
636 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
637 rflags
= LTT_RFLAG_ID_SIZE
;
639 if (unlikely(eID
>= LTT_FREE_EVENTS
))
640 rflags
= LTT_RFLAG_ID
;
645 * Skip channels added after trace creation.
647 if (unlikely(chan_index
>= trace
->nr_channels
))
649 channel
= &trace
->channels
[chan_index
];
650 if (!channel
->active
)
653 /* reserve space : header and data */
654 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
655 data_size
, &slot_size
, &buf_offset
,
658 if (unlikely(ret
< 0))
659 continue; /* buffer full */
661 va_copy(args_copy
, *args
);
662 /* FIXME : could probably encapsulate transport better. */
663 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
664 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
665 /* Out-of-order write : header and data */
666 buf_offset
= ltt_write_event_header(trace
,
667 channel
, buf
, buf_offset
,
668 eID
, data_size
, tsc
, rflags
);
669 ltt_write_event_data(buf
, buf_offset
, &closure
,
671 largest_align
, fmt
, &args_copy
);
673 /* Out-of-order commit */
674 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
676 printf("just commited event at offset %d and size %d\n", buf_offset
, slot_size
);
678 //ust// __get_cpu_var(ltt_nesting)--;
680 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
682 EXPORT_SYMBOL_GPL(ltt_vtrace
);
684 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
685 void *call_data
, const char *fmt
, ...)
690 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
693 EXPORT_SYMBOL_GPL(ltt_trace
);
695 //ust// MODULE_LICENSE("GPL");
696 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
697 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");