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-provider.h>
21 #include <linux/module.h> /* for KSYM_SYMBOL_LEN */
22 #include <linux/types.h>
23 #include <linux/string.h>
24 #include <linux/ctype.h>
25 #include <linux/kernel.h>
26 #include <linux/kallsyms.h>
27 #include <linux/math64.h>
28 #include <linux/uaccess.h>
29 #include <linux/ioport.h>
30 #include <linux/dcache.h>
31 #include <linux/cred.h>
32 #include <net/addrconf.h>
34 #include <asm/page.h> /* for PAGE_SIZE */
35 #include <asm/sections.h> /* for dereference_function_descriptor() */
37 #include <linux/string_helpers.h>
41 * simple_strtoull - convert a string to an unsigned long long
42 * @cp: The start of the string
43 * @endp: A pointer to the end of the parsed string will be placed here
44 * @base: The number base to use
46 * This function is obsolete. Please use kstrtoull instead.
48 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
50 unsigned long long result
;
53 cp
= _parse_integer_fixup_radix(cp
, &base
);
54 rv
= _parse_integer(cp
, base
, &result
);
56 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
63 EXPORT_SYMBOL(simple_strtoull
);
66 * simple_strtoul - convert a string to an unsigned long
67 * @cp: The start of the string
68 * @endp: A pointer to the end of the parsed string will be placed here
69 * @base: The number base to use
71 * This function is obsolete. Please use kstrtoul instead.
73 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
75 return simple_strtoull(cp
, endp
, base
);
77 EXPORT_SYMBOL(simple_strtoul
);
80 * simple_strtol - convert a string to a signed long
81 * @cp: The start of the string
82 * @endp: A pointer to the end of the parsed string will be placed here
83 * @base: The number base to use
85 * This function is obsolete. Please use kstrtol instead.
87 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
90 return -simple_strtoul(cp
+ 1, endp
, base
);
92 return simple_strtoul(cp
, endp
, base
);
94 EXPORT_SYMBOL(simple_strtol
);
97 * simple_strtoll - convert a string to a signed long long
98 * @cp: The start of the string
99 * @endp: A pointer to the end of the parsed string will be placed here
100 * @base: The number base to use
102 * This function is obsolete. Please use kstrtoll instead.
104 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
107 return -simple_strtoull(cp
+ 1, endp
, base
);
109 return simple_strtoull(cp
, endp
, base
);
111 EXPORT_SYMBOL(simple_strtoll
);
113 static noinline_for_stack
114 int skip_atoi(const char **s
)
119 i
= i
*10 + *((*s
)++) - '0';
120 } while (isdigit(**s
));
125 /* Decimal conversion is by far the most typical, and is used
126 * for /proc and /sys data. This directly impacts e.g. top performance
127 * with many processes running. We optimize it for speed
128 * using ideas described at <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
129 * (with permission from the author, Douglas W. Jones).
132 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
133 /* Formats correctly any integer in [0, 999999999] */
134 static noinline_for_stack
135 char *put_dec_full9(char *buf
, unsigned q
)
140 * Possible ways to approx. divide by 10
141 * (x * 0x1999999a) >> 32 x < 1073741829 (multiply must be 64-bit)
142 * (x * 0xcccd) >> 19 x < 81920 (x < 262149 when 64-bit mul)
143 * (x * 0x6667) >> 18 x < 43699
144 * (x * 0x3334) >> 17 x < 16389
145 * (x * 0x199a) >> 16 x < 16389
146 * (x * 0x0ccd) >> 15 x < 16389
147 * (x * 0x0667) >> 14 x < 2739
148 * (x * 0x0334) >> 13 x < 1029
149 * (x * 0x019a) >> 12 x < 1029
150 * (x * 0x00cd) >> 11 x < 1029 shorter code than * 0x67 (on i386)
151 * (x * 0x0067) >> 10 x < 179
152 * (x * 0x0034) >> 9 x < 69 same
153 * (x * 0x001a) >> 8 x < 69 same
154 * (x * 0x000d) >> 7 x < 69 same, shortest code (on i386)
155 * (x * 0x0007) >> 6 x < 19
156 * See <http://www.cs.uiowa.edu/~jones/bcd/divide.html>
158 r
= (q
* (uint64_t)0x1999999a) >> 32;
159 *buf
++ = (q
- 10 * r
) + '0'; /* 1 */
160 q
= (r
* (uint64_t)0x1999999a) >> 32;
161 *buf
++ = (r
- 10 * q
) + '0'; /* 2 */
162 r
= (q
* (uint64_t)0x1999999a) >> 32;
163 *buf
++ = (q
- 10 * r
) + '0'; /* 3 */
164 q
= (r
* (uint64_t)0x1999999a) >> 32;
165 *buf
++ = (r
- 10 * q
) + '0'; /* 4 */
166 r
= (q
* (uint64_t)0x1999999a) >> 32;
167 *buf
++ = (q
- 10 * r
) + '0'; /* 5 */
168 /* Now value is under 10000, can avoid 64-bit multiply */
169 q
= (r
* 0x199a) >> 16;
170 *buf
++ = (r
- 10 * q
) + '0'; /* 6 */
171 r
= (q
* 0xcd) >> 11;
172 *buf
++ = (q
- 10 * r
) + '0'; /* 7 */
173 q
= (r
* 0xcd) >> 11;
174 *buf
++ = (r
- 10 * q
) + '0'; /* 8 */
175 *buf
++ = q
+ '0'; /* 9 */
180 /* Similar to above but do not pad with zeros.
181 * Code can be easily arranged to print 9 digits too, but our callers
182 * always call put_dec_full9() instead when the number has 9 decimal digits.
184 static noinline_for_stack
185 char *put_dec_trunc8(char *buf
, unsigned r
)
189 /* Copy of previous function's body with added early returns */
192 r
= (r
* (uint64_t)0x1999999a) >> 32;
196 q
= (r
* 0x199a) >> 16; /* r <= 9999 */
197 *buf
++ = (r
- 10 * q
) + '0';
200 r
= (q
* 0xcd) >> 11; /* q <= 999 */
201 *buf
++ = (q
- 10 * r
) + '0';
204 q
= (r
* 0xcd) >> 11; /* r <= 99 */
205 *buf
++ = (r
- 10 * q
) + '0';
208 *buf
++ = q
+ '0'; /* q <= 9 */
212 /* There are two algorithms to print larger numbers.
213 * One is generic: divide by 1000000000 and repeatedly print
214 * groups of (up to) 9 digits. It's conceptually simple,
215 * but requires a (unsigned long long) / 1000000000 division.
217 * Second algorithm splits 64-bit unsigned long long into 16-bit chunks,
218 * manipulates them cleverly and generates groups of 4 decimal digits.
219 * It so happens that it does NOT require long long division.
221 * If long is > 32 bits, division of 64-bit values is relatively easy,
222 * and we will use the first algorithm.
223 * If long long is > 64 bits (strange architecture with VERY large long long),
224 * second algorithm can't be used, and we again use the first one.
226 * Else (if long is 32 bits and long long is 64 bits) we use second one.
229 #if BITS_PER_LONG != 32 || BITS_PER_LONG_LONG != 64
231 /* First algorithm: generic */
234 char *put_dec(char *buf
, unsigned long long n
)
236 if (n
>= 100*1000*1000) {
237 while (n
>= 1000*1000*1000)
238 buf
= put_dec_full9(buf
, do_div(n
, 1000*1000*1000));
239 if (n
>= 100*1000*1000)
240 return put_dec_full9(buf
, n
);
242 return put_dec_trunc8(buf
, n
);
247 /* Second algorithm: valid only for 64-bit long longs */
249 /* See comment in put_dec_full9 for choice of constants */
250 static noinline_for_stack
251 void put_dec_full4(char *buf
, unsigned q
)
254 r
= (q
* 0xccd) >> 15;
255 buf
[0] = (q
- 10 * r
) + '0';
256 q
= (r
* 0xcd) >> 11;
257 buf
[1] = (r
- 10 * q
) + '0';
258 r
= (q
* 0xcd) >> 11;
259 buf
[2] = (q
- 10 * r
) + '0';
264 * Call put_dec_full4 on x % 10000, return x / 10000.
265 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
266 * holds for all x < 1,128,869,999. The largest value this
267 * helper will ever be asked to convert is 1,125,520,955.
268 * (d1 in the put_dec code, assuming n is all-ones).
271 unsigned put_dec_helper4(char *buf
, unsigned x
)
273 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
275 put_dec_full4(buf
, x
- q
* 10000);
279 /* Based on code by Douglas W. Jones found at
280 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
281 * (with permission from the author).
282 * Performs no 64-bit division and hence should be fast on 32-bit machines.
285 char *put_dec(char *buf
, unsigned long long n
)
287 uint32_t d3
, d2
, d1
, q
, h
;
289 if (n
< 100*1000*1000)
290 return put_dec_trunc8(buf
, n
);
292 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
295 d3
= (h
>> 16); /* implicit "& 0xffff" */
297 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
298 q
= put_dec_helper4(buf
, q
);
300 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
301 q
= put_dec_helper4(buf
+4, q
);
303 q
+= 4749 * d3
+ 42 * d2
;
304 q
= put_dec_helper4(buf
+8, q
);
309 buf
= put_dec_trunc8(buf
, q
);
310 else while (buf
[-1] == '0')
319 * Convert passed number to decimal string.
320 * Returns the length of string. On buffer overflow, returns 0.
322 * If speed is not important, use snprintf(). It's easy to read the code.
324 int num_to_str(char *buf
, int size
, unsigned long long num
)
326 char tmp
[sizeof(num
) * 3];
329 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
334 len
= put_dec(tmp
, num
) - tmp
;
339 for (idx
= 0; idx
< len
; ++idx
)
340 buf
[idx
] = tmp
[len
- idx
- 1];
344 #define SIGN 1 /* unsigned/signed, must be 1 */
345 #define LEFT 2 /* left justified */
346 #define PLUS 4 /* show plus */
347 #define SPACE 8 /* space if plus */
348 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
349 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
350 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
353 FORMAT_TYPE_NONE
, /* Just a string part */
355 FORMAT_TYPE_PRECISION
,
359 FORMAT_TYPE_PERCENT_CHAR
,
361 FORMAT_TYPE_LONG_LONG
,
375 u8 type
; /* format_type enum */
376 u8 flags
; /* flags to number() */
377 u8 base
; /* number base, 8, 10 or 16 only */
378 u8 qualifier
; /* number qualifier, one of 'hHlLtzZ' */
379 s16 field_width
; /* width of output field */
380 s16 precision
; /* # of digits/chars */
383 static noinline_for_stack
384 char *number(char *buf
, char *end
, unsigned long long num
,
385 struct printf_spec spec
)
387 char tmp
[3 * sizeof(num
)];
390 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
392 bool is_zero
= num
== 0LL;
394 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
395 * produces same digits or (maybe lowercased) letters */
396 locase
= (spec
.flags
& SMALL
);
397 if (spec
.flags
& LEFT
)
398 spec
.flags
&= ~ZEROPAD
;
400 if (spec
.flags
& SIGN
) {
401 if ((signed long long)num
< 0) {
403 num
= -(signed long long)num
;
405 } else if (spec
.flags
& PLUS
) {
408 } else if (spec
.flags
& SPACE
) {
415 spec
.field_width
-= 2;
420 /* generate full string in tmp[], in reverse order */
423 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
424 else if (spec
.base
!= 10) { /* 8 or 16 */
425 int mask
= spec
.base
- 1;
431 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
434 } else { /* base 10 */
435 i
= put_dec(tmp
, num
) - tmp
;
438 /* printing 100 using %2d gives "100", not "00" */
439 if (i
> spec
.precision
)
441 /* leading space padding */
442 spec
.field_width
-= spec
.precision
;
443 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
444 while (--spec
.field_width
>= 0) {
456 /* "0x" / "0" prefix */
458 if (spec
.base
== 16 || !is_zero
) {
463 if (spec
.base
== 16) {
465 *buf
= ('X' | locase
);
469 /* zero or space padding */
470 if (!(spec
.flags
& LEFT
)) {
471 char c
= ' ' + (spec
.flags
& ZEROPAD
);
472 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
473 while (--spec
.field_width
>= 0) {
479 /* hmm even more zero padding? */
480 while (i
<= --spec
.precision
) {
485 /* actual digits of result */
491 /* trailing space padding */
492 while (--spec
.field_width
>= 0) {
501 static noinline_for_stack
502 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
506 if ((unsigned long)s
< PAGE_SIZE
)
509 len
= strnlen(s
, spec
.precision
);
511 if (!(spec
.flags
& LEFT
)) {
512 while (len
< spec
.field_width
--) {
518 for (i
= 0; i
< len
; ++i
) {
523 while (len
< spec
.field_width
--) {
532 static void widen(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
);
550 static noinline_for_stack
551 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
554 const char *array
[4], *s
;
555 const struct dentry
*p
;
560 case '2': case '3': case '4':
561 depth
= fmt
[1] - '0';
568 for (i
= 0; i
< depth
; i
++, d
= p
) {
569 p
= ACCESS_ONCE(d
->d_parent
);
570 array
[i
] = ACCESS_ONCE(d
->d_name
.name
);
579 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
591 if (n
< spec
.field_width
) {
592 /* we want to pad the sucker */
593 unsigned spaces
= spec
.field_width
- n
;
594 if (!(spec
.flags
& LEFT
)) {
595 widen(buf
- n
, end
, n
, spaces
);
607 static noinline_for_stack
608 char *symbol_string(char *buf
, char *end
, void *ptr
,
609 struct printf_spec spec
, const char *fmt
)
612 #ifdef CONFIG_KALLSYMS
613 char sym
[KSYM_SYMBOL_LEN
];
617 ptr
= __builtin_extract_return_addr(ptr
);
618 value
= (unsigned long)ptr
;
620 #ifdef CONFIG_KALLSYMS
622 sprint_backtrace(sym
, value
);
623 else if (*fmt
!= 'f' && *fmt
!= 's')
624 sprint_symbol(sym
, value
);
626 sprint_symbol_no_offset(sym
, value
);
628 return string(buf
, end
, sym
, spec
);
630 spec
.field_width
= 2 * sizeof(void *);
631 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
634 return number(buf
, end
, value
, spec
);
638 static noinline_for_stack
639 char *resource_string(char *buf
, char *end
, struct resource
*res
,
640 struct printf_spec spec
, const char *fmt
)
642 #ifndef IO_RSRC_PRINTK_SIZE
643 #define IO_RSRC_PRINTK_SIZE 6
646 #ifndef MEM_RSRC_PRINTK_SIZE
647 #define MEM_RSRC_PRINTK_SIZE 10
649 static const struct printf_spec io_spec
= {
651 .field_width
= IO_RSRC_PRINTK_SIZE
,
653 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
655 static const struct printf_spec mem_spec
= {
657 .field_width
= MEM_RSRC_PRINTK_SIZE
,
659 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
661 static const struct printf_spec bus_spec
= {
665 .flags
= SMALL
| ZEROPAD
,
667 static const struct printf_spec dec_spec
= {
672 static const struct printf_spec str_spec
= {
677 static const struct printf_spec flag_spec
= {
680 .flags
= SPECIAL
| SMALL
,
683 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
684 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
685 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
686 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
687 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
688 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
689 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
690 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
692 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
693 int decode
= (fmt
[0] == 'R') ? 1 : 0;
694 const struct printf_spec
*specp
;
697 if (res
->flags
& IORESOURCE_IO
) {
698 p
= string(p
, pend
, "io ", str_spec
);
700 } else if (res
->flags
& IORESOURCE_MEM
) {
701 p
= string(p
, pend
, "mem ", str_spec
);
703 } else if (res
->flags
& IORESOURCE_IRQ
) {
704 p
= string(p
, pend
, "irq ", str_spec
);
706 } else if (res
->flags
& IORESOURCE_DMA
) {
707 p
= string(p
, pend
, "dma ", str_spec
);
709 } else if (res
->flags
& IORESOURCE_BUS
) {
710 p
= string(p
, pend
, "bus ", str_spec
);
713 p
= string(p
, pend
, "??? ", str_spec
);
717 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
718 p
= string(p
, pend
, "size ", str_spec
);
719 p
= number(p
, pend
, resource_size(res
), *specp
);
721 p
= number(p
, pend
, res
->start
, *specp
);
722 if (res
->start
!= res
->end
) {
724 p
= number(p
, pend
, res
->end
, *specp
);
728 if (res
->flags
& IORESOURCE_MEM_64
)
729 p
= string(p
, pend
, " 64bit", str_spec
);
730 if (res
->flags
& IORESOURCE_PREFETCH
)
731 p
= string(p
, pend
, " pref", str_spec
);
732 if (res
->flags
& IORESOURCE_WINDOW
)
733 p
= string(p
, pend
, " window", str_spec
);
734 if (res
->flags
& IORESOURCE_DISABLED
)
735 p
= string(p
, pend
, " disabled", str_spec
);
737 p
= string(p
, pend
, " flags ", str_spec
);
738 p
= number(p
, pend
, res
->flags
, flag_spec
);
743 return string(buf
, end
, sym
, spec
);
746 static noinline_for_stack
747 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
750 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
751 negative value, fallback to the default */
754 if (spec
.field_width
== 0)
755 /* nothing to print */
758 if (ZERO_OR_NULL_PTR(addr
))
760 return string(buf
, end
, NULL
, spec
);
777 if (spec
.field_width
> 0)
778 len
= min_t(int, spec
.field_width
, 64);
780 for (i
= 0; i
< len
; ++i
) {
782 *buf
= hex_asc_hi(addr
[i
]);
785 *buf
= hex_asc_lo(addr
[i
]);
788 if (separator
&& i
!= len
- 1) {
798 static noinline_for_stack
799 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
800 struct printf_spec spec
, const char *fmt
)
802 const int CHUNKSZ
= 32;
803 int nr_bits
= max_t(int, spec
.field_width
, 0);
807 /* reused to print numbers */
808 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
810 chunksz
= nr_bits
& (CHUNKSZ
- 1);
814 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
815 for (; i
>= 0; i
-= CHUNKSZ
) {
819 chunkmask
= ((1ULL << chunksz
) - 1);
820 word
= i
/ BITS_PER_LONG
;
821 bit
= i
% BITS_PER_LONG
;
822 val
= (bitmap
[word
] >> bit
) & chunkmask
;
831 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
832 buf
= number(buf
, end
, val
, spec
);
839 static noinline_for_stack
840 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
841 struct printf_spec spec
, const char *fmt
)
843 int nr_bits
= max_t(int, spec
.field_width
, 0);
844 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
848 /* reused to print numbers */
849 spec
= (struct printf_spec
){ .base
= 10 };
851 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
852 while (cur
< nr_bits
) {
854 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
855 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
865 buf
= number(buf
, end
, rbot
, spec
);
871 buf
= number(buf
, end
, rtop
, spec
);
879 static noinline_for_stack
880 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
881 struct printf_spec spec
, const char *fmt
)
883 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
887 bool reversed
= false;
903 for (i
= 0; i
< 6; i
++) {
905 p
= hex_byte_pack(p
, addr
[5 - i
]);
907 p
= hex_byte_pack(p
, addr
[i
]);
909 if (fmt
[0] == 'M' && i
!= 5)
914 return string(buf
, end
, mac_addr
, spec
);
917 static noinline_for_stack
918 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
921 bool leading_zeros
= (fmt
[0] == 'i');
946 for (i
= 0; i
< 4; i
++) {
947 char temp
[3]; /* hold each IP quad in reverse order */
948 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
955 /* reverse the digits in the quad */
967 static noinline_for_stack
968 char *ip6_compressed_string(char *p
, const char *addr
)
971 unsigned char zerolength
[8];
976 bool needcolon
= false;
980 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
982 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
984 memset(zerolength
, 0, sizeof(zerolength
));
991 /* find position of longest 0 run */
992 for (i
= 0; i
< range
; i
++) {
993 for (j
= i
; j
< range
; j
++) {
994 if (in6
.s6_addr16
[j
] != 0)
999 for (i
= 0; i
< range
; i
++) {
1000 if (zerolength
[i
] > longest
) {
1001 longest
= zerolength
[i
];
1005 if (longest
== 1) /* don't compress a single 0 */
1009 for (i
= 0; i
< range
; i
++) {
1010 if (i
== colonpos
) {
1011 if (needcolon
|| i
== 0)
1022 /* hex u16 without leading 0s */
1023 word
= ntohs(in6
.s6_addr16
[i
]);
1028 p
= hex_byte_pack(p
, hi
);
1030 *p
++ = hex_asc_lo(hi
);
1031 p
= hex_byte_pack(p
, lo
);
1034 p
= hex_byte_pack(p
, lo
);
1036 *p
++ = hex_asc_lo(lo
);
1043 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1050 static noinline_for_stack
1051 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1055 for (i
= 0; i
< 8; i
++) {
1056 p
= hex_byte_pack(p
, *addr
++);
1057 p
= hex_byte_pack(p
, *addr
++);
1058 if (fmt
[0] == 'I' && i
!= 7)
1066 static noinline_for_stack
1067 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1068 struct printf_spec spec
, const char *fmt
)
1070 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1072 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1073 ip6_compressed_string(ip6_addr
, addr
);
1075 ip6_string(ip6_addr
, addr
, fmt
);
1077 return string(buf
, end
, ip6_addr
, spec
);
1080 static noinline_for_stack
1081 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1082 struct printf_spec spec
, const char *fmt
)
1084 char ip4_addr
[sizeof("255.255.255.255")];
1086 ip4_string(ip4_addr
, addr
, fmt
);
1088 return string(buf
, end
, ip4_addr
, spec
);
1091 static noinline_for_stack
1092 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1093 struct printf_spec spec
, const char *fmt
)
1095 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1096 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1097 sizeof(":12345") + sizeof("/123456789") +
1098 sizeof("%1234567890")];
1099 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1100 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1101 char fmt6
[2] = { fmt
[0], '6' };
1105 while (isalpha(*++fmt
)) {
1122 if (have_p
|| have_s
|| have_f
) {
1127 if (fmt6
[0] == 'I' && have_c
)
1128 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1130 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1132 if (have_p
|| have_s
|| have_f
)
1137 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1141 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1142 IPV6_FLOWINFO_MASK
), spec
);
1146 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1150 return string(buf
, end
, ip6_addr
, spec
);
1153 static noinline_for_stack
1154 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1155 struct printf_spec spec
, const char *fmt
)
1157 bool have_p
= false;
1158 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1159 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1160 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1161 char fmt4
[3] = { fmt
[0], '4', 0 };
1164 while (isalpha(*++fmt
)) {
1178 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1181 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1185 return string(buf
, end
, ip4_addr
, spec
);
1188 static noinline_for_stack
1189 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1194 unsigned int flags
= 0;
1197 if (spec
.field_width
== 0)
1198 return buf
; /* nothing to print */
1200 if (ZERO_OR_NULL_PTR(addr
))
1201 return string(buf
, end
, NULL
, spec
); /* NULL pointer */
1205 switch (fmt
[count
++]) {
1207 flags
|= ESCAPE_ANY
;
1210 flags
|= ESCAPE_SPECIAL
;
1213 flags
|= ESCAPE_HEX
;
1216 flags
|= ESCAPE_NULL
;
1219 flags
|= ESCAPE_OCTAL
;
1225 flags
|= ESCAPE_SPACE
;
1234 flags
= ESCAPE_ANY_NP
;
1236 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1239 * string_escape_mem() writes as many characters as it can to
1240 * the given buffer, and returns the total size of the output
1241 * had the buffer been big enough.
1243 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1248 static noinline_for_stack
1249 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1250 struct printf_spec spec
, const char *fmt
)
1252 char uuid
[sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")];
1255 static const u8 be
[16] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
1256 static const u8 le
[16] = {3,2,1,0,5,4,7,6,8,9,10,11,12,13,14,15};
1257 const u8
*index
= be
;
1262 uc
= true; /* fall-through */
1271 for (i
= 0; i
< 16; i
++) {
1272 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1292 return string(buf
, end
, uuid
, spec
);
1296 char *netdev_feature_string(char *buf
, char *end
, const u8
*addr
,
1297 struct printf_spec spec
)
1299 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
1300 if (spec
.field_width
== -1)
1301 spec
.field_width
= 2 + 2 * sizeof(netdev_features_t
);
1304 return number(buf
, end
, *(const netdev_features_t
*)addr
, spec
);
1307 static noinline_for_stack
1308 char *address_val(char *buf
, char *end
, const void *addr
,
1309 struct printf_spec spec
, const char *fmt
)
1311 unsigned long long num
;
1313 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
1318 num
= *(const dma_addr_t
*)addr
;
1319 spec
.field_width
= sizeof(dma_addr_t
) * 2 + 2;
1323 num
= *(const phys_addr_t
*)addr
;
1324 spec
.field_width
= sizeof(phys_addr_t
) * 2 + 2;
1328 return number(buf
, end
, num
, spec
);
1331 static noinline_for_stack
1332 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1335 if (!IS_ENABLED(CONFIG_HAVE_CLK
) || !clk
)
1336 return string(buf
, end
, NULL
, spec
);
1340 return number(buf
, end
, clk_get_rate(clk
), spec
);
1344 #ifdef CONFIG_COMMON_CLK
1345 return string(buf
, end
, __clk_get_name(clk
), spec
);
1348 spec
.field_width
= sizeof(unsigned long) * 2 + 2;
1349 spec
.flags
|= SPECIAL
| SMALL
| ZEROPAD
;
1350 return number(buf
, end
, (unsigned long)clk
, spec
);
1355 int kptr_restrict __read_mostly
;
1358 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1359 * by an extra set of alphanumeric characters that are extended format
1362 * Right now we handle:
1364 * - 'F' For symbolic function descriptor pointers with offset
1365 * - 'f' For simple symbolic function names without offset
1366 * - 'S' For symbolic direct pointers with offset
1367 * - 's' For symbolic direct pointers without offset
1368 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1369 * - 'B' For backtraced symbolic direct pointers with offset
1370 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1371 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1372 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1373 * width which must be explicitly specified either as part of the
1374 * format string '%32b[l]' or through '%*b[l]', [l] selects
1375 * range-list format instead of hex format
1376 * - 'M' For a 6-byte MAC address, it prints the address in the
1377 * usual colon-separated hex notation
1378 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1379 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1380 * with a dash-separated hex notation
1381 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1382 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1383 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1384 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1386 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1387 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1388 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1389 * IPv6 omits the colons (01020304...0f)
1390 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1392 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1393 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1394 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1395 * - 'I[6S]c' for IPv6 addresses printed as specified by
1396 * http://tools.ietf.org/html/rfc5952
1397 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1398 * of the following flags (see string_escape_mem() for the
1401 * c - ESCAPE_SPECIAL
1407 * By default ESCAPE_ANY_NP is used.
1408 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1409 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1410 * Options for %pU are:
1411 * b big endian lower case hex (default)
1412 * B big endian UPPER case hex
1413 * l little endian lower case hex
1414 * L little endian UPPER case hex
1415 * big endian output byte order is:
1416 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1417 * little endian output byte order is:
1418 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1419 * - 'V' For a struct va_format which contains a format string * and va_list *,
1420 * call vsnprintf(->format, *->va_list).
1421 * Implements a "recursive vsnprintf".
1422 * Do not use this feature without some mechanism to verify the
1423 * correctness of the format string and va_list arguments.
1424 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1425 * - 'NF' For a netdev_features_t
1426 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1427 * a certain separator (' ' by default):
1431 * The maximum supported length is 64 bytes of the input. Consider
1432 * to use print_hex_dump() for the larger input.
1433 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1434 * (default assumed to be phys_addr_t, passed by reference)
1435 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1436 * - 'D[234]' Same as 'd' but for a struct file
1437 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1438 * (legacy clock framework) of the clock
1439 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1440 * (legacy clock framework) of the clock
1441 * - 'Cr' For a clock, it prints the current rate of the clock
1443 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1444 * function pointers are really function descriptors, which contain a
1445 * pointer to the real address.
1447 static noinline_for_stack
1448 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1449 struct printf_spec spec
)
1451 int default_width
= 2 * sizeof(void *) + (spec
.flags
& SPECIAL
? 2 : 0);
1453 if (!ptr
&& *fmt
!= 'K') {
1455 * Print (null) with the same width as a pointer so it makes
1456 * tabular output look nice.
1458 if (spec
.field_width
== -1)
1459 spec
.field_width
= default_width
;
1460 return string(buf
, end
, "(null)", spec
);
1466 ptr
= dereference_function_descriptor(ptr
);
1471 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1474 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1476 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1480 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
1482 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
1484 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1485 case 'm': /* Contiguous: 000102030405 */
1487 /* [mM]R (Reverse order; Bluetooth) */
1488 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1489 case 'I': /* Formatted IP supported
1491 * 6: 0001:0203:...:0708
1492 * 6c: 1::708 or 1::1.2.3.4
1494 case 'i': /* Contiguous:
1495 * 4: 001.002.003.004
1500 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1502 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1505 struct sockaddr raw
;
1506 struct sockaddr_in v4
;
1507 struct sockaddr_in6 v6
;
1510 switch (sa
->raw
.sa_family
) {
1512 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1514 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1516 return string(buf
, end
, "(invalid address)", spec
);
1521 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
1523 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1528 va_copy(va
, *((struct va_format
*)ptr
)->va
);
1529 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
1530 ((struct va_format
*)ptr
)->fmt
, va
);
1536 * %pK cannot be used in IRQ context because its test
1537 * for CAP_SYSLOG would be meaningless.
1539 if (kptr_restrict
&& (in_irq() || in_serving_softirq() ||
1541 if (spec
.field_width
== -1)
1542 spec
.field_width
= default_width
;
1543 return string(buf
, end
, "pK-error", spec
);
1546 switch (kptr_restrict
) {
1548 /* Always print %pK values */
1552 * Only print the real pointer value if the current
1553 * process has CAP_SYSLOG and is running with the
1554 * same credentials it started with. This is because
1555 * access to files is checked at open() time, but %pK
1556 * checks permission at read() time. We don't want to
1557 * leak pointer values if a binary opens a file using
1558 * %pK and then elevates privileges before reading it.
1560 const struct cred
*cred
= current_cred();
1562 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
1563 !uid_eq(cred
->euid
, cred
->uid
) ||
1564 !gid_eq(cred
->egid
, cred
->gid
))
1570 /* Always print 0's for %pK */
1579 return netdev_feature_string(buf
, end
, ptr
, spec
);
1583 return address_val(buf
, end
, ptr
, spec
, fmt
);
1585 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
1587 return clock(buf
, end
, ptr
, spec
, fmt
);
1589 return dentry_name(buf
, end
,
1590 ((const struct file
*)ptr
)->f_path
.dentry
,
1593 spec
.flags
|= SMALL
;
1594 if (spec
.field_width
== -1) {
1595 spec
.field_width
= default_width
;
1596 spec
.flags
|= ZEROPAD
;
1600 return number(buf
, end
, (unsigned long) ptr
, spec
);
1604 * Helper function to decode printf style format.
1605 * Each call decode a token from the format and return the
1606 * number of characters read (or likely the delta where it wants
1607 * to go on the next call).
1608 * The decoded token is returned through the parameters
1610 * 'h', 'l', or 'L' for integer fields
1611 * 'z' support added 23/7/1999 S.H.
1612 * 'z' changed to 'Z' --davidm 1/25/99
1613 * 't' added for ptrdiff_t
1615 * @fmt: the format string
1616 * @type of the token returned
1617 * @flags: various flags such as +, -, # tokens..
1618 * @field_width: overwritten width
1619 * @base: base of the number (octal, hex, ...)
1620 * @precision: precision of a number
1621 * @qualifier: qualifier of a number (long, size_t, ...)
1623 static noinline_for_stack
1624 int format_decode(const char *fmt
, struct printf_spec
*spec
)
1626 const char *start
= fmt
;
1628 /* we finished early by reading the field width */
1629 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
1630 if (spec
->field_width
< 0) {
1631 spec
->field_width
= -spec
->field_width
;
1632 spec
->flags
|= LEFT
;
1634 spec
->type
= FORMAT_TYPE_NONE
;
1638 /* we finished early by reading the precision */
1639 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
1640 if (spec
->precision
< 0)
1641 spec
->precision
= 0;
1643 spec
->type
= FORMAT_TYPE_NONE
;
1648 spec
->type
= FORMAT_TYPE_NONE
;
1650 for (; *fmt
; ++fmt
) {
1655 /* Return the current non-format string */
1656 if (fmt
!= start
|| !*fmt
)
1662 while (1) { /* this also skips first '%' */
1668 case '-': spec
->flags
|= LEFT
; break;
1669 case '+': spec
->flags
|= PLUS
; break;
1670 case ' ': spec
->flags
|= SPACE
; break;
1671 case '#': spec
->flags
|= SPECIAL
; break;
1672 case '0': spec
->flags
|= ZEROPAD
; break;
1673 default: found
= false;
1680 /* get field width */
1681 spec
->field_width
= -1;
1684 spec
->field_width
= skip_atoi(&fmt
);
1685 else if (*fmt
== '*') {
1686 /* it's the next argument */
1687 spec
->type
= FORMAT_TYPE_WIDTH
;
1688 return ++fmt
- start
;
1692 /* get the precision */
1693 spec
->precision
= -1;
1696 if (isdigit(*fmt
)) {
1697 spec
->precision
= skip_atoi(&fmt
);
1698 if (spec
->precision
< 0)
1699 spec
->precision
= 0;
1700 } else if (*fmt
== '*') {
1701 /* it's the next argument */
1702 spec
->type
= FORMAT_TYPE_PRECISION
;
1703 return ++fmt
- start
;
1708 /* get the conversion qualifier */
1709 spec
->qualifier
= -1;
1710 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
1711 _tolower(*fmt
) == 'z' || *fmt
== 't') {
1712 spec
->qualifier
= *fmt
++;
1713 if (unlikely(spec
->qualifier
== *fmt
)) {
1714 if (spec
->qualifier
== 'l') {
1715 spec
->qualifier
= 'L';
1717 } else if (spec
->qualifier
== 'h') {
1718 spec
->qualifier
= 'H';
1728 spec
->type
= FORMAT_TYPE_CHAR
;
1729 return ++fmt
- start
;
1732 spec
->type
= FORMAT_TYPE_STR
;
1733 return ++fmt
- start
;
1736 spec
->type
= FORMAT_TYPE_PTR
;
1737 return ++fmt
- start
;
1740 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
1741 return ++fmt
- start
;
1743 /* integer number formats - set up the flags and "break" */
1749 spec
->flags
|= SMALL
;
1757 spec
->flags
|= SIGN
;
1763 * Since %n poses a greater security risk than utility, treat
1764 * it as an invalid format specifier. Warn about its use so
1765 * that new instances don't get added.
1767 WARN_ONCE(1, "Please remove ignored %%n in '%s'\n", fmt
);
1771 spec
->type
= FORMAT_TYPE_INVALID
;
1775 if (spec
->qualifier
== 'L')
1776 spec
->type
= FORMAT_TYPE_LONG_LONG
;
1777 else if (spec
->qualifier
== 'l') {
1778 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
1779 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
1780 } else if (_tolower(spec
->qualifier
) == 'z') {
1781 spec
->type
= FORMAT_TYPE_SIZE_T
;
1782 } else if (spec
->qualifier
== 't') {
1783 spec
->type
= FORMAT_TYPE_PTRDIFF
;
1784 } else if (spec
->qualifier
== 'H') {
1785 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
1786 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
1787 } else if (spec
->qualifier
== 'h') {
1788 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
1789 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
1791 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
1792 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
1795 return ++fmt
- start
;
1799 * vsnprintf - Format a string and place it in a buffer
1800 * @buf: The buffer to place the result into
1801 * @size: The size of the buffer, including the trailing null space
1802 * @fmt: The format string to use
1803 * @args: Arguments for the format string
1805 * This function follows C99 vsnprintf, but has some extensions:
1806 * %pS output the name of a text symbol with offset
1807 * %ps output the name of a text symbol without offset
1808 * %pF output the name of a function pointer with its offset
1809 * %pf output the name of a function pointer without its offset
1810 * %pB output the name of a backtrace symbol with its offset
1811 * %pR output the address range in a struct resource with decoded flags
1812 * %pr output the address range in a struct resource with raw flags
1813 * %pb output the bitmap with field width as the number of bits
1814 * %pbl output the bitmap as range list with field width as the number of bits
1815 * %pM output a 6-byte MAC address with colons
1816 * %pMR output a 6-byte MAC address with colons in reversed order
1817 * %pMF output a 6-byte MAC address with dashes
1818 * %pm output a 6-byte MAC address without colons
1819 * %pmR output a 6-byte MAC address without colons in reversed order
1820 * %pI4 print an IPv4 address without leading zeros
1821 * %pi4 print an IPv4 address with leading zeros
1822 * %pI6 print an IPv6 address with colons
1823 * %pi6 print an IPv6 address without colons
1824 * %pI6c print an IPv6 address as specified by RFC 5952
1825 * %pIS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1826 * %piS depending on sa_family of 'struct sockaddr *' print IPv4/IPv6 address
1827 * %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
1829 * %*pE[achnops] print an escaped buffer
1830 * %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1831 * bytes of the input)
1832 * %pC output the name (Common Clock Framework) or address (legacy clock
1833 * framework) of a clock
1834 * %pCn output the name (Common Clock Framework) or address (legacy clock
1835 * framework) of a clock
1836 * %pCr output the current rate of a clock
1839 * ** Please update Documentation/printk-formats.txt when making changes **
1841 * The return value is the number of characters which would
1842 * be generated for the given input, excluding the trailing
1843 * '\0', as per ISO C99. If you want to have the exact
1844 * number of characters written into @buf as return value
1845 * (not including the trailing '\0'), use vscnprintf(). If the
1846 * return is greater than or equal to @size, the resulting
1847 * string is truncated.
1849 * If you're not already dealing with a va_list consider using snprintf().
1851 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
1853 unsigned long long num
;
1855 struct printf_spec spec
= {0};
1857 /* Reject out-of-range values early. Large positive sizes are
1858 used for unknown buffer sizes. */
1859 if (WARN_ON_ONCE(size
> INT_MAX
))
1865 /* Make sure end is always >= buf */
1872 const char *old_fmt
= fmt
;
1873 int read
= format_decode(fmt
, &spec
);
1877 switch (spec
.type
) {
1878 case FORMAT_TYPE_NONE
: {
1881 if (copy
> end
- str
)
1883 memcpy(str
, old_fmt
, copy
);
1889 case FORMAT_TYPE_WIDTH
:
1890 spec
.field_width
= va_arg(args
, int);
1893 case FORMAT_TYPE_PRECISION
:
1894 spec
.precision
= va_arg(args
, int);
1897 case FORMAT_TYPE_CHAR
: {
1900 if (!(spec
.flags
& LEFT
)) {
1901 while (--spec
.field_width
> 0) {
1908 c
= (unsigned char) va_arg(args
, int);
1912 while (--spec
.field_width
> 0) {
1920 case FORMAT_TYPE_STR
:
1921 str
= string(str
, end
, va_arg(args
, char *), spec
);
1924 case FORMAT_TYPE_PTR
:
1925 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
1927 while (isalnum(*fmt
))
1931 case FORMAT_TYPE_PERCENT_CHAR
:
1937 case FORMAT_TYPE_INVALID
:
1944 switch (spec
.type
) {
1945 case FORMAT_TYPE_LONG_LONG
:
1946 num
= va_arg(args
, long long);
1948 case FORMAT_TYPE_ULONG
:
1949 num
= va_arg(args
, unsigned long);
1951 case FORMAT_TYPE_LONG
:
1952 num
= va_arg(args
, long);
1954 case FORMAT_TYPE_SIZE_T
:
1955 if (spec
.flags
& SIGN
)
1956 num
= va_arg(args
, ssize_t
);
1958 num
= va_arg(args
, size_t);
1960 case FORMAT_TYPE_PTRDIFF
:
1961 num
= va_arg(args
, ptrdiff_t);
1963 case FORMAT_TYPE_UBYTE
:
1964 num
= (unsigned char) va_arg(args
, int);
1966 case FORMAT_TYPE_BYTE
:
1967 num
= (signed char) va_arg(args
, int);
1969 case FORMAT_TYPE_USHORT
:
1970 num
= (unsigned short) va_arg(args
, int);
1972 case FORMAT_TYPE_SHORT
:
1973 num
= (short) va_arg(args
, int);
1975 case FORMAT_TYPE_INT
:
1976 num
= (int) va_arg(args
, int);
1979 num
= va_arg(args
, unsigned int);
1982 str
= number(str
, end
, num
, spec
);
1993 /* the trailing null byte doesn't count towards the total */
1997 EXPORT_SYMBOL(vsnprintf
);
2000 * vscnprintf - Format a string and place it in a buffer
2001 * @buf: The buffer to place the result into
2002 * @size: The size of the buffer, including the trailing null space
2003 * @fmt: The format string to use
2004 * @args: Arguments for the format string
2006 * The return value is the number of characters which have been written into
2007 * the @buf not including the trailing '\0'. If @size is == 0 the function
2010 * If you're not already dealing with a va_list consider using scnprintf().
2012 * See the vsnprintf() documentation for format string extensions over C99.
2014 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2018 i
= vsnprintf(buf
, size
, fmt
, args
);
2020 if (likely(i
< size
))
2026 EXPORT_SYMBOL(vscnprintf
);
2029 * snprintf - Format a string and place it in a buffer
2030 * @buf: The buffer to place the result into
2031 * @size: The size of the buffer, including the trailing null space
2032 * @fmt: The format string to use
2033 * @...: Arguments for the format string
2035 * The return value is the number of characters which would be
2036 * generated for the given input, excluding the trailing null,
2037 * as per ISO C99. If the return is greater than or equal to
2038 * @size, the resulting string is truncated.
2040 * See the vsnprintf() documentation for format string extensions over C99.
2042 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2047 va_start(args
, fmt
);
2048 i
= vsnprintf(buf
, size
, fmt
, args
);
2053 EXPORT_SYMBOL(snprintf
);
2056 * scnprintf - Format a string and place it in a buffer
2057 * @buf: The buffer to place the result into
2058 * @size: The size of the buffer, including the trailing null space
2059 * @fmt: The format string to use
2060 * @...: Arguments for the format string
2062 * The return value is the number of characters written into @buf not including
2063 * the trailing '\0'. If @size is == 0 the function returns 0.
2066 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2071 va_start(args
, fmt
);
2072 i
= vscnprintf(buf
, size
, fmt
, args
);
2077 EXPORT_SYMBOL(scnprintf
);
2080 * vsprintf - Format a string and place it in a buffer
2081 * @buf: The buffer to place the result into
2082 * @fmt: The format string to use
2083 * @args: Arguments for the format string
2085 * The function returns the number of characters written
2086 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2089 * If you're not already dealing with a va_list consider using sprintf().
2091 * See the vsnprintf() documentation for format string extensions over C99.
2093 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2095 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2097 EXPORT_SYMBOL(vsprintf
);
2100 * sprintf - Format a string and place it in a buffer
2101 * @buf: The buffer to place the result into
2102 * @fmt: The format string to use
2103 * @...: Arguments for the format string
2105 * The function returns the number of characters written
2106 * into @buf. Use snprintf() or scnprintf() in order to avoid
2109 * See the vsnprintf() documentation for format string extensions over C99.
2111 int sprintf(char *buf
, const char *fmt
, ...)
2116 va_start(args
, fmt
);
2117 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2122 EXPORT_SYMBOL(sprintf
);
2124 #ifdef CONFIG_BINARY_PRINTF
2127 * vbin_printf() - VA arguments to binary data
2128 * bstr_printf() - Binary data to text string
2132 * vbin_printf - Parse a format string and place args' binary value in a buffer
2133 * @bin_buf: The buffer to place args' binary value
2134 * @size: The size of the buffer(by words(32bits), not characters)
2135 * @fmt: The format string to use
2136 * @args: Arguments for the format string
2138 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2141 * The return value is the number of words(32bits) which would be generated for
2145 * If the return value is greater than @size, the resulting bin_buf is NOT
2146 * valid for bstr_printf().
2148 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2150 struct printf_spec spec
= {0};
2153 str
= (char *)bin_buf
;
2154 end
= (char *)(bin_buf
+ size
);
2156 #define save_arg(type) \
2158 if (sizeof(type) == 8) { \
2159 unsigned long long value; \
2160 str = PTR_ALIGN(str, sizeof(u32)); \
2161 value = va_arg(args, unsigned long long); \
2162 if (str + sizeof(type) <= end) { \
2163 *(u32 *)str = *(u32 *)&value; \
2164 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2167 unsigned long value; \
2168 str = PTR_ALIGN(str, sizeof(type)); \
2169 value = va_arg(args, int); \
2170 if (str + sizeof(type) <= end) \
2171 *(typeof(type) *)str = (type)value; \
2173 str += sizeof(type); \
2177 int read
= format_decode(fmt
, &spec
);
2181 switch (spec
.type
) {
2182 case FORMAT_TYPE_NONE
:
2183 case FORMAT_TYPE_INVALID
:
2184 case FORMAT_TYPE_PERCENT_CHAR
:
2187 case FORMAT_TYPE_WIDTH
:
2188 case FORMAT_TYPE_PRECISION
:
2192 case FORMAT_TYPE_CHAR
:
2196 case FORMAT_TYPE_STR
: {
2197 const char *save_str
= va_arg(args
, char *);
2200 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
2201 || (unsigned long)save_str
< PAGE_SIZE
)
2202 save_str
= "(null)";
2203 len
= strlen(save_str
) + 1;
2204 if (str
+ len
< end
)
2205 memcpy(str
, save_str
, len
);
2210 case FORMAT_TYPE_PTR
:
2212 /* skip all alphanumeric pointer suffixes */
2213 while (isalnum(*fmt
))
2218 switch (spec
.type
) {
2220 case FORMAT_TYPE_LONG_LONG
:
2221 save_arg(long long);
2223 case FORMAT_TYPE_ULONG
:
2224 case FORMAT_TYPE_LONG
:
2225 save_arg(unsigned long);
2227 case FORMAT_TYPE_SIZE_T
:
2230 case FORMAT_TYPE_PTRDIFF
:
2231 save_arg(ptrdiff_t);
2233 case FORMAT_TYPE_UBYTE
:
2234 case FORMAT_TYPE_BYTE
:
2237 case FORMAT_TYPE_USHORT
:
2238 case FORMAT_TYPE_SHORT
:
2247 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2250 EXPORT_SYMBOL_GPL(vbin_printf
);
2253 * bstr_printf - Format a string from binary arguments and place it in a buffer
2254 * @buf: The buffer to place the result into
2255 * @size: The size of the buffer, including the trailing null space
2256 * @fmt: The format string to use
2257 * @bin_buf: Binary arguments for the format string
2259 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2260 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2261 * a binary buffer that generated by vbin_printf.
2263 * The format follows C99 vsnprintf, but has some extensions:
2264 * see vsnprintf comment for details.
2266 * The return value is the number of characters which would
2267 * be generated for the given input, excluding the trailing
2268 * '\0', as per ISO C99. If you want to have the exact
2269 * number of characters written into @buf as return value
2270 * (not including the trailing '\0'), use vscnprintf(). If the
2271 * return is greater than or equal to @size, the resulting
2272 * string is truncated.
2274 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2276 struct printf_spec spec
= {0};
2278 const char *args
= (const char *)bin_buf
;
2280 if (WARN_ON_ONCE((int) size
< 0))
2286 #define get_arg(type) \
2288 typeof(type) value; \
2289 if (sizeof(type) == 8) { \
2290 args = PTR_ALIGN(args, sizeof(u32)); \
2291 *(u32 *)&value = *(u32 *)args; \
2292 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2294 args = PTR_ALIGN(args, sizeof(type)); \
2295 value = *(typeof(type) *)args; \
2297 args += sizeof(type); \
2301 /* Make sure end is always >= buf */
2308 const char *old_fmt
= fmt
;
2309 int read
= format_decode(fmt
, &spec
);
2313 switch (spec
.type
) {
2314 case FORMAT_TYPE_NONE
: {
2317 if (copy
> end
- str
)
2319 memcpy(str
, old_fmt
, copy
);
2325 case FORMAT_TYPE_WIDTH
:
2326 spec
.field_width
= get_arg(int);
2329 case FORMAT_TYPE_PRECISION
:
2330 spec
.precision
= get_arg(int);
2333 case FORMAT_TYPE_CHAR
: {
2336 if (!(spec
.flags
& LEFT
)) {
2337 while (--spec
.field_width
> 0) {
2343 c
= (unsigned char) get_arg(char);
2347 while (--spec
.field_width
> 0) {
2355 case FORMAT_TYPE_STR
: {
2356 const char *str_arg
= args
;
2357 args
+= strlen(str_arg
) + 1;
2358 str
= string(str
, end
, (char *)str_arg
, spec
);
2362 case FORMAT_TYPE_PTR
:
2363 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
2364 while (isalnum(*fmt
))
2368 case FORMAT_TYPE_PERCENT_CHAR
:
2369 case FORMAT_TYPE_INVALID
:
2376 unsigned long long num
;
2378 switch (spec
.type
) {
2380 case FORMAT_TYPE_LONG_LONG
:
2381 num
= get_arg(long long);
2383 case FORMAT_TYPE_ULONG
:
2384 case FORMAT_TYPE_LONG
:
2385 num
= get_arg(unsigned long);
2387 case FORMAT_TYPE_SIZE_T
:
2388 num
= get_arg(size_t);
2390 case FORMAT_TYPE_PTRDIFF
:
2391 num
= get_arg(ptrdiff_t);
2393 case FORMAT_TYPE_UBYTE
:
2394 num
= get_arg(unsigned char);
2396 case FORMAT_TYPE_BYTE
:
2397 num
= get_arg(signed char);
2399 case FORMAT_TYPE_USHORT
:
2400 num
= get_arg(unsigned short);
2402 case FORMAT_TYPE_SHORT
:
2403 num
= get_arg(short);
2405 case FORMAT_TYPE_UINT
:
2406 num
= get_arg(unsigned int);
2412 str
= number(str
, end
, num
, spec
);
2414 } /* switch(spec.type) */
2426 /* the trailing null byte doesn't count towards the total */
2429 EXPORT_SYMBOL_GPL(bstr_printf
);
2432 * bprintf - Parse a format string and place args' binary value in a buffer
2433 * @bin_buf: The buffer to place args' binary value
2434 * @size: The size of the buffer(by words(32bits), not characters)
2435 * @fmt: The format string to use
2436 * @...: Arguments for the format string
2438 * The function returns the number of words(u32) written
2441 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2446 va_start(args
, fmt
);
2447 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2452 EXPORT_SYMBOL_GPL(bprintf
);
2454 #endif /* CONFIG_BINARY_PRINTF */
2457 * vsscanf - Unformat a buffer into a list of arguments
2458 * @buf: input buffer
2459 * @fmt: format of buffer
2462 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
2464 const char *str
= buf
;
2472 unsigned long long u
;
2478 /* skip any white space in format */
2479 /* white space in format matchs any amount of
2480 * white space, including none, in the input.
2482 if (isspace(*fmt
)) {
2483 fmt
= skip_spaces(++fmt
);
2484 str
= skip_spaces(str
);
2487 /* anything that is not a conversion must match exactly */
2488 if (*fmt
!= '%' && *fmt
) {
2489 if (*fmt
++ != *str
++)
2498 /* skip this conversion.
2499 * advance both strings to next white space
2504 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
)
2506 while (!isspace(*str
) && *str
)
2511 /* get field width */
2513 if (isdigit(*fmt
)) {
2514 field_width
= skip_atoi(&fmt
);
2515 if (field_width
<= 0)
2519 /* get conversion qualifier */
2521 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2522 _tolower(*fmt
) == 'z') {
2524 if (unlikely(qualifier
== *fmt
)) {
2525 if (qualifier
== 'h') {
2528 } else if (qualifier
== 'l') {
2539 /* return number of characters read so far */
2540 *va_arg(args
, int *) = str
- buf
;
2554 char *s
= (char *)va_arg(args
, char*);
2555 if (field_width
== -1)
2559 } while (--field_width
> 0 && *str
);
2565 char *s
= (char *)va_arg(args
, char *);
2566 if (field_width
== -1)
2567 field_width
= SHRT_MAX
;
2568 /* first, skip leading white space in buffer */
2569 str
= skip_spaces(str
);
2571 /* now copy until next white space */
2572 while (*str
&& !isspace(*str
) && field_width
--)
2592 /* looking for '%' in str */
2597 /* invalid format; stop here */
2601 /* have some sort of integer conversion.
2602 * first, skip white space in buffer.
2604 str
= skip_spaces(str
);
2607 if (is_sign
&& digit
== '-')
2611 || (base
== 16 && !isxdigit(digit
))
2612 || (base
== 10 && !isdigit(digit
))
2613 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
2614 || (base
== 0 && !isdigit(digit
)))
2618 val
.s
= qualifier
!= 'L' ?
2619 simple_strtol(str
, &next
, base
) :
2620 simple_strtoll(str
, &next
, base
);
2622 val
.u
= qualifier
!= 'L' ?
2623 simple_strtoul(str
, &next
, base
) :
2624 simple_strtoull(str
, &next
, base
);
2626 if (field_width
> 0 && next
- str
> field_width
) {
2628 _parse_integer_fixup_radix(str
, &base
);
2629 while (next
- str
> field_width
) {
2631 val
.s
= div_s64(val
.s
, base
);
2633 val
.u
= div_u64(val
.u
, base
);
2638 switch (qualifier
) {
2639 case 'H': /* that's 'hh' in format */
2641 *va_arg(args
, signed char *) = val
.s
;
2643 *va_arg(args
, unsigned char *) = val
.u
;
2647 *va_arg(args
, short *) = val
.s
;
2649 *va_arg(args
, unsigned short *) = val
.u
;
2653 *va_arg(args
, long *) = val
.s
;
2655 *va_arg(args
, unsigned long *) = val
.u
;
2659 *va_arg(args
, long long *) = val
.s
;
2661 *va_arg(args
, unsigned long long *) = val
.u
;
2665 *va_arg(args
, size_t *) = val
.u
;
2669 *va_arg(args
, int *) = val
.s
;
2671 *va_arg(args
, unsigned int *) = val
.u
;
2683 EXPORT_SYMBOL(vsscanf
);
2686 * sscanf - Unformat a buffer into a list of arguments
2687 * @buf: input buffer
2688 * @fmt: formatting of buffer
2689 * @...: resulting arguments
2691 int sscanf(const char *buf
, const char *fmt
, ...)
2696 va_start(args
, fmt
);
2697 i
= vsscanf(buf
, fmt
, args
);
2702 EXPORT_SYMBOL(sscanf
);