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

     1  package proc
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"io"
     8  	"math"
     9  	"strings"
    10  
    11  	"github.com/undoio/delve/pkg/dwarf/frame"
    12  	"github.com/undoio/delve/pkg/dwarf/op"
    13  	"github.com/undoio/delve/pkg/dwarf/regnum"
    14  )
    15  
    16  var amd64BreakInstruction = []byte{0xCC}
    17  
    18  // AMD64Arch returns an initialized AMD64
    19  // struct.
    20  func AMD64Arch(goos string) *Arch {
    21  	return &Arch{
    22  		Name:                             "amd64",
    23  		ptrSize:                          8,
    24  		maxInstructionLength:             15,
    25  		breakpointInstruction:            amd64BreakInstruction,
    26  		breakInstrMovesPC:                true,
    27  		derefTLS:                         goos == "windows",
    28  		prologues:                        prologuesAMD64,
    29  		fixFrameUnwindContext:            amd64FixFrameUnwindContext,
    30  		switchStack:                      amd64SwitchStack,
    31  		regSize:                          amd64RegSize,
    32  		RegistersToDwarfRegisters:        amd64RegistersToDwarfRegisters,
    33  		addrAndStackRegsToDwarfRegisters: amd64AddrAndStackRegsToDwarfRegisters,
    34  		DwarfRegisterToString:            amd64DwarfRegisterToString,
    35  		inhibitStepInto:                  func(*BinaryInfo, uint64) bool { return false },
    36  		asmDecode:                        amd64AsmDecode,
    37  		PCRegNum:                         regnum.AMD64_Rip,
    38  		SPRegNum:                         regnum.AMD64_Rsp,
    39  		BPRegNum:                         regnum.AMD64_Rbp,
    40  		ContextRegNum:                    regnum.AMD64_Rdx,
    41  		asmRegisters:                     amd64AsmRegisters,
    42  		RegisterNameToDwarf:              nameToDwarfFunc(regnum.AMD64NameToDwarf),
    43  		debugCallMinStackSize:            256,
    44  		maxRegArgBytes:                   9*8 + 15*8,
    45  	}
    46  }
    47  
    48  func amd64FixFrameUnwindContext(fctxt *frame.FrameContext, pc uint64, bi *BinaryInfo) *frame.FrameContext {
    49  	a := bi.Arch
    50  	if a.sigreturnfn == nil {
    51  		a.sigreturnfn = bi.LookupFunc["runtime.sigreturn"]
    52  	}
    53  
    54  	if fctxt == nil || (a.sigreturnfn != nil && pc >= a.sigreturnfn.Entry && pc < a.sigreturnfn.End) {
    55  		// When there's no frame descriptor entry use BP (the frame pointer) instead
    56  		// - return register is [bp + a.PtrSize()] (i.e. [cfa-a.PtrSize()])
    57  		// - cfa is bp + a.PtrSize()*2
    58  		// - bp is [bp] (i.e. [cfa-a.PtrSize()*2])
    59  		// - sp is cfa
    60  
    61  		// When the signal handler runs it will move the execution to the signal
    62  		// handling stack (installed using the sigaltstack system call).
    63  		// This isn't a proper stack switch: the pointer to g in TLS will still
    64  		// refer to whatever g was executing on that thread before the signal was
    65  		// received.
    66  		// Since go did not execute a stack switch the previous value of sp, pc
    67  		// and bp is not saved inside g.sched, as it normally would.
    68  		// The only way to recover is to either read sp/pc from the signal context
    69  		// parameter (the ucontext_t* parameter) or to unconditionally follow the
    70  		// frame pointer when we get to runtime.sigreturn (which is what we do
    71  		// here).
    72  
    73  		return &frame.FrameContext{
    74  			RetAddrReg: regnum.AMD64_Rip,
    75  			Regs: map[uint64]frame.DWRule{
    76  				regnum.AMD64_Rip: {
    77  					Rule:   frame.RuleOffset,
    78  					Offset: int64(-a.PtrSize()),
    79  				},
    80  				regnum.AMD64_Rbp: {
    81  					Rule:   frame.RuleOffset,
    82  					Offset: int64(-2 * a.PtrSize()),
    83  				},
    84  				regnum.AMD64_Rsp: {
    85  					Rule:   frame.RuleValOffset,
    86  					Offset: 0,
    87  				},
    88  			},
    89  			CFA: frame.DWRule{
    90  				Rule:   frame.RuleCFA,
    91  				Reg:    regnum.AMD64_Rbp,
    92  				Offset: int64(2 * a.PtrSize()),
    93  			},
    94  		}
    95  	}
    96  
    97  	if a.crosscall2fn == nil {
    98  		a.crosscall2fn = bi.LookupFunc["crosscall2"]
    99  	}
   100  
   101  	if a.crosscall2fn != nil && pc >= a.crosscall2fn.Entry && pc < a.crosscall2fn.End {
   102  		rule := fctxt.CFA
   103  		if rule.Offset == crosscall2SPOffsetBad {
   104  			switch bi.GOOS {
   105  			case "windows":
   106  				rule.Offset += crosscall2SPOffsetWindows
   107  			default:
   108  				rule.Offset += crosscall2SPOffsetNonWindows
   109  			}
   110  		}
   111  		fctxt.CFA = rule
   112  	}
   113  
   114  	// We assume that RBP is the frame pointer and we want to keep it updated,
   115  	// so that we can use it to unwind the stack even when we encounter frames
   116  	// without descriptor entries.
   117  	// If there isn't a rule already we emit one.
   118  	if fctxt.Regs[regnum.AMD64_Rbp].Rule == frame.RuleUndefined {
   119  		fctxt.Regs[regnum.AMD64_Rbp] = frame.DWRule{
   120  			Rule:   frame.RuleFramePointer,
   121  			Reg:    regnum.AMD64_Rbp,
   122  			Offset: 0,
   123  		}
   124  	}
   125  
   126  	return fctxt
   127  }
   128  
   129  // cgocallSPOffsetSaveSlot is the offset from systemstack.SP where
   130  // (goroutine.SP - StackHi) is saved in runtime.asmcgocall after the stack
   131  // switch happens.
   132  const amd64cgocallSPOffsetSaveSlot = 0x28
   133  
   134  func amd64SwitchStack(it *stackIterator, _ *op.DwarfRegisters) bool {
   135  	if it.frame.Current.Fn == nil {
   136  		if it.systemstack && it.g != nil && it.top {
   137  			it.switchToGoroutineStack()
   138  			return true
   139  		}
   140  		return false
   141  	}
   142  	switch it.frame.Current.Fn.Name {
   143  	case "runtime.asmcgocall":
   144  		if it.top || !it.systemstack {
   145  			return false
   146  		}
   147  
   148  		// This function is called by a goroutine to execute a C function and
   149  		// switches from the goroutine stack to the system stack.
   150  		// Since we are unwinding the stack from callee to caller we have to switch
   151  		// from the system stack to the goroutine stack.
   152  		off, _ := readIntRaw(it.mem, uint64(it.regs.SP()+amd64cgocallSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize())) // reads "offset of SP from StackHi" from where runtime.asmcgocall saved it
   153  		oldsp := it.regs.SP()
   154  		it.regs.Reg(it.regs.SPRegNum).Uint64Val = uint64(int64(it.stackhi) - off)
   155  
   156  		// runtime.asmcgocall can also be called from inside the system stack,
   157  		// in that case no stack switch actually happens
   158  		if it.regs.SP() == oldsp {
   159  			return false
   160  		}
   161  		it.systemstack = false
   162  
   163  		// advances to the next frame in the call stack
   164  		it.frame.addrret = uint64(int64(it.regs.SP()) + int64(it.bi.Arch.PtrSize()))
   165  		it.frame.Ret, _ = readUintRaw(it.mem, it.frame.addrret, int64(it.bi.Arch.PtrSize()))
   166  		it.pc = it.frame.Ret
   167  
   168  		it.top = false
   169  		return true
   170  
   171  	case "runtime.cgocallback_gofunc", "runtime.cgocallback":
   172  		// For a detailed description of how this works read the long comment at
   173  		// the start of $GOROOT/src/runtime/cgocall.go and the source code of
   174  		// runtime.cgocallback_gofunc in $GOROOT/src/runtime/asm_amd64.s
   175  		//
   176  		// When a C functions calls back into go it will eventually call into
   177  		// runtime.cgocallback_gofunc which is the function that does the stack
   178  		// switch from the system stack back into the goroutine stack
   179  		// Since we are going backwards on the stack here we see the transition
   180  		// as goroutine stack -> system stack.
   181  
   182  		if it.top || it.systemstack {
   183  			return false
   184  		}
   185  
   186  		it.loadG0SchedSP()
   187  		if it.g0_sched_sp <= 0 {
   188  			return false
   189  		}
   190  		// entering the system stack
   191  		it.regs.Reg(it.regs.SPRegNum).Uint64Val = it.g0_sched_sp
   192  		// reads the previous value of g0.sched.sp that runtime.cgocallback_gofunc saved on the stack
   193  		it.g0_sched_sp, _ = readUintRaw(it.mem, uint64(it.regs.SP()), int64(it.bi.Arch.PtrSize()))
   194  		it.top = false
   195  		callFrameRegs, ret, retaddr := it.advanceRegs()
   196  		frameOnSystemStack := it.newStackframe(ret, retaddr)
   197  		it.pc = frameOnSystemStack.Ret
   198  		it.regs = callFrameRegs
   199  		it.systemstack = true
   200  		return true
   201  
   202  	case "runtime.goexit", "runtime.rt0_go", "runtime.mcall":
   203  		// Look for "top of stack" functions.
   204  		it.atend = true
   205  		return true
   206  
   207  	case "runtime.mstart":
   208  		// Calls to runtime.systemstack will switch to the systemstack then:
   209  		// 1. alter the goroutine stack so that it looks like systemstack_switch
   210  		//    was called
   211  		// 2. alter the system stack so that it looks like the bottom-most frame
   212  		//    belongs to runtime.mstart
   213  		// If we find a runtime.mstart frame on the system stack of a goroutine
   214  		// parked on runtime.systemstack_switch we assume runtime.systemstack was
   215  		// called and continue tracing from the parked position.
   216  
   217  		if it.top || !it.systemstack || it.g == nil {
   218  			return false
   219  		}
   220  		if fn := it.bi.PCToFunc(it.g.PC); fn == nil || fn.Name != "runtime.systemstack_switch" {
   221  			return false
   222  		}
   223  
   224  		it.switchToGoroutineStack()
   225  		return true
   226  
   227  	default:
   228  		if it.systemstack && it.top && it.g != nil && strings.HasPrefix(it.frame.Current.Fn.Name, "runtime.") && it.frame.Current.Fn.Name != "runtime.throw" && it.frame.Current.Fn.Name != "runtime.fatalthrow" {
   229  			// The runtime switches to the system stack in multiple places.
   230  			// This usually happens through a call to runtime.systemstack but there
   231  			// are functions that switch to the system stack manually (for example
   232  			// runtime.morestack).
   233  			// Since we are only interested in printing the system stack for cgo
   234  			// calls we switch directly to the goroutine stack if we detect that the
   235  			// function at the top of the stack is a runtime function.
   236  			//
   237  			// The function "runtime.throw" is deliberately excluded from this
   238  			// because it can end up in the stack during a cgo call and switching to
   239  			// the goroutine stack will exclude all the C functions from the stack
   240  			// trace.
   241  			it.switchToGoroutineStack()
   242  			return true
   243  		}
   244  
   245  		return false
   246  	}
   247  }
   248  
   249  // amd64RegSize returns the size (in bytes) of register regnum.
   250  // The mapping between hardware registers and DWARF registers is specified
   251  // in the System V ABI AMD64 Architecture Processor Supplement page 57,
   252  // figure 3.36
   253  // https://www.uclibc.org/docs/psABI-x86_64.pdf
   254  func amd64RegSize(rn uint64) int {
   255  	// XMM registers
   256  	if rn > regnum.AMD64_Rip && rn <= 32 {
   257  		return 16
   258  	}
   259  	// x87 registers
   260  	if rn >= 33 && rn <= 40 {
   261  		return 10
   262  	}
   263  	return 8
   264  }
   265  
   266  func amd64RegistersToDwarfRegisters(staticBase uint64, regs Registers) *op.DwarfRegisters {
   267  	dregs := initDwarfRegistersFromSlice(int(regnum.AMD64MaxRegNum()), regs, regnum.AMD64NameToDwarf)
   268  	dr := op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.AMD64_Rip, regnum.AMD64_Rsp, regnum.AMD64_Rbp, 0)
   269  	dr.SetLoadMoreCallback(loadMoreDwarfRegistersFromSliceFunc(dr, regs, regnum.AMD64NameToDwarf))
   270  	return dr
   271  }
   272  
   273  func initDwarfRegistersFromSlice(maxRegs int, regs Registers, nameToDwarf map[string]int) []*op.DwarfRegister {
   274  	dregs := make([]*op.DwarfRegister, maxRegs+1)
   275  	regslice, _ := regs.Slice(false)
   276  	for _, reg := range regslice {
   277  		if dwarfReg, ok := nameToDwarf[strings.ToLower(reg.Name)]; ok {
   278  			dregs[dwarfReg] = reg.Reg
   279  		}
   280  	}
   281  	return dregs
   282  }
   283  
   284  func loadMoreDwarfRegistersFromSliceFunc(dr *op.DwarfRegisters, regs Registers, nameToDwarf map[string]int) func() {
   285  	return func() {
   286  		regslice, err := regs.Slice(true)
   287  		dr.FloatLoadError = err
   288  		for _, reg := range regslice {
   289  			name := strings.ToLower(reg.Name)
   290  			if dwarfReg, ok := nameToDwarf[name]; ok {
   291  				dr.AddReg(uint64(dwarfReg), reg.Reg)
   292  			} else if reg.Reg.Bytes != nil && (strings.HasPrefix(name, "ymm") || strings.HasPrefix(name, "zmm")) {
   293  				xmmIdx, ok := nameToDwarf["x"+name[1:]]
   294  				if !ok {
   295  					continue
   296  				}
   297  				xmmReg := dr.Reg(uint64(xmmIdx))
   298  				if xmmReg == nil || xmmReg.Bytes == nil {
   299  					continue
   300  				}
   301  				nb := make([]byte, 0, len(xmmReg.Bytes)+len(reg.Reg.Bytes))
   302  				nb = append(nb, xmmReg.Bytes...)
   303  				nb = append(nb, reg.Reg.Bytes...)
   304  				xmmReg.Bytes = nb
   305  			}
   306  		}
   307  	}
   308  }
   309  
   310  func amd64AddrAndStackRegsToDwarfRegisters(staticBase, pc, sp, bp, lr uint64) op.DwarfRegisters {
   311  	dregs := make([]*op.DwarfRegister, regnum.AMD64_Rip+1)
   312  	dregs[regnum.AMD64_Rip] = op.DwarfRegisterFromUint64(pc)
   313  	dregs[regnum.AMD64_Rsp] = op.DwarfRegisterFromUint64(sp)
   314  	dregs[regnum.AMD64_Rbp] = op.DwarfRegisterFromUint64(bp)
   315  
   316  	return *op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.AMD64_Rip, regnum.AMD64_Rsp, regnum.AMD64_Rbp, 0)
   317  }
   318  
   319  func amd64DwarfRegisterToString(i int, reg *op.DwarfRegister) (name string, floatingPoint bool, repr string) {
   320  	name = regnum.AMD64ToName(uint64(i))
   321  
   322  	if reg == nil {
   323  		return name, false, ""
   324  	}
   325  
   326  	switch n := strings.ToLower(name); n {
   327  	case "rflags":
   328  		return name, false, eflagsDescription.Describe(reg.Uint64Val, 64)
   329  
   330  	case "cw", "sw", "tw", "fop":
   331  		return name, true, fmt.Sprintf("%#04x", reg.Uint64Val)
   332  
   333  	case "mxcsr_mask":
   334  		return name, true, fmt.Sprintf("%#08x", reg.Uint64Val)
   335  
   336  	case "mxcsr":
   337  		return name, true, mxcsrDescription.Describe(reg.Uint64Val, 32)
   338  
   339  	default:
   340  		if reg.Bytes != nil && strings.HasPrefix(n, "xmm") {
   341  			return name, true, formatSSEReg(name, reg.Bytes)
   342  		} else if reg.Bytes != nil && strings.HasPrefix(n, "st(") {
   343  			return name, true, formatX87Reg(reg.Bytes)
   344  		} else if reg.Bytes == nil || (reg.Bytes != nil && len(reg.Bytes) <= 8) {
   345  			return name, false, fmt.Sprintf("%#016x", reg.Uint64Val)
   346  		} else {
   347  			return name, false, fmt.Sprintf("%#x", reg.Bytes)
   348  		}
   349  	}
   350  }
   351  
   352  func formatSSEReg(name string, reg []byte) string {
   353  	out := new(bytes.Buffer)
   354  	formatSSERegInternal(reg, out)
   355  	if len(reg) < 32 {
   356  		return out.String()
   357  	}
   358  
   359  	fmt.Fprintf(out, "\n\t[%sh] ", "Y"+name[1:])
   360  	formatSSERegInternal(reg[16:], out)
   361  
   362  	if len(reg) < 64 {
   363  		return out.String()
   364  	}
   365  
   366  	fmt.Fprintf(out, "\n\t[%shl] ", "Z"+name[1:])
   367  	formatSSERegInternal(reg[32:], out)
   368  	fmt.Fprintf(out, "\n\t[%shh] ", "Z"+name[1:])
   369  	formatSSERegInternal(reg[48:], out)
   370  
   371  	return out.String()
   372  }
   373  
   374  func formatSSERegInternal(xmm []byte, out *bytes.Buffer) {
   375  	buf := bytes.NewReader(xmm)
   376  
   377  	var vi [16]uint8
   378  	for i := range vi {
   379  		binary.Read(buf, binary.LittleEndian, &vi[i])
   380  	}
   381  
   382  	fmt.Fprintf(out, "0x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x%02x", vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8], vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0])
   383  
   384  	fmt.Fprintf(out, "\tv2_int={ %02x%02x%02x%02x%02x%02x%02x%02x %02x%02x%02x%02x%02x%02x%02x%02x }", vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0], vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8])
   385  
   386  	fmt.Fprintf(out, "\tv4_int={ %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x %02x%02x%02x%02x }", vi[3], vi[2], vi[1], vi[0], vi[7], vi[6], vi[5], vi[4], vi[11], vi[10], vi[9], vi[8], vi[15], vi[14], vi[13], vi[12])
   387  
   388  	fmt.Fprintf(out, "\tv8_int={ %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x %02x%02x }", vi[1], vi[0], vi[3], vi[2], vi[5], vi[4], vi[7], vi[6], vi[9], vi[8], vi[11], vi[10], vi[13], vi[12], vi[15], vi[14])
   389  
   390  	fmt.Fprintf(out, "\tv16_int={ %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x }", vi[0], vi[1], vi[2], vi[3], vi[4], vi[5], vi[6], vi[7], vi[8], vi[9], vi[10], vi[11], vi[12], vi[13], vi[14], vi[15])
   391  
   392  	buf.Seek(0, io.SeekStart)
   393  	var v2 [2]float64
   394  	for i := range v2 {
   395  		binary.Read(buf, binary.LittleEndian, &v2[i])
   396  	}
   397  	fmt.Fprintf(out, "\tv2_float={ %g %g }", v2[0], v2[1])
   398  
   399  	buf.Seek(0, io.SeekStart)
   400  	var v4 [4]float32
   401  	for i := range v4 {
   402  		binary.Read(buf, binary.LittleEndian, &v4[i])
   403  	}
   404  	fmt.Fprintf(out, "\tv4_float={ %g %g %g %g }", v4[0], v4[1], v4[2], v4[3])
   405  }
   406  
   407  func formatX87Reg(b []byte) string {
   408  	if len(b) < 10 {
   409  		return fmt.Sprintf("%#x", b)
   410  	}
   411  	mantissa := binary.LittleEndian.Uint64(b[:8])
   412  	exponent := uint16(binary.LittleEndian.Uint16(b[8:]))
   413  
   414  	var f float64
   415  	fset := false
   416  
   417  	const (
   418  		_SIGNBIT    = 1 << 15
   419  		_EXP_BIAS   = (1 << 14) - 1 // 2^(n-1) - 1 = 16383
   420  		_SPECIALEXP = (1 << 15) - 1 // all bits set
   421  		_HIGHBIT    = 1 << 63
   422  		_QUIETBIT   = 1 << 62
   423  	)
   424  
   425  	sign := 1.0
   426  	if exponent&_SIGNBIT != 0 {
   427  		sign = -1.0
   428  	}
   429  	exponent &= ^uint16(_SIGNBIT)
   430  
   431  	NaN := math.NaN()
   432  	Inf := math.Inf(+1)
   433  
   434  	switch exponent {
   435  	case 0:
   436  		switch {
   437  		case mantissa == 0:
   438  			f = sign * 0.0
   439  			fset = true
   440  		case mantissa&_HIGHBIT != 0:
   441  			f = NaN
   442  			fset = true
   443  		}
   444  	case _SPECIALEXP:
   445  		switch {
   446  		case mantissa&_HIGHBIT == 0:
   447  			f = sign * Inf
   448  			fset = true
   449  		default:
   450  			f = NaN // signaling NaN
   451  			fset = true
   452  		}
   453  	default:
   454  		if mantissa&_HIGHBIT == 0 {
   455  			f = NaN
   456  			fset = true
   457  		}
   458  	}
   459  
   460  	if !fset {
   461  		significand := float64(mantissa) / (1 << 63)
   462  		f = sign * math.Ldexp(significand, int(exponent-_EXP_BIAS))
   463  	}
   464  
   465  	var buf bytes.Buffer
   466  	binary.Write(&buf, binary.LittleEndian, exponent)
   467  	binary.Write(&buf, binary.LittleEndian, mantissa)
   468  
   469  	return fmt.Sprintf("%#04x%016x\t%g", exponent, mantissa, f)
   470  }