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