aboutsummaryrefslogtreecommitdiff
path: root/src/runtime
diff options
context:
space:
mode:
authorCherry Zhang <cherryyz@google.com>2021-01-15 17:58:41 -0500
committerCherry Zhang <cherryyz@google.com>2021-04-22 17:47:59 +0000
commit537cde0b4b411f1dc3016cac430b9494cf91caf0 (patch)
tree6c0e4d168e328702d73f85e00eb4c894bc5bac05 /src/runtime
parentd4aa72002e76c09f81a8fd82f37781f5126c9cbe (diff)
downloadgo-537cde0b4b411f1dc3016cac430b9494cf91caf0.tar.xz
cmd/compile, runtime: add metadata for argument printing in traceback
Currently, when the runtime printing a stack track (at panic, or when runtime.Stack is called), it prints the function arguments as words in memory. With a register-based calling convention, the layout of argument area of the memory changes, so the printing also needs to change. In particular, the memory order and the syntax order of the arguments may differ. To address that, this CL lets the compiler to emit some metadata about the memory layout of the arguments, and the runtime will use this information to print arguments in syntax order. Previously we print the memory contents of the results along with the arguments. The results are likely uninitialized when the traceback is taken, so that information is rarely useful. Also, with a register-based calling convention the results may not have corresponding locations in memory. This CL changes it to not print results. Previously the runtime simply prints the memory contents as pointer-sized words. With a register-based calling convention, as the layout changes, arguments that were packed in one word may no longer be in one word. Also, as the spill slots are not always initialized, it is possible that some part of a word contains useful informationwhile the rest contains garbage. Instead of letting the runtime recreating the ABI0 layout and print them as words, we now print each component separately. Aggregate-typed argument/component is surrounded by "{}". For example, for a function F(int, [3]byte, byte) int when called as F(1, [3]byte{2, 3, 4}, 5), it used to print F(0x1, 0x5040302, 0xXXXXXXXX) // assuming little endian, 0xXXXXXXXX is uninitilized result Now prints F(0x1, {0x2, 0x3, 0x4}, 0x5). Note: the liveness tracking of the spill splots has not been implemented in this CL. Currently the runtime just assumes all the slots are live and print them all. Increase binary sizes by ~1.5%. old new hello (println) 1171328 1187712 (+1.4%) hello (fmt) 1877024 1901600 (+1.3%) cmd/compile 22326928 22662800 (+1.5%) cmd/go 13505024 13726208 (+1.6%) Updates #40724. Change-Id: I351e0bf497f99bdbb3f91df2fb17e3c2c5c316dc Reviewed-on: https://go-review.googlesource.com/c/go/+/304470 Trust: Cherry Zhang <cherryyz@google.com> Run-TryBot: Cherry Zhang <cherryyz@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Michael Knyszek <mknyszek@google.com>
Diffstat (limited to 'src/runtime')
-rw-r--r--src/runtime/funcdata.h1
-rw-r--r--src/runtime/symtab.go1
-rw-r--r--src/runtime/traceback.go89
-rw-r--r--src/runtime/traceback_test.go121
4 files changed, 201 insertions, 11 deletions
diff --git a/src/runtime/funcdata.h b/src/runtime/funcdata.h
index 798dbaceab..1002b181e4 100644
--- a/src/runtime/funcdata.h
+++ b/src/runtime/funcdata.h
@@ -17,6 +17,7 @@
#define FUNCDATA_StackObjects 2
#define FUNCDATA_InlTree 3
#define FUNCDATA_OpenCodedDeferInfo 4 /* info for func with open-coded defers */
+#define FUNCDATA_ArgInfo 5
// Pseudo-assembly statements.
diff --git a/src/runtime/symtab.go b/src/runtime/symtab.go
index e8c7447f61..6b535dfcbf 100644
--- a/src/runtime/symtab.go
+++ b/src/runtime/symtab.go
@@ -281,6 +281,7 @@ const (
_FUNCDATA_StackObjects = 2
_FUNCDATA_InlTree = 3
_FUNCDATA_OpenCodedDeferInfo = 4
+ _FUNCDATA_ArgInfo = 5
_ArgsSizeUnknown = -0x80000000
)
diff --git a/src/runtime/traceback.go b/src/runtime/traceback.go
index 0969af1a21..167d51c452 100644
--- a/src/runtime/traceback.go
+++ b/src/runtime/traceback.go
@@ -457,17 +457,8 @@ func gentraceback(pc0, sp0, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max in
name = "panic"
}
print(name, "(")
- argp := (*[100]uintptr)(unsafe.Pointer(frame.argp))
- for i := uintptr(0); i < frame.arglen/sys.PtrSize; i++ {
- if i >= 10 {
- print(", ...")
- break
- }
- if i != 0 {
- print(", ")
- }
- print(hex(argp[i]))
- }
+ argp := unsafe.Pointer(frame.argp)
+ printArgs(f, argp)
print(")\n")
print("\t", file, ":", line)
if frame.pc > f.entry {
@@ -579,6 +570,82 @@ func gentraceback(pc0, sp0, lr0 uintptr, gp *g, skip int, pcbuf *uintptr, max in
return n
}
+// printArgs prints function arguments in traceback.
+func printArgs(f funcInfo, argp unsafe.Pointer) {
+ // The "instruction" of argument printing is encoded in _FUNCDATA_ArgInfo.
+ // See cmd/compile/internal/ssagen.emitArgInfo for the description of the
+ // encoding.
+ // These constants need to be in sync with the compiler.
+ const (
+ _endSeq = 0xff
+ _startAgg = 0xfe
+ _endAgg = 0xfd
+ _dotdotdot = 0xfc
+ _offsetTooLarge = 0xfb
+ )
+
+ const (
+ limit = 10 // print no more than 10 args/components
+ maxDepth = 5 // no more than 5 layers of nesting
+ maxLen = (maxDepth*3+2)*limit + 1 // max length of _FUNCDATA_ArgInfo (see the compiler side for reasoning)
+ )
+
+ p := (*[maxLen]uint8)(funcdata(f, _FUNCDATA_ArgInfo))
+ if p == nil {
+ return
+ }
+
+ print1 := func(off, sz uint8) {
+ x := readUnaligned64(add(argp, uintptr(off)))
+ // mask out irrelavant bits
+ if sz < 8 {
+ shift := 64 - sz*8
+ if sys.BigEndian {
+ x = x >> shift
+ } else {
+ x = x << shift >> shift
+ }
+ }
+ print(hex(x))
+ }
+
+ start := true
+ printcomma := func() {
+ if !start {
+ print(", ")
+ }
+ }
+ pi := 0
+printloop:
+ for {
+ o := p[pi]
+ pi++
+ switch o {
+ case _endSeq:
+ break printloop
+ case _startAgg:
+ printcomma()
+ print("{")
+ start = true
+ continue
+ case _endAgg:
+ print("}")
+ case _dotdotdot:
+ printcomma()
+ print("...")
+ case _offsetTooLarge:
+ printcomma()
+ print("_")
+ default:
+ printcomma()
+ sz := p[pi]
+ pi++
+ print1(o, sz)
+ }
+ start = false
+ }
+}
+
// reflectMethodValue is a partial duplicate of reflect.makeFuncImpl
// and reflect.methodValue.
type reflectMethodValue struct {
diff --git a/src/runtime/traceback_test.go b/src/runtime/traceback_test.go
new file mode 100644
index 0000000000..2a0497e9a9
--- /dev/null
+++ b/src/runtime/traceback_test.go
@@ -0,0 +1,121 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package runtime_test
+
+import (
+ "bytes"
+ "runtime"
+ "testing"
+)
+
+var testTracebackArgsBuf [1000]byte
+
+func TestTracebackArgs(t *testing.T) {
+ tests := []struct {
+ fn func() int
+ expect string
+ }{
+ // simple ints
+ {
+ func() int { return testTracebackArgs1(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) },
+ "testTracebackArgs1(0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, ...)",
+ },
+ // some aggregates
+ {
+ func() int {
+ return testTracebackArgs2(false, struct {
+ a, b, c int
+ x [2]int
+ }{1, 2, 3, [2]int{4, 5}}, [0]int{}, [3]byte{6, 7, 8})
+ },
+ "testTracebackArgs2(0x0, {0x1, 0x2, 0x3, {0x4, 0x5}}, {}, {0x6, 0x7, 0x8})",
+ },
+ {
+ func() int { return testTracebackArgs3([3]byte{1, 2, 3}, 4, 5, 6, [3]byte{7, 8, 9}) },
+ "testTracebackArgs3({0x1, 0x2, 0x3}, 0x4, 0x5, 0x6, {0x7, 0x8, 0x9})",
+ },
+ // too deeply nested type
+ {
+ func() int { return testTracebackArgs4(false, [1][1][1][1][1][1][1][1][1][1]int{}) },
+ "testTracebackArgs4(0x0, {{{{{...}}}}})",
+ },
+ // a lot of zero-sized type
+ {
+ func() int {
+ z := [0]int{}
+ return testTracebackArgs5(false, struct {
+ x int
+ y [0]int
+ z [2][0]int
+ }{1, z, [2][0]int{}}, z, z, z, z, z, z, z, z, z, z, z, z)
+ },
+ "testTracebackArgs5(0x0, {0x1, {}, {{}, {}}}, {}, {}, {}, {}, {}, ...)",
+ },
+ }
+ for _, test := range tests {
+ n := test.fn()
+ got := testTracebackArgsBuf[:n]
+ if !bytes.Contains(got, []byte(test.expect)) {
+ t.Errorf("traceback does not contain expected string: want %q, got\n%s", test.expect, got)
+ }
+ }
+}
+
+//go:noinline
+func testTracebackArgs1(a, b, c, d, e, f, g, h, i, j, k, l int) int {
+ n := runtime.Stack(testTracebackArgsBuf[:], false)
+ if a < 0 {
+ // use in-reg args to keep them alive
+ return a + b + c + d + e + f + g + h + i + j + k + l
+ }
+ return n
+}
+
+//go:noinline
+func testTracebackArgs2(a bool, b struct {
+ a, b, c int
+ x [2]int
+}, _ [0]int, d [3]byte) int {
+ n := runtime.Stack(testTracebackArgsBuf[:], false)
+ if a {
+ // use in-reg args to keep them alive
+ return b.a + b.b + b.c + b.x[0] + b.x[1] + int(d[0]) + int(d[1]) + int(d[2])
+ }
+ return n
+
+}
+
+//go:noinline
+//go:registerparams
+func testTracebackArgs3(x [3]byte, a, b, c int, y [3]byte) int {
+ n := runtime.Stack(testTracebackArgsBuf[:], false)
+ if a < 0 {
+ // use in-reg args to keep them alive
+ return int(x[0]) + int(x[1]) + int(x[2]) + a + b + c + int(y[0]) + int(y[1]) + int(y[2])
+ }
+ return n
+}
+
+//go:noinline
+func testTracebackArgs4(a bool, x [1][1][1][1][1][1][1][1][1][1]int) int {
+ n := runtime.Stack(testTracebackArgsBuf[:], false)
+ if a {
+ panic(x) // use args to keep them alive
+ }
+ return n
+}
+
+//go:noinline
+func testTracebackArgs5(a bool, x struct {
+ x int
+ y [0]int
+ z [2][0]int
+}, _, _, _, _, _, _, _, _, _, _, _, _ [0]int) int {
+ n := runtime.Stack(testTracebackArgsBuf[:], false)
+ if a {
+ panic(x) // use args to keep them alive
+ }
+ return n
+}