github.com/undoio/delve@v1.9.0/pkg/proc/goroutine_cache.go (about)

     1  package proc
     2  
     3  type goroutineCache struct {
     4  	partialGCache map[int]*G
     5  	allGCache     []*G
     6  
     7  	allgentryAddr, allglenAddr uint64
     8  }
     9  
    10  func (gcache *goroutineCache) init(bi *BinaryInfo) {
    11  	var err error
    12  
    13  	exeimage := bi.Images[0]
    14  	rdr := exeimage.DwarfReader()
    15  
    16  	gcache.allglenAddr, _ = rdr.AddrFor("runtime.allglen", exeimage.StaticBase, bi.Arch.PtrSize())
    17  
    18  	rdr.Seek(0)
    19  	gcache.allgentryAddr, err = rdr.AddrFor("runtime.allgs", exeimage.StaticBase, bi.Arch.PtrSize())
    20  	if err != nil {
    21  		// try old name (pre Go 1.6)
    22  		gcache.allgentryAddr, _ = rdr.AddrFor("runtime.allg", exeimage.StaticBase, bi.Arch.PtrSize())
    23  	}
    24  }
    25  
    26  func (gcache *goroutineCache) getRuntimeAllg(bi *BinaryInfo, mem MemoryReadWriter) (uint64, uint64, error) {
    27  	if gcache.allglenAddr == 0 || gcache.allgentryAddr == 0 {
    28  		return 0, 0, ErrNoRuntimeAllG
    29  	}
    30  	allglen, err := readUintRaw(mem, gcache.allglenAddr, int64(bi.Arch.PtrSize()))
    31  	if err != nil {
    32  		return 0, 0, err
    33  	}
    34  
    35  	allgptr, err := readUintRaw(mem, gcache.allgentryAddr, int64(bi.Arch.PtrSize()))
    36  	if err != nil {
    37  		return 0, 0, err
    38  	}
    39  	return allgptr, allglen, nil
    40  }
    41  
    42  func (gcache *goroutineCache) addGoroutine(g *G) {
    43  	if gcache.partialGCache == nil {
    44  		gcache.partialGCache = make(map[int]*G)
    45  	}
    46  	gcache.partialGCache[g.ID] = g
    47  }
    48  
    49  // Clear clears the cached contents of the cache for runtime.allgs.
    50  func (gcache *goroutineCache) Clear() {
    51  	gcache.partialGCache = nil
    52  	gcache.allGCache = nil
    53  }