github.com/bibaroc/wingman@v0.0.2-0.20200911182922-33c2085136b1/pkg/logger/runtime.go (about)

     1  package logger
     2  
     3  import (
     4  	"runtime"
     5  	"unsafe"
     6  )
     7  
     8  // there functions are implemented in the runtime package
     9  // here I prefer to use the functions directly in order to speed up caller lookup
    10  // as the public functionality exposed by runtime pkg is ~30% slower than using the methods directly
    11  
    12  // callers will populate pcbuf with pcs of calling functions
    13  //  returns the total number of pc read
    14  //go:linkname callers runtime.callers
    15  func callers(skip int, pcbuf []uintptr) int
    16  
    17  //go:linkname findfunc runtime.findfunc
    18  func findfunc(pc uintptr) funcInfo
    19  
    20  //go:linkname funcname runtime.funcname
    21  func funcname(f funcInfo) string
    22  
    23  // funcInfo is copy-pasted from src/runtime/symtab.go
    24  //  methods are copied becouse they influence the memory layout
    25  type funcInfo struct {
    26  	*_func
    27  	datap *moduledata
    28  }
    29  
    30  func (f funcInfo) valid() bool {
    31  	return f._func != nil
    32  }
    33  
    34  func (f funcInfo) _Func() *runtime.Func {
    35  	return (*runtime.Func)(unsafe.Pointer(f._func))
    36  }
    37  
    38  // _func is copy-pasted from src/runtime/runtime2.go
    39  type _func struct {
    40  	entry   uintptr // start pc
    41  	nameoff int32   // function name
    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      int32
    47  	pcfile    int32
    48  	pcln      int32
    49  	npcdata   int32
    50  	funcID    funcID  // set for certain special runtime functions
    51  	_         [2]int8 // unused
    52  	nfuncdata uint8   // must be last
    53  }
    54  
    55  // funcID is copy-pasted from src/runtime/symtab.go
    56  type funcID uint8
    57  
    58  // moduledata is copy-pasted from src/runtime/symtab.go
    59  type moduledata struct {
    60  	pclntable    []byte
    61  	ftab         []functab
    62  	filetab      []uint32
    63  	findfunctab  uintptr
    64  	minpc, maxpc uintptr
    65  
    66  	text, etext           uintptr
    67  	noptrdata, enoptrdata uintptr
    68  	data, edata           uintptr
    69  	bss, ebss             uintptr
    70  	noptrbss, enoptrbss   uintptr
    71  	end, gcdata, gcbss    uintptr
    72  	types, etypes         uintptr
    73  
    74  	textsectmap []textsect
    75  	typelinks   []int32 // offsets from types
    76  	itablinks   []*itab
    77  
    78  	ptab []ptabEntry
    79  
    80  	pluginpath string
    81  	pkghashes  []modulehash
    82  
    83  	modulename   string
    84  	modulehashes []modulehash
    85  
    86  	hasmain uint8 // 1 if module contains the main function, 0 otherwise
    87  
    88  	gcdatamask, gcbssmask bitvector
    89  
    90  	typemap map[typeOff]*_type // offset to *_rtype in previous module
    91  
    92  	bad bool // module failed to load and should be ignored
    93  
    94  	next *moduledata
    95  }
    96  
    97  // functab is copy-pasted from src/runtime/symtab.go
    98  type functab struct {
    99  	entry   uintptr
   100  	funcoff uintptr
   101  }
   102  
   103  // textsect is copy-pasted from src/runtime/symtab.go
   104  type textsect struct {
   105  	vaddr    uintptr // prelinked section vaddr
   106  	length   uintptr // section length
   107  	baseaddr uintptr // relocated section address
   108  }
   109  
   110  // itab is copy-pasted from src/runtime/runtime2.go
   111  type itab struct {
   112  	inter *interfacetype
   113  	_type *_type
   114  	hash  uint32 // copy of _type.hash. Used for type switches.
   115  	_     [4]byte
   116  	fun   [1]uintptr // variable sized. fun[0]==0 means _type does not implement inter.
   117  }
   118  
   119  // interfacetype is copy-pasted from src/runtime/type.go
   120  type interfacetype struct {
   121  	typ     _type
   122  	pkgpath name
   123  	mhdr    []imethod
   124  }
   125  
   126  // _type is copy-pasted from src/runtime/type.go
   127  type _type struct {
   128  	size       uintptr
   129  	ptrdata    uintptr // size of memory prefix holding all pointers
   130  	hash       uint32
   131  	tflag      tflag
   132  	align      uint8
   133  	fieldAlign uint8
   134  	kind       uint8
   135  	// function for comparing objects of this type
   136  	// (ptr to object A, ptr to object B) -> ==?
   137  	equal func(unsafe.Pointer, unsafe.Pointer) bool
   138  	// gcdata stores the GC type data for the garbage collector.
   139  	// If the KindGCProg bit is set in kind, gcdata is a GC program.
   140  	// Otherwise it is a ptrmask bitmap. See mbitmap.go for details.
   141  	gcdata    *byte
   142  	str       nameOff
   143  	ptrToThis typeOff
   144  }
   145  
   146  // tflag nameOff typeOff are copy-pasted from src/runtime/type.go
   147  type tflag uint8
   148  type nameOff int32
   149  type typeOff int32
   150  
   151  // name is copy-pasted from src/runtime/type.go
   152  type name struct {
   153  	bytes *byte
   154  }
   155  
   156  // imethod is copy-pasted from src/runtime/type.go
   157  type imethod struct {
   158  	name nameOff
   159  	ityp typeOff
   160  }
   161  
   162  // ptanEntry is copy-pasted from src/runtime/plugin.go
   163  type ptabEntry struct {
   164  	name nameOff
   165  	typ  typeOff
   166  }
   167  
   168  // modulehash is copy-pasted from src/runtime/symtab.go
   169  type modulehash struct {
   170  	modulename   string
   171  	linktimehash string
   172  	runtimehash  *string
   173  }
   174  
   175  // bitvector is copy-pasted from src/runtime/stack.go
   176  type bitvector struct {
   177  	n        int32 // # of bits
   178  	bytedata *uint8
   179  }