aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/ssagen
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/cmd/compile/internal/ssagen
parentd4aa72002e76c09f81a8fd82f37781f5126c9cbe (diff)
downloadgo-537cde0b4b411f1dc3016cac430b9494cf91caf0.tar.gz
go-537cde0b4b411f1dc3016cac430b9494cf91caf0.zip
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/cmd/compile/internal/ssagen')
-rw-r--r--src/cmd/compile/internal/ssagen/ssa.go158
1 files changed, 158 insertions, 0 deletions
diff --git a/src/cmd/compile/internal/ssagen/ssa.go b/src/cmd/compile/internal/ssagen/ssa.go
index 10f02fc987..c293e4db19 100644
--- a/src/cmd/compile/internal/ssagen/ssa.go
+++ b/src/cmd/compile/internal/ssagen/ssa.go
@@ -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 {