tlog.app/go/loc@v0.6.2-0.20231112073106-b6382a0ac518/name_file_line_go1.20.go (about)

     1  //go:build nikandfor_loc_unsafe && go1.20 && !go1.21
     2  // +build nikandfor_loc_unsafe,go1.20,!go1.21
     3  
     4  package loc
     5  
     6  import "unsafe"
     7  
     8  func (l PC) nameFileLine() (name, file string, line int) {
     9  	funcInfo := findfunc(l)
    10  	if funcInfo._func == nil {
    11  		return
    12  	}
    13  
    14  	entry := funcInfoEntry(funcInfo)
    15  
    16  	if l > entry {
    17  		// We store the pc of the start of the instruction following
    18  		// the instruction in question (the call or the inline mark).
    19  		// This is done for historical reasons, and to make FuncForPC
    20  		// work correctly for entries in the result of runtime.Callers.
    21  		l--
    22  	}
    23  
    24  	name = funcname(funcInfo)
    25  	file, line32 := funcline1(funcInfo, l, false)
    26  	line = int(line32)
    27  
    28  	if inldata := funcdata(funcInfo, _FUNCDATA_InlTree); inldata != nil {
    29  		inltree := (*[1 << 20]inlinedCall)(inldata)
    30  		// Non-strict as cgoTraceback may have added bogus PCs
    31  		// with a valid funcInfo but invalid PCDATA.
    32  		ix := pcdatavalue1(funcInfo, _PCDATA_InlTreeIndex, l, nil, false)
    33  		if ix >= 0 {
    34  			// Note: entry is not modified. It always refers to a real frame, not an inlined one.
    35  			ic := inltree[ix]
    36  			name = funcnameFromNameOff(funcInfo, ic.nameOff)
    37  			// File/line from funcline1 below are already correct.
    38  		}
    39  	}
    40  
    41  	return
    42  }
    43  
    44  //go:linkname findfunc runtime.findfunc
    45  func findfunc(pc PC) funcInfo
    46  
    47  //go:linkname funcline1 runtime.funcline1
    48  func funcline1(f funcInfo, targetpc PC, strict bool) (file string, line int32)
    49  
    50  //go:linkname funcname runtime.funcname
    51  func funcname(f funcInfo) string
    52  
    53  //go:linkname funcdata runtime.funcdata
    54  func funcdata(f funcInfo, i uint8) unsafe.Pointer
    55  
    56  //go:linkname pcdatavalue runtime.pcdatavalue
    57  func pcdatavalue(f funcInfo, table int32, targetpc PC, cache unsafe.Pointer) int32
    58  
    59  //go:linkname pcdatavalue1 runtime.pcdatavalue1
    60  func pcdatavalue1(f funcInfo, table int32, targetpc PC, cache unsafe.Pointer, strict bool) int32
    61  
    62  //go:linkname funcnameFromNameOff runtime.funcnameFromNameOff
    63  func funcnameFromNameOff(f funcInfo, nameoff int32) string
    64  
    65  //go:linkname funcInfoEntry runtime.funcInfo.entry
    66  func funcInfoEntry(f funcInfo) PC