From a1e9148e3dbb20a18e0139583e7d835cc7a820bf Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 26 Jan 2021 21:26:01 -0500 Subject: [PATCH] runtime: print hex numbers with hex prefixes in traceback debug If traceback fails, it prints a helpful hex dump of the stack. But the hex numbers have no 0x prefix, which might make it a little unclear that they are hex. We only print two per line, so there is plenty of room for the 0x. Print it, which lets us delete a custom hex formatter. Also, in the translated hints, print off in hex (with a 0x prefix). The offsets were previously decimal, which could have been confused for hex since none of the hex had 0x prefixes. And decimal is kind of useless anyway since the offsets shown in the main traceback are hex, so you can't easily match them up without mental base conversions. Just print hex everywhere, clearly marked by 0x. This CL is part of a stack adding windows/arm64 support (#36439), intended to land in the Go 1.17 cycle. This CL is, however, not windows/arm64-specific. It is cleanup meant to make the port (and future ports) easier. Change-Id: I72d26a4e41ada38b620bf8fe3576d787a2e59b47 Reviewed-on: https://go-review.googlesource.com/c/go/+/288809 Trust: Russ Cox Reviewed-by: Cherry Zhang --- src/runtime/print.go | 26 ++++++++------------------ 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/runtime/print.go b/src/runtime/print.go index 64055a34cc..f15296cf02 100644 --- a/src/runtime/print.go +++ b/src/runtime/print.go @@ -216,13 +216,15 @@ func printint(v int64) { printuint(uint64(v)) } +var minhexdigits = 0 // protected by printlock + func printhex(v uint64) { const dig = "0123456789abcdef" var buf [100]byte i := len(buf) for i--; i > 0; i-- { buf[i] = dig[v%16] - if v < 16 { + if v < 16 && len(buf)-i >= minhexdigits { break } v /= 16 @@ -265,29 +267,16 @@ func printiface(i iface) { // and should return a character mark to appear just before that // word's value. It can return 0 to indicate no mark. func hexdumpWords(p, end uintptr, mark func(uintptr) byte) { - p1 := func(x uintptr) { - var buf [2 * sys.PtrSize]byte - for i := len(buf) - 1; i >= 0; i-- { - if x&0xF < 10 { - buf[i] = byte(x&0xF) + '0' - } else { - buf[i] = byte(x&0xF) - 10 + 'a' - } - x >>= 4 - } - gwrite(buf[:]) - } - printlock() var markbuf [1]byte markbuf[0] = ' ' + minhexdigits = int(unsafe.Sizeof(uintptr(0)) * 2) for i := uintptr(0); p+i < end; i += sys.PtrSize { if i%16 == 0 { if i != 0 { println() } - p1(p + i) - print(": ") + print(hex(p+i), ": ") } if mark != nil { @@ -298,15 +287,16 @@ func hexdumpWords(p, end uintptr, mark func(uintptr) byte) { } gwrite(markbuf[:]) val := *(*uintptr)(unsafe.Pointer(p + i)) - p1(val) + print(hex(val)) print(" ") // Can we symbolize val? fn := findfunc(val) if fn.valid() { - print("<", funcname(fn), "+", val-fn.entry, "> ") + print("<", funcname(fn), "+", hex(val-fn.entry), "> ") } } + minhexdigits = 0 println() printunlock() }