1
0
mirror of https://github.com/golang/go synced 2024-11-26 04:07:59 -07:00

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>
This commit is contained in:
Cherry Zhang 2021-01-15 17:58:41 -05:00
parent d4aa72002e
commit 537cde0b4b
9 changed files with 368 additions and 13 deletions

View File

@ -257,6 +257,11 @@ func addGCLocals() {
if x := fn.OpenCodedDeferInfo; x != nil {
objw.Global(x, int32(len(x.P)), obj.RODATA|obj.DUPOK)
}
if x := fn.ArgInfo; x != nil {
objw.Global(x, int32(len(x.P)), obj.RODATA|obj.DUPOK)
x.Set(obj.AttrStatic, true)
x.Set(obj.AttrContentAddressable, true)
}
}
}

View File

@ -6554,6 +6554,163 @@ func (s *State) DebugFriendlySetPosFrom(v *ssa.Value) {
}
}
// emit argument info (locations on stack) for traceback.
func emitArgInfo(e *ssafn, pp *objw.Progs) {
ft := e.curfn.Type()
if ft.NumRecvs() == 0 && ft.NumParams() == 0 {
return
}
x := base.Ctxt.Lookup(fmt.Sprintf("%s.arginfo%d", e.curfn.LSym.Name, e.curfn.LSym.ABI()))
e.curfn.LSym.Func().ArgInfo = x
PtrSize := int64(types.PtrSize)
isAggregate := func(t *types.Type) bool {
return t.IsStruct() || t.IsArray() || t.IsComplex() || t.IsInterface() || t.IsString() || t.IsSlice()
}
// Populate the data.
// The data is a stream of bytes, which contains the offsets and sizes of the
// non-aggregate arguments or non-aggregate fields/elements of aggregate-typed
// arguments, along with special "operators". Specifically,
// - for each non-aggrgate arg/field/element, its offset from FP (1 byte) and
// size (1 byte)
// - special operators:
// - 0xff - end of sequence
// - 0xfe - print { (at the start of an aggregate-typed argument)
// - 0xfd - print } (at the end of an aggregate-typed argument)
// - 0xfc - print ... (more args/fields/elements)
// - 0xfb - print _ (offset too large)
// These constants need to be in sync with runtime.traceback.go:printArgs.
const (
_endSeq = 0xff
_startAgg = 0xfe
_endAgg = 0xfd
_dotdotdot = 0xfc
_offsetTooLarge = 0xfb
_special = 0xf0 // above this are operators, below this are ordinary offsets
)
const (
limit = 10 // print no more than 10 args/components
maxDepth = 5 // no more than 5 layers of nesting
// maxLen is a (conservative) upper bound of the byte stream length. For
// each arg/component, it has no more than 2 bytes of data (size, offset),
// and no more than one {, }, ... at each level (it cannot have both the
// data and ... unless it is the last one, just be conservative). Plus 1
// for _endSeq.
maxLen = (maxDepth*3+2)*limit + 1
)
wOff := 0
n := 0
writebyte := func(o uint8) { wOff = objw.Uint8(x, wOff, o) }
// Write one non-aggrgate arg/field/element if there is room.
// Returns whether to continue.
write1 := func(sz, offset int64) bool {
if n >= limit {
return false
}
if offset >= _special {
writebyte(_offsetTooLarge)
} else {
writebyte(uint8(offset))
writebyte(uint8(sz))
}
n++
return true
}
// Visit t recursively and write it out.
// Returns whether to continue visiting.
var visitType func(baseOffset int64, t *types.Type, depth int) bool
visitType = func(baseOffset int64, t *types.Type, depth int) bool {
if n >= limit {
return false
}
if !isAggregate(t) {
return write1(t.Size(), baseOffset)
}
writebyte(_startAgg)
depth++
if depth >= maxDepth {
writebyte(_dotdotdot)
writebyte(_endAgg)
n++
return true
}
var r bool
switch {
case t.IsInterface(), t.IsString():
r = write1(PtrSize, baseOffset) &&
write1(PtrSize, baseOffset+PtrSize)
case t.IsSlice():
r = write1(PtrSize, baseOffset) &&
write1(PtrSize, baseOffset+PtrSize) &&
write1(PtrSize, baseOffset+PtrSize*2)
case t.IsComplex():
r = write1(t.Size()/2, baseOffset) &&
write1(t.Size()/2, baseOffset+t.Size()/2)
case t.IsArray():
r = true
if t.NumElem() == 0 {
n++ // {} counts as a component
break
}
for i := int64(0); i < t.NumElem(); i++ {
if !visitType(baseOffset, t.Elem(), depth) {
r = false
break
}
baseOffset += t.Elem().Size()
}
case t.IsStruct():
r = true
if t.NumFields() == 0 {
n++ // {} counts as a component
break
}
for _, field := range t.Fields().Slice() {
if !visitType(baseOffset+field.Offset, field.Type, depth) {
r = false
break
}
}
}
if !r {
writebyte(_dotdotdot)
}
writebyte(_endAgg)
return r
}
c := true
outer:
for _, fs := range &types.RecvsParams {
for _, a := range fs(ft).Fields().Slice() {
if !c {
writebyte(_dotdotdot)
break outer
}
c = visitType(a.Offset, a.Type, 0)
}
}
writebyte(_endSeq)
if wOff > maxLen {
base.Fatalf("ArgInfo too large")
}
// Emit a funcdata pointing at the arg info data.
p := pp.Prog(obj.AFUNCDATA)
p.From.SetConst(objabi.FUNCDATA_ArgInfo)
p.To.Type = obj.TYPE_MEM
p.To.Name = obj.NAME_EXTERN
p.To.Sym = x
}
// genssa appends entries to pp for each instruction in f.
func genssa(f *ssa.Func, pp *objw.Progs) {
var s State
@ -6562,6 +6719,7 @@ func genssa(f *ssa.Func, pp *objw.Progs) {
e := f.Frontend().(*ssafn)
s.livenessMap, s.partLiveArgs = liveness.Compute(e.curfn, f, e.stkptrsize, pp)
emitArgInfo(e, pp)
openDeferInfo := e.curfn.LSym.Func().OpenCodedDeferInfo
if openDeferInfo != nil {

View File

@ -485,6 +485,7 @@ type FuncInfo struct {
GCLocals *LSym
StackObjects *LSym
OpenCodedDeferInfo *LSym
ArgInfo *LSym // argument info for traceback
FuncInfoSym *LSym
}

View File

@ -20,6 +20,7 @@ const (
FUNCDATA_StackObjects = 2
FUNCDATA_InlTree = 3
FUNCDATA_OpenCodedDeferInfo = 4
FUNCDATA_ArgInfo = 5
// ArgsSizeUnknown is set in Func.argsize to mark all functions
// whose argument size is unknown (C vararg functions, and

View File

@ -40,10 +40,10 @@ func TestDeadcode(t *testing.T) {
if err != nil {
t.Fatalf("%v: %v:\n%s", cmd.Args, err, out)
}
if test.pos != "" && !bytes.Contains(out, []byte(test.pos)) {
if test.pos != "" && !bytes.Contains(out, []byte(test.pos+"\n")) {
t.Errorf("%s should be reachable. Output:\n%s", test.pos, out)
}
if test.neg != "" && bytes.Contains(out, []byte(test.neg)) {
if test.neg != "" && bytes.Contains(out, []byte(test.neg+"\n")) {
t.Errorf("%s should not be reachable. Output:\n%s", test.neg, out)
}
})

View File

@ -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.

View File

@ -281,6 +281,7 @@ const (
_FUNCDATA_StackObjects = 2
_FUNCDATA_InlTree = 3
_FUNCDATA_OpenCodedDeferInfo = 4
_FUNCDATA_ArgInfo = 5
_ArgsSizeUnknown = -0x80000000
)

View File

@ -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 {

View File

@ -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
}