Skip to contentSkip to conversation

improve report/printf formatting

PATCH In review Radim Krčmář 4 participants 12 messages May 17, 2017 – May 18, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover improve report/printf formatting

This series is a mix of three logical units:

[1-2/6]: add format checking to report() and fix revealed bugs

[3/6]: fix padding in %p conversions

[4-6/6]: fix random 0x%d and support %# to make this mistake harder

Radim Krčmář (6):
fix conversions in report()s
libcflat: add format checking to report()
libcflat: fix padding in printf
pci-edu: remove 0x before a decimal value
libcflat: support # flag in printf
use %# instead of 0x% in all format strings

 arm/pmu.c               |  2 +-
 arm/selftest.c          |  2 +-
 lib/alloc.c             | 10 +++++-----
 lib/arm64/processor.c   |  6 +++---
 lib/libcflat.h          | 18 ++++++++++++------
 lib/pci-edu.c           |  2 +-
 lib/pci.c               |  6 +++---
 lib/powerpc/processor.c |  2 +-
 lib/printf.c            | 20 ++++++++++++++++++--
 lib/x86/desc.c          |  2 +-
 lib/x86/intel-iommu.c   | 16 ++++++++--------
 lib/x86/vm.c            |  4 ++--
 powerpc/emulator.c      |  4 ++--
 powerpc/selftest.c      |  2 +-
 powerpc/spapr_hcall.c   |  2 +-
 powerpc/sprs.c          |  4 ++--
 x86/asyncpf.c           |  6 +++---
 x86/hyperv_synic.c      |  2 +-
 x86/msr.c               |  4 ++--
 x86/tsc.c               |  2 +-
 x86/vmexit.c            |  2 +-
 x86/vmx.c               | 14 +++++++-------
 x86/vmx.h               |  4 ++--
 x86/vmx_tests.c         |  2 +-
 x86/xsave.c             |  2 +-
 25 files changed, 81 insertions(+), 59 deletions(-)
Signature · 1 line
2.13.0
#1 lore raw

PATCH 1/6 fix conversions in report()s

Fix conversions that would trigger a warning if the format was checked by a compiler.

---
 arm/selftest.c        | 2 +-
 powerpc/selftest.c    | 2 +-
 powerpc/spapr_hcall.c | 2 +-
 x86/asyncpf.c         | 6 +++---
 x86/hyperv_synic.c    | 2 +-
 x86/tsc.c             | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)
arm/selftest.c +1 -1
index 750e90893141..f305f4e3be19 100644
@@ -37,7 +37,7 @@ static void check_setup(int argc, char **argv)
 			phys_addr_t memsize = PHYS_END - PHYS_OFFSET;
 			phys_addr_t expected = ((phys_addr_t)val)*1024*1024;
 
