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