github.com/Rookout/GoSDK@v0.1.48/pkg/services/instrumentation/binary_info/image.go (about)

     1  // The MIT License (MIT)
     2  
     3  // Copyright (c) 2014 Derek Parker
     4  
     5  // Permission is hereby granted, free of charge, to any person obtaining a copy of
     6  // this software and associated documentation files (the "Software"), to deal in
     7  // the Software without restriction, including without limitation the rights to
     8  // use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
     9  // the Software, and to permit persons to whom the Software is furnished to do so,
    10  // subject to the following conditions:
    11  
    12  // The above copyright notice and this permission notice shall be included in all
    13  // copies or substantial portions of the Software.
    14  
    15  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    16  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
    17  // FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
    18  // COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
    19  // IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
    20  // CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
    21  
    22  package binary_info
    23  
    24  import (
    25  	"debug/dwarf"
    26  	"io"
    27  	"sort"
    28  	"sync"
    29  
    30  	godwarf2 "github.com/Rookout/GoSDK/pkg/services/instrumentation/dwarf/godwarf"
    31  	"github.com/hashicorp/golang-lru/simplelru"
    32  )
    33  
    34  
    35  type Image struct {
    36  	Path       string
    37  	StaticBase uint64
    38  	addr       uint64
    39  
    40  	Index int 
    41  
    42  	closer         io.Closer
    43  	sepDebugCloser io.Closer
    44  
    45  	Dwarf     *dwarf.Data
    46  	debugAddr *godwarf2.DebugAddrSection
    47  
    48  	TypeCache sync.Map
    49  
    50  	compileUnits []*compileUnit 
    51  
    52  	dwarfTreeCache *simplelru.LRU
    53  	dwarfTreeLock  sync.Mutex
    54  
    55  	
    56  	
    57  	
    58  	
    59  	RuntimeTypeToDIE map[uint64]runtimeTypeDIE
    60  
    61  	loadErr      error
    62  	debugLineStr []byte
    63  }
    64  
    65  type runtimeTypeDIE struct {
    66  	Offset dwarf.Offset
    67  	Kind   int64
    68  }
    69  
    70  func (i *Image) registerRuntimeTypeToDIE(entry *dwarf.Entry) {
    71  	if off, ok := entry.Val(godwarf2.AttrGoRuntimeType).(uint64); ok {
    72  		if _, ok := i.RuntimeTypeToDIE[off]; !ok {
    73  			i.RuntimeTypeToDIE[off] = runtimeTypeDIE{entry.Offset, -1}
    74  		}
    75  	}
    76  }
    77  
    78  func (i *Image) GetDwarfTree(off dwarf.Offset) (*godwarf2.Tree, error) {
    79  	i.dwarfTreeLock.Lock()
    80  	defer i.dwarfTreeLock.Unlock()
    81  
    82  	if r, ok := i.dwarfTreeCache.Get(off); ok {
    83  		return r.(*godwarf2.Tree), nil
    84  	}
    85  	r, err := godwarf2.LoadTree(off, i.Dwarf, i.StaticBase)
    86  	if err != nil {
    87  		return nil, err
    88  	}
    89  	i.dwarfTreeCache.Add(off, r)
    90  	return r, nil
    91  }
    92  
    93  func (i *Image) FindCompileUnitForOffset(off dwarf.Offset) *compileUnit {
    94  	index := sort.Search(len(i.compileUnits), func(index int) bool {
    95  		return i.compileUnits[index].offset >= off
    96  	})
    97  	if index > 0 {
    98  		index--
    99  	}
   100  	return i.compileUnits[index]
   101  }