github.com/Rookout/GoSDK@v0.1.48/pkg/locations_set/breakpoint_storage.go (about)

     1  package locations_set
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/Rookout/GoSDK/pkg/augs"
     7  )
     8  
     9  type BreakpointStorage struct {
    10  	functions   map[*augs.Function][]*augs.BreakpointInstance 
    11  	storageLock *sync.Mutex
    12  }
    13  
    14  func newBreakpointStorage() *BreakpointStorage {
    15  	return &BreakpointStorage{
    16  		functions:   make(map[*augs.Function][]*augs.BreakpointInstance),
    17  		storageLock: &sync.Mutex{},
    18  	}
    19  }
    20  
    21  func (b *BreakpointStorage) GetBreakpointInstances() []*augs.BreakpointInstance {
    22  	b.storageLock.Lock()
    23  	defer b.storageLock.Unlock()
    24  
    25  	var bpInstances []*augs.BreakpointInstance
    26  	for _, instances := range b.functions {
    27  		bpInstances = append(bpInstances, instances...)
    28  	}
    29  
    30  	return bpInstances
    31  }
    32  
    33  func (b *BreakpointStorage) AddBreakpointInstance(bpInstance *augs.BreakpointInstance) {
    34  	b.storageLock.Lock()
    35  	defer b.storageLock.Unlock()
    36  
    37  	var bpInstances []*augs.BreakpointInstance
    38  	if prevInstances, ok := b.functions[bpInstance.Function]; ok {
    39  		bpInstances = prevInstances
    40  	} else {
    41  		b.addFunction(bpInstance.Function)
    42  	}
    43  
    44  	bpInstances = append(bpInstances, bpInstance)
    45  	b.functions[bpInstance.Function] = bpInstances
    46  }
    47  
    48  func (b *BreakpointStorage) AddFunction(function *augs.Function) {
    49  	b.storageLock.Lock()
    50  	defer b.storageLock.Unlock()
    51  
    52  	b.addFunction(function)
    53  }
    54  
    55  func (b *BreakpointStorage) addFunction(function *augs.Function) {
    56  	function.GetBreakpointInstances = func() []*augs.BreakpointInstance {
    57  		return b.FindBreakpointsByFunctionEntry(function.Entry)
    58  	}
    59  	b.functions[function] = nil
    60  }
    61  
    62  func (b *BreakpointStorage) RemoveBreakpointInstance(bpInstance *augs.BreakpointInstance) {
    63  	b.storageLock.Lock()
    64  	defer b.storageLock.Unlock()
    65  
    66  	if _, ok := b.functions[bpInstance.Function]; !ok {
    67  		return
    68  	}
    69  
    70  	var bpInstances []*augs.BreakpointInstance
    71  	for _, instance := range b.functions[bpInstance.Function] {
    72  		if instance.Addr == bpInstance.Addr {
    73  			continue
    74  		}
    75  		bpInstances = append(bpInstances, instance)
    76  	}
    77  	b.functions[bpInstance.Function] = bpInstances
    78  }
    79  
    80  func (b *BreakpointStorage) FindBreakpointsByFunctionEntry(entry uint64) []*augs.BreakpointInstance {
    81  	b.storageLock.Lock()
    82  	defer b.storageLock.Unlock()
    83  
    84  	for f, bps := range b.functions {
    85  		if f.Entry == entry {
    86  			return bps
    87  		}
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func (b *BreakpointStorage) FindFunctionByEntry(entry uint64) (function *augs.Function, exists bool) {
    94  	b.storageLock.Lock()
    95  	defer b.storageLock.Unlock()
    96  
    97  	for f := range b.functions {
    98  		if f.Entry == entry {
    99  			return f, true
   100  		}
   101  	}
   102  	return nil, false
   103  }
   104  
   105  func (b *BreakpointStorage) FindBreakpointByAddr(addr uint64) (breakpoint *augs.BreakpointInstance, exists bool) {
   106  	for _, bps := range b.functions {
   107  		for _, bp := range bps {
   108  			if bp.Addr == addr {
   109  				return bp, true
   110  			}
   111  		}
   112  	}
   113  
   114  	return nil, false
   115  }