aboutsummaryrefslogtreecommitdiff
path: root/src/os
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2021-01-30 07:27:24 -0500
committerRuss Cox <rsc@golang.org>2021-02-19 00:01:17 +0000
commite7ee3c1fa87556245e38b662ea5b3002bbeb32b9 (patch)
tree5b4679a1cce9d75c33190edd0b2e9403ef2d815b /src/os
parenteb982727e33263c0bb67de607beb44c5e0bd2bea (diff)
downloadgo-e7ee3c1fa87556245e38b662ea5b3002bbeb32b9.tar.gz
go-e7ee3c1fa87556245e38b662ea5b3002bbeb32b9.zip
os: report Windows exit status in hex
We print things like “exit status 3221225477” but the standard Windows form is 0xc0000005. 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: Iefe447d4d1781b53bef9619f68d386f2866b2934 Reviewed-on: https://go-review.googlesource.com/c/go/+/288792 Trust: Russ Cox <rsc@golang.org> Trust: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Alex Brainman <alex.brainman@gmail.com> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: Jason A. Donenfeld <Jason@zx2c4.com> Reviewed-by: Ian Lance Taylor <iant@golang.org>
Diffstat (limited to 'src/os')
-rw-r--r--src/os/exec_posix.go7
-rw-r--r--src/os/str.go36
2 files changed, 40 insertions, 3 deletions
diff --git a/src/os/exec_posix.go b/src/os/exec_posix.go
index 7ecddaed37..39f11c7ec1 100644
--- a/src/os/exec_posix.go
+++ b/src/os/exec_posix.go
@@ -102,7 +102,12 @@ func (p *ProcessState) String() string {
res := ""
switch {
case status.Exited():
- res = "exit status " + itoa(status.ExitStatus())
+ code := status.ExitStatus()
+ if runtime.GOOS == "windows" && code >= 1<<16 { // windows uses large hex numbers
+ res = "exit status " + uitox(uint(code))
+ } else { // unix systems use small decimal integers
+ res = "exit status " + itoa(code) // unix
+ }
case status.Signaled():
res = "signal: " + status.Signal().String()
case status.Stopped():
diff --git a/src/os/str.go b/src/os/str.go
index cba9fa3e8d..9bfcc15aa8 100644
--- a/src/os/str.go
+++ b/src/os/str.go
@@ -6,7 +6,7 @@
package os
-// Convert integer to decimal string
+// itoa converts val (an int) to a decimal string.
func itoa(val int) string {
if val < 0 {
return "-" + uitoa(uint(-val))
@@ -14,7 +14,7 @@ func itoa(val int) string {
return uitoa(uint(val))
}
-// Convert unsigned integer to decimal string
+// uitoa converts val (a uint) to a decimal string.
func uitoa(val uint) string {
if val == 0 { // avoid string allocation
return "0"
@@ -31,3 +31,35 @@ func uitoa(val uint) string {
buf[i] = byte('0' + val)
return string(buf[i:])
}
+
+// itox converts val (an int) to a hexdecimal string.
+func itox(val int) string {
+ if val < 0 {
+ return "-" + uitox(uint(-val))
+ }
+ return uitox(uint(val))
+}
+
+const hex = "0123456789abcdef"
+
+// uitox converts val (a uint) to a hexdecimal string.
+func uitox(val uint) string {
+ if val == 0 { // avoid string allocation
+ return "0x0"
+ }
+ var buf [20]byte // big enough for 64bit value base 16 + 0x
+ i := len(buf) - 1
+ for val >= 16 {
+ q := val / 16
+ buf[i] = hex[val%16]
+ i--
+ val = q
+ }
+ // val < 16
+ buf[i] = hex[val%16]
+ i--
+ buf[i] = 'x'
+ i--
+ buf[i] = '0'
+ return string(buf[i:])
+}