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

     1  //go:build amd64
     2  // +build amd64
     3  
     4  package module
     5  
     6  import (
     7  	"github.com/Rookout/GoSDK/pkg/logger"
     8  	"github.com/Rookout/GoSDK/pkg/rookoutErrors"
     9  	"github.com/Rookout/GoSDK/pkg/services/disassembler"
    10  	"golang.org/x/arch/x86/x86asm"
    11  )
    12  
    13  type baseInstruction = x86asm.Inst
    14  
    15  type regState struct {
    16  	stackSize int
    17  }
    18  
    19  func newRegState() *regState {
    20  	return &regState{}
    21  }
    22  
    23  func (r *regState) getStackSize() int {
    24  	return r.stackSize
    25  }
    26  
    27  func (r *regState) setStackSize(stackSize int) {
    28  	r.stackSize = stackSize
    29  }
    30  
    31  func (r *regState) update(i *disassembler.Instruction) {
    32  	switch i.Op {
    33  	case x86asm.ADD, x86asm.SUB:
    34  		if i.Args[0] != x86asm.RSP {
    35  			return
    36  		}
    37  
    38  		imm, ok := i.Args[1].(x86asm.Imm)
    39  		if !ok {
    40  			logger.Logger().Warningf("Got unexpected source reg in add/sub: %v [%T], instruction = %v", i.Args[1], i.Args[1], i)
    41  			return
    42  		}
    43  
    44  		
    45  		if i.Op == x86asm.ADD {
    46  			r.setStackSize(r.getStackSize() - int(imm))
    47  		} else {
    48  			r.setStackSize(r.getStackSize() + int(imm))
    49  		}
    50  
    51  	
    52  	case x86asm.PUSH, x86asm.PUSHFQ:
    53  		r.setStackSize(r.getStackSize() + 8)
    54  
    55  	
    56  	case x86asm.POP, x86asm.POPFQ:
    57  		r.setStackSize(r.getStackSize() - 8)
    58  	}
    59  }
    60  
    61  func read(startPC uintptr, endPC uintptr) ([]*disassembler.Instruction, rookoutErrors.RookoutError) {
    62  	return disassembler.Decode(startPC, endPC, true)
    63  }
    64  
    65  func instructionSizeBytes(pc uintptr) (uintptr, rookoutErrors.RookoutError) {
    66  	inst, err := disassembler.DecodeOne(pc)
    67  	if err != nil {
    68  		return 0, err
    69  	}
    70  	return uintptr(inst.Len), nil
    71  }