vsprintf: introduce %*ph extended format specifier for hex buffers
authorAndrew Cooper <andrew.cooper3@citrix.com>
Wed, 1 Oct 2014 09:32:56 +0000 (11:32 +0200)
committerJan Beulich <jbeulich@suse.com>
Wed, 1 Oct 2014 09:32:56 +0000 (11:32 +0200)
This behaves in the same way as Linux.  The 64 byte limit is arbitrary but
long enough for practical purposes.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Release-acked-by: Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>
Re-structured code.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
docs/misc/printk-formats.txt
xen/common/vsprintf.c

index 3b4323a64af44696d26dc17934e07bf1662e1e92..dee0f3e8f77b6dcb4e77ddeda5ff1fa278d230e0 100644 (file)
@@ -3,6 +3,11 @@ Xen custom %p format options.  A subset, borrowed from Linux.
 All parameters to a %p option should be compatible with void*.  Regular
 pointers are fine.  Numbers should make use of the _p() macro.
 
+Raw buffer as hex string:
+
+       %*ph    Up to 64 characters, printed as "00 01 02 ... ff".  Buffer length
+               expected via the field_width paramter. i.e. printk("%*ph", 8, buffer);
+
 Symbol/Function pointers:
 
        %ps     Symbol name with condition offset and size (iff offset != 0)
@@ -16,5 +21,7 @@ Symbol/Function pointers:
        In the case that an appropriate symbol name can't be found, %p[sS] will
        fall back to '%p' and print the address in hex.
 
+Domain and vCPU information:
+
        %pv     Domain and vCPU ID from a 'struct vcpu *' (printed as
                "d<domid>v<vcpuid>")
index e68886a1c2bfca350593c14fdefbbfd389d8a26a..065cc42525d85b2272fa982211a78218d33bcbeb 100644 (file)
@@ -272,6 +272,34 @@ static char *pointer(char *str, char *end, const char **fmt_ptr,
     /* Custom %p suffixes. See XEN_ROOT/docs/misc/printk-formats.txt */
     switch ( fmt[1] )
     {
+    case 'h': /* Raw buffer as hex string. */
+    {
+        const uint8_t *hex_buffer = arg;
+        unsigned int i;
+
+        /* Consumed 'h' from the format string. */
+        ++*fmt_ptr;
+
+        /* Bound user count from %* to between 0 and 64 bytes. */
+        if ( field_width <= 0 )
+            return str;
+        if ( field_width > 64 )
+            field_width = 64;
+
+        for ( i = 0; ; )
+        {
+            /* Each byte: 2 chars, 0-padded, base 16, no hex prefix. */
+            str = number(str, end, hex_buffer[i], 16, 2, -1, ZEROPAD);
+
+            if ( ++i == field_width )
+                return str;
+
+            if ( str < end )
+                *str = ' ';
+            ++str;
+        }
+    }
+
     case 's': /* Symbol name with offset and size (iff offset != 0) */
     case 'S': /* Symbol name unconditionally with offset and size */
     {