4 * Copyright (C) 1991, 1992 Linus Torvalds
7 /* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
9 * Wirzenius wrote this portably, Torvalds fucked it up :-)
13 * Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
14 * - changed to provide snprintf and vsnprintf functions
15 * So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
16 * - scnprintf and vscnprintf
20 #include <linux/clk.h>
21 #include <linux/clk-provider.h>
22 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
23 #include <linux/types.h>
24 #include <linux/string.h>
25 #include <linux/ctype.h>
26 #include <linux/kernel.h>
27 #include <linux/kallsyms.h>
28 #include <linux/math64.h>
29 #include <linux/uaccess.h>
30 #include <linux/ioport.h>
31 #include <linux/dcache.h>
32 #include <linux/cred.h>
33 #include <net/addrconf.h>
35 #include <linux/blkdev.h>
38 #include <asm/page.h> /* for PAGE_SIZE */
39 #include <asm/sections.h> /* for dereference_function_descriptor() */
40 #include <asm/byteorder.h> /* cpu_to_le16 */
42 #include <linux/string_helpers.h>
46 * simple_strtoull - convert a string to an unsigned long long
47 * @cp: The start of the string
48 * @endp: A pointer to the end of the parsed string will be placed here
49 * @base: The number base to use
51 * This function is obsolete. Please use kstrtoull instead.
53 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
55 unsigned long long result
;
58 cp
= _parse_integer_fixup_radix(cp
, &base
);
59 rv
= _parse_integer(cp
, base
, &result
);
61 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
68 EXPORT_SYMBOL(simple_strtoull
);
71 * simple_strtoul - convert a string to an unsigned long
72 * @cp: The start of the string
73 * @endp: A pointer to the end of the parsed string will be placed here
74 * @base: The number base to use
76 * This function is obsolete. Please use kstrtoul instead.
78 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
80 return simple_strtoull(cp
, endp
, base
);
82 EXPORT_SYMBOL(simple_strtoul
);
85 * simple_strtol - convert a string to a signed long
86 * @cp: The start of the string
87 * @endp: A pointer to the end of the parsed string will be placed here
88 * @base: The number base to use
90 * This function is obsolete. Please use kstrtol instead.
92 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
95 return -simple_strtoul(cp
+ 1, endp
, base
);
97 return simple_strtoul(cp
, endp
, base
);
99 EXPORT_SYMBOL(simple_strtol
);
102 * simple_strtoll - convert a string to a signed long long
103 * @cp: The start of the string
104 * @endp: A pointer to the end of the parsed string will be placed here
105 * @base: The number base to use
107 * This function is obsolete. Please use kstrtoll instead.
109 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
112 return -simple_strtoull(cp
+ 1, endp
, base
);
114 return simple_strtoull(cp
, endp
, base
);
116 EXPORT_SYMBOL(simple_strtoll
);
118 static noinline_for_stack
119 int skip_atoi(const char **s
)
124 i
= i
*10 + *((*s
)++) - '0';
125 } while (isdigit(**s
));
131 * Decimal conversion is by far the most typical, and is used for
132 * /proc and /sys data. This directly impacts e.g. top performance
133 * with many processes running. We optimize it for speed by emitting
134 * two characters at a time, using a 200 byte lookup table. This
135 * roughly halves the number of multiplications compared to computing
136 * the digits one at a time. Implementation strongly inspired by the
137 * previous version, which in turn used ideas described at
138 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
139 * from the author, Douglas W. Jones).
141 * It turns out there is precisely one 26 bit fixed-point
142 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
143 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
144 * range happens to be somewhat larger (x <= 1073741898), but that's
145 * irrelevant for our purpose.
147 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
148 * need a 32x32->64 bit multiply, so we simply use the same constant.
150 * For dividing a number in the range [100, 10^4-1] by 100, there are
151 * several options. The simplest is (x * 0x147b) >> 19, which is valid
152 * for all x <= 43698.
155 static const u16 decpair
[100] = {
156 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
157 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
158 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
159 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
160 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
161 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
162 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
163 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
164 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
165 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
166 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
171 * This will print a single '0' even if r == 0, since we would
172 * immediately jump to out_r where two 0s would be written but only
173 * one of them accounted for in buf. This is needed by ip4_string
174 * below. All other callers pass a non-zero value of r.
176 static noinline_for_stack
177 char *put_dec_trunc8(char *buf
, unsigned r
)
185 /* 100 <= r < 10^8 */
186 q
= (r
* (u64
)0x28f5c29) >> 32;
187 *((u16
*)buf
) = decpair
[r
- 100*q
];
194 /* 100 <= q < 10^6 */
195 r
= (q
* (u64
)0x28f5c29) >> 32;
196 *((u16
*)buf
) = decpair
[q
- 100*r
];
203 /* 100 <= r < 10^4 */
204 q
= (r
* 0x147b) >> 19;
205 *((u16
*)buf
) = decpair
[r
- 100*q
];
212 *((u16
*)buf
) = decpair
[r
];
213 buf
+= r
< 10 ? 1 : 2;
217 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
218 static noinline_for_stack
219 char *put_dec_full8(char *buf
, unsigned r
)
224 q
= (r
* (u64
)0x28f5c29) >> 32;
225 *((u16
*)buf
) = decpair
[r
- 100*q
];
229 r
= (q
* (u64
)0x28f5c29) >> 32;
230 *((u16
*)buf
) = decpair
[q
- 100*r
];
234 q
= (r
* 0x147b) >> 19;
235 *((u16
*)buf
) = decpair
[r
- 100*q
];
239 *((u16
*)buf
) = decpair
[q
];
244 static noinline_for_stack
245 char *put_dec(char *buf
, unsigned long long n
)
247 if (n
>= 100*1000*1000)
248 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
249 /* 1 <= n <= 1.6e11 */
250 if (n
>= 100*1000*1000)
251 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
253 return put_dec_trunc8(buf
, n
);
256 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
259 put_dec_full4(char *buf
, unsigned r
)
264 q
= (r
* 0x147b) >> 19;
265 *((u16
*)buf
) = decpair
[r
- 100*q
];
268 *((u16
*)buf
) = decpair
[q
];
272 * Call put_dec_full4 on x % 10000, return x / 10000.
273 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
274 * holds for all x < 1,128,869,999. The largest value this
275 * helper will ever be asked to convert is 1,125,520,955.
276 * (second call in the put_dec code, assuming n is all-ones).
278 static noinline_for_stack
279 unsigned put_dec_helper4(char *buf
, unsigned x
)
281 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
283 put_dec_full4(buf
, x
- q
* 10000);
287 /* Based on code by Douglas W. Jones found at
288 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
289 * (with permission from the author).
290 * Performs no 64-bit division and hence should be fast on 32-bit machines.
293 char *put_dec(char *buf
, unsigned long long n
)
295 uint32_t d3
, d2
, d1
, q
, h
;
297 if (n
< 100*1000*1000)
298 return put_dec_trunc8(buf
, n
);
300 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
303 d3
= (h
>> 16); /* implicit "& 0xffff" */
305 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
306 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
307 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
308 q
= put_dec_helper4(buf
, q
);
310 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
311 q
= put_dec_helper4(buf
+4, q
);
313 q
+= 4749 * d3
+ 42 * d2
;
314 q
= put_dec_helper4(buf
+8, q
);
319 buf
= put_dec_trunc8(buf
, q
);
320 else while (buf
[-1] == '0')
329 * Convert passed number to decimal string.
330 * Returns the length of string. On buffer overflow, returns 0.
332 * If speed is not important, use snprintf(). It's easy to read the code.
334 int num_to_str(char *buf
, int size
, unsigned long long num
)
336 /* put_dec requires 2-byte alignment of the buffer. */
337 char tmp
[sizeof(num
) * 3] __aligned(2);
340 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
345 len
= put_dec(tmp
, num
) - tmp
;
350 for (idx
= 0; idx
< len
; ++idx
)
351 buf
[idx
] = tmp
[len
- idx
- 1];
355 #define SIGN 1 /* unsigned/signed, must be 1 */
356 #define LEFT 2 /* left justified */
357 #define PLUS 4 /* show plus */
358 #define SPACE 8 /* space if plus */
359 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
360 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
361 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
364 FORMAT_TYPE_NONE
, /* Just a string part */
366 FORMAT_TYPE_PRECISION
,
370 FORMAT_TYPE_PERCENT_CHAR
,
372 FORMAT_TYPE_LONG_LONG
,
386 unsigned int type
:8; /* format_type enum */
387 signed int field_width
:24; /* width of output field */
388 unsigned int flags
:8; /* flags to number() */
389 unsigned int base
:8; /* number base, 8, 10 or 16 only */
390 signed int precision
:16; /* # of digits/chars */
392 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
393 #define PRECISION_MAX ((1 << 15) - 1)
395 static noinline_for_stack
396 char *number(char *buf
, char *end
, unsigned long long num
,
397 struct printf_spec spec
)
399 /* put_dec requires 2-byte alignment of the buffer. */
400 char tmp
[3 * sizeof(num
)] __aligned(2);
403 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
405 bool is_zero
= num
== 0LL;
406 int field_width
= spec
.field_width
;
407 int precision
= spec
.precision
;
409 BUILD_BUG_ON(sizeof(struct printf_spec
) != 8);
411 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
412 * produces same digits or (maybe lowercased) letters */
413 locase
= (spec
.flags
& SMALL
);
414 if (spec
.flags
& LEFT
)
415 spec
.flags
&= ~ZEROPAD
;
417 if (spec
.flags
& SIGN
) {
418 if ((signed long long)num
< 0) {
420 num
= -(signed long long)num
;
422 } else if (spec
.flags
& PLUS
) {
425 } else if (spec
.flags
& SPACE
) {
437 /* generate full string in tmp[], in reverse order */
440 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
441 else if (spec
.base
!= 10) { /* 8 or 16 */
442 int mask
= spec
.base
- 1;
448 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
451 } else { /* base 10 */
452 i
= put_dec(tmp
, num
) - tmp
;
455 /* printing 100 using %2d gives "100", not "00" */
458 /* leading space padding */
459 field_width
-= precision
;
460 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
461 while (--field_width
>= 0) {
473 /* "0x" / "0" prefix */
475 if (spec
.base
== 16 || !is_zero
) {
480 if (spec
.base
== 16) {
482 *buf
= ('X' | locase
);
486 /* zero or space padding */
487 if (!(spec
.flags
& LEFT
)) {
488 char c
= ' ' + (spec
.flags
& ZEROPAD
);
489 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
490 while (--field_width
>= 0) {
496 /* hmm even more zero padding? */
497 while (i
<= --precision
) {
502 /* actual digits of result */
508 /* trailing space padding */
509 while (--field_width
>= 0) {
518 static noinline_for_stack
519 char *special_hex_number(char *buf
, char *end
, unsigned long long num
, int size
)
521 struct printf_spec spec
;
523 spec
.type
= FORMAT_TYPE_PTR
;
524 spec
.field_width
= 2 + 2 * size
; /* 0x + hex */
525 spec
.flags
= SPECIAL
| SMALL
| ZEROPAD
;
529 return number(buf
, end
, num
, spec
);
532 static void move_right(char *buf
, char *end
, unsigned len
, unsigned spaces
)
535 if (buf
>= end
) /* nowhere to put anything */
538 if (size
<= spaces
) {
539 memset(buf
, ' ', size
);
543 if (len
> size
- spaces
)
545 memmove(buf
+ spaces
, buf
, len
);
547 memset(buf
, ' ', spaces
);
551 * Handle field width padding for a string.
552 * @buf: current buffer position
553 * @n: length of string
554 * @end: end of output buffer
555 * @spec: for field width and flags
556 * Returns: new buffer position after padding.
558 static noinline_for_stack
559 char *widen_string(char *buf
, int n
, char *end
, struct printf_spec spec
)
563 if (likely(n
>= spec
.field_width
))
565 /* we want to pad the sucker */
566 spaces
= spec
.field_width
- n
;
567 if (!(spec
.flags
& LEFT
)) {
568 move_right(buf
- n
, end
, n
, spaces
);
579 static noinline_for_stack
580 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
583 size_t lim
= spec
.precision
;
585 if ((unsigned long)s
< PAGE_SIZE
)
597 return widen_string(buf
, len
, end
, spec
);
600 static noinline_for_stack
601 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
604 const char *array
[4], *s
;
605 const struct dentry
*p
;
610 case '2': case '3': case '4':
611 depth
= fmt
[1] - '0';
618 for (i
= 0; i
< depth
; i
++, d
= p
) {
619 p
= ACCESS_ONCE(d
->d_parent
);
620 array
[i
] = ACCESS_ONCE(d
->d_name
.name
);
629 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
641 return widen_string(buf
, n
, end
, spec
);
645 static noinline_for_stack
646 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
647 struct printf_spec spec
, const char *fmt
)
649 struct gendisk
*hd
= bdev
->bd_disk
;
651 buf
= string(buf
, end
, hd
->disk_name
, spec
);
652 if (bdev
->bd_part
->partno
) {
653 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
658 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
664 static noinline_for_stack
665 char *symbol_string(char *buf
, char *end
, void *ptr
,
666 struct printf_spec spec
, const char *fmt
)
669 #ifdef CONFIG_KALLSYMS
670 char sym
[KSYM_SYMBOL_LEN
];
674 ptr
= __builtin_extract_return_addr(ptr
);
675 value
= (unsigned long)ptr
;
677 #ifdef CONFIG_KALLSYMS
679 sprint_backtrace(sym
, value
);
680 else if (*fmt
!= 'f' && *fmt
!= 's')
681 sprint_symbol(sym
, value
);
683 sprint_symbol_no_offset(sym
, value
);
685 return string(buf
, end
, sym
, spec
);
687 return special_hex_number(buf
, end
, value
, sizeof(void *));
691 static noinline_for_stack
692 char *resource_string(char *buf
, char *end
, struct resource
*res
,
693 struct printf_spec spec
, const char *fmt
)
695 #ifndef IO_RSRC_PRINTK_SIZE
696 #define IO_RSRC_PRINTK_SIZE 6
699 #ifndef MEM_RSRC_PRINTK_SIZE
700 #define MEM_RSRC_PRINTK_SIZE 10
702 static const struct printf_spec io_spec
= {
704 .field_width
= IO_RSRC_PRINTK_SIZE
,
706 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
708 static const struct printf_spec mem_spec
= {
710 .field_width
= MEM_RSRC_PRINTK_SIZE
,
712 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
714 static const struct printf_spec bus_spec
= {
718 .flags
= SMALL
| ZEROPAD
,
720 static const struct printf_spec dec_spec
= {
725 static const struct printf_spec str_spec
= {
730 static const struct printf_spec flag_spec
= {
733 .flags
= SPECIAL
| SMALL
,
736 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
737 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
738 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
739 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
740 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
741 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
742 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
743 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
745 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
746 int decode
= (fmt
[0] == 'R') ? 1 : 0;
747 const struct printf_spec
*specp
;
750 if (res
->flags
& IORESOURCE_IO
) {
751 p
= string(p
, pend
, "io ", str_spec
);
753 } else if (res
->flags
& IORESOURCE_MEM
) {
754 p
= string(p
, pend
, "mem ", str_spec
);
756 } else if (res
->flags
& IORESOURCE_IRQ
) {
757 p
= string(p
, pend
, "irq ", str_spec
);
759 } else if (res
->flags
& IORESOURCE_DMA
) {
760 p
= string(p
, pend
, "dma ", str_spec
);
762 } else if (res
->flags
& IORESOURCE_BUS
) {
763 p
= string(p
, pend
, "bus ", str_spec
);
766 p
= string(p
, pend
, "??? ", str_spec
);
770 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
771 p
= string(p
, pend
, "size ", str_spec
);
772 p
= number(p
, pend
, resource_size(res
), *specp
);
774 p
= number(p
, pend
, res
->start
, *specp
);
775 if (res
->start
!= res
->end
) {
777 p
= number(p
, pend
, res
->end
, *specp
);
781 if (res
->flags
& IORESOURCE_MEM_64
)
782 p
= string(p
, pend
, " 64bit", str_spec
);
783 if (res
->flags
& IORESOURCE_PREFETCH
)
784 p
= string(p
, pend
, " pref", str_spec
);
785 if (res
->flags
& IORESOURCE_WINDOW
)
786 p
= string(p
, pend
, " window", str_spec
);
787 if (res
->flags
& IORESOURCE_DISABLED
)
788 p
= string(p
, pend
, " disabled", str_spec
);
790 p
= string(p
, pend
, " flags ", str_spec
);
791 p
= number(p
, pend
, res
->flags
, flag_spec
);
796 return string(buf
, end
, sym
, spec
);
799 static noinline_for_stack
800 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
803 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
804 negative value, fallback to the default */
807 if (spec
.field_width
== 0)
808 /* nothing to print */
811 if (ZERO_OR_NULL_PTR(addr
))
813 return string(buf
, end
, NULL
, spec
);
830 if (spec
.field_width
> 0)
831 len
= min_t(int, spec
.field_width
, 64);
833 for (i
= 0; i
< len
; ++i
) {
835 *buf
= hex_asc_hi(addr
[i
]);
838 *buf
= hex_asc_lo(addr
[i
]);
841 if (separator
&& i
!= len
- 1) {
851 static noinline_for_stack
852 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
853 struct printf_spec spec
, const char *fmt
)
855 const int CHUNKSZ
= 32;
856 int nr_bits
= max_t(int, spec
.field_width
, 0);
860 /* reused to print numbers */
861 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
863 chunksz
= nr_bits
& (CHUNKSZ
- 1);
867 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
868 for (; i
>= 0; i
-= CHUNKSZ
) {
872 chunkmask
= ((1ULL << chunksz
) - 1);
873 word
= i
/ BITS_PER_LONG
;
874 bit
= i
% BITS_PER_LONG
;
875 val
= (bitmap
[word
] >> bit
) & chunkmask
;
884 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
885 buf
= number(buf
, end
, val
, spec
);
892 static noinline_for_stack
893 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
894 struct printf_spec spec
, const char *fmt
)
896 int nr_bits
= max_t(int, spec
.field_width
, 0);
897 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
901 /* reused to print numbers */
902 spec
= (struct printf_spec
){ .base
= 10 };
904 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
905 while (cur
< nr_bits
) {
907 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
908 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
918 buf
= number(buf
, end
, rbot
, spec
);
924 buf
= number(buf
, end
, rtop
, spec
);
932 static noinline_for_stack
933 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
934 struct printf_spec spec
, const char *fmt
)
936 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
940 bool reversed
= false;
956 for (i
= 0; i
< 6; i
++) {
958 p
= hex_byte_pack(p
, addr
[5 - i
]);
960 p
= hex_byte_pack(p
, addr
[i
]);
962 if (fmt
[0] == 'M' && i
!= 5)
967 return string(buf
, end
, mac_addr
, spec
);
970 static noinline_for_stack
971 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
974 bool leading_zeros
= (fmt
[0] == 'i');
999 for (i
= 0; i
< 4; i
++) {
1000 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1001 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1002 if (leading_zeros
) {
1008 /* reverse the digits in the quad */
1010 *p
++ = temp
[digits
];
1020 static noinline_for_stack
1021 char *ip6_compressed_string(char *p
, const char *addr
)
1024 unsigned char zerolength
[8];
1029 bool needcolon
= false;
1031 struct in6_addr in6
;
1033 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1035 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1037 memset(zerolength
, 0, sizeof(zerolength
));
1044 /* find position of longest 0 run */
1045 for (i
= 0; i
< range
; i
++) {
1046 for (j
= i
; j
< range
; j
++) {
1047 if (in6
.s6_addr16
[j
] != 0)
1052 for (i
= 0; i
< range
; i
++) {
1053 if (zerolength
[i
] > longest
) {
1054 longest
= zerolength
[i
];
1058 if (longest
== 1) /* don't compress a single 0 */
1062 for (i
= 0; i
< range
; i
++) {
1063 if (i
== colonpos
) {
1064 if (needcolon
|| i
== 0)
1075 /* hex u16 without leading 0s */
1076 word
= ntohs(in6
.s6_addr16
[i
]);
1081 p
= hex_byte_pack(p
, hi
);
1083 *p
++ = hex_asc_lo(hi
);
1084 p
= hex_byte_pack(p
, lo
);
1087 p
= hex_byte_pack(p
, lo
);
1089 *p
++ = hex_asc_lo(lo
);
1096 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1103 static noinline_for_stack
1104 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1108 for (i
= 0; i
< 8; i
++) {
1109 p
= hex_byte_pack(p
, *addr
++);
1110 p
= hex_byte_pack(p
, *addr
++);
1111 if (fmt
[0] == 'I' && i
!= 7)
1119 static noinline_for_stack
1120 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1121 struct printf_spec spec
, const char *fmt
)
1123 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1125 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1126 ip6_compressed_string(ip6_addr
, addr
);
1128 ip6_string(ip6_addr
, addr
, fmt
);
1130 return string(buf
, end
, ip6_addr
, spec
);
1133 static noinline_for_stack
1134 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1135 struct printf_spec spec
, const char *fmt
)
1137 char ip4_addr
[sizeof("255.255.255.255")];
1139 ip4_string(ip4_addr
, addr
, fmt
);
1141 return string(buf
, end
, ip4_addr
, spec
);
1144 static noinline_for_stack
1145 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1146 struct printf_spec spec
, const char *fmt
)
1148 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1149 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1150 sizeof(":12345") + sizeof("/123456789") +
1151 sizeof("%1234567890")];
1152 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1153 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1154 char fmt6
[2] = { fmt
[0], '6' };
1158 while (isalpha(*++fmt
)) {
1175 if (have_p
|| have_s
|| have_f
) {
1180 if (fmt6
[0] == 'I' && have_c
)
1181 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1183 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1185 if (have_p
|| have_s
|| have_f
)
1190 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1194 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1195 IPV6_FLOWINFO_MASK
), spec
);
1199 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1203 return string(buf
, end
, ip6_addr
, spec
);
1206 static noinline_for_stack
1207 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1208 struct printf_spec spec
, const char *fmt
)
1210 bool have_p
= false;
1211 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1212 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1213 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1214 char fmt4
[3] = { fmt
[0], '4', 0 };
1217 while (isalpha(*++fmt
)) {
1231 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1234 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1238 return string(buf
, end
, ip4_addr
, spec
);
1241 static noinline_for_stack
1242 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1247 unsigned int flags
= 0;
1250 if (spec
.field_width
== 0)
1251 return buf
; /* nothing to print */
1253 if (ZERO_OR_NULL_PTR(addr
))
1254 return string(buf
, end
, NULL
, spec
); /* NULL pointer */
1258 switch (fmt
[count
++]) {
1260 flags
|= ESCAPE_ANY
;
1263 flags
|= ESCAPE_SPECIAL
;
1266 flags
|= ESCAPE_HEX
;
1269 flags
|= ESCAPE_NULL
;
1272 flags
|= ESCAPE_OCTAL
;
1278 flags
|= ESCAPE_SPACE
;
1287 flags
= ESCAPE_ANY_NP
;
1289 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1292 * string_escape_mem() writes as many characters as it can to
1293 * the given buffer, and returns the total size of the output
1294 * had the buffer been big enough.
1296 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1301 static noinline_for_stack
1302 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1303 struct printf_spec spec
, const char *fmt
)
1305 char uuid
[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1308 static const u8 be
[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1309 static const u8 le
[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1310 const u8
*index
= be
;
1315 uc
= true; /* fall-through */
1324 for (i
= 0; i
< 16; i
++) {
1325 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1345 return string(buf
, end
, uuid
, spec
);
1348 static noinline_for_stack
1349 char *netdev_bits(char *buf
, char *end
, const void *addr
, const char *fmt
)
1351 unsigned long long num
;
1356 num
= *(const netdev_features_t
*)addr
;
1357 size
= sizeof(netdev_features_t
);
1360 num
= (unsigned long)addr
;
1361 size
= sizeof(unsigned long);
1365 return special_hex_number(buf
, end
, num
, size
);
1368 static noinline_for_stack
1369 char *address_val(char *buf
, char *end
, const void *addr
, const char *fmt
)
1371 unsigned long long num
;
1376 num
= *(const dma_addr_t
*)addr
;
1377 size
= sizeof(dma_addr_t
);
1381 num
= *(const phys_addr_t
*)addr
;
1382 size
= sizeof(phys_addr_t
);
1386 return special_hex_number(buf
, end
, num
, size
);
1389 static noinline_for_stack
1390 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1393 if (!IS_ENABLED(CONFIG_HAVE_CLK
) || !clk
)
1394 return string(buf
, end
, NULL
, spec
);
1398 return number(buf
, end
, clk_get_rate(clk
), spec
);
1402 #ifdef CONFIG_COMMON_CLK
1403 return string(buf
, end
, __clk_get_name(clk
), spec
);
1405 return special_hex_number(buf
, end
, (unsigned long)clk
, sizeof(unsigned long));
1410 int kptr_restrict __read_mostly
;
1413 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1414 * by an extra set of alphanumeric characters that are extended format
1417 * Right now we handle:
1419 * - 'F' For symbolic function descriptor pointers with offset
1420 * - 'f' For simple symbolic function names without offset
1421 * - 'S' For symbolic direct pointers with offset
1422 * - 's' For symbolic direct pointers without offset
1423 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1424 * - 'B' For backtraced symbolic direct pointers with offset
1425 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1426 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1427 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1428 * width which must be explicitly specified either as part of the
1429 * format string '%32b[l]' or through '%*b[l]', [l] selects
1430 * range-list format instead of hex format
1431 * - 'M' For a 6-byte MAC address, it prints the address in the
1432 * usual colon-separated hex notation
1433 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1434 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1435 * with a dash-separated hex notation
1436 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1437 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1438 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1439 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1441 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1442 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1443 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1444 * IPv6 omits the colons (01020304...0f)
1445 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1447 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1448 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1449 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1450 * - 'I[6S]c' for IPv6 addresses printed as specified by
1451 * http://tools.ietf.org/html/rfc5952
1452 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1453 * of the following flags (see string_escape_mem() for the
1456 * c - ESCAPE_SPECIAL
1462 * By default ESCAPE_ANY_NP is used.
1463 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1464 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1465 * Options for %pU are:
1466 * b big endian lower case hex (default)
1467 * B big endian UPPER case hex
1468 * l little endian lower case hex
1469 * L little endian UPPER case hex
1470 * big endian output byte order is:
1471 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1472 * little endian output byte order is:
1473 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1474 * - 'V' For a struct va_format which contains a format string * and va_list *,
1475 * call vsnprintf(->format, *->va_list).
1476 * Implements a "recursive vsnprintf".
1477 * Do not use this feature without some mechanism to verify the
1478 * correctness of the format string and va_list arguments.
1479 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1480 * - 'NF' For a netdev_features_t
1481 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1482 * a certain separator (' ' by default):
1486 * The maximum supported length is 64 bytes of the input. Consider
1487 * to use print_hex_dump() for the larger input.
1488 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1489 * (default assumed to be phys_addr_t, passed by reference)
1490 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1491 * - 'D[234]' Same as 'd' but for a struct file
1492 * - 'g' For block_device name (gendisk + partition number)
1493 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1494 * (legacy clock framework) of the clock
1495 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1496 * (legacy clock framework) of the clock
1497 * - 'Cr' For a clock, it prints the current rate of the clock
1499 * ** Please update also Documentation/printk-formats.txt when making changes **
1501 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1502 * function pointers are really function descriptors, which contain a
1503 * pointer to the real address.
1505 static noinline_for_stack
1506 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1507 struct printf_spec spec
)
1509 const int default_width
= 2 * sizeof(void *);
1511 if (!ptr
&& *fmt
!= 'K') {
1513 * Print (null) with the same width as a pointer so it makes
1514 * tabular output look nice.
1516 if (spec
.field_width
== -1)
1517 spec
.field_width
= default_width
;
1518 return string(buf
, end
, "(null)", spec
);
1524 ptr
= dereference_function_descriptor(ptr
);
1529 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1532 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1534 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1538 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
1540 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
1542 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1543 case 'm': /* Contiguous: 000102030405 */
1545 /* [mM]R (Reverse order; Bluetooth) */
1546 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1547 case 'I': /* Formatted IP supported
1549 * 6: 0001:0203:...:0708
1550 * 6c: 1::708 or 1::1.2.3.4
1552 case 'i': /* Contiguous:
1553 * 4: 001.002.003.004
1558 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1560 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1563 struct sockaddr raw
;
1564 struct sockaddr_in v4
;
1565 struct sockaddr_in6 v6
;
1568 switch (sa
->raw
.sa_family
) {
1570 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1572 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1574 return string(buf
, end
, "(invalid address)", spec
);
1579 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
1581 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1586 va_copy(va
, *((struct va_format
*)ptr
)->va
);
1587 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
1588 ((struct va_format
*)ptr
)->fmt
, va
);
1594 * %pK cannot be used in IRQ context because its test
1595 * for CAP_SYSLOG would be meaningless.
1597 if (kptr_restrict
&& (in_irq() || in_serving_softirq() ||
1599 if (spec
.field_width
== -1)
1600 spec
.field_width
= default_width
;
1601 return string(buf
, end
, "pK-error", spec
);
1604 switch (kptr_restrict
) {
1606 /* Always print %pK values */
1610 * Only print the real pointer value if the current
1611 * process has CAP_SYSLOG and is running with the
1612 * same credentials it started with. This is because
1613 * access to files is checked at open() time, but %pK
1614 * checks permission at read() time. We don't want to
1615 * leak pointer values if a binary opens a file using
1616 * %pK and then elevates privileges before reading it.
1618 const struct cred
*cred
= current_cred();
1620 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
1621 !uid_eq(cred
->euid
, cred
->uid
) ||
1622 !gid_eq(cred
->egid
, cred
->gid
))
1628 /* Always print 0's for %pK */
1635 return netdev_bits(buf
, end
, ptr
, fmt
);
1637 return address_val(buf
, end
, ptr
, fmt
);
1639 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
1641 return clock(buf
, end
, ptr
, spec
, fmt
);
1643 return dentry_name(buf
, end
,
1644 ((const struct file
*)ptr
)->f_path
.dentry
,
1648 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
1652 spec
.flags
|= SMALL
;
1653 if (spec
.field_width
== -1) {
1654 spec
.field_width
= default_width
;
1655 spec
.flags
|= ZEROPAD
;
1659 return number(buf
, end
, (unsigned long) ptr
, spec
);
1663 * Helper function to decode printf style format.
1664 * Each call decode a token from the format and return the
1665 * number of characters read (or likely the delta where it wants
1666 * to go on the next call).
1667 * The decoded token is returned through the parameters
1669 * 'h', 'l', or 'L' for integer fields
1670 * 'z' support added 23/7/1999 S.H.
1671 * 'z' changed to 'Z' --davidm 1/25/99
1672 * 't' added for ptrdiff_t
1674 * @fmt: the format string
1675 * @type of the token returned
1676 * @flags: various flags such as +, -, # tokens..
1677 * @field_width: overwritten width
1678 * @base: base of the number (octal, hex, ...)
1679 * @precision: precision of a number
1680 * @qualifier: qualifier of a number (long, size_t, ...)
1682 static noinline_for_stack
1683 int format_decode(const char *fmt
, struct printf_spec
*spec
)
1685 const char *start
= fmt
;
1688 /* we finished early by reading the field width */
1689 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
1690 if (spec
->field_width
< 0) {
1691 spec
->field_width
= -spec
->field_width
;
1692 spec
->flags
|= LEFT
;
1694 spec
->type
= FORMAT_TYPE_NONE
;
1698 /* we finished early by reading the precision */
1699 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
1700 if (spec
->precision
< 0)
1701 spec
->precision
= 0;
1703 spec
->type
= FORMAT_TYPE_NONE
;
1708 spec
->type
= FORMAT_TYPE_NONE
;
1710 for (; *fmt
; ++fmt
) {
1715 /* Return the current non-format string */
1716 if (fmt
!= start
|| !*fmt
)
1722 while (1) { /* this also skips first '%' */
1728 case '-': spec
->flags
|= LEFT
; break;
1729 case '+': spec
->flags
|= PLUS
; break;
1730 case ' ': spec
->flags
|= SPACE
; break;
1731 case '#': spec
->flags
|= SPECIAL
; break;
1732 case '0': spec
->flags
|= ZEROPAD
; break;
1733 default: found
= false;
1740 /* get field width */
1741 spec
->field_width
= -1;
1744 spec
->field_width
= skip_atoi(&fmt
);
1745 else if (*fmt
== '*') {
1746 /* it's the next argument */
1747 spec
->type
= FORMAT_TYPE_WIDTH
;
1748 return ++fmt
- start
;
1752 /* get the precision */
1753 spec
->precision
= -1;
1756 if (isdigit(*fmt
)) {
1757 spec
->precision
= skip_atoi(&fmt
);
1758 if (spec
->precision
< 0)
1759 spec
->precision
= 0;
1760 } else if (*fmt
== '*') {
1761 /* it's the next argument */
1762 spec
->type
= FORMAT_TYPE_PRECISION
;
1763 return ++fmt
- start
;
1768 /* get the conversion qualifier */
1770 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
1771 _tolower(*fmt
) == 'z' || *fmt
== 't') {
1773 if (unlikely(qualifier
== *fmt
)) {
1774 if (qualifier
== 'l') {
1777 } else if (qualifier
== 'h') {
1788 spec
->type
= FORMAT_TYPE_CHAR
;
1789 return ++fmt
- start
;
1792 spec
->type
= FORMAT_TYPE_STR
;
1793 return ++fmt
- start
;
1796 spec
->type
= FORMAT_TYPE_PTR
;
1797 return ++fmt
- start
;
1800 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
1801 return ++fmt
- start
;
1803 /* integer number formats - set up the flags and "break" */
1809 spec
->flags
|= SMALL
;
1817 spec
->flags
|= SIGN
;
1823 * Since %n poses a greater security risk than
1824 * utility, treat it as any other invalid or
1825 * unsupported format specifier.
1830 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
1831 spec
->type
= FORMAT_TYPE_INVALID
;
1835 if (qualifier
== 'L')
1836 spec
->type
= FORMAT_TYPE_LONG_LONG
;
1837 else if (qualifier
== 'l') {
1838 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
1839 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
1840 } else if (_tolower(qualifier
) == 'z') {
1841 spec
->type
= FORMAT_TYPE_SIZE_T
;
1842 } else if (qualifier
== 't') {
1843 spec
->type
= FORMAT_TYPE_PTRDIFF
;
1844 } else if (qualifier
== 'H') {
1845 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
1846 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
1847 } else if (qualifier
== 'h') {
1848 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
1849 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
1851 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
1852 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
1855 return ++fmt
- start
;
1859 set_field_width(struct printf_spec
*spec
, int width
)
1861 spec
->field_width
= width
;
1862 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
1863 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
1868 set_precision(struct printf_spec
*spec
, int prec
)
1870 spec
->precision
= prec
;
1871 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
1872 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
1877 * vsnprintf - Format a string and place it in a buffer
1878 * @buf: The buffer to place the result into
1879 * @size: The size of the buffer, including the trailing null space
1880 * @fmt: The format string to use
1881 * @args: Arguments for the format string
1883 * This function generally follows C99 vsnprintf, but has some
1884 * extensions and a few limitations:
1887 * %p* is handled by pointer()
1889 * See pointer() or Documentation/printk-formats.txt for more
1890 * extensive description.
1892 * ** Please update the documentation in both places when making changes **
1894 * The return value is the number of characters which would
1895 * be generated for the given input, excluding the trailing
1896 * '\0', as per ISO C99. If you want to have the exact
1897 * number of characters written into @buf as return value
1898 * (not including the trailing '\0'), use vscnprintf(). If the
1899 * return is greater than or equal to @size, the resulting
1900 * string is truncated.
1902 * If you're not already dealing with a va_list consider using snprintf().
1904 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1906 unsigned long long num
;
1908 struct printf_spec spec
= {0};
1910 /* Reject out-of-range values early. Large positive sizes are
1911 used for unknown buffer sizes. */
1912 if (WARN_ON_ONCE(size
> INT_MAX
))
1918 /* Make sure end is always >= buf */
1925 const char *old_fmt
= fmt
;
1926 int read
= format_decode(fmt
, &spec
);
1930 switch (spec
.type
) {
1931 case FORMAT_TYPE_NONE
: {
1934 if (copy
> end
- str
)
1936 memcpy(str
, old_fmt
, copy
);
1942 case FORMAT_TYPE_WIDTH
:
1943 set_field_width(&spec
, va_arg(args
, int));
1946 case FORMAT_TYPE_PRECISION
:
1947 set_precision(&spec
, va_arg(args
, int));
1950 case FORMAT_TYPE_CHAR
: {
1953 if (!(spec
.flags
& LEFT
)) {
1954 while (--spec
.field_width
> 0) {
1961 c
= (unsigned char) va_arg(args
, int);
1965 while (--spec
.field_width
> 0) {
1973 case FORMAT_TYPE_STR
:
1974 str
= string(str
, end
, va_arg(args
, char *), spec
);
1977 case FORMAT_TYPE_PTR
:
1978 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
1980 while (isalnum(*fmt
))
1984 case FORMAT_TYPE_PERCENT_CHAR
:
1990 case FORMAT_TYPE_INVALID
:
1992 * Presumably the arguments passed gcc's type
1993 * checking, but there is no safe or sane way
1994 * for us to continue parsing the format and
1995 * fetching from the va_list; the remaining
1996 * specifiers and arguments would be out of
2002 switch (spec
.type
) {
2003 case FORMAT_TYPE_LONG_LONG
:
2004 num
= va_arg(args
, long long);
2006 case FORMAT_TYPE_ULONG
:
2007 num
= va_arg(args
, unsigned long);
2009 case FORMAT_TYPE_LONG
:
2010 num
= va_arg(args
, long);
2012 case FORMAT_TYPE_SIZE_T
:
2013 if (spec
.flags
& SIGN
)
2014 num
= va_arg(args
, ssize_t
);
2016 num
= va_arg(args
, size_t);
2018 case FORMAT_TYPE_PTRDIFF
:
2019 num
= va_arg(args
, ptrdiff_t);
2021 case FORMAT_TYPE_UBYTE
:
2022 num
= (unsigned char) va_arg(args
, int);
2024 case FORMAT_TYPE_BYTE
:
2025 num
= (signed char) va_arg(args
, int);
2027 case FORMAT_TYPE_USHORT
:
2028 num
= (unsigned short) va_arg(args
, int);
2030 case FORMAT_TYPE_SHORT
:
2031 num
= (short) va_arg(args
, int);
2033 case FORMAT_TYPE_INT
:
2034 num
= (int) va_arg(args
, int);
2037 num
= va_arg(args
, unsigned int);
2040 str
= number(str
, end
, num
, spec
);
2052 /* the trailing null byte doesn't count towards the total */
2056 EXPORT_SYMBOL(vsnprintf
);
2059 * vscnprintf - Format a string and place it in a buffer
2060 * @buf: The buffer to place the result into
2061 * @size: The size of the buffer, including the trailing null space
2062 * @fmt: The format string to use
2063 * @args: Arguments for the format string
2065 * The return value is the number of characters which have been written into
2066 * the @buf not including the trailing '\0'. If @size is == 0 the function
2069 * If you're not already dealing with a va_list consider using scnprintf().
2071 * See the vsnprintf() documentation for format string extensions over C99.
2073 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2077 i
= vsnprintf(buf
, size
, fmt
, args
);
2079 if (likely(i
< size
))
2085 EXPORT_SYMBOL(vscnprintf
);
2088 * snprintf - Format a string and place it in a buffer
2089 * @buf: The buffer to place the result into
2090 * @size: The size of the buffer, including the trailing null space
2091 * @fmt: The format string to use
2092 * @...: Arguments for the format string
2094 * The return value is the number of characters which would be
2095 * generated for the given input, excluding the trailing null,
2096 * as per ISO C99. If the return is greater than or equal to
2097 * @size, the resulting string is truncated.
2099 * See the vsnprintf() documentation for format string extensions over C99.
2101 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2106 va_start(args
, fmt
);
2107 i
= vsnprintf(buf
, size
, fmt
, args
);
2112 EXPORT_SYMBOL(snprintf
);
2115 * scnprintf - Format a string and place it in a buffer
2116 * @buf: The buffer to place the result into
2117 * @size: The size of the buffer, including the trailing null space
2118 * @fmt: The format string to use
2119 * @...: Arguments for the format string
2121 * The return value is the number of characters written into @buf not including
2122 * the trailing '\0'. If @size is == 0 the function returns 0.
2125 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2130 va_start(args
, fmt
);
2131 i
= vscnprintf(buf
, size
, fmt
, args
);
2136 EXPORT_SYMBOL(scnprintf
);
2139 * vsprintf - Format a string and place it in a buffer
2140 * @buf: The buffer to place the result into
2141 * @fmt: The format string to use
2142 * @args: Arguments for the format string
2144 * The function returns the number of characters written
2145 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2148 * If you're not already dealing with a va_list consider using sprintf().
2150 * See the vsnprintf() documentation for format string extensions over C99.
2152 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2154 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2156 EXPORT_SYMBOL(vsprintf
);
2159 * sprintf - Format a string and place it in a buffer
2160 * @buf: The buffer to place the result into
2161 * @fmt: The format string to use
2162 * @...: Arguments for the format string
2164 * The function returns the number of characters written
2165 * into @buf. Use snprintf() or scnprintf() in order to avoid
2168 * See the vsnprintf() documentation for format string extensions over C99.
2170 int sprintf(char *buf
, const char *fmt
, ...)
2175 va_start(args
, fmt
);
2176 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2181 EXPORT_SYMBOL(sprintf
);
2183 #ifdef CONFIG_BINARY_PRINTF
2186 * vbin_printf() - VA arguments to binary data
2187 * bstr_printf() - Binary data to text string
2191 * vbin_printf - Parse a format string and place args' binary value in a buffer
2192 * @bin_buf: The buffer to place args' binary value
2193 * @size: The size of the buffer(by words(32bits), not characters)
2194 * @fmt: The format string to use
2195 * @args: Arguments for the format string
2197 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2200 * The return value is the number of words(32bits) which would be generated for
2204 * If the return value is greater than @size, the resulting bin_buf is NOT
2205 * valid for bstr_printf().
2207 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2209 struct printf_spec spec
= {0};
2212 str
= (char *)bin_buf
;
2213 end
= (char *)(bin_buf
+ size
);
2215 #define save_arg(type) \
2217 if (sizeof(type) == 8) { \
2218 unsigned long long value; \
2219 str = PTR_ALIGN(str, sizeof(u32)); \
2220 value = va_arg(args, unsigned long long); \
2221 if (str + sizeof(type) <= end) { \
2222 *(u32 *)str = *(u32 *)&value; \
2223 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2226 unsigned long value; \
2227 str = PTR_ALIGN(str, sizeof(type)); \
2228 value = va_arg(args, int); \
2229 if (str + sizeof(type) <= end) \
2230 *(typeof(type) *)str = (type)value; \
2232 str += sizeof(type); \
2236 int read
= format_decode(fmt
, &spec
);
2240 switch (spec
.type
) {
2241 case FORMAT_TYPE_NONE
:
2242 case FORMAT_TYPE_PERCENT_CHAR
:
2244 case FORMAT_TYPE_INVALID
:
2247 case FORMAT_TYPE_WIDTH
:
2248 case FORMAT_TYPE_PRECISION
:
2252 case FORMAT_TYPE_CHAR
:
2256 case FORMAT_TYPE_STR
: {
2257 const char *save_str
= va_arg(args
, char *);
2260 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
2261 || (unsigned long)save_str
< PAGE_SIZE
)
2262 save_str
= "(null)";
2263 len
= strlen(save_str
) + 1;
2264 if (str
+ len
< end
)
2265 memcpy(str
, save_str
, len
);
2270 case FORMAT_TYPE_PTR
:
2272 /* skip all alphanumeric pointer suffixes */
2273 while (isalnum(*fmt
))
2278 switch (spec
.type
) {
2280 case FORMAT_TYPE_LONG_LONG
:
2281 save_arg(long long);
2283 case FORMAT_TYPE_ULONG
:
2284 case FORMAT_TYPE_LONG
:
2285 save_arg(unsigned long);
2287 case FORMAT_TYPE_SIZE_T
:
2290 case FORMAT_TYPE_PTRDIFF
:
2291 save_arg(ptrdiff_t);
2293 case FORMAT_TYPE_UBYTE
:
2294 case FORMAT_TYPE_BYTE
:
2297 case FORMAT_TYPE_USHORT
:
2298 case FORMAT_TYPE_SHORT
:
2308 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2311 EXPORT_SYMBOL_GPL(vbin_printf
);
2314 * bstr_printf - Format a string from binary arguments and place it in a buffer
2315 * @buf: The buffer to place the result into
2316 * @size: The size of the buffer, including the trailing null space
2317 * @fmt: The format string to use
2318 * @bin_buf: Binary arguments for the format string
2320 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2321 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2322 * a binary buffer that generated by vbin_printf.
2324 * The format follows C99 vsnprintf, but has some extensions:
2325 * see vsnprintf comment for details.
2327 * The return value is the number of characters which would
2328 * be generated for the given input, excluding the trailing
2329 * '\0', as per ISO C99. If you want to have the exact
2330 * number of characters written into @buf as return value
2331 * (not including the trailing '\0'), use vscnprintf(). If the
2332 * return is greater than or equal to @size, the resulting
2333 * string is truncated.
2335 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2337 struct printf_spec spec
= {0};
2339 const char *args
= (const char *)bin_buf
;
2341 if (WARN_ON_ONCE(size
> INT_MAX
))
2347 #define get_arg(type) \
2349 typeof(type) value; \
2350 if (sizeof(type) == 8) { \
2351 args = PTR_ALIGN(args, sizeof(u32)); \
2352 *(u32 *)&value = *(u32 *)args; \
2353 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2355 args = PTR_ALIGN(args, sizeof(type)); \
2356 value = *(typeof(type) *)args; \
2358 args += sizeof(type); \
2362 /* Make sure end is always >= buf */
2369 const char *old_fmt
= fmt
;
2370 int read
= format_decode(fmt
, &spec
);
2374 switch (spec
.type
) {
2375 case FORMAT_TYPE_NONE
: {
2378 if (copy
> end
- str
)
2380 memcpy(str
, old_fmt
, copy
);
2386 case FORMAT_TYPE_WIDTH
:
2387 set_field_width(&spec
, get_arg(int));
2390 case FORMAT_TYPE_PRECISION
:
2391 set_precision(&spec
, get_arg(int));
2394 case FORMAT_TYPE_CHAR
: {
2397 if (!(spec
.flags
& LEFT
)) {
2398 while (--spec
.field_width
> 0) {
2404 c
= (unsigned char) get_arg(char);
2408 while (--spec
.field_width
> 0) {
2416 case FORMAT_TYPE_STR
: {
2417 const char *str_arg
= args
;
2418 args
+= strlen(str_arg
) + 1;
2419 str
= string(str
, end
, (char *)str_arg
, spec
);
2423 case FORMAT_TYPE_PTR
:
2424 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
2425 while (isalnum(*fmt
))
2429 case FORMAT_TYPE_PERCENT_CHAR
:
2435 case FORMAT_TYPE_INVALID
:
2439 unsigned long long num
;
2441 switch (spec
.type
) {
2443 case FORMAT_TYPE_LONG_LONG
:
2444 num
= get_arg(long long);
2446 case FORMAT_TYPE_ULONG
:
2447 case FORMAT_TYPE_LONG
:
2448 num
= get_arg(unsigned long);
2450 case FORMAT_TYPE_SIZE_T
:
2451 num
= get_arg(size_t);
2453 case FORMAT_TYPE_PTRDIFF
:
2454 num
= get_arg(ptrdiff_t);
2456 case FORMAT_TYPE_UBYTE
:
2457 num
= get_arg(unsigned char);
2459 case FORMAT_TYPE_BYTE
:
2460 num
= get_arg(signed char);
2462 case FORMAT_TYPE_USHORT
:
2463 num
= get_arg(unsigned short);
2465 case FORMAT_TYPE_SHORT
:
2466 num
= get_arg(short);
2468 case FORMAT_TYPE_UINT
:
2469 num
= get_arg(unsigned int);
2475 str
= number(str
, end
, num
, spec
);
2477 } /* switch(spec.type) */
2490 /* the trailing null byte doesn't count towards the total */
2493 EXPORT_SYMBOL_GPL(bstr_printf
);
2496 * bprintf - Parse a format string and place args' binary value in a buffer
2497 * @bin_buf: The buffer to place args' binary value
2498 * @size: The size of the buffer(by words(32bits), not characters)
2499 * @fmt: The format string to use
2500 * @...: Arguments for the format string
2502 * The function returns the number of words(u32) written
2505 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2510 va_start(args
, fmt
);
2511 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2516 EXPORT_SYMBOL_GPL(bprintf
);
2518 #endif /* CONFIG_BINARY_PRINTF */
2521 * vsscanf - Unformat a buffer into a list of arguments
2522 * @buf: input buffer
2523 * @fmt: format of buffer
2526 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
2528 const char *str
= buf
;
2536 unsigned long long u
;
2542 /* skip any white space in format */
2543 /* white space in format matchs any amount of
2544 * white space, including none, in the input.
2546 if (isspace(*fmt
)) {
2547 fmt
= skip_spaces(++fmt
);
2548 str
= skip_spaces(str
);
2551 /* anything that is not a conversion must match exactly */
2552 if (*fmt
!= '%' && *fmt
) {
2553 if (*fmt
++ != *str
++)
2562 /* skip this conversion.
2563 * advance both strings to next white space
2568 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
)
2570 while (!isspace(*str
) && *str
)
2575 /* get field width */
2577 if (isdigit(*fmt
)) {
2578 field_width
= skip_atoi(&fmt
);
2579 if (field_width
<= 0)
2583 /* get conversion qualifier */
2585 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2586 _tolower(*fmt
) == 'z') {
2588 if (unlikely(qualifier
== *fmt
)) {
2589 if (qualifier
== 'h') {
2592 } else if (qualifier
== 'l') {
2603 /* return number of characters read so far */
2604 *va_arg(args
, int *) = str
- buf
;
2618 char *s
= (char *)va_arg(args
, char*);
2619 if (field_width
== -1)
2623 } while (--field_width
> 0 && *str
);
2629 char *s
= (char *)va_arg(args
, char *);
2630 if (field_width
== -1)
2631 field_width
= SHRT_MAX
;
2632 /* first, skip leading white space in buffer */
2633 str
= skip_spaces(str
);
2635 /* now copy until next white space */
2636 while (*str
&& !isspace(*str
) && field_width
--)
2656 /* looking for '%' in str */
2661 /* invalid format; stop here */
2665 /* have some sort of integer conversion.
2666 * first, skip white space in buffer.
2668 str
= skip_spaces(str
);
2671 if (is_sign
&& digit
== '-')
2675 || (base
== 16 && !isxdigit(digit
))
2676 || (base
== 10 && !isdigit(digit
))
2677 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
2678 || (base
== 0 && !isdigit(digit
)))
2682 val
.s
= qualifier
!= 'L' ?
2683 simple_strtol(str
, &next
, base
) :
2684 simple_strtoll(str
, &next
, base
);
2686 val
.u
= qualifier
!= 'L' ?
2687 simple_strtoul(str
, &next
, base
) :
2688 simple_strtoull(str
, &next
, base
);
2690 if (field_width
> 0 && next
- str
> field_width
) {
2692 _parse_integer_fixup_radix(str
, &base
);
2693 while (next
- str
> field_width
) {
2695 val
.s
= div_s64(val
.s
, base
);
2697 val
.u
= div_u64(val
.u
, base
);
2702 switch (qualifier
) {
2703 case 'H': /* that's 'hh' in format */
2705 *va_arg(args
, signed char *) = val
.s
;
2707 *va_arg(args
, unsigned char *) = val
.u
;
2711 *va_arg(args
, short *) = val
.s
;
2713 *va_arg(args
, unsigned short *) = val
.u
;
2717 *va_arg(args
, long *) = val
.s
;
2719 *va_arg(args
, unsigned long *) = val
.u
;
2723 *va_arg(args
, long long *) = val
.s
;
2725 *va_arg(args
, unsigned long long *) = val
.u
;
2729 *va_arg(args
, size_t *) = val
.u
;
2733 *va_arg(args
, int *) = val
.s
;
2735 *va_arg(args
, unsigned int *) = val
.u
;
2747 EXPORT_SYMBOL(vsscanf
);
2750 * sscanf - Unformat a buffer into a list of arguments
2751 * @buf: input buffer
2752 * @fmt: formatting of buffer
2753 * @...: resulting arguments
2755 int sscanf(const char *buf
, const char *fmt
, ...)
2760 va_start(args
, fmt
);
2761 i
= vsscanf(buf
, fmt
, args
);
2766 EXPORT_SYMBOL(sscanf
);