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>
22 #include "kernelcompat.h"
33 LTT_TYPE_UNSIGNED_INT
,
38 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
41 * Inspired from vsnprintf
43 * The serialization format string supports the basic printf format strings.
44 * In addition, it defines new formats that can be used to serialize more
45 * complex/non portable data structures.
50 * field_name #tracetype %ctype
51 * field_name #tracetype %ctype1 %ctype2 ...
53 * A conversion is performed between format string types supported by GCC and
54 * the trace type requested. GCC type is used to perform type checking on format
55 * strings. Trace type is used to specify the exact binary representation
56 * in the trace. A mapping is done between one or more GCC types to one trace
57 * type. Sign extension, if required by the conversion, is performed following
60 * If a gcc format is not declared with a trace format, the gcc format is
61 * also used as binary representation in the trace.
63 * Strings are supported with %s.
64 * A single tracetype (sequence) can take multiple c types as parameter.
70 * Note: to write a uint32_t in a trace, the following expression is recommended
71 * si it can be portable:
73 * ("#4u%lu", (unsigned long)var)
77 * Serialization specific formats :
89 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
93 * n: (for network byte order)
95 * is written in the trace in network byte order.
97 * i.e.: #bn4u%lu, #n%lu, #b%u
100 * Variable length sequence
101 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
103 * #a specifies that this is a sequence
104 * #tracetype1 is the type of elements in the sequence
105 * #tracetype2 is the type of the element count
107 * array_ptr is a pointer to an array that contains members of size
109 * num_elems is the number of elements in the array.
110 * i.e.: #a #lu #lu %p %lu %u
113 * #k callback (taken from the probe data)
114 * The following % arguments are exepected by the callback
116 * i.e.: #a #lu #lu #k %p
118 * Note: No conversion is done from floats to integers, nor from integers to
119 * floats between c types and trace types. float conversion from double to float
120 * or from float to double is also not supported.
123 * %*b expects sizeof(data), data
124 * where sizeof(data) is 1, 2, 4 or 8
126 * Fixed length struct, union or array.
127 * FIXME: unable to extract those sizes statically.
128 * %*r expects sizeof(*ptr), ptr
129 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
130 * struct and unions removed.
131 * Fixed length array:
132 * [%p]#a[len #tracetype]
133 * i.e.: [%p]#a[12 #lu]
135 * Variable length sequence
136 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
137 * where elem_num is the number of elements in the sequence
139 static inline const char *parse_trace_type(const char *fmt
,
140 char *trace_size
, enum ltt_type
*trace_type
,
141 unsigned long *attributes
)
143 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
144 /* 'z' support added 23/7/1999 S.H. */
145 /* 'z' changed to 'Z' --davidm 1/25/99 */
146 /* 't' added for ptrdiff_t */
148 /* parse attributes. */
152 *attributes
|= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER
;
157 /* get the conversion qualifier */
159 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
160 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
161 *fmt
== 'S' || *fmt
== '1' || *fmt
== '2' ||
162 *fmt
== '4' || *fmt
== 8) {
165 if (qualifier
== 'l' && *fmt
== 'l') {
173 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
174 *trace_size
= sizeof(unsigned char);
177 *trace_type
= LTT_TYPE_STRING
;
180 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
181 *trace_size
= sizeof(void *);
185 *trace_type
= LTT_TYPE_SIGNED_INT
;
191 *trace_type
= LTT_TYPE_UNSIGNED_INT
;
200 *trace_size
= sizeof(long long);
203 *trace_size
= sizeof(long);
207 *trace_size
= sizeof(size_t);
210 //ust// *trace_size = sizeof(ptrdiff_t);
213 *trace_size
= sizeof(short);
216 *trace_size
= sizeof(uint8_t);
219 *trace_size
= sizeof(uint16_t);
222 *trace_size
= sizeof(uint32_t);
225 *trace_size
= sizeof(uint64_t);
228 *trace_size
= sizeof(int);
237 * Field width and precision are *not* supported.
240 static inline const char *parse_c_type(const char *fmt
,
241 char *c_size
, enum ltt_type
*c_type
)
243 int qualifier
; /* 'h', 'l', or 'L' for integer fields */
244 /* 'z' support added 23/7/1999 S.H. */
245 /* 'z' changed to 'Z' --davidm 1/25/99 */
246 /* 't' added for ptrdiff_t */
248 /* process flags : ignore standard print formats for now. */
260 /* get the conversion qualifier */
262 if (*fmt
== 'h' || *fmt
== 'l' || *fmt
== 'L' ||
263 *fmt
== 'Z' || *fmt
== 'z' || *fmt
== 't' ||
267 if (qualifier
== 'l' && *fmt
== 'l') {
275 *c_type
= LTT_TYPE_UNSIGNED_INT
;
276 *c_size
= sizeof(unsigned char);
279 *c_type
= LTT_TYPE_STRING
;
282 *c_type
= LTT_TYPE_UNSIGNED_INT
;
283 *c_size
= sizeof(void *);
287 *c_type
= LTT_TYPE_SIGNED_INT
;
293 *c_type
= LTT_TYPE_UNSIGNED_INT
;
302 *c_size
= sizeof(long long);
305 *c_size
= sizeof(long);
309 *c_size
= sizeof(size_t);
312 //ust// *c_size = sizeof(ptrdiff_t);
315 *c_size
= sizeof(short);
318 *c_size
= sizeof(int);
325 static inline size_t serialize_trace_data(struct rchan_buf
*buf
,
327 char trace_size
, enum ltt_type trace_type
,
328 char c_size
, enum ltt_type c_type
,
329 int *largest_align
, va_list *args
)
332 unsigned long v_ulong
;
341 * Be careful about sign extension here.
342 * Sign extension is done with the destination (trace) type.
344 switch (trace_type
) {
345 case LTT_TYPE_SIGNED_INT
:
348 tmp
.v_ulong
= (long)(int8_t)va_arg(*args
, int);
351 tmp
.v_ulong
= (long)(int16_t)va_arg(*args
, int);
354 tmp
.v_ulong
= (long)(int32_t)va_arg(*args
, int);
357 tmp
.v_uint64
= va_arg(*args
, int64_t);
363 case LTT_TYPE_UNSIGNED_INT
:
366 tmp
.v_ulong
= (unsigned long)(uint8_t)
367 va_arg(*args
, unsigned int);
370 tmp
.v_ulong
= (unsigned long)(uint16_t)
371 va_arg(*args
, unsigned int);
374 tmp
.v_ulong
= (unsigned long)(uint32_t)
375 va_arg(*args
, unsigned int);
378 tmp
.v_uint64
= va_arg(*args
, uint64_t);
384 case LTT_TYPE_STRING
:
385 tmp
.v_string
.s
= va_arg(*args
, const char *);
386 if ((unsigned long)tmp
.v_string
.s
< PAGE_SIZE
)
387 tmp
.v_string
.s
= "<NULL>";
388 tmp
.v_string
.len
= strlen(tmp
.v_string
.s
)+1;
390 ltt_relay_write(buf
, buf_offset
, tmp
.v_string
.s
,
392 buf_offset
+= tmp
.v_string
.len
;
399 * If trace_size is lower or equal to 4 bytes, there is no sign
400 * extension to do because we are already encoded in a long. Therefore,
401 * we can combine signed and unsigned ops. 4 bytes float also works
402 * with this, because we do a simple copy of 4 bytes into 4 bytes
403 * without manipulation (and we do not support conversion from integers
405 * It is also the case if c_size is 8 bytes, which is the largest
408 if (ltt_get_alignment()) {
409 buf_offset
+= ltt_align(buf_offset
, trace_size
);
411 *largest_align
= max_t(int, *largest_align
, trace_size
);
413 if (trace_size
<= 4 || c_size
== 8) {
415 switch (trace_size
) {
418 ltt_relay_write(buf
, buf_offset
,
419 (uint8_t[]){ (uint8_t)tmp
.v_uint64
},
422 ltt_relay_write(buf
, buf_offset
,
423 (uint8_t[]){ (uint8_t)tmp
.v_ulong
},
428 ltt_relay_write(buf
, buf_offset
,
429 (uint16_t[]){ (uint16_t)tmp
.v_uint64
},
432 ltt_relay_write(buf
, buf_offset
,
433 (uint16_t[]){ (uint16_t)tmp
.v_ulong
},
438 ltt_relay_write(buf
, buf_offset
,
439 (uint32_t[]){ (uint32_t)tmp
.v_uint64
},
442 ltt_relay_write(buf
, buf_offset
,
443 (uint32_t[]){ (uint32_t)tmp
.v_ulong
},
448 * c_size cannot be other than 8 here because
451 ltt_relay_write(buf
, buf_offset
,
452 (uint64_t[]){ (uint64_t)tmp
.v_uint64
},
459 buf_offset
+= trace_size
;
463 * Perform sign extension.
466 switch (trace_type
) {
467 case LTT_TYPE_SIGNED_INT
:
468 ltt_relay_write(buf
, buf_offset
,
469 (int64_t[]){ (int64_t)tmp
.v_ulong
},
472 case LTT_TYPE_UNSIGNED_INT
:
473 ltt_relay_write(buf
, buf_offset
,
474 (uint64_t[]){ (uint64_t)tmp
.v_ulong
},
481 buf_offset
+= trace_size
;
489 notrace
size_t ltt_serialize_data(struct rchan_buf
*buf
, size_t buf_offset
,
490 struct ltt_serialize_closure
*closure
,
491 void *serialize_private
, int *largest_align
,
492 const char *fmt
, va_list *args
)
494 char trace_size
= 0, c_size
= 0; /*
495 * 0 (unset), 1, 2, 4, 8 bytes.
497 enum ltt_type trace_type
= LTT_TYPE_NONE
, c_type
= LTT_TYPE_NONE
;
498 unsigned long attributes
= 0;
500 for (; *fmt
; ++fmt
) {
504 ++fmt
; /* skip first '#' */
505 if (*fmt
== '#') /* Escaped ## */
508 fmt
= parse_trace_type(fmt
, &trace_size
, &trace_type
,
513 ++fmt
; /* skip first '%' */
514 if (*fmt
== '%') /* Escaped %% */
516 fmt
= parse_c_type(fmt
, &c_size
, &c_type
);
518 * Output c types if no trace types has been
523 if (trace_type
== LTT_TYPE_NONE
)
525 if (c_type
== LTT_TYPE_STRING
)
526 trace_type
= LTT_TYPE_STRING
;
527 /* perform trace write */
528 buf_offset
= serialize_trace_data(buf
,
529 buf_offset
, trace_size
,
530 trace_type
, c_size
, c_type
,
531 largest_align
, args
);
534 trace_type
= LTT_TYPE_NONE
;
535 c_size
= LTT_TYPE_NONE
;
538 /* default is to skip the text, doing nothing */
543 EXPORT_SYMBOL_GPL(ltt_serialize_data
);
546 * Calculate data size
547 * Assume that the padding for alignment starts at a sizeof(void *) address.
549 static notrace
size_t ltt_get_data_size(struct ltt_serialize_closure
*closure
,
550 void *serialize_private
, int *largest_align
,
551 const char *fmt
, va_list *args
)
553 ltt_serialize_cb cb
= closure
->callbacks
[0];
555 return (size_t)cb(NULL
, 0, closure
, serialize_private
,
556 largest_align
, fmt
, args
);
560 void ltt_write_event_data(struct rchan_buf
*buf
, size_t buf_offset
,
561 struct ltt_serialize_closure
*closure
,
562 void *serialize_private
, int largest_align
,
563 const char *fmt
, va_list *args
)
565 ltt_serialize_cb cb
= closure
->callbacks
[0];
567 buf_offset
+= ltt_align(buf_offset
, largest_align
);
568 cb(buf
, buf_offset
, closure
, serialize_private
, NULL
, fmt
, args
);
572 notrace
void ltt_vtrace(const struct marker
*mdata
, void *probe_data
,
573 void *call_data
, const char *fmt
, va_list *args
)
575 int largest_align
, ret
;
576 struct ltt_active_marker
*pdata
;
578 size_t data_size
, slot_size
;
579 unsigned int chan_index
;
580 struct ltt_channel_struct
*channel
;
581 struct ltt_trace_struct
*trace
, *dest_trace
= NULL
;
582 struct rchan_buf
*buf
;
583 void *transport_data
;
587 struct ltt_serialize_closure closure
;
588 struct ltt_probe_private_data
*private_data
= call_data
;
589 void *serialize_private
= NULL
;
594 * This test is useful for quickly exiting static tracing when no trace
595 * is active. We expect to have an active trace when we get here.
597 if (unlikely(ltt_traces
.num_active_traces
== 0))
600 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
601 cpu
= smp_processor_id();
602 //ust// __get_cpu_var(ltt_nesting)++;
605 pdata
= (struct ltt_active_marker
*)probe_data
;
606 eID
= mdata
->event_id
;
607 chan_index
= mdata
->channel_id
;
608 closure
.callbacks
= pdata
->probe
->callbacks
;
610 if (unlikely(private_data
)) {
611 dest_trace
= private_data
->trace
;
612 if (private_data
->serializer
)
613 closure
.callbacks
= &private_data
->serializer
;
614 serialize_private
= private_data
->serialize_private
;
617 va_copy(args_copy
, *args
);
619 * Assumes event payload to start on largest_align alignment.
621 largest_align
= 1; /* must be non-zero for ltt_align */
622 data_size
= ltt_get_data_size(&closure
, serialize_private
,
623 &largest_align
, fmt
, &args_copy
);
624 largest_align
= min_t(int, largest_align
, sizeof(void *));
627 /* Iterate on each trace */
628 list_for_each_entry_rcu(trace
, <t_traces
.head
, list
) {
630 * Expect the filter to filter out events. If we get here,
631 * we went through tracepoint activation as a first step.
633 if (unlikely(dest_trace
&& trace
!= dest_trace
))
635 if (unlikely(!trace
->active
))
637 if (unlikely(!ltt_run_filter(trace
, eID
)))
639 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
640 rflags
= LTT_RFLAG_ID_SIZE
;
642 if (unlikely(eID
>= LTT_FREE_EVENTS
))
643 rflags
= LTT_RFLAG_ID
;
648 * Skip channels added after trace creation.
650 if (unlikely(chan_index
>= trace
->nr_channels
))
652 channel
= &trace
->channels
[chan_index
];
653 if (!channel
->active
)
656 /* reserve space : header and data */
657 ret
= ltt_reserve_slot(trace
, channel
, &transport_data
,
658 data_size
, &slot_size
, &buf_offset
,
661 if (unlikely(ret
< 0))
662 continue; /* buffer full */
664 va_copy(args_copy
, *args
);
665 /* FIXME : could probably encapsulate transport better. */
666 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
667 buf
= ((struct rchan
*)channel
->trans_channel_data
)->buf
;
668 /* Out-of-order write : header and data */
669 buf_offset
= ltt_write_event_header(trace
,
670 channel
, buf
, buf_offset
,
671 eID
, data_size
, tsc
, rflags
);
672 ltt_write_event_data(buf
, buf_offset
, &closure
,
674 largest_align
, fmt
, &args_copy
);
676 /* Out-of-order commit */
677 ltt_commit_slot(channel
, &transport_data
, buf_offset
,
678 data_size
, slot_size
);
679 printf("just commited event at offset %d and size %d\n", buf_offset
, slot_size
);
681 //ust// __get_cpu_var(ltt_nesting)--;
683 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
685 EXPORT_SYMBOL_GPL(ltt_vtrace
);
687 notrace
void ltt_trace(const struct marker
*mdata
, void *probe_data
,
688 void *call_data
, const char *fmt
, ...)
693 ltt_vtrace(mdata
, probe_data
, call_data
, fmt
, &args
);
696 EXPORT_SYMBOL_GPL(ltt_trace
);
698 //ust// MODULE_LICENSE("GPL");
699 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
700 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");