add registers.h
[ust.git] / libust / serialize.c
1 /*
2 * LTTng serializing code.
3 *
4 * Copyright Mathieu Desnoyers, March 2007.
5 *
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.
10 *
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.
15 *
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
19 *
20 *
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.
25 */
26
27 #include <stdarg.h>
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>
32 #include <string.h>
33 #include <stdint.h>
34
35 #include "kernelcompat.h"
36 #define _LGPL_SOURCE
37 #include <urcu-bp.h>
38 #include <urcu/rculist.h>
39
40 #include "relay.h"
41 #include "tracer.h"
42 //#include "list.h"
43 #include "usterr.h"
44
45 enum ltt_type {
46 LTT_TYPE_SIGNED_INT,
47 LTT_TYPE_UNSIGNED_INT,
48 LTT_TYPE_STRING,
49 LTT_TYPE_NONE,
50 };
51
52 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
53
54 /*
55 * Inspired from vsnprintf
56 *
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.
60 *
61 * Typical use:
62 *
63 * field_name %ctype
64 * field_name #tracetype %ctype
65 * field_name #tracetype %ctype1 %ctype2 ...
66 *
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
72 * the trace type.
73 *
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.
76 *
77 * Strings are supported with %s.
78 * A single tracetype (sequence) can take multiple c types as parameter.
79 *
80 * c types:
81 *
82 * see printf(3).
83 *
84 * Note: to write a uint32_t in a trace, the following expression is recommended
85 * si it can be portable:
86 *
87 * ("#4u%lu", (unsigned long)var)
88 *
89 * trace types:
90 *
91 * Serialization specific formats :
92 *
93 * Fixed size integers
94 * #1u writes uint8_t
95 * #2u writes uint16_t
96 * #4u writes uint32_t
97 * #8u writes uint64_t
98 * #1d writes int8_t
99 * #2d writes int16_t
100 * #4d writes int32_t
101 * #8d writes int64_t
102 * i.e.:
103 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
104 *
105 * * Attributes:
106 *
107 * n: (for network byte order)
108 * #ntracetype%ctype
109 * is written in the trace in network byte order.
110 *
111 * i.e.: #bn4u%lu, #n%lu, #b%u
112 *
113 * TODO (eventually)
114 * Variable length sequence
115 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
116 * In the trace:
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
120 * GCC input:
121 * array_ptr is a pointer to an array that contains members of size
122 * elem_size.
123 * num_elems is the number of elements in the array.
124 * i.e.: #a #lu #lu %p %lu %u
125 *
126 * Callback
127 * #k callback (taken from the probe data)
128 * The following % arguments are exepected by the callback
129 *
130 * i.e.: #a #lu #lu #k %p
131 *
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.
135 *
136 * REMOVE
137 * %*b expects sizeof(data), data
138 * where sizeof(data) is 1, 2, 4 or 8
139 *
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]
148 *
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
152 */
153 static inline const char *parse_trace_type(const char *fmt,
154 char *trace_size, enum ltt_type *trace_type,
155 unsigned long *attributes)
156 {
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 */
161
162 /* parse attributes. */
163 repeat:
164 switch (*fmt) {
165 case 'n':
166 *attributes |= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER;
167 ++fmt;
168 goto repeat;
169 }
170
171 /* get the conversion qualifier */
172 qualifier = -1;
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) {
177 qualifier = *fmt;
178 ++fmt;
179 if (qualifier == 'l' && *fmt == 'l') {
180 qualifier = 'L';
181 ++fmt;
182 }
183 }
184
185 switch (*fmt) {
186 case 'c':
187 *trace_type = LTT_TYPE_UNSIGNED_INT;
188 *trace_size = sizeof(unsigned char);
189 goto parse_end;
190 case 's':
191 *trace_type = LTT_TYPE_STRING;
192 goto parse_end;
193 case 'p':
194 *trace_type = LTT_TYPE_UNSIGNED_INT;
195 *trace_size = sizeof(void *);
196 goto parse_end;
197 case 'd':
198 case 'i':
199 *trace_type = LTT_TYPE_SIGNED_INT;
200 break;
201 case 'o':
202 case 'u':
203 case 'x':
204 case 'X':
205 *trace_type = LTT_TYPE_UNSIGNED_INT;
206 break;
207 default:
208 if (!*fmt)
209 --fmt;
210 goto parse_end;
211 }
212 switch (qualifier) {
213 case 'L':
214 *trace_size = sizeof(long long);
215 break;
216 case 'l':
217 *trace_size = sizeof(long);
218 break;
219 case 'Z':
220 case 'z':
221 *trace_size = sizeof(size_t);
222 break;
223 //ust// case 't':
224 //ust// *trace_size = sizeof(ptrdiff_t);
225 //ust// break;
226 case 'h':
227 *trace_size = sizeof(short);
228 break;
229 case '1':
230 *trace_size = sizeof(uint8_t);
231 break;
232 case '2':
233 *trace_size = sizeof(uint16_t);
234 break;
235 case '4':
236 *trace_size = sizeof(uint32_t);
237 break;
238 case '8':
239 *trace_size = sizeof(uint64_t);
240 break;
241 default:
242 *trace_size = sizeof(int);
243 }
244
245 parse_end:
246 return fmt;
247 }
248
249 /*
250 * Restrictions:
251 * Field width and precision are *not* supported.
252 * %n not supported.
253 */
254 static inline const char *parse_c_type(const char *fmt,
255 char *c_size, enum ltt_type *c_type)
256 {
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 */
261
262 /* process flags : ignore standard print formats for now. */
263 repeat:
264 switch (*fmt) {
265 case '-':
266 case '+':
267 case ' ':
268 case '#':
269 case '0':
270 ++fmt;
271 goto repeat;
272 }
273
274 /* get the conversion qualifier */
275 qualifier = -1;
276 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
277 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
278 *fmt == 'S') {
279 qualifier = *fmt;
280 ++fmt;
281 if (qualifier == 'l' && *fmt == 'l') {
282 qualifier = 'L';
283 ++fmt;
284 }
285 }
286
287 switch (*fmt) {
288 case 'c':
289 *c_type = LTT_TYPE_UNSIGNED_INT;
290 *c_size = sizeof(unsigned char);
291 goto parse_end;
292 case 's':
293 *c_type = LTT_TYPE_STRING;
294 goto parse_end;
295 case 'p':
296 *c_type = LTT_TYPE_UNSIGNED_INT;
297 *c_size = sizeof(void *);
298 goto parse_end;
299 case 'd':
300 case 'i':
301 *c_type = LTT_TYPE_SIGNED_INT;
302 break;
303 case 'o':
304 case 'u':
305 case 'x':
306 case 'X':
307 *c_type = LTT_TYPE_UNSIGNED_INT;
308 break;
309 default:
310 if (!*fmt)
311 --fmt;
312 goto parse_end;
313 }
314 switch (qualifier) {
315 case 'L':
316 *c_size = sizeof(long long);
317 break;
318 case 'l':
319 *c_size = sizeof(long);
320 break;
321 case 'Z':
322 case 'z':
323 *c_size = sizeof(size_t);
324 break;
325 //ust// case 't':
326 //ust// *c_size = sizeof(ptrdiff_t);
327 //ust// break;
328 case 'h':
329 *c_size = sizeof(short);
330 break;
331 default:
332 *c_size = sizeof(int);
333 }
334
335 parse_end:
336 return fmt;
337 }
338
339 static inline size_t serialize_trace_data(struct rchan_buf *buf,
340 size_t buf_offset,
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)
344 {
345 union {
346 unsigned long v_ulong;
347 uint64_t v_uint64;
348 struct {
349 const char *s;
350 size_t len;
351 } v_string;
352 } tmp;
353
354 /*
355 * Be careful about sign extension here.
356 * Sign extension is done with the destination (trace) type.
357 */
358 switch (trace_type) {
359 case LTT_TYPE_SIGNED_INT:
360 switch (c_size) {
361 case 1:
362 tmp.v_ulong = (long)(int8_t)va_arg(*args, int);
363 break;
364 case 2:
365 tmp.v_ulong = (long)(int16_t)va_arg(*args, int);
366 break;
367 case 4:
368 tmp.v_ulong = (long)(int32_t)va_arg(*args, int);
369 break;
370 case 8:
371 tmp.v_uint64 = va_arg(*args, int64_t);
372 break;
373 default:
374 BUG();
375 }
376 break;
377 case LTT_TYPE_UNSIGNED_INT:
378 switch (c_size) {
379 case 1:
380 tmp.v_ulong = (unsigned long)(uint8_t)
381 va_arg(*args, unsigned int);
382 break;
383 case 2:
384 tmp.v_ulong = (unsigned long)(uint16_t)
385 va_arg(*args, unsigned int);
386 break;
387 case 4:
388 tmp.v_ulong = (unsigned long)(uint32_t)
389 va_arg(*args, unsigned int);
390 break;
391 case 8:
392 tmp.v_uint64 = va_arg(*args, uint64_t);
393 break;
394 default:
395 BUG();
396 }
397 break;
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;
403 if (buf)
404 ltt_relay_write(buf, buf_offset, tmp.v_string.s,
405 tmp.v_string.len);
406 buf_offset += tmp.v_string.len;
407 goto copydone;
408 default:
409 BUG();
410 }
411
412 /*
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
418 * to floats).
419 * It is also the case if c_size is 8 bytes, which is the largest
420 * possible integer.
421 */
422 if (ltt_get_alignment()) {
423 buf_offset += ltt_align(buf_offset, trace_size);
424 if (largest_align)
425 *largest_align = max_t(int, *largest_align, trace_size);
426 }
427 if (trace_size <= 4 || c_size == 8) {
428 if (buf) {
429 switch (trace_size) {
430 case 1:
431 if (c_size == 8)
432 ltt_relay_write(buf, buf_offset,
433 (uint8_t[]){ (uint8_t)tmp.v_uint64 },
434 sizeof(uint8_t));
435 else
436 ltt_relay_write(buf, buf_offset,
437 (uint8_t[]){ (uint8_t)tmp.v_ulong },
438 sizeof(uint8_t));
439 break;
440 case 2:
441 if (c_size == 8)
442 ltt_relay_write(buf, buf_offset,
443 (uint16_t[]){ (uint16_t)tmp.v_uint64 },
444 sizeof(uint16_t));
445 else
446 ltt_relay_write(buf, buf_offset,
447 (uint16_t[]){ (uint16_t)tmp.v_ulong },
448 sizeof(uint16_t));
449 break;
450 case 4:
451 if (c_size == 8)
452 ltt_relay_write(buf, buf_offset,
453 (uint32_t[]){ (uint32_t)tmp.v_uint64 },
454 sizeof(uint32_t));
455 else
456 ltt_relay_write(buf, buf_offset,
457 (uint32_t[]){ (uint32_t)tmp.v_ulong },
458 sizeof(uint32_t));
459 break;
460 case 8:
461 /*
462 * c_size cannot be other than 8 here because
463 * trace_size > 4.
464 */
465 ltt_relay_write(buf, buf_offset,
466 (uint64_t[]){ (uint64_t)tmp.v_uint64 },
467 sizeof(uint64_t));
468 break;
469 default:
470 BUG();
471 }
472 }
473 buf_offset += trace_size;
474 goto copydone;
475 } else {
476 /*
477 * Perform sign extension.
478 */
479 if (buf) {
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 },
484 sizeof(int64_t));
485 break;
486 case LTT_TYPE_UNSIGNED_INT:
487 ltt_relay_write(buf, buf_offset,
488 (uint64_t[]){ (uint64_t)tmp.v_ulong },
489 sizeof(uint64_t));
490 break;
491 default:
492 BUG();
493 }
494 }
495 buf_offset += trace_size;
496 goto copydone;
497 }
498
499 copydone:
500 return buf_offset;
501 }
502
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)
507 {
508 char trace_size = 0, c_size = 0; /*
509 * 0 (unset), 1, 2, 4, 8 bytes.
510 */
511 enum ltt_type trace_type = LTT_TYPE_NONE, c_type = LTT_TYPE_NONE;
512 unsigned long attributes = 0;
513
514 for (; *fmt ; ++fmt) {
515 switch (*fmt) {
516 case '#':
517 /* tracetypes (#) */
518 ++fmt; /* skip first '#' */
519 if (*fmt == '#') /* Escaped ## */
520 break;
521 attributes = 0;
522 fmt = parse_trace_type(fmt, &trace_size, &trace_type,
523 &attributes);
524 break;
525 case '%':
526 /* c types (%) */
527 ++fmt; /* skip first '%' */
528 if (*fmt == '%') /* Escaped %% */
529 break;
530 fmt = parse_c_type(fmt, &c_size, &c_type);
531 /*
532 * Output c types if no trace types has been
533 * specified.
534 */
535 if (!trace_size)
536 trace_size = c_size;
537 if (trace_type == LTT_TYPE_NONE)
538 trace_type = c_type;
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);
546 trace_size = 0;
547 c_size = 0;
548 trace_type = LTT_TYPE_NONE;
549 c_size = LTT_TYPE_NONE;
550 attributes = 0;
551 break;
552 /* default is to skip the text, doing nothing */
553 }
554 }
555 return buf_offset;
556 }
557
558 /*
559 * Calculate data size
560 * Assume that the padding for alignment starts at a sizeof(void *) address.
561 */
562 static notrace size_t ltt_get_data_size(struct ltt_serialize_closure *closure,
563 void *serialize_private, int *largest_align,
564 const char *fmt, va_list *args)
565 {
566 ltt_serialize_cb cb = closure->callbacks[0];
567 closure->cb_idx = 0;
568 return (size_t)cb(NULL, 0, closure, serialize_private,
569 largest_align, fmt, args);
570 }
571
572 static notrace
573 void ltt_write_event_data(struct rchan_buf *buf, size_t buf_offset,
574 struct ltt_serialize_closure *closure,
575 void *serialize_private, int largest_align,
576 const char *fmt, va_list *args)
577 {
578 ltt_serialize_cb cb = closure->callbacks[0];
579 closure->cb_idx = 0;
580 buf_offset += ltt_align(buf_offset, largest_align);
581 cb(buf, buf_offset, closure, serialize_private, NULL, fmt, args);
582 }
583
584
585 notrace void ltt_vtrace(const struct marker *mdata, void *probe_data,
586 void *call_data, const char *fmt, va_list *args)
587 {
588 int largest_align, ret;
589 struct ltt_active_marker *pdata;
590 uint16_t eID;
591 size_t data_size, slot_size;
592 unsigned int chan_index;
593 struct ltt_channel_struct *channel;
594 struct ltt_trace_struct *trace, *dest_trace = NULL;
595 struct rchan_buf *buf;
596 void *transport_data;
597 u64 tsc;
598 long buf_offset;
599 va_list args_copy;
600 struct ltt_serialize_closure closure;
601 struct ltt_probe_private_data *private_data = call_data;
602 void *serialize_private = NULL;
603 //ust// int cpu;
604 unsigned int rflags;
605
606 /*
607 * This test is useful for quickly exiting static tracing when no trace
608 * is active. We expect to have an active trace when we get here.
609 */
610 if (unlikely(ltt_traces.num_active_traces == 0))
611 return;
612
613 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
614 //ust// cpu = smp_processor_id();
615 //ust// __get_cpu_var(ltt_nesting)++;
616 ltt_nesting++;
617
618 pdata = (struct ltt_active_marker *)probe_data;
619 eID = mdata->event_id;
620 chan_index = mdata->channel_id;
621 closure.callbacks = pdata->probe->callbacks;
622
623 if (unlikely(private_data)) {
624 dest_trace = private_data->trace;
625 if (private_data->serializer)
626 closure.callbacks = &private_data->serializer;
627 serialize_private = private_data->serialize_private;
628 }
629
630 va_copy(args_copy, *args);
631 /*
632 * Assumes event payload to start on largest_align alignment.
633 */
634 largest_align = 1; /* must be non-zero for ltt_align */
635 data_size = ltt_get_data_size(&closure, serialize_private,
636 &largest_align, fmt, &args_copy);
637 largest_align = min_t(int, largest_align, sizeof(void *));
638 va_end(args_copy);
639
640 /* Iterate on each trace */
641 list_for_each_entry_rcu(trace, &ltt_traces.head, list) {
642 /*
643 * Expect the filter to filter out events. If we get here,
644 * we went through tracepoint activation as a first step.
645 */
646 if (unlikely(dest_trace && trace != dest_trace))
647 continue;
648 if (unlikely(!trace->active))
649 continue;
650 if (unlikely(!ltt_run_filter(trace, eID)))
651 continue;
652 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
653 rflags = LTT_RFLAG_ID_SIZE;
654 #else
655 if (unlikely(eID >= LTT_FREE_EVENTS))
656 rflags = LTT_RFLAG_ID;
657 else
658 rflags = 0;
659 #endif
660 /*
661 * Skip channels added after trace creation.
662 */
663 if (unlikely(chan_index >= trace->nr_channels))
664 continue;
665 channel = &trace->channels[chan_index];
666 if (!channel->active)
667 continue;
668
669 /* reserve space : header and data */
670 ret = ltt_reserve_slot(trace, channel, &transport_data,
671 data_size, &slot_size, &buf_offset,
672 &tsc, &rflags,
673 largest_align);
674 if (unlikely(ret < 0))
675 continue; /* buffer full */
676
677 va_copy(args_copy, *args);
678 /* FIXME : could probably encapsulate transport better. */
679 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
680 buf = ((struct rchan *)channel->trans_channel_data)->buf;
681 /* Out-of-order write : header and data */
682 buf_offset = ltt_write_event_header(trace,
683 channel, buf, buf_offset,
684 eID, data_size, tsc, rflags);
685 ltt_write_event_data(buf, buf_offset, &closure,
686 serialize_private,
687 largest_align, fmt, &args_copy);
688 va_end(args_copy);
689 /* Out-of-order commit */
690 ltt_commit_slot(channel, &transport_data, buf_offset,
691 data_size, slot_size);
692 DBG("just commited event at offset %ld and size %zd", buf_offset, slot_size);
693 }
694 //ust// __get_cpu_var(ltt_nesting)--;
695 ltt_nesting--;
696 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
697 }
698
699 notrace void ltt_trace(const struct marker *mdata, void *probe_data,
700 void *call_data, const char *fmt, ...)
701 {
702 va_list args;
703
704 va_start(args, fmt);
705 ltt_vtrace(mdata, probe_data, call_data, fmt, &args);
706 va_end(args);
707 }
708
709 //ust// MODULE_LICENSE("GPL");
710 //ust// MODULE_AUTHOR("Mathieu Desnoyers");
711 //ust// MODULE_DESCRIPTION("Linux Trace Toolkit Next Generation Serializer");
This page took 0.082218 seconds and 4 git commands to generate.