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

     1  package proc
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"strings"
     8  
     9  	"github.com/undoio/delve/pkg/dwarf/frame"
    10  	"github.com/undoio/delve/pkg/dwarf/op"
    11  	"github.com/undoio/delve/pkg/dwarf/regnum"
    12  	"github.com/undoio/delve/pkg/goversion"
    13  )
    14  
    15  var arm64BreakInstruction = []byte{0x0, 0x0, 0x20, 0xd4}
    16  
    17  // ARM64Arch returns an initialized ARM64
    18  // struct.
    19  func ARM64Arch(goos string) *Arch {
    20  	return &Arch{
    21  		Name:                             "arm64",
    22  		ptrSize:                          8,
    23  		maxInstructionLength:             4,
    24  		breakpointInstruction:            arm64BreakInstruction,
    25  		breakInstrMovesPC:                false,
    26  		derefTLS:                         false,
    27  		prologues:                        prologuesARM64,
    28  		fixFrameUnwindContext:            arm64FixFrameUnwindContext,
    29  		switchStack:                      arm64SwitchStack,
    30  		regSize:                          arm64RegSize,
    31  		RegistersToDwarfRegisters:        arm64RegistersToDwarfRegisters,
    32  		addrAndStackRegsToDwarfRegisters: arm64AddrAndStackRegsToDwarfRegisters,
    33  		DwarfRegisterToString:            arm64DwarfRegisterToString,
    34  		inhibitStepInto:                  func(*BinaryInfo, uint64) bool { return false },
    35  		asmDecode:                        arm64AsmDecode,
    36  		usesLR:                           true,
    37  		PCRegNum:                         regnum.ARM64_PC,
    38  		SPRegNum:                         regnum.ARM64_SP,
    39  		ContextRegNum:                    regnum.ARM64_X0 + 26,
    40  		LRRegNum:                         regnum.ARM64_LR,
    41  		asmRegisters:                     arm64AsmRegisters,
    42  		RegisterNameToDwarf:              nameToDwarfFunc(regnum.ARM64NameToDwarf),
    43  		debugCallMinStackSize:            288,
    44  		maxRegArgBytes:                   16*8 + 16*8, // 16 int argument registers plus 16 float argument registers
    45  	}
    46  }
    47  
    48  func arm64FixFrameUnwindContext(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.ARM64_PC,
    75  			Regs: map[uint64]frame.DWRule{
    76  				regnum.ARM64_PC: frame.DWRule{
    77  					Rule:   frame.RuleOffset,
    78  					Offset: int64(-a.PtrSize()),
    79  				},
    80  				regnum.ARM64_BP: frame.DWRule{
    81  					Rule:   frame.RuleOffset,
    82  					Offset: int64(-2 * a.PtrSize()),
    83  				},
    84  				regnum.ARM64_SP: frame.DWRule{
    85  					Rule:   frame.RuleValOffset,
    86  					Offset: 0,
    87  				},
    88  			},
    89  			CFA: frame.DWRule{
    90  				Rule:   frame.RuleCFA,
    91  				Reg:    regnum.ARM64_BP,
    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.ARM64_BP].Rule == frame.RuleUndefined {
   119  		fctxt.Regs[regnum.ARM64_BP] = frame.DWRule{
   120  			Rule:   frame.RuleFramePointer,
   121  			Reg:    regnum.ARM64_BP,
   122  			Offset: 0,
   123  		}
   124  	}
   125  	if fctxt.Regs[regnum.ARM64_LR].Rule == frame.RuleUndefined {
   126  		fctxt.Regs[regnum.ARM64_LR] = frame.DWRule{
   127  			Rule:   frame.RuleFramePointer,
   128  			Reg:    regnum.ARM64_LR,
   129  			Offset: 0,
   130  		}
   131  	}
   132  
   133  	return fctxt
   134  }
   135  
   136  const arm64cgocallSPOffsetSaveSlot = 0x8
   137  const prevG0schedSPOffsetSaveSlot = 0x10
   138  
   139  func arm64SwitchStack(it *stackIterator, callFrameRegs *op.DwarfRegisters) bool {
   140  	if it.frame.Current.Fn == nil && it.systemstack && it.g != nil && it.top {
   141  		it.switchToGoroutineStack()
   142  		return true
   143  	}
   144  	if it.frame.Current.Fn != nil {
   145  		switch it.frame.Current.Fn.Name {
   146  		case "runtime.asmcgocall", "runtime.cgocallback_gofunc", "runtime.sigpanic", "runtime.cgocallback":
   147  			//do nothing
   148  		case "runtime.goexit", "runtime.rt0_go", "runtime.mcall":
   149  			// Look for "top of stack" functions.
   150  			it.atend = true
   151  			return true
   152  		case "crosscall2":
   153  			//The offsets get from runtime/cgo/asm_arm64.s:10
   154  			bpoff := uint64(14)
   155  			lroff := uint64(15)
   156  			if producer := it.bi.Producer(); producer != "" && goversion.ProducerAfterOrEqual(producer, 1, 19) {
   157  				// In Go 1.19 (specifically eee6f9f82) the order registers are saved was changed.
   158  				bpoff = 22
   159  				lroff = 23
   160  			}
   161  			newsp, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*24), int64(it.bi.Arch.PtrSize()))
   162  			newbp, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*bpoff), int64(it.bi.Arch.PtrSize()))
   163  			newlr, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*lroff), int64(it.bi.Arch.PtrSize()))
   164  			if it.regs.Reg(it.regs.BPRegNum) != nil {
   165  				it.regs.Reg(it.regs.BPRegNum).Uint64Val = uint64(newbp)
   166  			} else {
   167  				reg, _ := it.readRegisterAt(it.regs.BPRegNum, it.regs.SP()+8*bpoff)
   168  				it.regs.AddReg(it.regs.BPRegNum, reg)
   169  			}
   170  			it.regs.Reg(it.regs.LRRegNum).Uint64Val = uint64(newlr)
   171  			it.regs.Reg(it.regs.SPRegNum).Uint64Val = uint64(newsp)
   172  			it.pc = newlr
   173  			return true
   174  		default:
   175  			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" {
   176  				// The runtime switches to the system stack in multiple places.
   177  				// This usually happens through a call to runtime.systemstack but there
   178  				// are functions that switch to the system stack manually (for example
   179  				// runtime.morestack).
   180  				// Since we are only interested in printing the system stack for cgo
   181  				// calls we switch directly to the goroutine stack if we detect that the
   182  				// function at the top of the stack is a runtime function.
   183  				it.switchToGoroutineStack()
   184  				return true
   185  			}
   186  		}
   187  	}
   188  
   189  	fn := it.bi.PCToFunc(it.frame.Ret)
   190  	if fn == nil {
   191  		return false
   192  	}
   193  	switch fn.Name {
   194  	case "runtime.asmcgocall":
   195  		if !it.systemstack {
   196  			return false
   197  		}
   198  
   199  		// This function is called by a goroutine to execute a C function and
   200  		// switches from the goroutine stack to the system stack.
   201  		// Since we are unwinding the stack from callee to caller we have to switch
   202  		// from the system stack to the goroutine stack.
   203  		off, _ := readIntRaw(it.mem, uint64(callFrameRegs.SP()+arm64cgocallSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize()))
   204  		oldsp := callFrameRegs.SP()
   205  		newsp := uint64(int64(it.stackhi) - off)
   206  
   207  		// runtime.asmcgocall can also be called from inside the system stack,
   208  		// in that case no stack switch actually happens
   209  		if newsp == oldsp {
   210  			return false
   211  		}
   212  		it.systemstack = false
   213  		callFrameRegs.Reg(callFrameRegs.SPRegNum).Uint64Val = uint64(int64(newsp))
   214  		return false
   215  
   216  	case "runtime.cgocallback_gofunc", "runtime.cgocallback":
   217  		// For a detailed description of how this works read the long comment at
   218  		// the start of $GOROOT/src/runtime/cgocall.go and the source code of
   219  		// runtime.cgocallback_gofunc in $GOROOT/src/runtime/asm_arm64.s
   220  		//
   221  		// When a C functions calls back into go it will eventually call into
   222  		// runtime.cgocallback_gofunc which is the function that does the stack
   223  		// switch from the system stack back into the goroutine stack
   224  		// Since we are going backwards on the stack here we see the transition
   225  		// as goroutine stack -> system stack.
   226  		if it.systemstack {
   227  			return false
   228  		}
   229  
   230  		it.loadG0SchedSP()
   231  		if it.g0_sched_sp <= 0 {
   232  			return false
   233  		}
   234  		// entering the system stack
   235  		callFrameRegs.Reg(callFrameRegs.SPRegNum).Uint64Val = it.g0_sched_sp
   236  		// reads the previous value of g0.sched.sp that runtime.cgocallback_gofunc saved on the stack
   237  
   238  		it.g0_sched_sp, _ = readUintRaw(it.mem, uint64(callFrameRegs.SP()+prevG0schedSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize()))
   239  		it.systemstack = true
   240  		return false
   241  	}
   242  
   243  	return false
   244  }
   245  
   246  func arm64RegSize(regnum uint64) int {
   247  	// fp registers
   248  	if regnum >= 64 && regnum <= 95 {
   249  		return 16
   250  	}
   251  
   252  	return 8 // general registers
   253  }
   254  
   255  var arm64NameToDwarf = func() map[string]int {
   256  	r := make(map[string]int)
   257  	for i := 0; i <= 30; i++ {
   258  		r[fmt.Sprintf("x%d", i)] = i
   259  	}
   260  	r["pc"] = int(regnum.ARM64_PC)
   261  	r["lr"] = int(regnum.ARM64_LR)
   262  	r["sp"] = 31
   263  	for i := 0; i <= 31; i++ {
   264  		r[fmt.Sprintf("v%d", i)] = i + 64
   265  	}
   266  	return r
   267  }()
   268  
   269  func arm64RegistersToDwarfRegisters(staticBase uint64, regs Registers) *op.DwarfRegisters {
   270  	dregs := initDwarfRegistersFromSlice(int(regnum.ARM64MaxRegNum()), regs, regnum.ARM64NameToDwarf)
   271  	dr := op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.ARM64_PC, regnum.ARM64_SP, regnum.ARM64_BP, regnum.ARM64_LR)
   272  	dr.SetLoadMoreCallback(loadMoreDwarfRegistersFromSliceFunc(dr, regs, arm64NameToDwarf))
   273  	return dr
   274  }
   275  
   276  func arm64AddrAndStackRegsToDwarfRegisters(staticBase, pc, sp, bp, lr uint64) op.DwarfRegisters {
   277  	dregs := make([]*op.DwarfRegister, regnum.ARM64_PC+1)
   278  	dregs[regnum.ARM64_PC] = op.DwarfRegisterFromUint64(pc)
   279  	dregs[regnum.ARM64_SP] = op.DwarfRegisterFromUint64(sp)
   280  	dregs[regnum.ARM64_BP] = op.DwarfRegisterFromUint64(bp)
   281  	dregs[regnum.ARM64_LR] = op.DwarfRegisterFromUint64(lr)
   282  
   283  	return *op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.ARM64_PC, regnum.ARM64_SP, regnum.ARM64_BP, regnum.ARM64_LR)
   284  }
   285  
   286  func arm64DwarfRegisterToString(i int, reg *op.DwarfRegister) (name string, floatingPoint bool, repr string) {
   287  	name = regnum.ARM64ToName(uint64(i))
   288  
   289  	if reg == nil {
   290  		return name, false, ""
   291  	}
   292  
   293  	if reg.Bytes != nil && name[0] == 'V' {
   294  		buf := bytes.NewReader(reg.Bytes)
   295  
   296  		var out bytes.Buffer
   297  		var vi [16]uint8
   298  		for i := range vi {
   299  			_ = binary.Read(buf, binary.LittleEndian, &vi[i])
   300  		}
   301  		//D
   302  		fmt.Fprintf(&out, " {\n\tD = {u = {0x%02x%02x%02x%02x%02x%02x%02x%02x,", vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0])
   303  		fmt.Fprintf(&out, " 0x%02x%02x%02x%02x%02x%02x%02x%02x},", vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8])
   304  		fmt.Fprintf(&out, " s = {0x%02x%02x%02x%02x%02x%02x%02x%02x,", vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0])
   305  		fmt.Fprintf(&out, " 0x%02x%02x%02x%02x%02x%02x%02x%02x}},", vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8])
   306  
   307  		//S
   308  		fmt.Fprintf(&out, " \n\tS = {u = {0x%02x%02x%02x%02x,0x%02x%02x%02x%02x,", vi[3], vi[2], vi[1], vi[0], vi[7], vi[6], vi[5], vi[4])
   309  		fmt.Fprintf(&out, " 0x%02x%02x%02x%02x,0x%02x%02x%02x%02x},", vi[11], vi[10], vi[9], vi[8], vi[15], vi[14], vi[13], vi[12])
   310  		fmt.Fprintf(&out, " s = {0x%02x%02x%02x%02x,0x%02x%02x%02x%02x,", vi[3], vi[2], vi[1], vi[0], vi[7], vi[6], vi[5], vi[4])
   311  		fmt.Fprintf(&out, " 0x%02x%02x%02x%02x,0x%02x%02x%02x%02x}},", vi[11], vi[10], vi[9], vi[8], vi[15], vi[14], vi[13], vi[12])
   312  
   313  		//H
   314  		fmt.Fprintf(&out, " \n\tH = {u = {0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x,", vi[1], vi[0], vi[3], vi[2], vi[5], vi[4], vi[7], vi[6])
   315  		fmt.Fprintf(&out, " 0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x},", vi[9], vi[8], vi[11], vi[10], vi[13], vi[12], vi[15], vi[14])
   316  		fmt.Fprintf(&out, " s = {0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x,", vi[1], vi[0], vi[3], vi[2], vi[5], vi[4], vi[7], vi[6])
   317  		fmt.Fprintf(&out, " 0x%02x%02x,0x%02x%02x,0x%02x%02x,0x%02x%02x}},", vi[9], vi[8], vi[11], vi[10], vi[13], vi[12], vi[15], vi[14])
   318  
   319  		//B
   320  		fmt.Fprintf(&out, " \n\tB = {u = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,", vi[0], vi[1], vi[2], vi[3], vi[4], vi[5], vi[6], vi[7])
   321  		fmt.Fprintf(&out, " 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x},", vi[8], vi[9], vi[10], vi[11], vi[12], vi[13], vi[14], vi[15])
   322  		fmt.Fprintf(&out, " s = {0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,", vi[0], vi[1], vi[2], vi[3], vi[4], vi[5], vi[6], vi[7])
   323  		fmt.Fprintf(&out, " 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x}}", vi[8], vi[9], vi[10], vi[11], vi[12], vi[13], vi[14], vi[15])
   324  
   325  		//Q
   326  		fmt.Fprintf(&out, " \n\tQ = {u = {0x%02x%02x%02x%02x%02x%02x%02x%02x", vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8])
   327  		fmt.Fprintf(&out, "%02x%02x%02x%02x%02x%02x%02x%02x},", vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0])
   328  		fmt.Fprintf(&out, " s = {0x%02x%02x%02x%02x%02x%02x%02x%02x", vi[15], vi[14], vi[13], vi[12], vi[11], vi[10], vi[9], vi[8])
   329  		fmt.Fprintf(&out, "%02x%02x%02x%02x%02x%02x%02x%02x}}\n\t}", vi[7], vi[6], vi[5], vi[4], vi[3], vi[2], vi[1], vi[0])
   330  		return name, true, out.String()
   331  	} else if reg.Bytes == nil || (reg.Bytes != nil && len(reg.Bytes) < 16) {
   332  		return name, false, fmt.Sprintf("%#016x", reg.Uint64Val)
   333  	}
   334  	return name, false, fmt.Sprintf("%#x", reg.Bytes)
   335  }