github.com/phuslu/log@v1.0.100/runtime_go1.22.go (about) 1 //go:build go1.22 && !go1.23 2 // +build go1.22,!go1.23 3 4 // MIT license, copy and modify from https://github.com/tlog-dev/loc 5 6 //nolint:unused 7 package log 8 9 import ( 10 "unsafe" 11 ) 12 13 // inlinedCall is the encoding of entries in the FUNCDATA_InlTree table. 14 type inlinedCall struct { 15 funcID uint8 // type of the called function 16 _ [3]byte 17 nameOff int32 // offset into pclntab for name of called function 18 parentPc int32 // position of an instruction whose source position is the call site (offset from entry) 19 startLine int32 // line number of start of function (func keyword/TEXT directive) 20 } 21 22 type inlineUnwinder struct { 23 f funcInfo 24 inlTree *[1 << 20]inlinedCall 25 } 26 27 type inlineFrame struct { 28 pc uintptr 29 index int32 30 } 31 32 type srcFunc struct { 33 datap unsafe.Pointer 34 nameOff int32 35 startLine int32 36 funcID uint8 37 } 38 39 type _func struct { 40 entryOff uint32 // start pc, as offset from moduledata.text/pcHeader.textStart 41 nameOff int32 // function name, as index into moduledata.funcnametab. 42 43 args int32 // in/out args size 44 deferreturn uint32 // offset of start of a deferreturn call instruction from entry, if any. 45 46 pcsp uint32 47 pcfile uint32 48 pcln uint32 49 npcdata uint32 50 cuOffset uint32 // runtime.cutab offset of this function's CU 51 startLine int32 // line number of start of function (func keyword/TEXT directive) 52 funcID uint8 // set for certain special runtime functions 53 flag uint8 54 _ [1]byte // pad 55 nfuncdata uint8 // must be last, must end on a uint32-aligned boundary 56 } 57 58 func pcFileLineName(pc uintptr) (file string, line int32, name string) { 59 funcInfo := findfunc(pc) 60 if funcInfo._func == nil { 61 return 62 } 63 64 entry := funcInfoEntry(funcInfo) 65 66 if pc > entry { 67 // We store the pc of the start of the instruction following 68 // the instruction in question (the call or the inline mark). 69 // This is done for historical reasons, and to make FuncForPC 70 // work correctly for entries in the result of runtime.Callers. 71 pc-- 72 } 73 74 file, line = funcline1(funcInfo, pc, false) 75 76 // It's important that interpret pc non-strictly as cgoTraceback may 77 // have added bogus PCs with a valid funcInfo but invalid PCDATA. 78 u, uf := newInlineUnwinder(funcInfo, pc) 79 var sf srcFunc 80 if uf.index < 0 { 81 f := (*_func)(funcInfo._func) 82 sf = srcFunc{funcInfo.datap, f.nameOff, f.startLine, f.funcID} 83 } else { 84 t := &u.inlTree[uf.index] 85 sf = srcFunc{u.f.datap, t.nameOff, t.startLine, t.funcID} 86 } 87 name = srcFunc_name(sf) 88 89 return 90 } 91 92 //go:linkname newInlineUnwinder runtime.newInlineUnwinder 93 func newInlineUnwinder(f funcInfo, pc uintptr) (inlineUnwinder, inlineFrame) 94 95 //go:linkname srcFunc_name runtime.srcFunc.name 96 func srcFunc_name(srcFunc) string 97 98 // Fastrandn returns a pseudorandom uint32 in [0,n). 99 // 100 //go:noescape 101 //go:linkname Fastrandn runtime.cheaprandn 102 func Fastrandn(x uint32) uint32