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 <linux/uuid.h>
35 #include <net/addrconf.h>
36 #include <linux/siphash.h>
37 #include <linux/compiler.h>
39 #include <linux/blkdev.h>
42 #include "../mm/internal.h" /* For the trace_print_flags arrays */
44 #include <asm/page.h> /* for PAGE_SIZE */
45 #include <asm/sections.h> /* for dereference_function_descriptor() */
46 #include <asm/byteorder.h> /* cpu_to_le16 */
48 #include <linux/string_helpers.h>
52 * simple_strtoull - convert a string to an unsigned long long
53 * @cp: The start of the string
54 * @endp: A pointer to the end of the parsed string will be placed here
55 * @base: The number base to use
57 * This function is obsolete. Please use kstrtoull instead.
59 unsigned long long simple_strtoull(const char *cp
, char **endp
, unsigned int base
)
61 unsigned long long result
;
64 cp
= _parse_integer_fixup_radix(cp
, &base
);
65 rv
= _parse_integer(cp
, base
, &result
);
67 cp
+= (rv
& ~KSTRTOX_OVERFLOW
);
74 EXPORT_SYMBOL(simple_strtoull
);
77 * simple_strtoul - convert a string to an unsigned long
78 * @cp: The start of the string
79 * @endp: A pointer to the end of the parsed string will be placed here
80 * @base: The number base to use
82 * This function is obsolete. Please use kstrtoul instead.
84 unsigned long simple_strtoul(const char *cp
, char **endp
, unsigned int base
)
86 return simple_strtoull(cp
, endp
, base
);
88 EXPORT_SYMBOL(simple_strtoul
);
91 * simple_strtol - convert a string to a signed long
92 * @cp: The start of the string
93 * @endp: A pointer to the end of the parsed string will be placed here
94 * @base: The number base to use
96 * This function is obsolete. Please use kstrtol instead.
98 long simple_strtol(const char *cp
, char **endp
, unsigned int base
)
101 return -simple_strtoul(cp
+ 1, endp
, base
);
103 return simple_strtoul(cp
, endp
, base
);
105 EXPORT_SYMBOL(simple_strtol
);
108 * simple_strtoll - convert a string to a signed long long
109 * @cp: The start of the string
110 * @endp: A pointer to the end of the parsed string will be placed here
111 * @base: The number base to use
113 * This function is obsolete. Please use kstrtoll instead.
115 long long simple_strtoll(const char *cp
, char **endp
, unsigned int base
)
118 return -simple_strtoull(cp
+ 1, endp
, base
);
120 return simple_strtoull(cp
, endp
, base
);
122 EXPORT_SYMBOL(simple_strtoll
);
124 static noinline_for_stack
125 int skip_atoi(const char **s
)
130 i
= i
*10 + *((*s
)++) - '0';
131 } while (isdigit(**s
));
137 * Decimal conversion is by far the most typical, and is used for
138 * /proc and /sys data. This directly impacts e.g. top performance
139 * with many processes running. We optimize it for speed by emitting
140 * two characters at a time, using a 200 byte lookup table. This
141 * roughly halves the number of multiplications compared to computing
142 * the digits one at a time. Implementation strongly inspired by the
143 * previous version, which in turn used ideas described at
144 * <http://www.cs.uiowa.edu/~jones/bcd/divide.html> (with permission
145 * from the author, Douglas W. Jones).
147 * It turns out there is precisely one 26 bit fixed-point
148 * approximation a of 64/100 for which x/100 == (x * (u64)a) >> 32
149 * holds for all x in [0, 10^8-1], namely a = 0x28f5c29. The actual
150 * range happens to be somewhat larger (x <= 1073741898), but that's
151 * irrelevant for our purpose.
153 * For dividing a number in the range [10^4, 10^6-1] by 100, we still
154 * need a 32x32->64 bit multiply, so we simply use the same constant.
156 * For dividing a number in the range [100, 10^4-1] by 100, there are
157 * several options. The simplest is (x * 0x147b) >> 19, which is valid
158 * for all x <= 43698.
161 static const u16 decpair
[100] = {
162 #define _(x) (__force u16) cpu_to_le16(((x % 10) | ((x / 10) << 8)) + 0x3030)
163 _( 0), _( 1), _( 2), _( 3), _( 4), _( 5), _( 6), _( 7), _( 8), _( 9),
164 _(10), _(11), _(12), _(13), _(14), _(15), _(16), _(17), _(18), _(19),
165 _(20), _(21), _(22), _(23), _(24), _(25), _(26), _(27), _(28), _(29),
166 _(30), _(31), _(32), _(33), _(34), _(35), _(36), _(37), _(38), _(39),
167 _(40), _(41), _(42), _(43), _(44), _(45), _(46), _(47), _(48), _(49),
168 _(50), _(51), _(52), _(53), _(54), _(55), _(56), _(57), _(58), _(59),
169 _(60), _(61), _(62), _(63), _(64), _(65), _(66), _(67), _(68), _(69),
170 _(70), _(71), _(72), _(73), _(74), _(75), _(76), _(77), _(78), _(79),
171 _(80), _(81), _(82), _(83), _(84), _(85), _(86), _(87), _(88), _(89),
172 _(90), _(91), _(92), _(93), _(94), _(95), _(96), _(97), _(98), _(99),
177 * This will print a single '0' even if r == 0, since we would
178 * immediately jump to out_r where two 0s would be written but only
179 * one of them accounted for in buf. This is needed by ip4_string
180 * below. All other callers pass a non-zero value of r.
182 static noinline_for_stack
183 char *put_dec_trunc8(char *buf
, unsigned r
)
191 /* 100 <= r < 10^8 */
192 q
= (r
* (u64
)0x28f5c29) >> 32;
193 *((u16
*)buf
) = decpair
[r
- 100*q
];
200 /* 100 <= q < 10^6 */
201 r
= (q
* (u64
)0x28f5c29) >> 32;
202 *((u16
*)buf
) = decpair
[q
- 100*r
];
209 /* 100 <= r < 10^4 */
210 q
= (r
* 0x147b) >> 19;
211 *((u16
*)buf
) = decpair
[r
- 100*q
];
218 *((u16
*)buf
) = decpair
[r
];
219 buf
+= r
< 10 ? 1 : 2;
223 #if BITS_PER_LONG == 64 && BITS_PER_LONG_LONG == 64
224 static noinline_for_stack
225 char *put_dec_full8(char *buf
, unsigned r
)
230 q
= (r
* (u64
)0x28f5c29) >> 32;
231 *((u16
*)buf
) = decpair
[r
- 100*q
];
235 r
= (q
* (u64
)0x28f5c29) >> 32;
236 *((u16
*)buf
) = decpair
[q
- 100*r
];
240 q
= (r
* 0x147b) >> 19;
241 *((u16
*)buf
) = decpair
[r
- 100*q
];
245 *((u16
*)buf
) = decpair
[q
];
250 static noinline_for_stack
251 char *put_dec(char *buf
, unsigned long long n
)
253 if (n
>= 100*1000*1000)
254 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
255 /* 1 <= n <= 1.6e11 */
256 if (n
>= 100*1000*1000)
257 buf
= put_dec_full8(buf
, do_div(n
, 100*1000*1000));
259 return put_dec_trunc8(buf
, n
);
262 #elif BITS_PER_LONG == 32 && BITS_PER_LONG_LONG == 64
265 put_dec_full4(char *buf
, unsigned r
)
270 q
= (r
* 0x147b) >> 19;
271 *((u16
*)buf
) = decpair
[r
- 100*q
];
274 *((u16
*)buf
) = decpair
[q
];
278 * Call put_dec_full4 on x % 10000, return x / 10000.
279 * The approximation x/10000 == (x * 0x346DC5D7) >> 43
280 * holds for all x < 1,128,869,999. The largest value this
281 * helper will ever be asked to convert is 1,125,520,955.
282 * (second call in the put_dec code, assuming n is all-ones).
284 static noinline_for_stack
285 unsigned put_dec_helper4(char *buf
, unsigned x
)
287 uint32_t q
= (x
* (uint64_t)0x346DC5D7) >> 43;
289 put_dec_full4(buf
, x
- q
* 10000);
293 /* Based on code by Douglas W. Jones found at
294 * <http://www.cs.uiowa.edu/~jones/bcd/decimal.html#sixtyfour>
295 * (with permission from the author).
296 * Performs no 64-bit division and hence should be fast on 32-bit machines.
299 char *put_dec(char *buf
, unsigned long long n
)
301 uint32_t d3
, d2
, d1
, q
, h
;
303 if (n
< 100*1000*1000)
304 return put_dec_trunc8(buf
, n
);
306 d1
= ((uint32_t)n
>> 16); /* implicit "& 0xffff" */
309 d3
= (h
>> 16); /* implicit "& 0xffff" */
311 /* n = 2^48 d3 + 2^32 d2 + 2^16 d1 + d0
312 = 281_4749_7671_0656 d3 + 42_9496_7296 d2 + 6_5536 d1 + d0 */
313 q
= 656 * d3
+ 7296 * d2
+ 5536 * d1
+ ((uint32_t)n
& 0xffff);
314 q
= put_dec_helper4(buf
, q
);
316 q
+= 7671 * d3
+ 9496 * d2
+ 6 * d1
;
317 q
= put_dec_helper4(buf
+4, q
);
319 q
+= 4749 * d3
+ 42 * d2
;
320 q
= put_dec_helper4(buf
+8, q
);
325 buf
= put_dec_trunc8(buf
, q
);
326 else while (buf
[-1] == '0')
335 * Convert passed number to decimal string.
336 * Returns the length of string. On buffer overflow, returns 0.
338 * If speed is not important, use snprintf(). It's easy to read the code.
340 int num_to_str(char *buf
, int size
, unsigned long long num
)
342 /* put_dec requires 2-byte alignment of the buffer. */
343 char tmp
[sizeof(num
) * 3] __aligned(2);
346 /* put_dec() may work incorrectly for num = 0 (generate "", not "0") */
351 len
= put_dec(tmp
, num
) - tmp
;
356 for (idx
= 0; idx
< len
; ++idx
)
357 buf
[idx
] = tmp
[len
- idx
- 1];
361 #define SIGN 1 /* unsigned/signed, must be 1 */
362 #define LEFT 2 /* left justified */
363 #define PLUS 4 /* show plus */
364 #define SPACE 8 /* space if plus */
365 #define ZEROPAD 16 /* pad with zero, must be 16 == '0' - ' ' */
366 #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
367 #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
370 FORMAT_TYPE_NONE
, /* Just a string part */
372 FORMAT_TYPE_PRECISION
,
376 FORMAT_TYPE_PERCENT_CHAR
,
378 FORMAT_TYPE_LONG_LONG
,
392 unsigned int type
:8; /* format_type enum */
393 signed int field_width
:24; /* width of output field */
394 unsigned int flags
:8; /* flags to number() */
395 unsigned int base
:8; /* number base, 8, 10 or 16 only */
396 signed int precision
:16; /* # of digits/chars */
398 #define FIELD_WIDTH_MAX ((1 << 23) - 1)
399 #define PRECISION_MAX ((1 << 15) - 1)
401 static noinline_for_stack
402 char *number(char *buf
, char *end
, unsigned long long num
,
403 struct printf_spec spec
)
405 /* put_dec requires 2-byte alignment of the buffer. */
406 char tmp
[3 * sizeof(num
)] __aligned(2);
409 int need_pfx
= ((spec
.flags
& SPECIAL
) && spec
.base
!= 10);
411 bool is_zero
= num
== 0LL;
412 int field_width
= spec
.field_width
;
413 int precision
= spec
.precision
;
415 BUILD_BUG_ON(sizeof(struct printf_spec
) != 8);
417 /* locase = 0 or 0x20. ORing digits or letters with 'locase'
418 * produces same digits or (maybe lowercased) letters */
419 locase
= (spec
.flags
& SMALL
);
420 if (spec
.flags
& LEFT
)
421 spec
.flags
&= ~ZEROPAD
;
423 if (spec
.flags
& SIGN
) {
424 if ((signed long long)num
< 0) {
426 num
= -(signed long long)num
;
428 } else if (spec
.flags
& PLUS
) {
431 } else if (spec
.flags
& SPACE
) {
443 /* generate full string in tmp[], in reverse order */
446 tmp
[i
++] = hex_asc_upper
[num
] | locase
;
447 else if (spec
.base
!= 10) { /* 8 or 16 */
448 int mask
= spec
.base
- 1;
454 tmp
[i
++] = (hex_asc_upper
[((unsigned char)num
) & mask
] | locase
);
457 } else { /* base 10 */
458 i
= put_dec(tmp
, num
) - tmp
;
461 /* printing 100 using %2d gives "100", not "00" */
464 /* leading space padding */
465 field_width
-= precision
;
466 if (!(spec
.flags
& (ZEROPAD
| LEFT
))) {
467 while (--field_width
>= 0) {
479 /* "0x" / "0" prefix */
481 if (spec
.base
== 16 || !is_zero
) {
486 if (spec
.base
== 16) {
488 *buf
= ('X' | locase
);
492 /* zero or space padding */
493 if (!(spec
.flags
& LEFT
)) {
494 char c
= ' ' + (spec
.flags
& ZEROPAD
);
495 BUILD_BUG_ON(' ' + ZEROPAD
!= '0');
496 while (--field_width
>= 0) {
502 /* hmm even more zero padding? */
503 while (i
<= --precision
) {
508 /* actual digits of result */
514 /* trailing space padding */
515 while (--field_width
>= 0) {
524 static noinline_for_stack
525 char *special_hex_number(char *buf
, char *end
, unsigned long long num
, int size
)
527 struct printf_spec spec
;
529 spec
.type
= FORMAT_TYPE_PTR
;
530 spec
.field_width
= 2 + 2 * size
; /* 0x + hex */
531 spec
.flags
= SPECIAL
| SMALL
| ZEROPAD
;
535 return number(buf
, end
, num
, spec
);
538 static void move_right(char *buf
, char *end
, unsigned len
, unsigned spaces
)
541 if (buf
>= end
) /* nowhere to put anything */
544 if (size
<= spaces
) {
545 memset(buf
, ' ', size
);
549 if (len
> size
- spaces
)
551 memmove(buf
+ spaces
, buf
, len
);
553 memset(buf
, ' ', spaces
);
557 * Handle field width padding for a string.
558 * @buf: current buffer position
559 * @n: length of string
560 * @end: end of output buffer
561 * @spec: for field width and flags
562 * Returns: new buffer position after padding.
564 static noinline_for_stack
565 char *widen_string(char *buf
, int n
, char *end
, struct printf_spec spec
)
569 if (likely(n
>= spec
.field_width
))
571 /* we want to pad the sucker */
572 spaces
= spec
.field_width
- n
;
573 if (!(spec
.flags
& LEFT
)) {
574 move_right(buf
- n
, end
, n
, spaces
);
585 static noinline_for_stack
586 char *string(char *buf
, char *end
, const char *s
, struct printf_spec spec
)
589 size_t lim
= spec
.precision
;
591 if ((unsigned long)s
< PAGE_SIZE
)
603 return widen_string(buf
, len
, end
, spec
);
606 static noinline_for_stack
607 char *dentry_name(char *buf
, char *end
, const struct dentry
*d
, struct printf_spec spec
,
610 const char *array
[4], *s
;
611 const struct dentry
*p
;
616 case '2': case '3': case '4':
617 depth
= fmt
[1] - '0';
624 for (i
= 0; i
< depth
; i
++, d
= p
) {
625 p
= READ_ONCE(d
->d_parent
);
626 array
[i
] = READ_ONCE(d
->d_name
.name
);
635 for (n
= 0; n
!= spec
.precision
; n
++, buf
++) {
647 return widen_string(buf
, n
, end
, spec
);
651 static noinline_for_stack
652 char *bdev_name(char *buf
, char *end
, struct block_device
*bdev
,
653 struct printf_spec spec
, const char *fmt
)
655 struct gendisk
*hd
= bdev
->bd_disk
;
657 buf
= string(buf
, end
, hd
->disk_name
, spec
);
658 if (bdev
->bd_part
->partno
) {
659 if (isdigit(hd
->disk_name
[strlen(hd
->disk_name
)-1])) {
664 buf
= number(buf
, end
, bdev
->bd_part
->partno
, spec
);
670 static noinline_for_stack
671 char *symbol_string(char *buf
, char *end
, void *ptr
,
672 struct printf_spec spec
, const char *fmt
)
675 #ifdef CONFIG_KALLSYMS
676 char sym
[KSYM_SYMBOL_LEN
];
680 ptr
= __builtin_extract_return_addr(ptr
);
681 value
= (unsigned long)ptr
;
683 #ifdef CONFIG_KALLSYMS
685 sprint_backtrace(sym
, value
);
686 else if (*fmt
!= 'f' && *fmt
!= 's')
687 sprint_symbol(sym
, value
);
689 sprint_symbol_no_offset(sym
, value
);
691 return string(buf
, end
, sym
, spec
);
693 return special_hex_number(buf
, end
, value
, sizeof(void *));
697 static noinline_for_stack
698 char *resource_string(char *buf
, char *end
, struct resource
*res
,
699 struct printf_spec spec
, const char *fmt
)
701 #ifndef IO_RSRC_PRINTK_SIZE
702 #define IO_RSRC_PRINTK_SIZE 6
705 #ifndef MEM_RSRC_PRINTK_SIZE
706 #define MEM_RSRC_PRINTK_SIZE 10
708 static const struct printf_spec io_spec
= {
710 .field_width
= IO_RSRC_PRINTK_SIZE
,
712 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
714 static const struct printf_spec mem_spec
= {
716 .field_width
= MEM_RSRC_PRINTK_SIZE
,
718 .flags
= SPECIAL
| SMALL
| ZEROPAD
,
720 static const struct printf_spec bus_spec
= {
724 .flags
= SMALL
| ZEROPAD
,
726 static const struct printf_spec dec_spec
= {
731 static const struct printf_spec str_spec
= {
736 static const struct printf_spec flag_spec
= {
739 .flags
= SPECIAL
| SMALL
,
742 /* 32-bit res (sizeof==4): 10 chars in dec, 10 in hex ("0x" + 8)
743 * 64-bit res (sizeof==8): 20 chars in dec, 18 in hex ("0x" + 16) */
744 #define RSRC_BUF_SIZE ((2 * sizeof(resource_size_t)) + 4)
745 #define FLAG_BUF_SIZE (2 * sizeof(res->flags))
746 #define DECODED_BUF_SIZE sizeof("[mem - 64bit pref window disabled]")
747 #define RAW_BUF_SIZE sizeof("[mem - flags 0x]")
748 char sym
[max(2*RSRC_BUF_SIZE
+ DECODED_BUF_SIZE
,
749 2*RSRC_BUF_SIZE
+ FLAG_BUF_SIZE
+ RAW_BUF_SIZE
)];
751 char *p
= sym
, *pend
= sym
+ sizeof(sym
);
752 int decode
= (fmt
[0] == 'R') ? 1 : 0;
753 const struct printf_spec
*specp
;
756 if (res
->flags
& IORESOURCE_IO
) {
757 p
= string(p
, pend
, "io ", str_spec
);
759 } else if (res
->flags
& IORESOURCE_MEM
) {
760 p
= string(p
, pend
, "mem ", str_spec
);
762 } else if (res
->flags
& IORESOURCE_IRQ
) {
763 p
= string(p
, pend
, "irq ", str_spec
);
765 } else if (res
->flags
& IORESOURCE_DMA
) {
766 p
= string(p
, pend
, "dma ", str_spec
);
768 } else if (res
->flags
& IORESOURCE_BUS
) {
769 p
= string(p
, pend
, "bus ", str_spec
);
772 p
= string(p
, pend
, "??? ", str_spec
);
776 if (decode
&& res
->flags
& IORESOURCE_UNSET
) {
777 p
= string(p
, pend
, "size ", str_spec
);
778 p
= number(p
, pend
, resource_size(res
), *specp
);
780 p
= number(p
, pend
, res
->start
, *specp
);
781 if (res
->start
!= res
->end
) {
783 p
= number(p
, pend
, res
->end
, *specp
);
787 if (res
->flags
& IORESOURCE_MEM_64
)
788 p
= string(p
, pend
, " 64bit", str_spec
);
789 if (res
->flags
& IORESOURCE_PREFETCH
)
790 p
= string(p
, pend
, " pref", str_spec
);
791 if (res
->flags
& IORESOURCE_WINDOW
)
792 p
= string(p
, pend
, " window", str_spec
);
793 if (res
->flags
& IORESOURCE_DISABLED
)
794 p
= string(p
, pend
, " disabled", str_spec
);
796 p
= string(p
, pend
, " flags ", str_spec
);
797 p
= number(p
, pend
, res
->flags
, flag_spec
);
802 return string(buf
, end
, sym
, spec
);
805 static noinline_for_stack
806 char *hex_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
809 int i
, len
= 1; /* if we pass '%ph[CDN]', field width remains
810 negative value, fallback to the default */
813 if (spec
.field_width
== 0)
814 /* nothing to print */
817 if (ZERO_OR_NULL_PTR(addr
))
819 return string(buf
, end
, NULL
, spec
);
836 if (spec
.field_width
> 0)
837 len
= min_t(int, spec
.field_width
, 64);
839 for (i
= 0; i
< len
; ++i
) {
841 *buf
= hex_asc_hi(addr
[i
]);
844 *buf
= hex_asc_lo(addr
[i
]);
847 if (separator
&& i
!= len
- 1) {
857 static noinline_for_stack
858 char *bitmap_string(char *buf
, char *end
, unsigned long *bitmap
,
859 struct printf_spec spec
, const char *fmt
)
861 const int CHUNKSZ
= 32;
862 int nr_bits
= max_t(int, spec
.field_width
, 0);
866 /* reused to print numbers */
867 spec
= (struct printf_spec
){ .flags
= SMALL
| ZEROPAD
, .base
= 16 };
869 chunksz
= nr_bits
& (CHUNKSZ
- 1);
873 i
= ALIGN(nr_bits
, CHUNKSZ
) - CHUNKSZ
;
874 for (; i
>= 0; i
-= CHUNKSZ
) {
878 chunkmask
= ((1ULL << chunksz
) - 1);
879 word
= i
/ BITS_PER_LONG
;
880 bit
= i
% BITS_PER_LONG
;
881 val
= (bitmap
[word
] >> bit
) & chunkmask
;
890 spec
.field_width
= DIV_ROUND_UP(chunksz
, 4);
891 buf
= number(buf
, end
, val
, spec
);
898 static noinline_for_stack
899 char *bitmap_list_string(char *buf
, char *end
, unsigned long *bitmap
,
900 struct printf_spec spec
, const char *fmt
)
902 int nr_bits
= max_t(int, spec
.field_width
, 0);
903 /* current bit is 'cur', most recently seen range is [rbot, rtop] */
907 /* reused to print numbers */
908 spec
= (struct printf_spec
){ .base
= 10 };
910 rbot
= cur
= find_first_bit(bitmap
, nr_bits
);
911 while (cur
< nr_bits
) {
913 cur
= find_next_bit(bitmap
, nr_bits
, cur
+ 1);
914 if (cur
< nr_bits
&& cur
<= rtop
+ 1)
924 buf
= number(buf
, end
, rbot
, spec
);
930 buf
= number(buf
, end
, rtop
, spec
);
938 static noinline_for_stack
939 char *mac_address_string(char *buf
, char *end
, u8
*addr
,
940 struct printf_spec spec
, const char *fmt
)
942 char mac_addr
[sizeof("xx:xx:xx:xx:xx:xx")];
946 bool reversed
= false;
962 for (i
= 0; i
< 6; i
++) {
964 p
= hex_byte_pack(p
, addr
[5 - i
]);
966 p
= hex_byte_pack(p
, addr
[i
]);
968 if (fmt
[0] == 'M' && i
!= 5)
973 return string(buf
, end
, mac_addr
, spec
);
976 static noinline_for_stack
977 char *ip4_string(char *p
, const u8
*addr
, const char *fmt
)
980 bool leading_zeros
= (fmt
[0] == 'i');
1005 for (i
= 0; i
< 4; i
++) {
1006 char temp
[4] __aligned(2); /* hold each IP quad in reverse order */
1007 int digits
= put_dec_trunc8(temp
, addr
[index
]) - temp
;
1008 if (leading_zeros
) {
1014 /* reverse the digits in the quad */
1016 *p
++ = temp
[digits
];
1026 static noinline_for_stack
1027 char *ip6_compressed_string(char *p
, const char *addr
)
1030 unsigned char zerolength
[8];
1035 bool needcolon
= false;
1037 struct in6_addr in6
;
1039 memcpy(&in6
, addr
, sizeof(struct in6_addr
));
1041 useIPv4
= ipv6_addr_v4mapped(&in6
) || ipv6_addr_is_isatap(&in6
);
1043 memset(zerolength
, 0, sizeof(zerolength
));
1050 /* find position of longest 0 run */
1051 for (i
= 0; i
< range
; i
++) {
1052 for (j
= i
; j
< range
; j
++) {
1053 if (in6
.s6_addr16
[j
] != 0)
1058 for (i
= 0; i
< range
; i
++) {
1059 if (zerolength
[i
] > longest
) {
1060 longest
= zerolength
[i
];
1064 if (longest
== 1) /* don't compress a single 0 */
1068 for (i
= 0; i
< range
; i
++) {
1069 if (i
== colonpos
) {
1070 if (needcolon
|| i
== 0)
1081 /* hex u16 without leading 0s */
1082 word
= ntohs(in6
.s6_addr16
[i
]);
1087 p
= hex_byte_pack(p
, hi
);
1089 *p
++ = hex_asc_lo(hi
);
1090 p
= hex_byte_pack(p
, lo
);
1093 p
= hex_byte_pack(p
, lo
);
1095 *p
++ = hex_asc_lo(lo
);
1102 p
= ip4_string(p
, &in6
.s6_addr
[12], "I4");
1109 static noinline_for_stack
1110 char *ip6_string(char *p
, const char *addr
, const char *fmt
)
1114 for (i
= 0; i
< 8; i
++) {
1115 p
= hex_byte_pack(p
, *addr
++);
1116 p
= hex_byte_pack(p
, *addr
++);
1117 if (fmt
[0] == 'I' && i
!= 7)
1125 static noinline_for_stack
1126 char *ip6_addr_string(char *buf
, char *end
, const u8
*addr
,
1127 struct printf_spec spec
, const char *fmt
)
1129 char ip6_addr
[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255")];
1131 if (fmt
[0] == 'I' && fmt
[2] == 'c')
1132 ip6_compressed_string(ip6_addr
, addr
);
1134 ip6_string(ip6_addr
, addr
, fmt
);
1136 return string(buf
, end
, ip6_addr
, spec
);
1139 static noinline_for_stack
1140 char *ip4_addr_string(char *buf
, char *end
, const u8
*addr
,
1141 struct printf_spec spec
, const char *fmt
)
1143 char ip4_addr
[sizeof("255.255.255.255")];
1145 ip4_string(ip4_addr
, addr
, fmt
);
1147 return string(buf
, end
, ip4_addr
, spec
);
1150 static noinline_for_stack
1151 char *ip6_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in6
*sa
,
1152 struct printf_spec spec
, const char *fmt
)
1154 bool have_p
= false, have_s
= false, have_f
= false, have_c
= false;
1155 char ip6_addr
[sizeof("[xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:255.255.255.255]") +
1156 sizeof(":12345") + sizeof("/123456789") +
1157 sizeof("%1234567890")];
1158 char *p
= ip6_addr
, *pend
= ip6_addr
+ sizeof(ip6_addr
);
1159 const u8
*addr
= (const u8
*) &sa
->sin6_addr
;
1160 char fmt6
[2] = { fmt
[0], '6' };
1164 while (isalpha(*++fmt
)) {
1181 if (have_p
|| have_s
|| have_f
) {
1186 if (fmt6
[0] == 'I' && have_c
)
1187 p
= ip6_compressed_string(ip6_addr
+ off
, addr
);
1189 p
= ip6_string(ip6_addr
+ off
, addr
, fmt6
);
1191 if (have_p
|| have_s
|| have_f
)
1196 p
= number(p
, pend
, ntohs(sa
->sin6_port
), spec
);
1200 p
= number(p
, pend
, ntohl(sa
->sin6_flowinfo
&
1201 IPV6_FLOWINFO_MASK
), spec
);
1205 p
= number(p
, pend
, sa
->sin6_scope_id
, spec
);
1209 return string(buf
, end
, ip6_addr
, spec
);
1212 static noinline_for_stack
1213 char *ip4_addr_string_sa(char *buf
, char *end
, const struct sockaddr_in
*sa
,
1214 struct printf_spec spec
, const char *fmt
)
1216 bool have_p
= false;
1217 char *p
, ip4_addr
[sizeof("255.255.255.255") + sizeof(":12345")];
1218 char *pend
= ip4_addr
+ sizeof(ip4_addr
);
1219 const u8
*addr
= (const u8
*) &sa
->sin_addr
.s_addr
;
1220 char fmt4
[3] = { fmt
[0], '4', 0 };
1223 while (isalpha(*++fmt
)) {
1237 p
= ip4_string(ip4_addr
, addr
, fmt4
);
1240 p
= number(p
, pend
, ntohs(sa
->sin_port
), spec
);
1244 return string(buf
, end
, ip4_addr
, spec
);
1247 static noinline_for_stack
1248 char *escaped_string(char *buf
, char *end
, u8
*addr
, struct printf_spec spec
,
1253 unsigned int flags
= 0;
1256 if (spec
.field_width
== 0)
1257 return buf
; /* nothing to print */
1259 if (ZERO_OR_NULL_PTR(addr
))
1260 return string(buf
, end
, NULL
, spec
); /* NULL pointer */
1264 switch (fmt
[count
++]) {
1266 flags
|= ESCAPE_ANY
;
1269 flags
|= ESCAPE_SPECIAL
;
1272 flags
|= ESCAPE_HEX
;
1275 flags
|= ESCAPE_NULL
;
1278 flags
|= ESCAPE_OCTAL
;
1284 flags
|= ESCAPE_SPACE
;
1293 flags
= ESCAPE_ANY_NP
;
1295 len
= spec
.field_width
< 0 ? 1 : spec
.field_width
;
1298 * string_escape_mem() writes as many characters as it can to
1299 * the given buffer, and returns the total size of the output
1300 * had the buffer been big enough.
1302 buf
+= string_escape_mem(addr
, len
, buf
, buf
< end
? end
- buf
: 0, flags
, NULL
);
1307 static noinline_for_stack
1308 char *uuid_string(char *buf
, char *end
, const u8
*addr
,
1309 struct printf_spec spec
, const char *fmt
)
1311 char uuid
[UUID_STRING_LEN
+ 1];
1314 const u8
*index
= uuid_index
;
1319 uc
= true; /* fall-through */
1328 for (i
= 0; i
< 16; i
++) {
1330 p
= hex_byte_pack_upper(p
, addr
[index
[i
]]);
1332 p
= hex_byte_pack(p
, addr
[index
[i
]]);
1345 return string(buf
, end
, uuid
, spec
);
1348 int kptr_restrict __read_mostly
;
1350 static noinline_for_stack
1351 char *restricted_pointer(char *buf
, char *end
, const void *ptr
,
1352 struct printf_spec spec
)
1355 spec
.flags
|= SMALL
;
1356 if (spec
.field_width
== -1) {
1357 spec
.field_width
= 2 * sizeof(ptr
);
1358 spec
.flags
|= ZEROPAD
;
1361 switch (kptr_restrict
) {
1363 /* Always print %pK values */
1366 const struct cred
*cred
;
1369 * kptr_restrict==1 cannot be used in IRQ context
1370 * because its test for CAP_SYSLOG would be meaningless.
1372 if (in_irq() || in_serving_softirq() || in_nmi())
1373 return string(buf
, end
, "pK-error", spec
);
1376 * Only print the real pointer value if the current
1377 * process has CAP_SYSLOG and is running with the
1378 * same credentials it started with. This is because
1379 * access to files is checked at open() time, but %pK
1380 * checks permission at read() time. We don't want to
1381 * leak pointer values if a binary opens a file using
1382 * %pK and then elevates privileges before reading it.
1384 cred
= current_cred();
1385 if (!has_capability_noaudit(current
, CAP_SYSLOG
) ||
1386 !uid_eq(cred
->euid
, cred
->uid
) ||
1387 !gid_eq(cred
->egid
, cred
->gid
))
1393 /* Always print 0's for %pK */
1398 return number(buf
, end
, (unsigned long)ptr
, spec
);
1401 static noinline_for_stack
1402 char *netdev_bits(char *buf
, char *end
, const void *addr
, const char *fmt
)
1404 unsigned long long num
;
1409 num
= *(const netdev_features_t
*)addr
;
1410 size
= sizeof(netdev_features_t
);
1413 num
= (unsigned long)addr
;
1414 size
= sizeof(unsigned long);
1418 return special_hex_number(buf
, end
, num
, size
);
1421 static noinline_for_stack
1422 char *address_val(char *buf
, char *end
, const void *addr
, const char *fmt
)
1424 unsigned long long num
;
1429 num
= *(const dma_addr_t
*)addr
;
1430 size
= sizeof(dma_addr_t
);
1434 num
= *(const phys_addr_t
*)addr
;
1435 size
= sizeof(phys_addr_t
);
1439 return special_hex_number(buf
, end
, num
, size
);
1442 static noinline_for_stack
1443 char *clock(char *buf
, char *end
, struct clk
*clk
, struct printf_spec spec
,
1446 if (!IS_ENABLED(CONFIG_HAVE_CLK
) || !clk
)
1447 return string(buf
, end
, NULL
, spec
);
1452 #ifdef CONFIG_COMMON_CLK
1453 return string(buf
, end
, __clk_get_name(clk
), spec
);
1455 return special_hex_number(buf
, end
, (unsigned long)clk
, sizeof(unsigned long));
1461 char *format_flags(char *buf
, char *end
, unsigned long flags
,
1462 const struct trace_print_flags
*names
)
1465 const struct printf_spec strspec
= {
1469 const struct printf_spec numspec
= {
1470 .flags
= SPECIAL
|SMALL
,
1476 for ( ; flags
&& names
->name
; names
++) {
1478 if ((flags
& mask
) != mask
)
1481 buf
= string(buf
, end
, names
->name
, strspec
);
1492 buf
= number(buf
, end
, flags
, numspec
);
1497 static noinline_for_stack
1498 char *flags_string(char *buf
, char *end
, void *flags_ptr
, const char *fmt
)
1500 unsigned long flags
;
1501 const struct trace_print_flags
*names
;
1505 flags
= *(unsigned long *)flags_ptr
;
1506 /* Remove zone id */
1507 flags
&= (1UL << NR_PAGEFLAGS
) - 1;
1508 names
= pageflag_names
;
1511 flags
= *(unsigned long *)flags_ptr
;
1512 names
= vmaflag_names
;
1515 flags
= *(gfp_t
*)flags_ptr
;
1516 names
= gfpflag_names
;
1519 WARN_ONCE(1, "Unsupported flags modifier: %c\n", fmt
[1]);
1523 return format_flags(buf
, end
, flags
, names
);
1526 static const char *device_node_name_for_depth(const struct device_node
*np
, int depth
)
1528 for ( ; np
&& depth
; depth
--)
1531 return kbasename(np
->full_name
);
1534 static noinline_for_stack
1535 char *device_node_gen_full_name(const struct device_node
*np
, char *buf
, char *end
)
1538 const struct device_node
*parent
= np
->parent
;
1539 static const struct printf_spec strspec
= {
1544 /* special case for root node */
1546 return string(buf
, end
, "/", strspec
);
1548 for (depth
= 0; parent
->parent
; depth
++)
1549 parent
= parent
->parent
;
1551 for ( ; depth
>= 0; depth
--) {
1552 buf
= string(buf
, end
, "/", strspec
);
1553 buf
= string(buf
, end
, device_node_name_for_depth(np
, depth
),
1559 static noinline_for_stack
1560 char *device_node_string(char *buf
, char *end
, struct device_node
*dn
,
1561 struct printf_spec spec
, const char *fmt
)
1563 char tbuf
[sizeof("xxxx") + 1];
1566 char *buf_start
= buf
;
1567 struct property
*prop
;
1568 bool has_mult
, pass
;
1569 static const struct printf_spec num_spec
= {
1576 struct printf_spec str_spec
= spec
;
1577 str_spec
.field_width
= -1;
1579 if (!IS_ENABLED(CONFIG_OF
))
1580 return string(buf
, end
, "(!OF)", spec
);
1582 if ((unsigned long)dn
< PAGE_SIZE
)
1583 return string(buf
, end
, "(null)", spec
);
1585 /* simple case without anything any more format specifiers */
1587 if (fmt
[0] == '\0' || strcspn(fmt
,"fnpPFcC") > 0)
1590 for (pass
= false; strspn(fmt
,"fnpPFcC"); fmt
++, pass
= true) {
1598 case 'f': /* full_name */
1599 buf
= device_node_gen_full_name(dn
, buf
, end
);
1601 case 'n': /* name */
1602 buf
= string(buf
, end
, dn
->name
, str_spec
);
1604 case 'p': /* phandle */
1605 buf
= number(buf
, end
, (unsigned int)dn
->phandle
, num_spec
);
1607 case 'P': /* path-spec */
1608 p
= kbasename(of_node_full_name(dn
));
1611 buf
= string(buf
, end
, p
, str_spec
);
1613 case 'F': /* flags */
1614 tbuf
[0] = of_node_check_flag(dn
, OF_DYNAMIC
) ? 'D' : '-';
1615 tbuf
[1] = of_node_check_flag(dn
, OF_DETACHED
) ? 'd' : '-';
1616 tbuf
[2] = of_node_check_flag(dn
, OF_POPULATED
) ? 'P' : '-';
1617 tbuf
[3] = of_node_check_flag(dn
, OF_POPULATED_BUS
) ? 'B' : '-';
1619 buf
= string(buf
, end
, tbuf
, str_spec
);
1621 case 'c': /* major compatible string */
1622 ret
= of_property_read_string(dn
, "compatible", &p
);
1624 buf
= string(buf
, end
, p
, str_spec
);
1626 case 'C': /* full compatible string */
1628 of_property_for_each_string(dn
, "compatible", prop
, p
) {
1630 buf
= string(buf
, end
, ",", str_spec
);
1631 buf
= string(buf
, end
, "\"", str_spec
);
1632 buf
= string(buf
, end
, p
, str_spec
);
1633 buf
= string(buf
, end
, "\"", str_spec
);
1643 return widen_string(buf
, buf
- buf_start
, end
, spec
);
1646 static noinline_for_stack
1647 char *pointer_string(char *buf
, char *end
, const void *ptr
,
1648 struct printf_spec spec
)
1651 spec
.flags
|= SMALL
;
1652 if (spec
.field_width
== -1) {
1653 spec
.field_width
= 2 * sizeof(ptr
);
1654 spec
.flags
|= ZEROPAD
;
1657 return number(buf
, end
, (unsigned long int)ptr
, spec
);
1660 static DEFINE_STATIC_KEY_TRUE(not_filled_random_ptr_key
);
1661 static siphash_key_t ptr_key __read_mostly
;
1663 static void enable_ptr_key_workfn(struct work_struct
*work
)
1665 get_random_bytes(&ptr_key
, sizeof(ptr_key
));
1666 /* Needs to run from preemptible context */
1667 static_branch_disable(¬_filled_random_ptr_key
);
1670 static DECLARE_WORK(enable_ptr_key_work
, enable_ptr_key_workfn
);
1672 static void fill_random_ptr_key(struct random_ready_callback
*unused
)
1674 /* This may be in an interrupt handler. */
1675 queue_work(system_unbound_wq
, &enable_ptr_key_work
);
1678 static struct random_ready_callback random_ready
= {
1679 .func
= fill_random_ptr_key
1682 static int __init
initialize_ptr_random(void)
1684 int ret
= add_random_ready_callback(&random_ready
);
1688 } else if (ret
== -EALREADY
) {
1689 /* This is in preemptible context */
1690 enable_ptr_key_workfn(&enable_ptr_key_work
);
1696 early_initcall(initialize_ptr_random
);
1698 /* Maps a pointer to a 32 bit unique identifier. */
1699 static char *ptr_to_id(char *buf
, char *end
, void *ptr
, struct printf_spec spec
)
1701 unsigned long hashval
;
1702 const int default_width
= 2 * sizeof(ptr
);
1704 if (static_branch_unlikely(¬_filled_random_ptr_key
)) {
1705 spec
.field_width
= default_width
;
1706 /* string length must be less than default_width */
1707 return string(buf
, end
, "(ptrval)", spec
);
1711 hashval
= (unsigned long)siphash_1u64((u64
)ptr
, &ptr_key
);
1713 * Mask off the first 32 bits, this makes explicit that we have
1714 * modified the address (and 32 bits is plenty for a unique ID).
1716 hashval
= hashval
& 0xffffffff;
1718 hashval
= (unsigned long)siphash_1u32((u32
)ptr
, &ptr_key
);
1721 spec
.flags
|= SMALL
;
1722 if (spec
.field_width
== -1) {
1723 spec
.field_width
= default_width
;
1724 spec
.flags
|= ZEROPAD
;
1728 return number(buf
, end
, hashval
, spec
);
1731 #ifdef CONFIG_KMSG_IDS
1733 unsigned long long __jhash_string(const char *str
);
1735 static noinline_for_stack
1736 char *jhash_string(char *buf
, char *end
, const char *str
, const char *fmt
)
1738 struct printf_spec spec
;
1739 unsigned long long num
;
1741 num
= __jhash_string(str
);
1743 spec
.type
= FORMAT_TYPE_PTR
;
1744 spec
.field_width
= 6;
1745 spec
.flags
= SMALL
| ZEROPAD
;
1747 spec
.precision
= -1;
1749 return number(buf
, end
, num
, spec
);
1755 * Show a '%p' thing. A kernel extension is that the '%p' is followed
1756 * by an extra set of alphanumeric characters that are extended format
1759 * Please update scripts/checkpatch.pl when adding/removing conversion
1760 * characters. (Search for "check for vsprintf extension").
1762 * Right now we handle:
1764 * - 'F' For symbolic function descriptor pointers with offset
1765 * - 'f' For simple symbolic function names without offset
1766 * - 'S' For symbolic direct pointers with offset
1767 * - 's' For symbolic direct pointers without offset
1768 * - '[FfSs]R' as above with __builtin_extract_return_addr() translation
1769 * - 'B' For backtraced symbolic direct pointers with offset
1770 * - 'R' For decoded struct resource, e.g., [mem 0x0-0x1f 64bit pref]
1771 * - 'r' For raw struct resource, e.g., [mem 0x0-0x1f flags 0x201]
1772 * - 'b[l]' For a bitmap, the number of bits is determined by the field
1773 * width which must be explicitly specified either as part of the
1774 * format string '%32b[l]' or through '%*b[l]', [l] selects
1775 * range-list format instead of hex format
1776 * - 'M' For a 6-byte MAC address, it prints the address in the
1777 * usual colon-separated hex notation
1778 * - 'm' For a 6-byte MAC address, it prints the hex address without colons
1779 * - 'MF' For a 6-byte MAC FDDI address, it prints the address
1780 * with a dash-separated hex notation
1781 * - '[mM]R' For a 6-byte MAC address, Reverse order (Bluetooth)
1782 * - 'I' [46] for IPv4/IPv6 addresses printed in the usual way
1783 * IPv4 uses dot-separated decimal without leading 0's (1.2.3.4)
1784 * IPv6 uses colon separated network-order 16 bit hex with leading 0's
1786 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1787 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1788 * - 'i' [46] for 'raw' IPv4/IPv6 addresses
1789 * IPv6 omits the colons (01020304...0f)
1790 * IPv4 uses dot-separated decimal with leading 0's (010.123.045.006)
1792 * Generic IPv4/IPv6 address (struct sockaddr *) that falls back to
1793 * [4] or [6] and is able to print port [p], flowinfo [f], scope [s]
1794 * - '[Ii][4S][hnbl]' IPv4 addresses in host, network, big or little endian order
1795 * - 'I[6S]c' for IPv6 addresses printed as specified by
1796 * http://tools.ietf.org/html/rfc5952
1797 * - 'E[achnops]' For an escaped buffer, where rules are defined by combination
1798 * of the following flags (see string_escape_mem() for the
1801 * c - ESCAPE_SPECIAL
1807 * By default ESCAPE_ANY_NP is used.
1808 * - 'U' For a 16 byte UUID/GUID, it prints the UUID/GUID in the form
1809 * "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
1810 * Options for %pU are:
1811 * b big endian lower case hex (default)
1812 * B big endian UPPER case hex
1813 * l little endian lower case hex
1814 * L little endian UPPER case hex
1815 * big endian output byte order is:
1816 * [0][1][2][3]-[4][5]-[6][7]-[8][9]-[10][11][12][13][14][15]
1817 * little endian output byte order is:
1818 * [3][2][1][0]-[5][4]-[7][6]-[8][9]-[10][11][12][13][14][15]
1819 * - 'V' For a struct va_format which contains a format string * and va_list *,
1820 * call vsnprintf(->format, *->va_list).
1821 * Implements a "recursive vsnprintf".
1822 * Do not use this feature without some mechanism to verify the
1823 * correctness of the format string and va_list arguments.
1824 * - 'K' For a kernel pointer that should be hidden from unprivileged users
1825 * - 'NF' For a netdev_features_t
1826 * - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1827 * a certain separator (' ' by default):
1831 * The maximum supported length is 64 bytes of the input. Consider
1832 * to use print_hex_dump() for the larger input.
1833 * - 'a[pd]' For address types [p] phys_addr_t, [d] dma_addr_t and derivatives
1834 * (default assumed to be phys_addr_t, passed by reference)
1835 * - 'd[234]' For a dentry name (optionally 2-4 last components)
1836 * - 'D[234]' Same as 'd' but for a struct file
1837 * - 'g' For block_device name (gendisk + partition number)
1838 * - 'C' For a clock, it prints the name (Common Clock Framework) or address
1839 * (legacy clock framework) of the clock
1840 * - 'Cn' For a clock, it prints the name (Common Clock Framework) or address
1841 * (legacy clock framework) of the clock
1842 * - 'Cr' For a clock, it prints the current rate of the clock
1843 * - 'G' For flags to be printed as a collection of symbolic strings that would
1844 * construct the specific value. Supported flags given by option:
1845 * p page flags (see struct page) given as pointer to unsigned long
1846 * g gfp flags (GFP_* and __GFP_*) given as pointer to gfp_t
1847 * v vma flags (VM_*) given as pointer to unsigned long
1848 * - 'j' Kernel message catalog jhash for System z
1849 * - 'O' For a kobject based struct. Must be one of the following:
1850 * - 'OF[fnpPcCF]' For a device tree object
1851 * Without any optional arguments prints the full_name
1852 * f device node full_name
1853 * n device node name
1854 * p device node phandle
1855 * P device node path spec (name + @unit)
1856 * F device node flags
1857 * c major compatible string
1858 * C full compatible string
1860 * - 'x' For printing the address. Equivalent to "%lx".
1862 * ** Please update also Documentation/printk-formats.txt when making changes **
1864 * Note: The difference between 'S' and 'F' is that on ia64 and ppc64
1865 * function pointers are really function descriptors, which contain a
1866 * pointer to the real address.
1868 * Note: The default behaviour (unadorned %p) is to hash the address,
1869 * rendering it useful as a unique identifier.
1871 static noinline_for_stack
1872 char *pointer(const char *fmt
, char *buf
, char *end
, void *ptr
,
1873 struct printf_spec spec
)
1875 const int default_width
= 2 * sizeof(void *);
1877 if (!ptr
&& *fmt
!= 'K' && *fmt
!= 'x') {
1879 * Print (null) with the same width as a pointer so it makes
1880 * tabular output look nice.
1882 if (spec
.field_width
== -1)
1883 spec
.field_width
= default_width
;
1884 return string(buf
, end
, "(null)", spec
);
1890 ptr
= dereference_function_descriptor(ptr
);
1895 return symbol_string(buf
, end
, ptr
, spec
, fmt
);
1898 return resource_string(buf
, end
, ptr
, spec
, fmt
);
1900 return hex_string(buf
, end
, ptr
, spec
, fmt
);
1904 return bitmap_list_string(buf
, end
, ptr
, spec
, fmt
);
1906 return bitmap_string(buf
, end
, ptr
, spec
, fmt
);
1908 case 'M': /* Colon separated: 00:01:02:03:04:05 */
1909 case 'm': /* Contiguous: 000102030405 */
1911 /* [mM]R (Reverse order; Bluetooth) */
1912 return mac_address_string(buf
, end
, ptr
, spec
, fmt
);
1913 case 'I': /* Formatted IP supported
1915 * 6: 0001:0203:...:0708
1916 * 6c: 1::708 or 1::1.2.3.4
1918 case 'i': /* Contiguous:
1919 * 4: 001.002.003.004
1924 return ip6_addr_string(buf
, end
, ptr
, spec
, fmt
);
1926 return ip4_addr_string(buf
, end
, ptr
, spec
, fmt
);
1929 struct sockaddr raw
;
1930 struct sockaddr_in v4
;
1931 struct sockaddr_in6 v6
;
1934 switch (sa
->raw
.sa_family
) {
1936 return ip4_addr_string_sa(buf
, end
, &sa
->v4
, spec
, fmt
);
1938 return ip6_addr_string_sa(buf
, end
, &sa
->v6
, spec
, fmt
);
1940 return string(buf
, end
, "(invalid address)", spec
);
1945 return escaped_string(buf
, end
, ptr
, spec
, fmt
);
1947 return uuid_string(buf
, end
, ptr
, spec
, fmt
);
1952 va_copy(va
, *((struct va_format
*)ptr
)->va
);
1953 buf
+= vsnprintf(buf
, end
> buf
? end
- buf
: 0,
1954 ((struct va_format
*)ptr
)->fmt
, va
);
1961 return restricted_pointer(buf
, end
, ptr
, spec
);
1963 return netdev_bits(buf
, end
, ptr
, fmt
);
1965 return address_val(buf
, end
, ptr
, fmt
);
1967 return dentry_name(buf
, end
, ptr
, spec
, fmt
);
1969 return clock(buf
, end
, ptr
, spec
, fmt
);
1971 return dentry_name(buf
, end
,
1972 ((const struct file
*)ptr
)->f_path
.dentry
,
1976 return bdev_name(buf
, end
, ptr
, spec
, fmt
);
1980 return flags_string(buf
, end
, ptr
, fmt
);
1984 return device_node_string(buf
, end
, ptr
, spec
, fmt
+ 1);
1988 return pointer_string(buf
, end
, ptr
, spec
);
1989 #ifdef CONFIG_KMSG_IDS
1991 return jhash_string(buf
, end
, ptr
, fmt
);
1995 /* default is to _not_ leak addresses, hash before printing */
1996 return ptr_to_id(buf
, end
, ptr
, spec
);
2000 * Helper function to decode printf style format.
2001 * Each call decode a token from the format and return the
2002 * number of characters read (or likely the delta where it wants
2003 * to go on the next call).
2004 * The decoded token is returned through the parameters
2006 * 'h', 'l', or 'L' for integer fields
2007 * 'z' support added 23/7/1999 S.H.
2008 * 'z' changed to 'Z' --davidm 1/25/99
2009 * 'Z' changed to 'z' --adobriyan 2017-01-25
2010 * 't' added for ptrdiff_t
2012 * @fmt: the format string
2013 * @type of the token returned
2014 * @flags: various flags such as +, -, # tokens..
2015 * @field_width: overwritten width
2016 * @base: base of the number (octal, hex, ...)
2017 * @precision: precision of a number
2018 * @qualifier: qualifier of a number (long, size_t, ...)
2020 static noinline_for_stack
2021 int format_decode(const char *fmt
, struct printf_spec
*spec
)
2023 const char *start
= fmt
;
2026 /* we finished early by reading the field width */
2027 if (spec
->type
== FORMAT_TYPE_WIDTH
) {
2028 if (spec
->field_width
< 0) {
2029 spec
->field_width
= -spec
->field_width
;
2030 spec
->flags
|= LEFT
;
2032 spec
->type
= FORMAT_TYPE_NONE
;
2036 /* we finished early by reading the precision */
2037 if (spec
->type
== FORMAT_TYPE_PRECISION
) {
2038 if (spec
->precision
< 0)
2039 spec
->precision
= 0;
2041 spec
->type
= FORMAT_TYPE_NONE
;
2046 spec
->type
= FORMAT_TYPE_NONE
;
2048 for (; *fmt
; ++fmt
) {
2053 /* Return the current non-format string */
2054 if (fmt
!= start
|| !*fmt
)
2060 while (1) { /* this also skips first '%' */
2066 case '-': spec
->flags
|= LEFT
; break;
2067 case '+': spec
->flags
|= PLUS
; break;
2068 case ' ': spec
->flags
|= SPACE
; break;
2069 case '#': spec
->flags
|= SPECIAL
; break;
2070 case '0': spec
->flags
|= ZEROPAD
; break;
2071 default: found
= false;
2078 /* get field width */
2079 spec
->field_width
= -1;
2082 spec
->field_width
= skip_atoi(&fmt
);
2083 else if (*fmt
== '*') {
2084 /* it's the next argument */
2085 spec
->type
= FORMAT_TYPE_WIDTH
;
2086 return ++fmt
- start
;
2090 /* get the precision */
2091 spec
->precision
= -1;
2094 if (isdigit(*fmt
)) {
2095 spec
->precision
= skip_atoi(&fmt
);
2096 if (spec
->precision
< 0)
2097 spec
->precision
= 0;
2098 } else if (*fmt
== '*') {
2099 /* it's the next argument */
2100 spec
->type
= FORMAT_TYPE_PRECISION
;
2101 return ++fmt
- start
;
2106 /* get the conversion qualifier */
2108 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2109 *fmt
== 'z' || *fmt
== 't') {
2111 if (unlikely(qualifier
== *fmt
)) {
2112 if (qualifier
== 'l') {
2115 } else if (qualifier
== 'h') {
2126 spec
->type
= FORMAT_TYPE_CHAR
;
2127 return ++fmt
- start
;
2130 spec
->type
= FORMAT_TYPE_STR
;
2131 return ++fmt
- start
;
2134 spec
->type
= FORMAT_TYPE_PTR
;
2135 return ++fmt
- start
;
2138 spec
->type
= FORMAT_TYPE_PERCENT_CHAR
;
2139 return ++fmt
- start
;
2141 /* integer number formats - set up the flags and "break" */
2147 spec
->flags
|= SMALL
;
2155 spec
->flags
|= SIGN
;
2161 * Since %n poses a greater security risk than
2162 * utility, treat it as any other invalid or
2163 * unsupported format specifier.
2168 WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt
);
2169 spec
->type
= FORMAT_TYPE_INVALID
;
2173 if (qualifier
== 'L')
2174 spec
->type
= FORMAT_TYPE_LONG_LONG
;
2175 else if (qualifier
== 'l') {
2176 BUILD_BUG_ON(FORMAT_TYPE_ULONG
+ SIGN
!= FORMAT_TYPE_LONG
);
2177 spec
->type
= FORMAT_TYPE_ULONG
+ (spec
->flags
& SIGN
);
2178 } else if (qualifier
== 'z') {
2179 spec
->type
= FORMAT_TYPE_SIZE_T
;
2180 } else if (qualifier
== 't') {
2181 spec
->type
= FORMAT_TYPE_PTRDIFF
;
2182 } else if (qualifier
== 'H') {
2183 BUILD_BUG_ON(FORMAT_TYPE_UBYTE
+ SIGN
!= FORMAT_TYPE_BYTE
);
2184 spec
->type
= FORMAT_TYPE_UBYTE
+ (spec
->flags
& SIGN
);
2185 } else if (qualifier
== 'h') {
2186 BUILD_BUG_ON(FORMAT_TYPE_USHORT
+ SIGN
!= FORMAT_TYPE_SHORT
);
2187 spec
->type
= FORMAT_TYPE_USHORT
+ (spec
->flags
& SIGN
);
2189 BUILD_BUG_ON(FORMAT_TYPE_UINT
+ SIGN
!= FORMAT_TYPE_INT
);
2190 spec
->type
= FORMAT_TYPE_UINT
+ (spec
->flags
& SIGN
);
2193 return ++fmt
- start
;
2197 set_field_width(struct printf_spec
*spec
, int width
)
2199 spec
->field_width
= width
;
2200 if (WARN_ONCE(spec
->field_width
!= width
, "field width %d too large", width
)) {
2201 spec
->field_width
= clamp(width
, -FIELD_WIDTH_MAX
, FIELD_WIDTH_MAX
);
2206 set_precision(struct printf_spec
*spec
, int prec
)
2208 spec
->precision
= prec
;
2209 if (WARN_ONCE(spec
->precision
!= prec
, "precision %d too large", prec
)) {
2210 spec
->precision
= clamp(prec
, 0, PRECISION_MAX
);
2215 * vsnprintf - Format a string and place it in a buffer
2216 * @buf: The buffer to place the result into
2217 * @size: The size of the buffer, including the trailing null space
2218 * @fmt: The format string to use
2219 * @args: Arguments for the format string
2221 * This function generally follows C99 vsnprintf, but has some
2222 * extensions and a few limitations:
2224 * - ``%n`` is unsupported
2225 * - ``%p*`` is handled by pointer()
2227 * See pointer() or Documentation/printk-formats.txt for more
2228 * extensive description.
2230 * **Please update the documentation in both places when making changes**
2232 * The return value is the number of characters which would
2233 * be generated for the given input, excluding the trailing
2234 * '\0', as per ISO C99. If you want to have the exact
2235 * number of characters written into @buf as return value
2236 * (not including the trailing '\0'), use vscnprintf(). If the
2237 * return is greater than or equal to @size, the resulting
2238 * string is truncated.
2240 * If you're not already dealing with a va_list consider using snprintf().
2242 int vsnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2244 unsigned long long num
;
2246 struct printf_spec spec
= {0};
2248 /* Reject out-of-range values early. Large positive sizes are
2249 used for unknown buffer sizes. */
2250 if (WARN_ON_ONCE(size
> INT_MAX
))
2256 /* Make sure end is always >= buf */
2263 const char *old_fmt
= fmt
;
2264 int read
= format_decode(fmt
, &spec
);
2268 switch (spec
.type
) {
2269 case FORMAT_TYPE_NONE
: {
2272 if (copy
> end
- str
)
2274 memcpy(str
, old_fmt
, copy
);
2280 case FORMAT_TYPE_WIDTH
:
2281 set_field_width(&spec
, va_arg(args
, int));
2284 case FORMAT_TYPE_PRECISION
:
2285 set_precision(&spec
, va_arg(args
, int));
2288 case FORMAT_TYPE_CHAR
: {
2291 if (!(spec
.flags
& LEFT
)) {
2292 while (--spec
.field_width
> 0) {
2299 c
= (unsigned char) va_arg(args
, int);
2303 while (--spec
.field_width
> 0) {
2311 case FORMAT_TYPE_STR
:
2312 str
= string(str
, end
, va_arg(args
, char *), spec
);
2315 case FORMAT_TYPE_PTR
:
2316 str
= pointer(fmt
, str
, end
, va_arg(args
, void *),
2318 while (isalnum(*fmt
))
2322 case FORMAT_TYPE_PERCENT_CHAR
:
2328 case FORMAT_TYPE_INVALID
:
2330 * Presumably the arguments passed gcc's type
2331 * checking, but there is no safe or sane way
2332 * for us to continue parsing the format and
2333 * fetching from the va_list; the remaining
2334 * specifiers and arguments would be out of
2340 switch (spec
.type
) {
2341 case FORMAT_TYPE_LONG_LONG
:
2342 num
= va_arg(args
, long long);
2344 case FORMAT_TYPE_ULONG
:
2345 num
= va_arg(args
, unsigned long);
2347 case FORMAT_TYPE_LONG
:
2348 num
= va_arg(args
, long);
2350 case FORMAT_TYPE_SIZE_T
:
2351 if (spec
.flags
& SIGN
)
2352 num
= va_arg(args
, ssize_t
);
2354 num
= va_arg(args
, size_t);
2356 case FORMAT_TYPE_PTRDIFF
:
2357 num
= va_arg(args
, ptrdiff_t);
2359 case FORMAT_TYPE_UBYTE
:
2360 num
= (unsigned char) va_arg(args
, int);
2362 case FORMAT_TYPE_BYTE
:
2363 num
= (signed char) va_arg(args
, int);
2365 case FORMAT_TYPE_USHORT
:
2366 num
= (unsigned short) va_arg(args
, int);
2368 case FORMAT_TYPE_SHORT
:
2369 num
= (short) va_arg(args
, int);
2371 case FORMAT_TYPE_INT
:
2372 num
= (int) va_arg(args
, int);
2375 num
= va_arg(args
, unsigned int);
2378 str
= number(str
, end
, num
, spec
);
2390 /* the trailing null byte doesn't count towards the total */
2394 EXPORT_SYMBOL(vsnprintf
);
2397 * vscnprintf - Format a string and place it in a buffer
2398 * @buf: The buffer to place the result into
2399 * @size: The size of the buffer, including the trailing null space
2400 * @fmt: The format string to use
2401 * @args: Arguments for the format string
2403 * The return value is the number of characters which have been written into
2404 * the @buf not including the trailing '\0'. If @size is == 0 the function
2407 * If you're not already dealing with a va_list consider using scnprintf().
2409 * See the vsnprintf() documentation for format string extensions over C99.
2411 int vscnprintf(char *buf
, size_t size
, const char *fmt
, va_list args
)
2415 i
= vsnprintf(buf
, size
, fmt
, args
);
2417 if (likely(i
< size
))
2423 EXPORT_SYMBOL(vscnprintf
);
2426 * snprintf - Format a string and place it in a buffer
2427 * @buf: The buffer to place the result into
2428 * @size: The size of the buffer, including the trailing null space
2429 * @fmt: The format string to use
2430 * @...: Arguments for the format string
2432 * The return value is the number of characters which would be
2433 * generated for the given input, excluding the trailing null,
2434 * as per ISO C99. If the return is greater than or equal to
2435 * @size, the resulting string is truncated.
2437 * See the vsnprintf() documentation for format string extensions over C99.
2439 int snprintf(char *buf
, size_t size
, const char *fmt
, ...)
2444 va_start(args
, fmt
);
2445 i
= vsnprintf(buf
, size
, fmt
, args
);
2450 EXPORT_SYMBOL(snprintf
);
2453 * scnprintf - Format a string and place it in a buffer
2454 * @buf: The buffer to place the result into
2455 * @size: The size of the buffer, including the trailing null space
2456 * @fmt: The format string to use
2457 * @...: Arguments for the format string
2459 * The return value is the number of characters written into @buf not including
2460 * the trailing '\0'. If @size is == 0 the function returns 0.
2463 int scnprintf(char *buf
, size_t size
, const char *fmt
, ...)
2468 va_start(args
, fmt
);
2469 i
= vscnprintf(buf
, size
, fmt
, args
);
2474 EXPORT_SYMBOL(scnprintf
);
2477 * vsprintf - Format a string and place it in a buffer
2478 * @buf: The buffer to place the result into
2479 * @fmt: The format string to use
2480 * @args: Arguments for the format string
2482 * The function returns the number of characters written
2483 * into @buf. Use vsnprintf() or vscnprintf() in order to avoid
2486 * If you're not already dealing with a va_list consider using sprintf().
2488 * See the vsnprintf() documentation for format string extensions over C99.
2490 int vsprintf(char *buf
, const char *fmt
, va_list args
)
2492 return vsnprintf(buf
, INT_MAX
, fmt
, args
);
2494 EXPORT_SYMBOL(vsprintf
);
2497 * sprintf - Format a string and place it in a buffer
2498 * @buf: The buffer to place the result into
2499 * @fmt: The format string to use
2500 * @...: Arguments for the format string
2502 * The function returns the number of characters written
2503 * into @buf. Use snprintf() or scnprintf() in order to avoid
2506 * See the vsnprintf() documentation for format string extensions over C99.
2508 int sprintf(char *buf
, const char *fmt
, ...)
2513 va_start(args
, fmt
);
2514 i
= vsnprintf(buf
, INT_MAX
, fmt
, args
);
2519 EXPORT_SYMBOL(sprintf
);
2521 #ifdef CONFIG_BINARY_PRINTF
2524 * vbin_printf() - VA arguments to binary data
2525 * bstr_printf() - Binary data to text string
2529 * vbin_printf - Parse a format string and place args' binary value in a buffer
2530 * @bin_buf: The buffer to place args' binary value
2531 * @size: The size of the buffer(by words(32bits), not characters)
2532 * @fmt: The format string to use
2533 * @args: Arguments for the format string
2535 * The format follows C99 vsnprintf, except %n is ignored, and its argument
2538 * The return value is the number of words(32bits) which would be generated for
2542 * If the return value is greater than @size, the resulting bin_buf is NOT
2543 * valid for bstr_printf().
2545 int vbin_printf(u32
*bin_buf
, size_t size
, const char *fmt
, va_list args
)
2547 struct printf_spec spec
= {0};
2550 str
= (char *)bin_buf
;
2551 end
= (char *)(bin_buf
+ size
);
2553 #define save_arg(type) \
2555 if (sizeof(type) == 8) { \
2556 unsigned long long value; \
2557 str = PTR_ALIGN(str, sizeof(u32)); \
2558 value = va_arg(args, unsigned long long); \
2559 if (str + sizeof(type) <= end) { \
2560 *(u32 *)str = *(u32 *)&value; \
2561 *(u32 *)(str + 4) = *((u32 *)&value + 1); \
2564 unsigned long value; \
2565 str = PTR_ALIGN(str, sizeof(type)); \
2566 value = va_arg(args, int); \
2567 if (str + sizeof(type) <= end) \
2568 *(typeof(type) *)str = (type)value; \
2570 str += sizeof(type); \
2574 int read
= format_decode(fmt
, &spec
);
2578 switch (spec
.type
) {
2579 case FORMAT_TYPE_NONE
:
2580 case FORMAT_TYPE_PERCENT_CHAR
:
2582 case FORMAT_TYPE_INVALID
:
2585 case FORMAT_TYPE_WIDTH
:
2586 case FORMAT_TYPE_PRECISION
:
2590 case FORMAT_TYPE_CHAR
:
2594 case FORMAT_TYPE_STR
: {
2595 const char *save_str
= va_arg(args
, char *);
2598 if ((unsigned long)save_str
> (unsigned long)-PAGE_SIZE
2599 || (unsigned long)save_str
< PAGE_SIZE
)
2600 save_str
= "(null)";
2601 len
= strlen(save_str
) + 1;
2602 if (str
+ len
< end
)
2603 memcpy(str
, save_str
, len
);
2608 case FORMAT_TYPE_PTR
:
2610 /* skip all alphanumeric pointer suffixes */
2611 while (isalnum(*fmt
))
2616 switch (spec
.type
) {
2618 case FORMAT_TYPE_LONG_LONG
:
2619 save_arg(long long);
2621 case FORMAT_TYPE_ULONG
:
2622 case FORMAT_TYPE_LONG
:
2623 save_arg(unsigned long);
2625 case FORMAT_TYPE_SIZE_T
:
2628 case FORMAT_TYPE_PTRDIFF
:
2629 save_arg(ptrdiff_t);
2631 case FORMAT_TYPE_UBYTE
:
2632 case FORMAT_TYPE_BYTE
:
2635 case FORMAT_TYPE_USHORT
:
2636 case FORMAT_TYPE_SHORT
:
2646 return (u32
*)(PTR_ALIGN(str
, sizeof(u32
))) - bin_buf
;
2649 EXPORT_SYMBOL_GPL(vbin_printf
);
2652 * bstr_printf - Format a string from binary arguments and place it in a buffer
2653 * @buf: The buffer to place the result into
2654 * @size: The size of the buffer, including the trailing null space
2655 * @fmt: The format string to use
2656 * @bin_buf: Binary arguments for the format string
2658 * This function like C99 vsnprintf, but the difference is that vsnprintf gets
2659 * arguments from stack, and bstr_printf gets arguments from @bin_buf which is
2660 * a binary buffer that generated by vbin_printf.
2662 * The format follows C99 vsnprintf, but has some extensions:
2663 * see vsnprintf comment for details.
2665 * The return value is the number of characters which would
2666 * be generated for the given input, excluding the trailing
2667 * '\0', as per ISO C99. If you want to have the exact
2668 * number of characters written into @buf as return value
2669 * (not including the trailing '\0'), use vscnprintf(). If the
2670 * return is greater than or equal to @size, the resulting
2671 * string is truncated.
2673 int bstr_printf(char *buf
, size_t size
, const char *fmt
, const u32
*bin_buf
)
2675 struct printf_spec spec
= {0};
2677 const char *args
= (const char *)bin_buf
;
2679 if (WARN_ON_ONCE(size
> INT_MAX
))
2685 #define get_arg(type) \
2687 typeof(type) value; \
2688 if (sizeof(type) == 8) { \
2689 args = PTR_ALIGN(args, sizeof(u32)); \
2690 *(u32 *)&value = *(u32 *)args; \
2691 *((u32 *)&value + 1) = *(u32 *)(args + 4); \
2693 args = PTR_ALIGN(args, sizeof(type)); \
2694 value = *(typeof(type) *)args; \
2696 args += sizeof(type); \
2700 /* Make sure end is always >= buf */
2707 const char *old_fmt
= fmt
;
2708 int read
= format_decode(fmt
, &spec
);
2712 switch (spec
.type
) {
2713 case FORMAT_TYPE_NONE
: {
2716 if (copy
> end
- str
)
2718 memcpy(str
, old_fmt
, copy
);
2724 case FORMAT_TYPE_WIDTH
:
2725 set_field_width(&spec
, get_arg(int));
2728 case FORMAT_TYPE_PRECISION
:
2729 set_precision(&spec
, get_arg(int));
2732 case FORMAT_TYPE_CHAR
: {
2735 if (!(spec
.flags
& LEFT
)) {
2736 while (--spec
.field_width
> 0) {
2742 c
= (unsigned char) get_arg(char);
2746 while (--spec
.field_width
> 0) {
2754 case FORMAT_TYPE_STR
: {
2755 const char *str_arg
= args
;
2756 args
+= strlen(str_arg
) + 1;
2757 str
= string(str
, end
, (char *)str_arg
, spec
);
2761 case FORMAT_TYPE_PTR
:
2762 str
= pointer(fmt
, str
, end
, get_arg(void *), spec
);
2763 while (isalnum(*fmt
))
2767 case FORMAT_TYPE_PERCENT_CHAR
:
2773 case FORMAT_TYPE_INVALID
:
2777 unsigned long long num
;
2779 switch (spec
.type
) {
2781 case FORMAT_TYPE_LONG_LONG
:
2782 num
= get_arg(long long);
2784 case FORMAT_TYPE_ULONG
:
2785 case FORMAT_TYPE_LONG
:
2786 num
= get_arg(unsigned long);
2788 case FORMAT_TYPE_SIZE_T
:
2789 num
= get_arg(size_t);
2791 case FORMAT_TYPE_PTRDIFF
:
2792 num
= get_arg(ptrdiff_t);
2794 case FORMAT_TYPE_UBYTE
:
2795 num
= get_arg(unsigned char);
2797 case FORMAT_TYPE_BYTE
:
2798 num
= get_arg(signed char);
2800 case FORMAT_TYPE_USHORT
:
2801 num
= get_arg(unsigned short);
2803 case FORMAT_TYPE_SHORT
:
2804 num
= get_arg(short);
2806 case FORMAT_TYPE_UINT
:
2807 num
= get_arg(unsigned int);
2813 str
= number(str
, end
, num
, spec
);
2815 } /* switch(spec.type) */
2828 /* the trailing null byte doesn't count towards the total */
2831 EXPORT_SYMBOL_GPL(bstr_printf
);
2834 * bprintf - Parse a format string and place args' binary value in a buffer
2835 * @bin_buf: The buffer to place args' binary value
2836 * @size: The size of the buffer(by words(32bits), not characters)
2837 * @fmt: The format string to use
2838 * @...: Arguments for the format string
2840 * The function returns the number of words(u32) written
2843 int bprintf(u32
*bin_buf
, size_t size
, const char *fmt
, ...)
2848 va_start(args
, fmt
);
2849 ret
= vbin_printf(bin_buf
, size
, fmt
, args
);
2854 EXPORT_SYMBOL_GPL(bprintf
);
2856 #endif /* CONFIG_BINARY_PRINTF */
2859 * vsscanf - Unformat a buffer into a list of arguments
2860 * @buf: input buffer
2861 * @fmt: format of buffer
2864 int vsscanf(const char *buf
, const char *fmt
, va_list args
)
2866 const char *str
= buf
;
2874 unsigned long long u
;
2880 /* skip any white space in format */
2881 /* white space in format matchs any amount of
2882 * white space, including none, in the input.
2884 if (isspace(*fmt
)) {
2885 fmt
= skip_spaces(++fmt
);
2886 str
= skip_spaces(str
);
2889 /* anything that is not a conversion must match exactly */
2890 if (*fmt
!= '%' && *fmt
) {
2891 if (*fmt
++ != *str
++)
2900 /* skip this conversion.
2901 * advance both strings to next white space
2906 while (!isspace(*fmt
) && *fmt
!= '%' && *fmt
) {
2907 /* '%*[' not yet supported, invalid format */
2912 while (!isspace(*str
) && *str
)
2917 /* get field width */
2919 if (isdigit(*fmt
)) {
2920 field_width
= skip_atoi(&fmt
);
2921 if (field_width
<= 0)
2925 /* get conversion qualifier */
2927 if (*fmt
== 'h' || _tolower(*fmt
) == 'l' ||
2930 if (unlikely(qualifier
== *fmt
)) {
2931 if (qualifier
== 'h') {
2934 } else if (qualifier
== 'l') {
2945 /* return number of characters read so far */
2946 *va_arg(args
, int *) = str
- buf
;
2960 char *s
= (char *)va_arg(args
, char*);
2961 if (field_width
== -1)
2965 } while (--field_width
> 0 && *str
);
2971 char *s
= (char *)va_arg(args
, char *);
2972 if (field_width
== -1)
2973 field_width
= SHRT_MAX
;
2974 /* first, skip leading white space in buffer */
2975 str
= skip_spaces(str
);
2977 /* now copy until next white space */
2978 while (*str
&& !isspace(*str
) && field_width
--)
2985 * Warning: This implementation of the '[' conversion specifier
2986 * deviates from its glibc counterpart in the following ways:
2987 * (1) It does NOT support ranges i.e. '-' is NOT a special
2989 * (2) It cannot match the closing bracket ']' itself
2990 * (3) A field width is required
2991 * (4) '%*[' (discard matching input) is currently not supported
2994 * ret = sscanf("00:0a:95","%2[^:]:%2[^:]:%2[^:]",
2995 * buf1, buf2, buf3);
3001 char *s
= (char *)va_arg(args
, char *);
3002 DECLARE_BITMAP(set
, 256) = {0};
3003 unsigned int len
= 0;
3004 bool negate
= (*fmt
== '^');
3006 /* field width is required */
3007 if (field_width
== -1)
3013 for ( ; *fmt
&& *fmt
!= ']'; ++fmt
, ++len
)
3014 set_bit((u8
)*fmt
, set
);
3016 /* no ']' or no character set found */
3022 bitmap_complement(set
, set
, 256);
3023 /* exclude null '\0' byte */
3027 /* match must be non-empty */
3028 if (!test_bit((u8
)*str
, set
))
3031 while (test_bit((u8
)*str
, set
) && field_width
--)
3051 /* looking for '%' in str */
3056 /* invalid format; stop here */
3060 /* have some sort of integer conversion.
3061 * first, skip white space in buffer.
3063 str
= skip_spaces(str
);
3066 if (is_sign
&& digit
== '-')
3070 || (base
== 16 && !isxdigit(digit
))
3071 || (base
== 10 && !isdigit(digit
))
3072 || (base
== 8 && (!isdigit(digit
) || digit
> '7'))
3073 || (base
== 0 && !isdigit(digit
)))
3077 val
.s
= qualifier
!= 'L' ?
3078 simple_strtol(str
, &next
, base
) :
3079 simple_strtoll(str
, &next
, base
);
3081 val
.u
= qualifier
!= 'L' ?
3082 simple_strtoul(str
, &next
, base
) :
3083 simple_strtoull(str
, &next
, base
);
3085 if (field_width
> 0 && next
- str
> field_width
) {
3087 _parse_integer_fixup_radix(str
, &base
);
3088 while (next
- str
> field_width
) {
3090 val
.s
= div_s64(val
.s
, base
);
3092 val
.u
= div_u64(val
.u
, base
);
3097 switch (qualifier
) {
3098 case 'H': /* that's 'hh' in format */
3100 *va_arg(args
, signed char *) = val
.s
;
3102 *va_arg(args
, unsigned char *) = val
.u
;
3106 *va_arg(args
, short *) = val
.s
;
3108 *va_arg(args
, unsigned short *) = val
.u
;
3112 *va_arg(args
, long *) = val
.s
;
3114 *va_arg(args
, unsigned long *) = val
.u
;
3118 *va_arg(args
, long long *) = val
.s
;
3120 *va_arg(args
, unsigned long long *) = val
.u
;
3123 *va_arg(args
, size_t *) = val
.u
;
3127 *va_arg(args
, int *) = val
.s
;
3129 *va_arg(args
, unsigned int *) = val
.u
;
3141 EXPORT_SYMBOL(vsscanf
);
3144 * sscanf - Unformat a buffer into a list of arguments
3145 * @buf: input buffer
3146 * @fmt: formatting of buffer
3147 * @...: resulting arguments
3149 int sscanf(const char *buf
, const char *fmt
, ...)
3154 va_start(args
, fmt
);
3155 i
= vsscanf(buf
, fmt
, args
);
3160 EXPORT_SYMBOL(sscanf
);