-			report("size = %d MB", memsize == expected,
+			report("size = %" PRIu64 " MB", memsize == expected,
 							memsize/1024/1024);
 			++nr_tests;
 
powerpc/selftest.c +1 -1
index 8c5ff0ac889d..4e29fd6bbd56 100644
@@ -28,7 +28,7 @@ static void check_setup(int argc, char **argv)
 			phys_addr_t memsize = PHYSICAL_END - PHYSICAL_START;
 			phys_addr_t expected = ((phys_addr_t)val)*1024*1024;
 
-			report("size = %d MB", memsize == expected,
+			report("size = %" PRIu64 " MB", memsize == expected,
 							memsize/1024/1024);
 			++nr_tests;
 
powerpc/spapr_hcall.c +1 -1
index 656aaff61405..bd6c287a2f0c 100644
@@ -49,7 +49,7 @@ static void test_h_set_sprg0(int argc, char **argv)
 
 	rc = hcall(H_SET_SPRG0, sprg0_orig);
 	sprg0 = mfspr(SPR_SPRG0);
-	report("sprg0 = 0x%llx",
+	report("sprg0 = 0x%" PRIx64,
 		rc == H_SUCCESS && sprg0 == sprg0_orig, sprg0_orig);
 }
 
x86/asyncpf.c +3 -3
index e29e07c556f3..fea1d28ef375 100644
@@ -52,13 +52,13 @@ static void pf_isr(struct ex_regs *r)
 
 	switch (reason) {
 		case 0:
-			report("unexpected #PF at %p", false, read_cr2());
+			report("unexpected #PF at 0x%lx", false, read_cr2());
 			break;
 		case KVM_PV_REASON_PAGE_NOT_PRESENT:
 			phys = virt_to_phys_cr3(virt);
 			install_pte(phys_to_virt(read_cr3()), 1, virt, phys, 0);
 			write_cr3(read_cr3());
-			report("Got not present #PF token %x virt addr %p phys addr %p",
+			report("Got not present #PF token %lx virt addr %p phys addr 0x%" PRIx64,
 					true, read_cr2(), virt, phys);
 			while(phys) {
 				safe_halt(); /* enables irq */
@@ -66,7 +66,7 @@ static void pf_isr(struct ex_regs *r)
 			}
 			break;
 		case KVM_PV_REASON_PAGE_READY:
-			report("Got present #PF token %x", true, read_cr2());
+			report("Got present #PF token %lx", true, read_cr2());
 			if ((uint32_t)read_cr2() == ~0)
 				break;
 			install_pte(phys_to_virt(read_cr3()), 1, virt, phys | PT_PRESENT_MASK | PT_WRITABLE_MASK, 0);
x86/hyperv_synic.c +1 -1
index 74bbd58ee983..d1f75c588e1c 100644
@@ -90,7 +90,7 @@ static void synic_test_prepare(void *ctx)
     }
     r = rdmsr(HV_X64_MSR_EOM);
     if (r != 0) {
-        report("Hyper-V SynIC test, EOM read 0x%llx", false, r);
+        report("Hyper-V SynIC test, EOM read 0x%" PRIx64, false, r);
         goto ret;
     }
 
x86/tsc.c +1 -1
index 62450e71725c..6dc05af27eaf 100644
@@ -23,7 +23,7 @@ void test_rdtscp(u64 aux)
 
        wrmsr(MSR_TSC_AUX, aux);
        rdtscp(&ecx);
-       report("Test RDTSCP %d", ecx == aux, aux);
+       report("Test RDTSCP %" PRIu64, ecx == aux, aux);
 }
 
 int main()
Signature · 1 line
2.13.0
Reviewed-by on v1 1/6
#2 lore raw

Re: [kvm-unit-tests PATCH 1/6] fix conversions in report()s

Quoted 4 lines · Radim Krčmář

Fix conversions that would trigger a warning if the format was checked by a compiler.

Quoted 8 lines
---
 arm/selftest.c        | 2 +-
 powerpc/selftest.c    | 2 +-
 powerpc/spapr_hcall.c | 2 +-
 x86/asyncpf.c         | 6 +++---
 x86/hyperv_synic.c    | 2 +-
 x86/tsc.c             | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)
Reviewed-by on v1 1/6
#3 lore raw

Re: [kvm-unit-tests PATCH 1/6] fix conversions in report()s

In reply to Radim Krčmář
Quoted 4 lines · Radim Krčmář

Fix conversions that would trigger a warning if the format was checked by a compiler.

Quoted 9 lines
---
 arm/selftest.c        | 2 +-
 powerpc/selftest.c    | 2 +-
 powerpc/spapr_hcall.c | 2 +-
 x86/asyncpf.c         | 6 +++---
 x86/hyperv_synic.c    | 2 +-
 x86/tsc.c             | 2 +-
 6 files changed, 8 insertions(+), 8 deletions(-)
Signature · 3 lines
Thanks,

David
#4 lore raw

PATCH 2/6 libcflat: add format checking to report()

In reply to Radim Krčmář

report() is a wrapper for printf(), but the compiler was not aware.

---
 lib/libcflat.h | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
lib/libcflat.h +12 -6
index b1ea5e607033..a5c42903b11f 100644
@@ -96,14 +96,20 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
-void report_prefix_pushf(const char *prefix_fmt, ...);
+void report_prefix_pushf(const char *prefix_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
-extern void report(const char *msg_fmt, bool pass, ...);
-extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
-extern void report_abort(const char *msg_fmt, ...);
-extern void report_skip(const char *msg_fmt, ...);
-extern void report_info(const char *msg_fmt, ...);
+extern void report(const char *msg_fmt, bool pass, ...)
+					__attribute__((format(printf, 1, 3)));
+extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
+					__attribute__((format(printf, 1, 4)));
+extern void report_abort(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
+extern void report_skip(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
+extern void report_info(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
 extern void report_pass(void);
 extern int report_summary(void);
 
Signature · 1 line
2.13.0
Reviewed-by on v1 2/6
#5 lore raw

Re: [kvm-unit-tests PATCH 2/6] libcflat: add format checking to report()

Quoted 27 lines · Radim Krčmář

report() is a wrapper for printf(), but the compiler was not aware.

---
 lib/libcflat.h | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)
lib/libcflat.h +4 -6
index b1ea5e607033..a5c42903b11f 100644
@@ -96,14 +96,20 @@ extern int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 extern int vprintf(const char *fmt, va_list va)
 					__attribute__((format(printf, 1, 0)));
 
-void report_prefix_pushf(const char *prefix_fmt, ...);
+void report_prefix_pushf(const char *prefix_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
 extern void report_prefix_push(const char *prefix);
 extern void report_prefix_pop(void);
-extern void report(const char *msg_fmt, bool pass, ...);
-extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...);
-extern void report_abort(const char *msg_fmt, ...);
-extern void report_skip(const char *msg_fmt, ...);
-extern void report_info(const char *msg_fmt, ...);
+extern void report(const char *msg_fmt, bool pass, ...)
+					__attribute__((format(printf, 1, 3)));

lib/libcflat.h extern int vsnprintf(char *buf, int size, const char *fmt, va_list va) · line 114

Quoted 10 lines
lib/libcflat.h +8 -0
+extern void report_xfail(const char *msg_fmt, bool xfail, bool pass, ...)
+					__attribute__((format(printf, 1, 4)));
+extern void report_abort(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
+extern void report_skip(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
+extern void report_info(const char *msg_fmt, ...)
+					__attribute__((format(printf, 1, 2)));
 extern void report_pass(void);
 extern int report_summary(void);
#6 lore raw

PATCH 3/6 libcflat: fix padding in printf

In reply to Radim Krčmář

A simple test:

  printf(".%8p.\n",  (void *)0);
  printf(".%-8p.\n", (void *)0);
  printf(".%8p.\n",  (void *)1);
  printf(".%-8p.\n", (void *)1);
  printf(".%8p.\n",  (void *)0x123456);
  printf(".%-8p.\n", (void *)0x123456);

  printf(".%2p.\n",  (void *)0);
  printf(".%-2p.\n", (void *)0);
  printf(".%2p.\n",  (void *)1);
  printf(".%-2p.\n", (void *)1);

glibc:

. (nil).
.(nil) .
. 0x1.
.0x1 .
.0x123456.
.0x123456.
.(nil).
.(nil).
.0x1.
.0x1.

before patch:

. 0x 0.
.0x 0 .
. 0x 1.
.0x 1 .
. 0x 123456.
.0x 123456 .
.0x 0.
.0x0 .
.0x 1.
.0x1 .

after patch:

. 0.
.0 .
. 0x1.
.0x1 .
.0x123456.
.0x123456.
. 0.
.0 .
.0x1.
.0x1.

(nil) would be possible with a small change, but the standard leaves it to the implementation and 0 is acceptable, IMO.

---
 lib/printf.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
lib/printf.c +7 -2
index e7818e1ded0e..944768f3080d 100644
@@ -18,6 +18,7 @@ typedef struct pstream {
 typedef struct strprops {
     char pad;
     int npad;
+    bool alternate;
 } strprops_t;
 
 static void addchar(pstream_t *p, char c)
@@ -94,7 +95,7 @@ void print_int(pstream_t *ps, long long n, int base, strprops_t props)
 void print_unsigned(pstream_t *ps, unsigned long long n, int base,
 		    strprops_t props)
 {
-    char buf[sizeof(long) * 3 + 1], *p = buf;
+    char buf[sizeof(long) * 3 + 3], *p = buf;
     int i;
 
     while (n) {
@@ -104,6 +105,10 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base,
 
     if (p == buf)
 	*p++ = '0';
+    else if (props.alternate && base == 16) {
+	*p++ = 'x';
+	*p++ = '0';
+    }
 
     for (i = 0; i < (p - buf) / 2; ++i) {
 	char tmp;
@@ -225,7 +230,7 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 	    }
 	    break;
 	case 'p':
-	    print_str(&s, "0x", props);
+	    props.alternate = true;
 	    print_unsigned(&s, (unsigned long)va_arg(va, void *), 16, props);
 	    break;
 	case 's':
Signature · 1 line
2.13.0
#7 lore raw

PATCH 4/6 pci-edu: remove 0x before a decimal value

In reply to Radim Krčmář
---
 lib/pci-edu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pci-edu.c +1 -1
index f94962f0001a..58ce0b56554d 100644
@@ -48,7 +48,7 @@ void edu_dma(struct pci_edu_dev *dev, iova_t iova,
 	assert(size <= EDU_DMA_SIZE_MAX);
 	assert(dev_offset < EDU_DMA_SIZE_MAX);
 
-	printf("edu device DMA start %s addr 0x%" PRIx64 " size 0x%lu off 0x%x\n",
+	printf("edu device DMA start %s addr 0x%" PRIx64 " size %lu off 0x%x\n",
 	       from_device ? "FROM" : "TO",
 	       iova, (ulong)size, dev_offset);
 
Signature · 1 line
2.13.0
Reviewed-by on v1 4/6
#8 lore raw

Re: [kvm-unit-tests PATCH 4/6] pci-edu: remove 0x before a decimal value

Quoted 10 lines · Radim Krčmář
---
 lib/pci-edu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
lib/pci-edu.c +0 -0
index f94962f0001a..58ce0b56554d 100644

lib/pci-edu.c void edu_dma(struct pci_edu_dev *dev, iova_t iova, · line 54

Quoted 8 lines
lib/pci-edu.c +1 -1
@@ -48,7 +48,7 @@ void edu_dma(struct pci_edu_dev *dev, iova_t iova,
 	assert(size <= EDU_DMA_SIZE_MAX);
 	assert(dev_offset < EDU_DMA_SIZE_MAX);
 
-	printf("edu device DMA start %s addr 0x%" PRIx64 " size 0x%lu off 0x%x\n",
+	printf("edu device DMA start %s addr 0x%" PRIx64 " size %lu off 0x%x\n",
 	       from_device ? "FROM" : "TO",
 	       iova, (ulong)size, dev_offset);
 
#9 lore raw

PATCH 5/6 libcflat: support # flag in printf

In reply to Radim Krčmář

The only use is with %#x, where we'll automatically get 0x prefix. Advantage over 0x%x can be seen with padding.

A simple test:

  printf(".%#08x.\n", 0);
  printf(".%#8x.\n",  0);
  printf(".%#-8x.\n", 0);

  printf(".%#08x.\n", 1);
  printf(".%#8x.\n",  1);
  printf(".%#-8x.\n", 1);

  printf(".%#08x.\n", 0x123456);
  printf(".%#8x.\n",  0x123456);
  printf(".%#-8x.\n", 0x123456);

  printf(".%#02x.\n", 0);
  printf(".%#2x.\n",  0);
  printf(".%#-2x.\n", 0);

  printf(".%#02x.\n", 1);
  printf(".%#2x.\n",  1);
  printf(".%#-2x.\n", 1);

looks just like glibc:

.00000000.
. 0.
.0 .
.0x000001.
. 0x1.
.0x1 .
.0x123456.
.0x123456.
.0x123456.
.00.
. 0.
.0 .
.0x1.
.0x1.
.0x1.

---
 lib/printf.c | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)
lib/printf.c +13 -2
index 944768f3080d..bfe05f573f08 100644
@@ -106,8 +106,16 @@ void print_unsigned(pstream_t *ps, unsigned long long n, int base,
     if (p == buf)
 	*p++ = '0';
     else if (props.alternate && base == 16) {
-	*p++ = 'x';
-	*p++ = '0';
+	if (props.pad == '0') {
+	    addchar(ps, '0');
+	    addchar(ps, 'x');
+
+	    if (props.npad > 0)
+		props.npad = MAX(props.npad - 2, 0);
+	} else {
+	    *p++ = 'x';
+	    *p++ = '0';
+	}
     }
 
     for (i = 0; i < (p - buf) / 2; ++i) {
@@ -169,6 +177,9 @@ int vsnprintf(char *buf, int size, const char *fmt, va_list va)
 	case '\0':
 	    --fmt;
 	    break;
+	case '#':
+	    props.alternate = true;
+	    goto morefmt;
 	case '0':
 	    props.pad = '0';
 	    ++fmt;
Signature · 1 line
2.13.0
#10 lore raw

PATCH 6/6 use %# instead of 0x% in all format strings

In reply to Radim Krčmář

It's one character shorter, properly prefixed, and also provides error
detection: %#d triggers a warning.

Done with sed -i 's/0x%/%#/g' **/*.[ch] to motivate the use of %#, existing padding was raised by 2 to account for the counted "0x" (output should be the same).

---
 arm/pmu.c               |  2 +-
 lib/alloc.c             | 10 +++++-----
 lib/arm64/processor.c   |  6 +++---
 lib/pci-edu.c           |  2 +-
 lib/pci.c               |  6 +++---
 lib/powerpc/processor.c |  2 +-
 lib/x86/desc.c          |  2 +-
 lib/x86/intel-iommu.c   | 16 ++++++++--------
 lib/x86/vm.c            |  4 ++--
 powerpc/emulator.c      |  4 ++--
 powerpc/spapr_hcall.c   |  2 +-
 powerpc/sprs.c          |  4 ++--
 x86/asyncpf.c           |  4 ++--
 x86/hyperv_synic.c      |  2 +-
 x86/msr.c               |  4 ++--
 x86/vmexit.c            |  2 +-
 x86/vmx.c               | 14 +++++++-------
 x86/vmx.h               |  4 ++--
 x86/vmx_tests.c         |  2 +-
 x86/xsave.c             |  2 +-
 20 files changed, 47 insertions(+), 47 deletions(-)
arm/pmu.c +1 -1
index c4d5c97dbf87..74848988a678 100644
@@ -153,7 +153,7 @@ static bool check_pmcr(void)
 
 	pmcr = get_pmcr();
 
-	report_info("PMU implementer/ID code/counters: 0x%x(\"%c\")/0x%x/%d",
+	report_info("PMU implementer/ID code/counters: %#x(\"%c\")/%#x/%d",
 		    (pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK,
 		    ((pmcr >> PMU_PMCR_IMP_SHIFT) & PMU_PMCR_IMP_MASK) ? : ' ',
 		    (pmcr >> PMU_PMCR_ID_SHIFT) & PMU_PMCR_ID_MASK,
lib/alloc.c +5 -5
index 58af52b360f4..d553a7ec4e1f 100644
@@ -25,7 +25,7 @@ void phys_alloc_show(void)
 	int i;
 
 	spin_lock(&lock);
-	printf("phys_alloc minimum alignment: 0x%" PRIx64 "\n",
+	printf("phys_alloc minimum alignment: %#" PRIx64 "\n",
 		(u64)align_min);
 	for (i = 0; i < nr_regions; ++i)
 		printf("%016" PRIx64 "-%016" PRIx64 " [%s]\n",
@@ -75,10 +75,10 @@ static phys_addr_t phys_alloc_aligned_safe(phys_addr_t size,
 	size += addr - base;
 
 	if ((top_safe - base) < size) {
-		printf("phys_alloc: requested=0x%" PRIx64
-		       " (align=0x%" PRIx64 "), "
-		       "need=0x%" PRIx64 ", but free=0x%" PRIx64 ". "
-		       "top=0x%" PRIx64 ", top_safe=0x%" PRIx64 "\n",
+		printf("phys_alloc: requested=%#" PRIx64
+		       " (align=%#" PRIx64 "), "
+		       "need=%#" PRIx64 ", but free=%#" PRIx64 ". "
+		       "top=%#" PRIx64 ", top_safe=%#" PRIx64 "\n",
 		       (u64)size_orig, (u64)align, (u64)size, top_safe - base,
 		       (u64)top, top_safe);
 		spin_unlock(&lock);
lib/arm64/processor.c +3 -3
index deeab4ec9c8a..165980a97d44 100644
@@ -114,14 +114,14 @@ static void bad_exception(enum vector v, struct pt_regs *regs,
 			printf("Got bad vector=%d\n", v);
 	} else if (esr_valid) {
 		if (ec_names[ec])
-			printf("Unhandled exception ec=0x%x (%s)\n", ec,
+			printf("Unhandled exception ec=%#x (%s)\n", ec,
 					ec_names[ec]);
 		else
-			printf("Got bad ec=0x%x\n", ec);
+			printf("Got bad ec=%#x\n", ec);
 	}
 
 	printf("Vector: %d (%s)\n", v, vector_names[v]);
-	printf("ESR_EL1: %8s%08x, ec=0x%x (%s)\n", "", esr, ec, ec_names[ec]);
+	printf("ESR_EL1: %8s%08x, ec=%#x (%s)\n", "", esr, ec, ec_names[ec]);
 	printf("FAR_EL1: %016lx (%svalid)\n", far, far_valid ? "" : "not ");
 	printf("Exception frame registers:\n");
 	show_regs(regs);
lib/pci-edu.c +1 -1
index 58ce0b56554d..0e0310545f2b 100644
@@ -48,7 +48,7 @@ void edu_dma(struct pci_edu_dev *dev, iova_t iova,
 	assert(size <= EDU_DMA_SIZE_MAX);
 	assert(dev_offset < EDU_DMA_SIZE_MAX);
 
-	printf("edu device DMA start %s addr 0x%" PRIx64 " size %lu off 0x%x\n",
+	printf("edu device DMA start %s addr %#" PRIx64 " size %lu off %#x\n",
 	       from_device ? "FROM" : "TO",
 	       iova, (ulong)size, dev_offset);
 
lib/pci.c +3 -3
index bbd576344049..daa33e1fd4c2 100644
@@ -49,7 +49,7 @@ bool pci_setup_msi(struct pci_dev *dev, uint64_t msi_addr, uint32_t msi_data)
 	assert(dev);
 
 	if (!dev->msi_offset) {
-		printf("MSI: dev 0x%x does not support MSI.\n", dev->bdf);
+		printf("MSI: dev %#x does not support MSI.\n", dev->bdf);
 		return false;
 	}
 
@@ -295,10 +295,10 @@ static void pci_cap_print(struct pci_dev *dev, int cap_offset, int cap_id)
 		break;
 	}
 	default:
-		printf("\tcapability 0x%02x ", cap_id);
+		printf("\tcapability %#04x ", cap_id);
 		break;
 	}
-	printf("at offset 0x%02x\n", cap_offset);
+	printf("at offset %#04x\n", cap_offset);
 }
 
 void pci_dev_print(struct pci_dev *dev)
lib/powerpc/processor.c +1 -1
index 8910f24f1674..ec85b9d854e9 100644
@@ -38,7 +38,7 @@ void do_handle_exception(struct pt_regs *regs)
 		return;
 	}
 
-	printf("unhandled cpu exception 0x%lx\n", regs->trap);
+	printf("unhandled cpu exception %#lx\n", regs->trap);
 	abort();
 }
 
lib/x86/desc.c +1 -1
index 402204ddcac4..4747b32e1248 100644
@@ -69,7 +69,7 @@ static void unhandled_exception(struct ex_regs *regs, bool cpu)
 	       cpu ? "cpu " : "", regs->vector,
 	       exception_mnemonic(regs->vector), regs->rip);
 	if (regs->vector == 14)
-		printf("PF at 0x%lx addr 0x%lx\n", regs->rip, read_cr2());
+		printf("PF at %#lx addr %#lx\n", regs->rip, read_cr2());
 
 	printf("error_code=%04lx      rflags=%08lx      cs=%08lx\n"
 	       "rax=%016lx rcx=%016lx rdx=%016lx rbx=%016lx\n"
lib/x86/intel-iommu.c +8 -8
index 7cc5a702aed4..9fdbd3bac6d1 100644
@@ -115,9 +115,9 @@ static void vtd_dump_init_info(void)
 	/* Major version >= 1 */
 	assert(((version >> 3) & 0xf) >= 1);
 
-	printf("VT-d version:   0x%x\n", version);
-	printf("     cap:       0x%016lx\n", vtd_readq(DMAR_CAP_REG));
-	printf("     ecap:      0x%016lx\n", vtd_readq(DMAR_ECAP_REG));
+	printf("VT-d version:   %#x\n", version);
+	printf("     cap:       %#018lx\n", vtd_readq(DMAR_CAP_REG));
+	printf("     ecap:      %#018lx\n", vtd_readq(DMAR_ECAP_REG));
 }
 
 static void vtd_setup_root_table(void)
@@ -127,7 +127,7 @@ static void vtd_setup_root_table(void)
 	memset(root, 0, PAGE_SIZE);
 	vtd_writeq(DMAR_RTADDR_REG, virt_to_phys(root));
 	vtd_gcmd_or(VTD_GCMD_ROOT);
-	printf("DMAR table address: 0x%016lx\n", vtd_root_table());
+	printf("DMAR table address: %#018lx\n", vtd_root_table());
 }
 
 static void vtd_setup_ir_table(void)
@@ -138,7 +138,7 @@ static void vtd_setup_ir_table(void)
 	/* 0xf stands for table size (2^(0xf+1) == 65536) */
 	vtd_writeq(DMAR_IRTA_REG, virt_to_phys(root) | 0xf);
 	vtd_gcmd_or(VTD_GCMD_IR_TABLE);
-	printf("IR table address: 0x%016lx\n", vtd_ir_table());
+	printf("IR table address: %#018lx\n", vtd_ir_table());
 }
 
 static void vtd_install_pte(vtd_pte_t *root, iova_t iova,
@@ -219,14 +219,14 @@ void vtd_map_range(uint16_t sid, iova_t iova, phys_addr_t pa, size_t size)
 		ce->present = 1;
 		/* No error reporting yet */
 		ce->disable_fault_report = 1;
-		printf("allocated vt-d context entry for devfn 0x%x\n",
+		printf("allocated vt-d context entry for devfn %#x\n",
 		       devfn);
 	} else
 		slptptr = phys_to_virt(ce->slptptr << VTD_PAGE_SHIFT);
 
 	while (size) {
 		/* TODO: currently we only map 4K pages (level = 1) */
-		printf("map 4K page IOVA 0x%lx to 0x%lx (sid=0x%04x)\n",
+		printf("map 4K page IOVA %#lx to %#lx (sid=%#06x)\n",
 		       iova, pa, sid);
 		vtd_install_pte(slptptr, iova, pa, 1);
 		size -= VTD_PAGE_SIZE;
@@ -324,7 +324,7 @@ bool vtd_setup_msi(struct pci_dev *dev, int vector, int dest_id)
 	msi_addr.head = 0xfee;
 	msi_data.subhandle = 0;
 
-	printf("%s: msi_addr=0x%" PRIx64 ", msi_data=0x%x\n", __func__,
+	printf("%s: msi_addr=%#" PRIx64 ", msi_data=%#x\n", __func__,
 		*(uint64_t *)&msi_addr, *(uint32_t *)&msi_data);
 
 	return pci_setup_msi(dev, *(uint64_t *)&msi_addr,
lib/x86/vm.c +2 -2
index d52bb53fcf5e..9b5f9220aa8b 100644
@@ -12,10 +12,10 @@ static void free_memory(void *mem, unsigned long size)
 	assert_msg((unsigned long) mem % PAGE_SIZE == 0,
 		   "mem not page aligned: %p", mem);
 
-	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: 0x%lx", size);
+	assert_msg(size % PAGE_SIZE == 0, "size not page aligned: %#lx", size);
 
 	assert_msg(size == 0 || mem + size > mem,
-		   "mem + size overflow: %p + 0x%lx", mem, size);
+		   "mem + size overflow: %p + %#lx", mem, size);
 
 	if (size == 0) {
 		free = NULL;
powerpc/emulator.c +2 -2
index 04c448014ae9..a7f454e02226 100644
@@ -14,7 +14,7 @@ static void program_check_handler(struct pt_regs *regs, void *opaque)
 	int *data = opaque;
 
 	if (verbose) {
-		printf("Detected invalid instruction 0x%016lx: %08x\n",
+		printf("Detected invalid instruction %#018lx: %08x\n",
 		       regs->nip, *(uint32_t*)regs->nip);
 	}
 
@@ -36,7 +36,7 @@ static void alignment_handler(struct pt_regs *regs, void *opaque)
 	int *data = opaque;
 
 	if (verbose) {
-		printf("Detected alignment exception 0x%016lx: %08x\n",
+		printf("Detected alignment exception %#018lx: %08x\n",
 		       regs->nip, *(uint32_t*)regs->nip);
 	}
 
powerpc/spapr_hcall.c +1 -1
index bd6c287a2f0c..ce495f45ca94 100644
@@ -49,7 +49,7 @@ static void test_h_set_sprg0(int argc, char **argv)
 
 	rc = hcall(H_SET_SPRG0, sprg0_orig);
 	sprg0 = mfspr(SPR_SPRG0);
-	report("sprg0 = 0x%" PRIx64,
+	report("sprg0 = %#" PRIx64,
 		rc == H_SUCCESS && sprg0 == sprg0_orig, sprg0_orig);
 }
 
powerpc/sprs.c +2 -2
index 9227eff9d81a..39644fab9b70 100644
@@ -271,7 +271,7 @@ int main(int argc, char **argv)
 		}
 	}
 
-	printf("Settings SPRs to 0x%lx...\n", pat);
+	printf("Settings SPRs to %#lx...\n", pat);
 	set_sprs(pat);
 
 	memset(before, 0, sizeof(before));
@@ -296,7 +296,7 @@ int main(int argc, char **argv)
 	puts("Checking SPRs...\n");
 	for (i = 0; i < 1024; i++) {
 		if (before[i] != 0 || after[i] != 0)
-			report("SPR %d:\t0x%016lx <==> 0x%016lx",
+			report("SPR %d:\t%#018lx <==> %#018lx",
 				before[i] == after[i], i, before[i], after[i]);
 	}
 
x86/asyncpf.c +2 -2
index fea1d28ef375..f04d7cb5ad7a 100644
@@ -52,13 +52,13 @@ static void pf_isr(struct ex_regs *r)
 
 	switch (reason) {
 		case 0:
-			report("unexpected #PF at 0x%lx", false, read_cr2());
+			report("unexpected #PF at %#lx", false, read_cr2());
 			break;
 		case KVM_PV_REASON_PAGE_NOT_PRESENT:
 			phys = virt_to_phys_cr3(virt);
 			install_pte(phys_to_virt(read_cr3()), 1, virt, phys, 0);
 			write_cr3(read_cr3());
-			report("Got not present #PF token %lx virt addr %p phys addr 0x%" PRIx64,
+			report("Got not present #PF token %lx virt addr %p phys addr %#" PRIx64,
 					true, read_cr2(), virt, phys);
 			while(phys) {
 				safe_halt(); /* enables irq */
x86/hyperv_synic.c +1 -1
index d1f75c588e1c..a08e69f7da5f 100644
@@ -90,7 +90,7 @@ static void synic_test_prepare(void *ctx)
     }
     r = rdmsr(HV_X64_MSR_EOM);
     if (r != 0) {
-        report("Hyper-V SynIC test, EOM read 0x%" PRIx64, false, r);
+        report("Hyper-V SynIC test, EOM read %#" PRIx64, false, r);
         goto ret;
     }
 
x86/msr.c +2 -2
index ded9424900eb..ab97d21d3acb 100644
@@ -82,13 +82,13 @@ static void test_msr_rw(int msr_index, unsigned long long input, unsigned long l
     if ((index = find_msr_info(msr_index)) != -1) {
         sptr = msr_info[index].name;
     } else {
-        printf("couldn't find name for msr # 0x%x, skipping\n", msr_index);
+        printf("couldn't find name for msr # %#x, skipping\n", msr_index);
         return;
     }
     wrmsr(msr_index, input);
     r = rdmsr(msr_index);
     if (expected != r) {
-        printf("testing %s: output = 0x%x:0x%x expected = 0x%x:0x%x\n", sptr,
+        printf("testing %s: output = %#x:%#x expected = %#x:%#x\n", sptr,
                (u32)(r >> 32), (u32)r, (u32)(expected >> 32), (u32)expected);
     }
     report(sptr, expected == r);
x86/vmexit.c +1 -1
index 5b821b5eb125..8d2bf1a31982 100644
@@ -524,7 +524,7 @@ int main(int ac, char **av)
 		membar = pcidev.resource[PCI_TESTDEV_BAR_MEM];
 		pci_test.memaddr = ioremap(membar, PAGE_SIZE);
 		pci_test.iobar = pcidev.resource[PCI_TESTDEV_BAR_IO];
-		printf("pci-testdev at 0x%x membar %lx iobar %x\n",
+		printf("pci-testdev at %#x membar %lx iobar %x\n",
 		       pcidev.bdf, membar, pci_test.iobar);
 	}
 
x86/vmx.c +7 -7
index f7a34d20ab6a..13366180f514 100644
@@ -521,16 +521,16 @@ void print_vmexit_info()
 	guest_rsp = vmcs_read(GUEST_RSP);
 	printf("VMEXIT info:\n");
 	printf("\tvmexit reason = %ld\n", reason);
-	printf("\texit qualification = 0x%lx\n", exit_qual);
+	printf("\texit qualification = %#lx\n", exit_qual);
 	printf("\tBit 31 of reason = %lx\n", (vmcs_read(EXI_REASON) >> 31) & 1);
-	printf("\tguest_rip = 0x%lx\n", guest_rip);
-	printf("\tRAX=0x%lx    RBX=0x%lx    RCX=0x%lx    RDX=0x%lx\n",
+	printf("\tguest_rip = %#lx\n", guest_rip);
+	printf("\tRAX=%#lx    RBX=%#lx    RCX=%#lx    RDX=%#lx\n",
 		regs.rax, regs.rbx, regs.rcx, regs.rdx);
-	printf("\tRSP=0x%lx    RBP=0x%lx    RSI=0x%lx    RDI=0x%lx\n",
+	printf("\tRSP=%#lx    RBP=%#lx    RSI=%#lx    RDI=%#lx\n",
 		guest_rsp, regs.rbp, regs.rsi, regs.rdi);
-	printf("\tR8 =0x%lx    R9 =0x%lx    R10=0x%lx    R11=0x%lx\n",
+	printf("\tR8 =%#lx    R9 =%#lx    R10=%#lx    R11=%#lx\n",
 		regs.r8, regs.r9, regs.r10, regs.r11);
-	printf("\tR12=0x%lx    R13=0x%lx    R14=0x%lx    R15=0x%lx\n",
+	printf("\tR12=%#lx    R13=%#lx    R14=%#lx    R15=%#lx\n",
 		regs.r12, regs.r13, regs.r14, regs.r15);
 }
 
@@ -553,7 +553,7 @@ print_vmentry_failure_info(struct vmentry_failure *failure) {
 		u64 reason = vmcs_read(EXI_REASON);
 		u64 qual = vmcs_read(EXI_QUALIFICATION);
 
-		printf("Non-early %s failure (reason=0x%lx, qual=0x%lx): ",
+		printf("Non-early %s failure (reason=%#lx, qual=%#lx): ",
 			failure->instr, reason, qual);
 
 		switch (reason & 0xff) {
x86/vmx.h +2 -2
index 392979ba8ae8..787466653f8b 100644
@@ -755,8 +755,8 @@ do { \
 		binstr(_a, _bin_a); \
 		binstr(_b, _bin_b); \
 		report("%s:%d: %s failed: (%s) == (%s)\n" \
-		       "\tLHS: 0x%016lx - %s - %lu\n" \
-		       "\tRHS: 0x%016lx - %s - %lu%s" fmt, 0, \
+		       "\tLHS: %#018lx - %s - %lu\n" \
+		       "\tRHS: %#018lx - %s - %lu%s" fmt, 0, \
 		       __FILE__, __LINE__, \
 		       assertion ? "Assertion" : "Expectation", a_str, b_str, \
 		       (unsigned long) _a, _bin_a, (unsigned long) _a, \
x86/vmx_tests.c +1 -1
index 136cc30abf6f..1f439522cad8 100644
@@ -719,7 +719,7 @@ static int iobmp_exit_handler()
 		vmcs_write(GUEST_RIP, guest_rip + insn_len);
 		return VMX_TEST_RESUME;
 	default:
-		printf("guest_rip = 0x%lx\n", guest_rip);
+		printf("guest_rip = %#lx\n", guest_rip);
 		printf("\tERROR : Undefined exit reason, reason = %ld.\n", reason);
 		break;
 	}
x86/xsave.c +1 -1
index 52142d2cdb93..2d8ca7c2c510 100644
@@ -75,7 +75,7 @@ void test_xsave(void)
     printf("Legal instruction testing:\n");
 
     supported_xcr0 = get_supported_xcr0();
-    printf("Supported XCR0 bits: 0x%lx\n", supported_xcr0);
+    printf("Supported XCR0 bits: %#lx\n", supported_xcr0);
 
     test_bits = XSTATE_FP | XSTATE_SSE;
     report("Check minimal XSAVE required bits",
Signature · 1 line
2.13.0
maintainer
#11 lore raw
In reply to Radim Krčmář
Quoted 7 lines · Radim Krčmář

This series is a mix of three logical units:

[1-2/6]: add format checking to report() and fix revealed bugs

[3/6]: fix padding in %p conversions

[4-6/6]: fix random 0x%d and support %# to make this mistake harder

Nice!

Paolo