github.com/cnboonhan/delve@v0.0.0-20230908061759-363f2388c2fb/pkg/proc/arm64_arch.go (about)

     1  package proc
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/binary"
     6  	"fmt"
     7  	"runtime"
     8  
     9  	"github.com/go-delve/delve/pkg/dwarf/frame"
    10  	"github.com/go-delve/delve/pkg/dwarf/op"
    11  	"github.com/go-delve/delve/pkg/dwarf/regnum"
    12  	"github.com/go-delve/delve/pkg/goversion"
    13  )
    14  
    15  var arm64BreakInstruction = []byte{0x0, 0x0, 0x20, 0xd4}
    16  
    17  // Windows ARM64 expects a breakpoint to be compiled to the instruction BRK #0xF000.
    18  // See go.dev/issues/53837.
    19  var arm64WindowsBreakInstruction = []byte{0x0, 0x0, 0x3e, 0xd4}
    20  
    21  // ARM64Arch returns an initialized ARM64
    22  // struct.
    23  func ARM64Arch(goos string) *Arch {
    24  	var brk []byte
    25  	if goos == "windows" {
    26  		brk = arm64WindowsBreakInstruction
    27  	} else {
    28  		brk = arm64BreakInstruction
    29  	}
    30  	return &Arch{
    31  		Name:                             "arm64",
    32  		ptrSize:                          8,
    33  		maxInstructionLength:             4,
    34  		breakpointInstruction:            brk,
    35  		breakInstrMovesPC:                goos == "windows",
    36  		derefTLS:                         false,
    37  		prologues:                        prologuesARM64,
    38  		fixFrameUnwindContext:            arm64FixFrameUnwindContext,
    39  		switchStack:                      arm64SwitchStack,
    40  		regSize:                          arm64RegSize,
    41  		RegistersToDwarfRegisters:        arm64RegistersToDwarfRegisters,
    42  		addrAndStackRegsToDwarfRegisters: arm64AddrAndStackRegsToDwarfRegisters,
    43  		DwarfRegisterToString:            arm64DwarfRegisterToString,
    44  		inhibitStepInto:                  func(*BinaryInfo, uint64) bool { return false },
    45  		asmDecode:                        arm64AsmDecode,
    46  		usesLR:                           true,
    47  		PCRegNum:                         regnum.ARM64_PC,
    48  		SPRegNum:                         regnum.ARM64_SP,
    49  		ContextRegNum:                    regnum.ARM64_X0 + 26,
    50  		LRRegNum:                         regnum.ARM64_LR,
    51  		asmRegisters:                     arm64AsmRegisters,
    52  		RegisterNameToDwarf:              nameToDwarfFunc(regnum.ARM64NameToDwarf),
    53  		RegnumToString:                   regnum.ARM64ToName,
    54  		debugCallMinStackSize:            288,
    55  		maxRegArgBytes:                   16*8 + 16*8, // 16 int argument registers plus 16 float argument registers
    56  	}
    57  }
    58  
    59  func arm64FixFrameUnwindContext(fctxt *frame.FrameContext, pc uint64, bi *BinaryInfo) *frame.FrameContext {
    60  	a := bi.Arch
    61  	if a.sigreturnfn == nil {
    62  		a.sigreturnfn = bi.lookupOneFunc("runtime.sigreturn")
    63  	}
    64  
    65  	if fctxt == nil || (a.sigreturnfn != nil && pc >= a.sigreturnfn.Entry && pc < a.sigreturnfn.End) {
    66  		// When there's no frame descriptor entry use BP (the frame pointer) instead
    67  		// - return register is [bp + a.PtrSize()] (i.e. [cfa-a.PtrSize()])
    68  		// - cfa is bp + a.PtrSize()*2
    69  		// - bp is [bp] (i.e. [cfa-a.PtrSize()*2])
    70  		// - sp is cfa
    71  
    72  		// When the signal handler runs it will move the execution to the signal
    73  		// handling stack (installed using the sigaltstack system call).
    74  		// This isn't a proper stack switch: the pointer to g in TLS will still
    75  		// refer to whatever g was executing on that thread before the signal was
    76  		// received.
    77  		// Since go did not execute a stack switch the previous value of sp, pc
    78  		// and bp is not saved inside g.sched, as it normally would.
    79  		// The only way to recover is to either read sp/pc from the signal context
    80  		// parameter (the ucontext_t* parameter) or to unconditionally follow the
    81  		// frame pointer when we get to runtime.sigreturn (which is what we do
    82  		// here).
    83  
    84  		return &frame.FrameContext{
    85  			RetAddrReg: regnum.ARM64_PC,
    86  			Regs: map[uint64]frame.DWRule{
    87  				regnum.ARM64_PC: {
    88  					Rule:   frame.RuleOffset,
    89  					Offset: int64(-a.PtrSize()),
    90  				},
    91  				regnum.ARM64_BP: {
    92  					Rule:   frame.RuleOffset,
    93  					Offset: int64(-2 * a.PtrSize()),
    94  				},
    95  				regnum.ARM64_SP: {
    96  					Rule:   frame.RuleValOffset,
    97  					Offset: 0,
    98  				},
    99  			},
   100  			CFA: frame.DWRule{
   101  				Rule:   frame.RuleCFA,
   102  				Reg:    regnum.ARM64_BP,
   103  				Offset: int64(2 * a.PtrSize()),
   104  			},
   105  		}
   106  	}
   107  
   108  	if a.crosscall2fn == nil {
   109  		a.crosscall2fn = bi.lookupOneFunc("crosscall2")
   110  	}
   111  
   112  	if a.crosscall2fn != nil && pc >= a.crosscall2fn.Entry && pc < a.crosscall2fn.End {
   113  		rule := fctxt.CFA
   114  		if rule.Offset == crosscall2SPOffsetBad {
   115  			rule.Offset += crosscall2SPOffset
   116  		}
   117  		fctxt.CFA = rule
   118  	}
   119  
   120  	// We assume that RBP is the frame pointer and we want to keep it updated,
   121  	// so that we can use it to unwind the stack even when we encounter frames
   122  	// without descriptor entries.
   123  	// If there isn't a rule already we emit one.
   124  	if fctxt.Regs[regnum.ARM64_BP].Rule == frame.RuleUndefined {
   125  		fctxt.Regs[regnum.ARM64_BP] = frame.DWRule{
   126  			Rule:   frame.RuleFramePointer,
   127  			Reg:    regnum.ARM64_BP,
   128  			Offset: 0,
   129  		}
   130  	}
   131  	if fctxt.Regs[regnum.ARM64_LR].Rule == frame.RuleUndefined {
   132  		fctxt.Regs[regnum.ARM64_LR] = frame.DWRule{
   133  			Rule:   frame.RuleRegister,
   134  			Reg:    regnum.ARM64_LR,
   135  			Offset: 0,
   136  		}
   137  	}
   138  
   139  	return fctxt
   140  }
   141  
   142  const arm64cgocallSPOffsetSaveSlot = 0x8
   143  const prevG0schedSPOffsetSaveSlot = 0x10
   144  
   145  func arm64SwitchStack(it *stackIterator, callFrameRegs *op.DwarfRegisters) bool {
   146  	linux := runtime.GOOS == "linux"
   147  	if it.frame.Current.Fn == nil {
   148  		if it.systemstack && it.g != nil && it.top {
   149  			it.switchToGoroutineStack()
   150  			return true
   151  		}
   152  		return false
   153  	}
   154  	switch it.frame.Current.Fn.Name {
   155  	case "runtime.cgocallback_gofunc", "runtime.cgocallback":
   156  		if linux {
   157  			// For a detailed description of how this works read the long comment at
   158  			// the start of $GOROOT/src/runtime/cgocall.go and the source code of
   159  			// runtime.cgocallback_gofunc in $GOROOT/src/runtime/asm_arm64.s
   160  			//
   161  			// When a C function calls back into go it will eventually call into
   162  			// runtime.cgocallback_gofunc which is the function that does the stack
   163  			// switch from the system stack back into the goroutine stack
   164  			// Since we are going backwards on the stack here we see the transition
   165  			// as goroutine stack -> system stack.
   166  			if it.top || it.systemstack {
   167  				return false
   168  			}
   169  
   170  			it.loadG0SchedSP()
   171  			if it.g0_sched_sp <= 0 {
   172  				return false
   173  			}
   174  			// Entering the system stack.
   175  			it.regs.Reg(callFrameRegs.SPRegNum).Uint64Val = it.g0_sched_sp
   176  			// Reads the previous value of g0.sched.sp that runtime.cgocallback_gofunc saved on the stack.
   177  			it.g0_sched_sp, _ = readUintRaw(it.mem, uint64(it.regs.SP()+prevG0schedSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize()))
   178  			it.top = false
   179  			callFrameRegs, ret, retaddr := it.advanceRegs()
   180  			frameOnSystemStack := it.newStackframe(ret, retaddr)
   181  			it.pc = frameOnSystemStack.Ret
   182  			it.regs = callFrameRegs
   183  			it.systemstack = true
   184  
   185  			return true
   186  		}
   187  
   188  	case "runtime.asmcgocall":
   189  		if linux {
   190  			if it.top || !it.systemstack {
   191  				return false
   192  			}
   193  
   194  			// This function is called by a goroutine to execute a C function and
   195  			// switches from the goroutine stack to the system stack.
   196  			// Since we are unwinding the stack from callee to caller we have to switch
   197  			// from the system stack to the goroutine stack.
   198  			off, _ := readIntRaw(it.mem, uint64(it.regs.SP()+arm64cgocallSPOffsetSaveSlot),
   199  				int64(it.bi.Arch.PtrSize()))
   200  			oldsp := it.regs.SP()
   201  			newsp := uint64(int64(it.stackhi) - off)
   202  
   203  			it.regs.Reg(it.regs.SPRegNum).Uint64Val = uint64(int64(newsp))
   204  			// runtime.asmcgocall can also be called from inside the system stack,
   205  			// in that case no stack switch actually happens
   206  			if it.regs.SP() == oldsp {
   207  				return false
   208  			}
   209  
   210  			it.top = false
   211  			it.systemstack = false
   212  			// The return value is stored in the LR register which is saved at 24(SP).
   213  			addrret := uint64(int64(it.regs.SP()) + int64(it.bi.Arch.PtrSize()*3))
   214  			it.frame.Ret, _ = readUintRaw(it.mem, addrret, int64(it.bi.Arch.PtrSize()))
   215  			it.pc = it.frame.Ret
   216  
   217  			return true
   218  		}
   219  
   220  	case "runtime.goexit", "runtime.rt0_go":
   221  		// Look for "top of stack" functions.
   222  		it.atend = true
   223  		return true
   224  
   225  	case "runtime.mcall":
   226  		if it.systemstack && it.g != nil {
   227  			it.switchToGoroutineStack()
   228  			return true
   229  		}
   230  		it.atend = true
   231  		return true
   232  
   233  	case "crosscall2":
   234  		// The offsets get from runtime/cgo/asm_arm64.s:10
   235  		bpoff := uint64(14)
   236  		lroff := uint64(15)
   237  		if producer := it.bi.Producer(); producer != "" && goversion.ProducerAfterOrEqual(producer, 1, 19) {
   238  			// In Go 1.19 (specifically eee6f9f82) the order registers are saved was changed.
   239  			bpoff = 22
   240  			lroff = 23
   241  		}
   242  		newsp, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*24), int64(it.bi.Arch.PtrSize()))
   243  		newbp, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*bpoff), int64(it.bi.Arch.PtrSize()))
   244  		newlr, _ := readUintRaw(it.mem, uint64(it.regs.SP()+8*lroff), int64(it.bi.Arch.PtrSize()))
   245  		if it.regs.Reg(it.regs.BPRegNum) != nil {
   246  			it.regs.Reg(it.regs.BPRegNum).Uint64Val = uint64(newbp)
   247  		} else {
   248  			reg, _ := it.readRegisterAt(it.regs.BPRegNum, it.regs.SP()+8*bpoff)
   249  			it.regs.AddReg(it.regs.BPRegNum, reg)
   250  		}
   251  		it.regs.Reg(it.regs.LRRegNum).Uint64Val = uint64(newlr)
   252  		if linux {
   253  			it.regs.Reg(it.regs.SPRegNum).Uint64Val = uint64(newbp)
   254  		} else {
   255  			it.regs.Reg(it.regs.SPRegNum).Uint64Val = uint64(newsp)
   256  		}
   257  		it.pc = newlr
   258  		return true
   259  	case "runtime.mstart":
   260  		if linux {
   261  			// Calls to runtime.systemstack will switch to the systemstack then:
   262  			// 1. alter the goroutine stack so that it looks like systemstack_switch
   263  			//    was called
   264  			// 2. alter the system stack so that it looks like the bottom-most frame
   265  			//    belongs to runtime.mstart
   266  			// If we find a runtime.mstart frame on the system stack of a goroutine
   267  			// parked on runtime.systemstack_switch we assume runtime.systemstack was
   268  			// called and continue tracing from the parked position.
   269  
   270  			if it.top || !it.systemstack || it.g == nil {
   271  				return false
   272  			}
   273  			if fn := it.bi.PCToFunc(it.g.PC); fn == nil || fn.Name != "runtime.systemstack_switch" {
   274  				return false
   275  			}
   276  
   277  			it.switchToGoroutineStack()
   278  			return true
   279  		}
   280  
   281  	case "runtime.newstack", "runtime.systemstack":
   282  		if it.systemstack && it.g != nil {
   283  			it.switchToGoroutineStack()
   284  			return true
   285  		}
   286  	}
   287  
   288  	fn := it.bi.PCToFunc(it.frame.Ret)
   289  	if fn == nil {
   290  		return false
   291  	}
   292  	switch fn.Name {
   293  	case "runtime.asmcgocall":
   294  		if !it.systemstack {
   295  			return false
   296  		}
   297  
   298  		// This function is called by a goroutine to execute a C function and
   299  		// switches from the goroutine stack to the system stack.
   300  		// Since we are unwinding the stack from callee to caller we have to switch
   301  		// from the system stack to the goroutine stack.
   302  		off, _ := readIntRaw(it.mem, uint64(callFrameRegs.SP()+arm64cgocallSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize()))
   303  		oldsp := callFrameRegs.SP()
   304  		newsp := uint64(int64(it.stackhi) - off)
   305  
   306  		// runtime.asmcgocall can also be called from inside the system stack,
   307  		// in that case no stack switch actually happens
   308  		if newsp == oldsp {
   309  			return false
   310  		}
   311  		it.systemstack = false
   312  		callFrameRegs.Reg(callFrameRegs.SPRegNum).Uint64Val = uint64(int64(newsp))
   313  		return false
   314  
   315  	case "runtime.cgocallback_gofunc", "runtime.cgocallback":
   316  		// For a detailed description of how this works read the long comment at
   317  		// the start of $GOROOT/src/runtime/cgocall.go and the source code of
   318  		// runtime.cgocallback_gofunc in $GOROOT/src/runtime/asm_arm64.s
   319  		//
   320  		// When a C functions calls back into go it will eventually call into
   321  		// runtime.cgocallback_gofunc which is the function that does the stack
   322  		// switch from the system stack back into the goroutine stack
   323  		// Since we are going backwards on the stack here we see the transition
   324  		// as goroutine stack -> system stack.
   325  		if it.systemstack {
   326  			return false
   327  		}
   328  
   329  		it.loadG0SchedSP()
   330  		if it.g0_sched_sp <= 0 {
   331  			return false
   332  		}
   333  		// entering the system stack
   334  		callFrameRegs.Reg(callFrameRegs.SPRegNum).Uint64Val = it.g0_sched_sp
   335  		// reads the previous value of g0.sched.sp that runtime.cgocallback_gofunc saved on the stack
   336  
   337  		it.g0_sched_sp, _ = readUintRaw(it.mem, uint64(callFrameRegs.SP()+prevG0schedSPOffsetSaveSlot), int64(it.bi.Arch.PtrSize()))
   338  		it.systemstack = true
   339  		return false
   340  	}
   341  
   342  	return false
   343  }
   344  
   345  func arm64RegSize(regnum uint64) int {
   346  	// fp registers
   347  	if regnum >= 64 && regnum <= 95 {
   348  		return 16
   349  	}
   350  
   351  	return 8 // general registers
   352  }
   353  
   354  var arm64NameToDwarf = func() map[string]int {
   355  	r := make(map[string]int)
   356  	for i := 0; i <= 30; i++ {
   357  		r[fmt.Sprintf("x%d", i)] = i
   358  	}
   359  	r["pc"] = int(regnum.ARM64_PC)
   360  	r["lr"] = int(regnum.ARM64_LR)
   361  	r["sp"] = 31
   362  	for i := 0; i <= 31; i++ {
   363  		r[fmt.Sprintf("v%d", i)] = i + 64
   364  	}
   365  	return r
   366  }()
   367  
   368  func arm64RegistersToDwarfRegisters(staticBase uint64, regs Registers) *op.DwarfRegisters {
   369  	dregs := initDwarfRegistersFromSlice(int(regnum.ARM64MaxRegNum()), regs, regnum.ARM64NameToDwarf)
   370  	dr := op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.ARM64_PC, regnum.ARM64_SP, regnum.ARM64_BP, regnum.ARM64_LR)
   371  	dr.SetLoadMoreCallback(loadMoreDwarfRegistersFromSliceFunc(dr, regs, arm64NameToDwarf))
   372  	return dr
   373  }
   374  
   375  func arm64AddrAndStackRegsToDwarfRegisters(staticBase, pc, sp, bp, lr uint64) op.DwarfRegisters {
   376  	dregs := make([]*op.DwarfRegister, regnum.ARM64_PC+1)
   377  	dregs[regnum.ARM64_PC] = op.DwarfRegisterFromUint64(pc)
   378  	dregs[regnum.ARM64_SP] = op.DwarfRegisterFromUint64(sp)
   379  	dregs[regnum.ARM64_BP] = op.DwarfRegisterFromUint64(bp)
   380  	dregs[regnum.ARM64_LR] = op.DwarfRegisterFromUint64(lr)
   381  
   382  	return *op.NewDwarfRegisters(staticBase, dregs, binary.LittleEndian, regnum.ARM64_PC, regnum.ARM64_SP, regnum.ARM64_BP, regnum.ARM64_LR)
   383  }
   384  
   385  func arm64DwarfRegisterToString(i int, reg *op.DwarfRegister) (name string, floatingPoint bool, repr string) {
   386  	name = regnum.ARM64ToName(uint64(i))
   387  
   388  	if reg == nil {
   389  		return name, false, ""
   390  	}
   391  
   392  	if reg.Bytes != nil && name[0] == 'V' {
   393  		buf := bytes.NewReader(reg.Bytes)
   394  
   395  		var out bytes.Buffer
   396  		var vi [16]uint8
   397  		for i := range vi {
   398  			_ = binary.Read(buf, binary.LittleEndian, &vi[i])
   399  		}
   400  		//D
   401  		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])
   402  		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])
   403  		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])
   404  		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])
   405  
   406  		//S
   407  		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])
   408  		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])
   409  		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])
   410  		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])
   411  
   412  		//H
   413  		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])
   414  		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])
   415  		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])
   416  		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])
   417  
   418  		//B
   419  		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])
   420  		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])
   421  		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])
   422  		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])
   423  
   424  		//Q
   425  		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])
   426  		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])
   427  		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])
   428  		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])
   429  		return name, true, out.String()
   430  	} else if reg.Bytes == nil || (reg.Bytes != nil && len(reg.Bytes) < 16) {
   431  		return name, false, fmt.Sprintf("%#016x", reg.Uint64Val)
   432  	}
   433  	return name, false, fmt.Sprintf("%#x", reg.Bytes)
   434  }