1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package runtime 6 7 import "internal/abi" 8 9 // inlinedCall is the encoding of entries in the FUNCDATA_InlTree table. 10 type inlinedCall struct { 11 funcID abi.FuncID // type of the called function 12 _ [3]byte 13 nameOff int32 // offset into pclntab for name of called function 14 parentPc int32 // position of an instruction whose source position is the call site (offset from entry) 15 startLine int32 // line number of start of function (func keyword/TEXT directive) 16 } 17 18 // An inlineUnwinder iterates over the stack of inlined calls at a PC by 19 // decoding the inline table. The last step of iteration is always the frame of 20 // the physical function, so there's always at least one frame. 21 // 22 // This is typically used as: 23 // 24 // for u, uf := newInlineUnwinder(...); uf.valid(); uf = u.next(uf) { ... } 25 // 26 // Implementation note: This is used in contexts that disallow write barriers. 27 // Hence, the constructor returns this by value and pointer receiver methods 28 // must not mutate pointer fields. Also, we keep the mutable state in a separate 29 // struct mostly to keep both structs SSA-able, which generates much better 30 // code. 31 type inlineUnwinder struct { 32 f funcInfo 33 inlTree *[1 << 20]inlinedCall 34 } 35 36 // An inlineFrame is a position in an inlineUnwinder. 37 type inlineFrame struct { 38 // pc is the PC giving the file/line metadata of the current frame. This is 39 // always a "call PC" (not a "return PC"). This is 0 when the iterator is 40 // exhausted. 41 pc uintptr 42 43 // index is the index of the current record in inlTree, or -1 if we are in 44 // the outermost function. 45 index int32 46 } 47 48 // newInlineUnwinder creates an inlineUnwinder initially set to the inner-most 49 // inlined frame at PC. PC should be a "call PC" (not a "return PC"). 50 // 51 // This unwinder uses non-strict handling of PC because it's assumed this is 52 // only ever used for symbolic debugging. If things go really wrong, it'll just 53 // fall back to the outermost frame. 54 func newInlineUnwinder(f funcInfo, pc uintptr) (inlineUnwinder, inlineFrame) { 55 inldata := funcdata(f, abi.FUNCDATA_InlTree) 56 if inldata == nil { 57 return inlineUnwinder{f: f}, inlineFrame{pc: pc, index: -1} 58 } 59 inlTree := (*[1 << 20]inlinedCall)(inldata) 60 u := inlineUnwinder{f: f, inlTree: inlTree} 61 return u, u.resolveInternal(pc) 62 } 63 64 func (u *inlineUnwinder) resolveInternal(pc uintptr) inlineFrame { 65 return inlineFrame{ 66 pc: pc, 67 // Conveniently, this returns -1 if there's an error, which is the same 68 // value we use for the outermost frame. 69 index: pcdatavalue1(u.f, abi.PCDATA_InlTreeIndex, pc, false), 70 } 71 } 72 73 func (uf inlineFrame) valid() bool { 74 return uf.pc != 0 75 } 76 77 // next returns the frame representing uf's logical caller. 78 func (u *inlineUnwinder) next(uf inlineFrame) inlineFrame { 79 if uf.index < 0 { 80 uf.pc = 0 81 return uf 82 } 83 parentPc := u.inlTree[uf.index].parentPc 84 return u.resolveInternal(u.f.entry() + uintptr(parentPc)) 85 } 86 87 // isInlined returns whether uf is an inlined frame. 88 func (u *inlineUnwinder) isInlined(uf inlineFrame) bool { 89 return uf.index >= 0 90 } 91 92 // srcFunc returns the srcFunc representing the given frame. 93 func (u *inlineUnwinder) srcFunc(uf inlineFrame) srcFunc { 94 if uf.index < 0 { 95 return u.f.srcFunc() 96 } 97 t := &u.inlTree[uf.index] 98 return srcFunc{ 99 u.f.datap, 100 t.nameOff, 101 t.startLine, 102 t.funcID, 103 } 104 } 105 106 // fileLine returns the file name and line number of the call within the given 107 // frame. As a convenience, for the innermost frame, it returns the file and 108 // line of the PC this unwinder was started at (often this is a call to another 109 // physical function). 110 // 111 // It returns "?", 0 if something goes wrong. 112 func (u *inlineUnwinder) fileLine(uf inlineFrame) (file string, line int) { 113 file, line32 := funcline1(u.f, uf.pc, false) 114 return file, int(line32) 115 } 116