github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/proc/goroutine_cache.go (about)

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