Skip to content

Commit 31550a1

Browse files
andy-shevtorvalds
authored andcommittedJul 31, 2012
vsprintf: add support of '%*ph[CDN]'
There are many places in the kernel where the drivers print small buffers as a hex string. This patch adds a support of the variable width buffer to print it as a hex string with a delimiter. The idea came from Pavel Roskin here: http://www.digipedia.pl/usenet/thread/18835/17449/ Sample output of pr_info("buf[%d:%d] %*phC\n", from, len, len, &buf[from]); could be look like this: [ 0.726130] buf[51:8] e8:16:b6:ef:e3:74:45:6e [ 0.750736] buf[59:15] 31:81:b8:3f:35:49:06:ae:df:32:06:05:4a:af:55 [ 0.757602] buf[17:5] ac:16:d5:2c:ef Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com> Cc: Joe Perches <joe@perches.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 3715c53 commit 31550a1

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed
 

‎Documentation/printk-formats.txt

+10
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,16 @@ Struct Resources:
5353
For printing struct resources. The 'R' and 'r' specifiers result in a
5454
printed resource with ('R') or without ('r') a decoded flags member.
5555

56+
Raw buffer as a hex string:
57+
%*ph 00 01 02 ... 3f
58+
%*phC 00:01:02: ... :3f
59+
%*phD 00-01-02- ... -3f
60+
%*phN 000102 ... 3f
61+
62+
For printing a small buffers (up to 64 bytes long) as a hex string with
63+
certain separator. For the larger buffers consider to use
64+
print_hex_dump().
65+
5666
MAC/FDDI addresses:
5767

5868
%pM 00:01:02:03:04:05

‎lib/vsprintf.c

+55
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,50 @@ char *resource_string(char *buf, char *end, struct resource *res,
654654
return string(buf, end, sym, spec);
655655
}
656656

657+
static noinline_for_stack
658+
char *hex_string(char *buf, char *end, u8 *addr, struct printf_spec spec,
659+
const char *fmt)
660+
{
661+
int i, len = 1; /* if we pass '%ph[CDN]', field witdh remains
662+
negative value, fallback to the default */
663+
char separator;
664+
665+
if (spec.field_width == 0)
666+
/* nothing to print */
667+
return buf;
668+
669+
if (ZERO_OR_NULL_PTR(addr))
670+
/* NULL pointer */
671+
return string(buf, end, NULL, spec);
672+
673+
switch (fmt[1]) {
674+
case 'C':
675+
separator = ':';
676+
break;
677+
case 'D':
678+
separator = '-';
679+
break;
680+
case 'N':
681+
separator = 0;
682+
break;
683+
default:
684+
separator = ' ';
685+
break;
686+
}
687+
688+
if (spec.field_width > 0)
689+
len = min_t(int, spec.field_width, 64);
690+
691+
for (i = 0; i < len && buf < end - 1; i++) {
692+
buf = hex_byte_pack(buf, addr[i]);
693+
694+
if (buf < end && separator && i != len - 1)
695+
*buf++ = separator;
696+
}
697+
698+
return buf;
699+
}
700+
657701
static noinline_for_stack
658702
char *mac_address_string(char *buf, char *end, u8 *addr,
659703
struct printf_spec spec, const char *fmt)
@@ -974,6 +1018,13 @@ int kptr_restrict __read_mostly;
9741018
* correctness of the format string and va_list arguments.
9751019
* - 'K' For a kernel pointer that should be hidden from unprivileged users
9761020
* - 'NF' For a netdev_features_t
1021+
* - 'h[CDN]' For a variable-length buffer, it prints it as a hex string with
1022+
* a certain separator (' ' by default):
1023+
* C colon
1024+
* D dash
1025+
* N no separator
1026+
* The maximum supported length is 64 bytes of the input. Consider
1027+
* to use print_hex_dump() for the larger input.
9771028
*
9781029
* Note: The difference between 'S' and 'F' is that on ia64 and ppc64
9791030
* function pointers are really function descriptors, which contain a
@@ -1007,6 +1058,8 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
10071058
case 'R':
10081059
case 'r':
10091060
return resource_string(buf, end, ptr, spec, fmt);
1061+
case 'h':
1062+
return hex_string(buf, end, ptr, spec, fmt);
10101063
case 'M': /* Colon separated: 00:01:02:03:04:05 */
10111064
case 'm': /* Contiguous: 000102030405 */
10121065
/* [mM]F (FDDI) */
@@ -1296,6 +1349,8 @@ int format_decode(const char *fmt, struct printf_spec *spec)
12961349
* %pI6c print an IPv6 address as specified by RFC 5952
12971350
* %pU[bBlL] print a UUID/GUID in big or little endian using lower or upper
12981351
* case.
1352+
* %*ph[CDN] a variable-length hex string with a separator (supports up to 64
1353+
* bytes of the input)
12991354
* %n is ignored
13001355
*
13011356
* ** Please update Documentation/printk-formats.txt when making changes **

0 commit comments

Comments
 (0)
Please sign in to comment.