github.com/phuslu/log@v1.0.100/runtime_go1.20.go (about) 1 //go:build go1.20 && !go1.21 2 // +build go1.20,!go1.21 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 func pcFileLineName(pc uintptr) (file string, line int32, name string) { 23 funcInfo := findfunc(pc) 24 if funcInfo._func == nil { 25 return 26 } 27 28 entry := funcInfoEntry(funcInfo) 29 30 if pc > entry { 31 // We store the pc of the start of the instruction following 32 // the instruction in question (the call or the inline mark). 33 // This is done for historical reasons, and to make FuncForPC 34 // work correctly for entries in the result of runtime.Callers. 35 pc-- 36 } 37 38 file, line = funcline1(funcInfo, pc, false) 39 40 name = funcname(funcInfo) 41 const _PCDATA_InlTreeIndex = 2 42 const _FUNCDATA_InlTree = 3 43 if inldata := funcdata(funcInfo, _FUNCDATA_InlTree); inldata != nil { 44 inltree := (*[1 << 20]inlinedCall)(inldata) 45 // Non-strict as cgoTraceback may have added bogus PCs 46 // with a valid funcInfo but invalid PCDATA. 47 ix := pcdatavalue1(funcInfo, _PCDATA_InlTreeIndex, pc, nil, false) 48 if ix >= 0 { 49 // Note: entry is not modified. It always refers to a real frame, not an inlined one. 50 ic := inltree[ix] 51 name = funcnameFromNameOff(funcInfo, ic.nameOff) 52 // File/line from funcline1 below are already correct. 53 } 54 } 55 56 return 57 } 58 59 //go:linkname funcname runtime.funcname 60 func funcname(f funcInfo) string 61 62 //go:linkname funcdata runtime.funcdata 63 func funcdata(f funcInfo, i uint8) unsafe.Pointer 64 65 //go:linkname pcdatavalue1 runtime.pcdatavalue1 66 func pcdatavalue1(f funcInfo, table int32, targetpc uintptr, cache unsafe.Pointer, strict bool) int32 67 68 //go:linkname funcnameFromNameOff runtime.funcnameFromNameOff 69 func funcnameFromNameOff(f funcInfo, nameoff int32) string 70 71 // Fastrandn returns a pseudorandom uint32 in [0,n). 72 // 73 //go:noescape 74 //go:linkname Fastrandn runtime.fastrandn 75 func Fastrandn(x uint32) uint32