update to trace format 2.6
[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 #define _GNU_SOURCE
28 #include <unistd.h>
29 #include <sys/syscall.h>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <stdio.h>
34
35 #include <ust/kernelcompat.h>
36 #define _LGPL_SOURCE
37 #include <urcu-bp.h>
38 #include <urcu/rculist.h>
39
40 #include "buffers.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 static int ust_get_cpu(void)
53 {
54 return sched_getcpu();
55 }
56
57 #define LTT_ATTRIBUTE_NETWORK_BYTE_ORDER (1<<1)
58
59 /*
60 * Inspired from vsnprintf
61 *
62 * The serialization format string supports the basic printf format strings.
63 * In addition, it defines new formats that can be used to serialize more
64 * complex/non portable data structures.
65 *
66 * Typical use:
67 *
68 * field_name %ctype
69 * field_name #tracetype %ctype
70 * field_name #tracetype %ctype1 %ctype2 ...
71 *
72 * A conversion is performed between format string types supported by GCC and
73 * the trace type requested. GCC type is used to perform type checking on format
74 * strings. Trace type is used to specify the exact binary representation
75 * in the trace. A mapping is done between one or more GCC types to one trace
76 * type. Sign extension, if required by the conversion, is performed following
77 * the trace type.
78 *
79 * If a gcc format is not declared with a trace format, the gcc format is
80 * also used as binary representation in the trace.
81 *
82 * Strings are supported with %s.
83 * A single tracetype (sequence) can take multiple c types as parameter.
84 *
85 * c types:
86 *
87 * see printf(3).
88 *
89 * Note: to write a uint32_t in a trace, the following expression is recommended
90 * si it can be portable:
91 *
92 * ("#4u%lu", (unsigned long)var)
93 *
94 * trace types:
95 *
96 * Serialization specific formats :
97 *
98 * Fixed size integers
99 * #1u writes uint8_t
100 * #2u writes uint16_t
101 * #4u writes uint32_t
102 * #8u writes uint64_t
103 * #1d writes int8_t
104 * #2d writes int16_t
105 * #4d writes int32_t
106 * #8d writes int64_t
107 * i.e.:
108 * #1u%lu #2u%lu #4d%lu #8d%lu #llu%hu #d%lu
109 *
110 * * Attributes:
111 *
112 * n: (for network byte order)
113 * #ntracetype%ctype
114 * is written in the trace in network byte order.
115 *
116 * i.e.: #bn4u%lu, #n%lu, #b%u
117 *
118 * TODO (eventually)
119 * Variable length sequence
120 * #a #tracetype1 #tracetype2 %array_ptr %elem_size %num_elems
121 * In the trace:
122 * #a specifies that this is a sequence
123 * #tracetype1 is the type of elements in the sequence
124 * #tracetype2 is the type of the element count
125 * GCC input:
126 * array_ptr is a pointer to an array that contains members of size
127 * elem_size.
128 * num_elems is the number of elements in the array.
129 * i.e.: #a #lu #lu %p %lu %u
130 *
131 * Callback
132 * #k callback (taken from the probe data)
133 * The following % arguments are exepected by the callback
134 *
135 * i.e.: #a #lu #lu #k %p
136 *
137 * Note: No conversion is done from floats to integers, nor from integers to
138 * floats between c types and trace types. float conversion from double to float
139 * or from float to double is also not supported.
140 *
141 * REMOVE
142 * %*b expects sizeof(data), data
143 * where sizeof(data) is 1, 2, 4 or 8
144 *
145 * Fixed length struct, union or array.
146 * FIXME: unable to extract those sizes statically.
147 * %*r expects sizeof(*ptr), ptr
148 * %*.*r expects sizeof(*ptr), __alignof__(*ptr), ptr
149 * struct and unions removed.
150 * Fixed length array:
151 * [%p]#a[len #tracetype]
152 * i.e.: [%p]#a[12 #lu]
153 *
154 * Variable length sequence
155 * %*.*:*v expects sizeof(*ptr), __alignof__(*ptr), elem_num, ptr
156 * where elem_num is the number of elements in the sequence
157 */
158 static inline const char *parse_trace_type(const char *fmt,
159 char *trace_size, enum ltt_type *trace_type,
160 unsigned long *attributes)
161 {
162 int qualifier; /* 'h', 'l', or 'L' for integer fields */
163 /* 'z' support added 23/7/1999 S.H. */
164 /* 'z' changed to 'Z' --davidm 1/25/99 */
165 /* 't' added for ptrdiff_t */
166
167 /* parse attributes. */
168 repeat:
169 switch (*fmt) {
170 case 'n':
171 *attributes |= LTT_ATTRIBUTE_NETWORK_BYTE_ORDER;
172 ++fmt;
173 goto repeat;
174 }
175
176 /* get the conversion qualifier */
177 qualifier = -1;
178 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
179 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
180 *fmt == 'S' || *fmt == '1' || *fmt == '2' ||
181 *fmt == '4' || *fmt == 8) {
182 qualifier = *fmt;
183 ++fmt;
184 if (qualifier == 'l' && *fmt == 'l') {
185 qualifier = 'L';
186 ++fmt;
187 }
188 }
189
190 switch (*fmt) {
191 case 'c':
192 *trace_type = LTT_TYPE_UNSIGNED_INT;
193 *trace_size = sizeof(unsigned char);
194 goto parse_end;
195 case 's':
196 *trace_type = LTT_TYPE_STRING;
197 goto parse_end;
198 case 'p':
199 *trace_type = LTT_TYPE_UNSIGNED_INT;
200 *trace_size = sizeof(void *);
201 goto parse_end;
202 case 'd':
203 case 'i':
204 *trace_type = LTT_TYPE_SIGNED_INT;
205 break;
206 case 'o':
207 case 'u':
208 case 'x':
209 case 'X':
210 *trace_type = LTT_TYPE_UNSIGNED_INT;
211 break;
212 default:
213 if (!*fmt)
214 --fmt;
215 goto parse_end;
216 }
217 switch (qualifier) {
218 case 'L':
219 *trace_size = sizeof(long long);
220 break;
221 case 'l':
222 *trace_size = sizeof(long);
223 break;
224 case 'Z':
225 case 'z':
226 *trace_size = sizeof(size_t);
227 break;
228 //ust// case 't':
229 //ust// *trace_size = sizeof(ptrdiff_t);
230 //ust// break;
231 case 'h':
232 *trace_size = sizeof(short);
233 break;
234 case '1':
235 *trace_size = sizeof(uint8_t);
236 break;
237 case '2':
238 *trace_size = sizeof(uint16_t);
239 break;
240 case '4':
241 *trace_size = sizeof(uint32_t);
242 break;
243 case '8':
244 *trace_size = sizeof(uint64_t);
245 break;
246 default:
247 *trace_size = sizeof(int);
248 }
249
250 parse_end:
251 return fmt;
252 }
253
254 /*
255 * Restrictions:
256 * Field width and precision are *not* supported.
257 * %n not supported.
258 */
259 static inline
260 const char *parse_c_type(const char *fmt, char *c_size, enum ltt_type *c_type,
261 char *outfmt)
262 {
263 int qualifier; /* 'h', 'l', or 'L' for integer fields */
264 /* 'z' support added 23/7/1999 S.H. */
265 /* 'z' changed to 'Z' --davidm 1/25/99 */
266 /* 't' added for ptrdiff_t */
267
268 /* process flags : ignore standard print formats for now. */
269 repeat:
270 switch (*fmt) {
271 case '-':
272 case '+':
273 case ' ':
274 case '#':
275 case '0':
276 ++fmt;
277 goto repeat;
278 }
279
280 /* get the conversion qualifier */
281 qualifier = -1;
282 if (*fmt == 'h' || *fmt == 'l' || *fmt == 'L' ||
283 *fmt == 'Z' || *fmt == 'z' || *fmt == 't' ||
284 *fmt == 'S') {
285 qualifier = *fmt;
286 ++fmt;
287 if (qualifier == 'l' && *fmt == 'l') {
288 qualifier = 'L';
289 ++fmt;
290 }
291 }
292
293 if (outfmt) {
294 if (qualifier != -1)
295 *outfmt++ = (char)qualifier;
296 *outfmt++ = *fmt;
297 *outfmt = 0;
298 }
299
300 switch (*fmt) {
301 case 'c':
302 *c_type = LTT_TYPE_UNSIGNED_INT;
303 *c_size = sizeof(unsigned char);
304 goto parse_end;
305 case 's':
306 *c_type = LTT_TYPE_STRING;
307 goto parse_end;
308 case 'p':
309 *c_type = LTT_TYPE_UNSIGNED_INT;
310 *c_size = sizeof(void *);
311 goto parse_end;
312 case 'd':
313 case 'i':
314 *c_type = LTT_TYPE_SIGNED_INT;
315 break;
316 case 'o':
317 case 'u':
318 case 'x':
319 case 'X':
320 *c_type = LTT_TYPE_UNSIGNED_INT;
321 break;
322 default:
323 if (!*fmt)
324 --fmt;
325 goto parse_end;
326 }
327 switch (qualifier) {
328 case 'L':
329 *c_size = sizeof(long long);
330 break;
331 case 'l':
332 *c_size = sizeof(long);
333 break;
334 case 'Z':
335 case 'z':
336 *c_size = sizeof(size_t);
337 break;
338 //ust// case 't':
339 //ust// *c_size = sizeof(ptrdiff_t);
340 //ust// break;
341 case 'h':
342 *c_size = sizeof(short);
343 break;
344 default:
345 *c_size = sizeof(int);
346 }
347
348 parse_end:
349 return fmt;
350 }
351
352 static inline size_t serialize_trace_data(struct ust_buffer *buf,
353 size_t buf_offset,
354 char trace_size, enum ltt_type trace_type,
355 char c_size, enum ltt_type c_type,
356 int *largest_align, va_list *args)
357 {
358 union {
359 unsigned long v_ulong;
360 uint64_t v_uint64;
361 struct {
362 const char *s;
363 size_t len;
364 } v_string;
365 } tmp;
366
367 /*
368 * Be careful about sign extension here.
369 * Sign extension is done with the destination (trace) type.
370 */
371 switch (trace_type) {
372 case LTT_TYPE_SIGNED_INT:
373 switch (c_size) {
374 case 1:
375 tmp.v_ulong = (long)(int8_t)va_arg(*args, int);
376 break;
377 case 2:
378 tmp.v_ulong = (long)(int16_t)va_arg(*args, int);
379 break;
380 case 4:
381 tmp.v_ulong = (long)(int32_t)va_arg(*args, int);
382 break;
383 case 8:
384 tmp.v_uint64 = va_arg(*args, int64_t);
385 break;
386 default:
387 BUG();
388 }
389 break;
390 case LTT_TYPE_UNSIGNED_INT:
391 switch (c_size) {
392 case 1:
393 tmp.v_ulong = (unsigned long)(uint8_t)va_arg(*args, unsigned int);
394 break;
395 case 2:
396 tmp.v_ulong = (unsigned long)(uint16_t)va_arg(*args, unsigned int);
397 break;
398 case 4:
399 tmp.v_ulong = (unsigned long)(uint32_t)va_arg(*args, unsigned int);
400 break;
401 case 8:
402 tmp.v_uint64 = va_arg(*args, uint64_t);
403 break;
404 default:
405 BUG();
406 }
407 break;
408 case LTT_TYPE_STRING:
409 tmp.v_string.s = va_arg(*args, const char *);
410 if ((unsigned long)tmp.v_string.s < PAGE_SIZE)
411 tmp.v_string.s = "<NULL>";
412 tmp.v_string.len = strlen(tmp.v_string.s)+1;
413 if (buf)
414 ust_buffers_write(buf, buf_offset, tmp.v_string.s,
415 tmp.v_string.len);
416 buf_offset += tmp.v_string.len;
417 goto copydone;
418 default:
419 BUG();
420 }
421
422 /*
423 * If trace_size is lower or equal to 4 bytes, there is no sign
424 * extension to do because we are already encoded in a long. Therefore,
425 * we can combine signed and unsigned ops. 4 bytes float also works
426 * with this, because we do a simple copy of 4 bytes into 4 bytes
427 * without manipulation (and we do not support conversion from integers
428 * to floats).
429 * It is also the case if c_size is 8 bytes, which is the largest
430 * possible integer.
431 */
432 if (ltt_get_alignment()) {
433 buf_offset += ltt_align(buf_offset, trace_size);
434 if (largest_align)
435 *largest_align = max_t(int, *largest_align, trace_size);
436 }
437 if (trace_size <= 4 || c_size == 8) {
438 if (buf) {
439 switch (trace_size) {
440 case 1:
441 if (c_size == 8)
442 ust_buffers_write(buf, buf_offset,
443 (uint8_t[]){ (uint8_t)tmp.v_uint64 },
444 sizeof(uint8_t));
445 else
446 ust_buffers_write(buf, buf_offset,
447 (uint8_t[]){ (uint8_t)tmp.v_ulong },
448 sizeof(uint8_t));
449 break;
450 case 2:
451 if (c_size == 8)
452 ust_buffers_write(buf, buf_offset,
453 (uint16_t[]){ (uint16_t)tmp.v_uint64 },
454 sizeof(uint16_t));
455 else
456 ust_buffers_write(buf, buf_offset,
457 (uint16_t[]){ (uint16_t)tmp.v_ulong },
458 sizeof(uint16_t));
459 break;
460 case 4:
461 if (c_size == 8)
462 ust_buffers_write(buf, buf_offset,
463 (uint32_t[]){ (uint32_t)tmp.v_uint64 },
464 sizeof(uint32_t));
465 else
466 ust_buffers_write(buf, buf_offset,
467 (uint32_t[]){ (uint32_t)tmp.v_ulong },
468 sizeof(uint32_t));
469 break;
470 case 8:
471 /*
472 * c_size cannot be other than 8 here because
473 * trace_size > 4.
474 */
475 ust_buffers_write(buf, buf_offset,
476 (uint64_t[]){ (uint64_t)tmp.v_uint64 },
477 sizeof(uint64_t));
478 break;
479 default:
480 BUG();
481 }
482 }
483 buf_offset += trace_size;
484 goto copydone;
485 } else {
486 /*
487 * Perform sign extension.
488 */
489 if (buf) {
490 switch (trace_type) {
491 case LTT_TYPE_SIGNED_INT:
492 ust_buffers_write(buf, buf_offset,
493 (int64_t[]){ (int64_t)tmp.v_ulong },
494 sizeof(int64_t));
495 break;
496 case LTT_TYPE_UNSIGNED_INT:
497 ust_buffers_write(buf, buf_offset,
498 (uint64_t[]){ (uint64_t)tmp.v_ulong },
499 sizeof(uint64_t));
500 break;
501 default:
502 BUG();
503 }
504 }
505 buf_offset += trace_size;
506 goto copydone;
507 }
508
509 copydone:
510 return buf_offset;
511 }
512
513 notrace size_t ltt_serialize_data(struct ust_buffer *buf, size_t buf_offset,
514 struct ltt_serialize_closure *closure,
515 void *serialize_private, int *largest_align,
516 const char *fmt, va_list *args)
517 {
518 char trace_size = 0, c_size = 0; /*
519 * 0 (unset), 1, 2, 4, 8 bytes.
520 */
521 enum ltt_type trace_type = LTT_TYPE_NONE, c_type = LTT_TYPE_NONE;
522 unsigned long attributes = 0;
523
524 for (; *fmt ; ++fmt) {
525 switch (*fmt) {
526 case '#':
527 /* tracetypes (#) */
528 ++fmt; /* skip first '#' */
529 if (*fmt == '#') /* Escaped ## */
530 break;
531 attributes = 0;
532 fmt = parse_trace_type(fmt, &trace_size, &trace_type,
533 &attributes);
534 break;
535 case '%':
536 /* c types (%) */
537 ++fmt; /* skip first '%' */
538 if (*fmt == '%') /* Escaped %% */
539 break;
540 fmt = parse_c_type(fmt, &c_size, &c_type, NULL);
541 /*
542 * Output c types if no trace types has been
543 * specified.
544 */
545 if (!trace_size)
546 trace_size = c_size;
547 if (trace_type == LTT_TYPE_NONE)
548 trace_type = c_type;
549 if (c_type == LTT_TYPE_STRING)
550 trace_type = LTT_TYPE_STRING;
551 /* perform trace write */
552 buf_offset = serialize_trace_data(buf,
553 buf_offset, trace_size,
554 trace_type, c_size, c_type,
555 largest_align, args);
556 trace_size = 0;
557 c_size = 0;
558 trace_type = LTT_TYPE_NONE;
559 c_size = LTT_TYPE_NONE;
560 attributes = 0;
561 break;
562 /* default is to skip the text, doing nothing */
563 }
564 }
565 return buf_offset;
566 }
567
568 /*
569 * Calculate data size
570 * Assume that the padding for alignment starts at a sizeof(void *) address.
571 */
572 static notrace size_t ltt_get_data_size(struct ltt_serialize_closure *closure,
573 void *serialize_private, int *largest_align,
574 const char *fmt, va_list *args)
575 {
576 ltt_serialize_cb cb = closure->callbacks[0];
577 closure->cb_idx = 0;
578 return (size_t)cb(NULL, 0, closure, serialize_private,
579 largest_align, fmt, args);
580 }
581
582 static notrace
583 void ltt_write_event_data(struct ust_buffer *buf, size_t buf_offset,
584 struct ltt_serialize_closure *closure,
585 void *serialize_private, int largest_align,
586 const char *fmt, va_list *args)
587 {
588 ltt_serialize_cb cb = closure->callbacks[0];
589 closure->cb_idx = 0;
590 buf_offset += ltt_align(buf_offset, largest_align);
591 cb(buf, buf_offset, closure, serialize_private, NULL, fmt, args);
592 }
593
594
595 notrace void ltt_vtrace(const struct marker *mdata, void *probe_data,
596 struct registers *regs, void *call_data,
597 const char *fmt, va_list *args)
598 {
599 int largest_align, ret;
600 struct ltt_active_marker *pdata;
601 uint16_t eID;
602 size_t data_size, slot_size;
603 unsigned int chan_index;
604 struct ust_channel *channel;
605 struct ust_trace *trace, *dest_trace = NULL;
606 struct ust_buffer *buf;
607 void *transport_data;
608 u64 tsc;
609 long buf_offset;
610 va_list args_copy;
611 struct ltt_serialize_closure closure;
612 struct ltt_probe_private_data *private_data = call_data;
613 void *serialize_private = NULL;
614 int cpu;
615 unsigned int rflags;
616
617 /*
618 * This test is useful for quickly exiting static tracing when no trace
619 * is active. We expect to have an active trace when we get here.
620 */
621 if (unlikely(ltt_traces.num_active_traces == 0))
622 return;
623
624 rcu_read_lock(); //ust// rcu_read_lock_sched_notrace();
625 //ust// cpu = smp_processor_id();
626 cpu = ust_get_cpu();
627 //ust// __get_cpu_var(ltt_nesting)++;
628 /* FIXME: should nesting be per-cpu? */
629 ltt_nesting++;
630
631 pdata = (struct ltt_active_marker *)probe_data;
632 eID = mdata->event_id;
633 chan_index = mdata->channel_id;
634 closure.callbacks = pdata->probe->callbacks;
635
636 if (unlikely(private_data)) {
637 dest_trace = private_data->trace;
638 if (private_data->serializer)
639 closure.callbacks = &private_data->serializer;
640 serialize_private = private_data->serialize_private;
641 }
642
643 va_copy(args_copy, *args);
644 /*
645 * Assumes event payload to start on largest_align alignment.
646 */
647 largest_align = 1; /* must be non-zero for ltt_align */
648 data_size = ltt_get_data_size(&closure, serialize_private,
649 &largest_align, fmt, &args_copy);
650 largest_align = min_t(int, largest_align, sizeof(void *));
651 va_end(args_copy);
652
653 /* Iterate on each trace */
654 list_for_each_entry_rcu(trace, &ltt_traces.head, list) {
655 /*
656 * Expect the filter to filter out events. If we get here,
657 * we went through tracepoint activation as a first step.
658 */
659 if (unlikely(dest_trace && trace != dest_trace))
660 continue;
661 if (unlikely(!trace->active))
662 continue;
663 if (unlikely(!ltt_run_filter(trace, eID)))
664 continue;
665 #ifdef CONFIG_LTT_DEBUG_EVENT_SIZE
666 rflags = LTT_RFLAG_ID_SIZE;
667 #else
668 if (unlikely(eID >= LTT_FREE_EVENTS))
669 rflags = LTT_RFLAG_ID;
670 else
671 rflags = 0;
672 #endif
673 /*
674 * Skip channels added after trace creation.
675 */
676 if (unlikely(chan_index >= trace->nr_channels))
677 continue;
678 channel = &trace->channels[chan_index];
679 if (!channel->active)
680 continue;
681
682 /* If a new cpu was plugged since the trace was started, we did
683 * not add it to the trace, and therefore we write the event to
684 * cpu 0.
685 */
686 if(cpu >= channel->n_cpus) {
687 cpu = 0;
688 }
689
690 /* reserve space : header and data */
691 ret = ltt_reserve_slot(trace, channel, &transport_data,
692 data_size, &slot_size, &buf_offset,
693 &tsc, &rflags,
694 largest_align, cpu);
695 if (unlikely(ret < 0))
696 continue; /* buffer full */
697
698 va_copy(args_copy, *args);
699 /* FIXME : could probably encapsulate transport better. */
700 //ust// buf = ((struct rchan *)channel->trans_channel_data)->buf[cpu];
701 buf = channel->buf[cpu];
702 /* Out-of-order write : header and data */
703 buf_offset = ltt_write_event_header(trace,
704 channel, buf, buf_offset,
705 eID, data_size, tsc, rflags);
706 ltt_write_event_data(buf, buf_offset, &closure,
707 serialize_private,
708 largest_align, fmt, &args_copy);
709 va_end(args_copy);
710 /* Out-of-order commit */
711 ltt_commit_slot(channel, buf, buf_offset, data_size, slot_size);
712 DBG("just commited event at offset %ld and size %zd", buf_offset, slot_size);
713 }
714 //ust// __get_cpu_var(ltt_nesting)--;
715 ltt_nesting--;
716 rcu_read_unlock(); //ust// rcu_read_unlock_sched_notrace();
717 }
718
719 notrace void ltt_trace(const struct marker *mdata, void *probe_data,
720 struct registers *regs, void *call_data,
721 const char *fmt, ...)
722 {
723 va_list args;
724
725 va_start(args, fmt);
726 ltt_vtrace(mdata, probe_data, regs, call_data, fmt, &args);
727 va_end(args);
728 }
729
730 static notrace void skip_space(const char **ps)
731 {
732 while(**ps == ' ')
733 (*ps)++;
734 }
735
736 static notrace void copy_token(char **out, const char **in)
737 {
738 while(**in != ' ' && **in != '\0') {
739 **out = **in;
740 (*out)++;
741 (*in)++;
742 }
743 }
744
745 /* serialize_to_text
746 *
747 * Given a format string and a va_list of arguments, convert them to a
748 * human-readable string.
749 *
750 * @outbuf: the buffer to output the string to
751 * @bufsize: the max size that can be used in outbuf
752 * @fmt: the marker format string
753 * @ap: a va_list that contains the arguments corresponding to fmt
754 *
755 * Return value: the number of chars that have been put in outbuf, excluding
756 * the final \0, or, if the buffer was too small, the number of chars that
757 * would have been written in outbuf if it had been large enough.
758 *
759 * outbuf may be NULL. The return value may then be used be allocate an
760 * appropriate outbuf.
761 *
762 */
763
764 notrace
765 int serialize_to_text(char *outbuf, int bufsize, const char *fmt, va_list ap)
766 {
767 int fmt_len = strlen(fmt);
768 char *new_fmt = alloca(fmt_len + 1);
769 const char *orig_fmt_p = fmt;
770 char *new_fmt_p = new_fmt;
771 char false_buf;
772 int result;
773 enum { none, cfmt, tracefmt, argname } prev_token = none;
774
775 while(*orig_fmt_p != '\0') {
776 if(*orig_fmt_p == '%') {
777 prev_token = cfmt;
778 copy_token(&new_fmt_p, &orig_fmt_p);
779 }
780 else if(*orig_fmt_p == '#') {
781 prev_token = tracefmt;
782 do {
783 orig_fmt_p++;
784 } while(*orig_fmt_p != ' ' && *orig_fmt_p != '\0');
785 }
786 else if(*orig_fmt_p == ' ') {
787 if(prev_token == argname) {
788 *new_fmt_p = '=';
789 new_fmt_p++;
790 }
791 else if(prev_token == cfmt) {
792 *new_fmt_p = ' ';
793 new_fmt_p++;
794 }
795
796 skip_space(&orig_fmt_p);
797 }
798 else {
799 prev_token = argname;
800 copy_token(&new_fmt_p, &orig_fmt_p);
801 }
802 }
803
804 *new_fmt_p = '\0';
805
806 if(outbuf == NULL) {
807 /* use this false_buffer for compatibility with pre-C99 */
808 outbuf = &false_buf;
809 bufsize = 1;
810 }
811 result = vsnprintf(outbuf, bufsize, new_fmt, ap);
812
813 return result;
814 }
This page took 0.044263 seconds and 5 git commands to generate.