github.com/oarkflow/log@v1.0.78/runtime_go1.21.go (about)

     1  //go:build go1.21 && !go1.22
     2  // +build go1.21,!go1.22
     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  	cache   unsafe.Pointer
    25  	inlTree *[1 << 20]inlinedCall
    26  }
    27  
    28  type inlineFrame struct {
    29  	pc    uintptr
    30  	index int32
    31  }
    32  
    33  type srcFunc struct {
    34  	datap     *uintptr
    35  	nameOff   int32
    36  	startLine int32
    37  	funcID    uint8
    38  }
    39  
    40  func pcNameFileLine(pc uintptr) (name, file string, line int32) {
    41  	funcInfo := findfunc(pc)
    42  	if funcInfo._func == nil {
    43  		return
    44  	}
    45  
    46  	entry := funcInfoEntry(funcInfo)
    47  
    48  	if pc > entry {
    49  		// We store the pc of the start of the instruction following
    50  		// the instruction in question (the call or the inline mark).
    51  		// This is done for historical reasons, and to make FuncForPC
    52  		// work correctly for entries in the result of runtime.Callers.
    53  		pc--
    54  	}
    55  
    56  	file, line = funcline1(funcInfo, pc, false)
    57  
    58  	// It's important that interpret pc non-strictly as cgoTraceback may
    59  	// have added bogus PCs with a valid funcInfo but invalid PCDATA.
    60  	u, uf := newInlineUnwinder(funcInfo, pc)
    61  	sf := inlineUnwinder_srcFunc(&u, uf)
    62  	name = srcFunc_name(sf)
    63  	// name = funcNameForPrint(srcFunc_name(sf))
    64  
    65  	return
    66  }
    67  
    68  //go:linkname newInlineUnwinder runtime.newInlineUnwinder
    69  func newInlineUnwinder(f funcInfo, pc uintptr) (inlineUnwinder, inlineFrame)
    70  
    71  //go:linkname inlineUnwinder_srcFunc runtime.(*inlineUnwinder).srcFunc
    72  func inlineUnwinder_srcFunc(*inlineUnwinder, inlineFrame) srcFunc
    73  
    74  //go:linkname inlineUnwinder_isInlined runtime.(*inlineUnwinder).isInlined
    75  func inlineUnwinder_isInlined(*inlineUnwinder, inlineFrame) bool
    76  
    77  //go:linkname srcFunc_name runtime.srcFunc.name
    78  func srcFunc_name(srcFunc) string
    79  
    80  //go:linkname funcNameForPrint runtime.funcNameForPrint
    81  func funcNameForPrint(name string) string
    82  
    83  // Fastrandn returns a pseudorandom uint32 in [0,n).
    84  //
    85  //go:noescape
    86  //go:linkname Fastrandn runtime.fastrandn
    87  func Fastrandn(x uint32) uint32