{"record":{"id":"c68ec1d4fdddb9db","repo":"go-delve/delve","slug":"could-not-restore-registers-v","errorCode":null,"errorMessage":"could not restore registers: %v","messagePattern":"could not restore registers: (.+?)","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/proc/fncall.go","lineNumber":863,"sourceCode":"\t\t}\n\t\t// get error from top of the stack and return it to user\n\t\terrvar, err := readStackVariable(p, thread, regs, archoff, \"string\", LoadFullValue())\n\t\tif err != nil {\n\t\t\tfncall.err = fmt.Errorf(\"could not get precheck error reason: %v\", err)\n\t\t\tbreak\n\t\t}\n\t\terrvar.Name = \"err\"\n\t\tfncall.err = fmt.Errorf(\"%v\", constant.StringVal(errvar.Value))\n\n\tcase debugCallRegCompleteCall: // 0\n\t\tp.fncallForG[callScope.g.ID].startThreadID = 0\n\n\tcase debugCallRegRestoreRegisters: // 16\n\t\t// runtime requests that we restore the registers (all except pc and sp),\n\t\t// this is also the last step of the function call protocol.\n\t\tpc, sp := regs.PC(), regs.SP()\n\t\tif err := thread.RestoreRegisters(fncall.savedRegs); err != nil {\n\t\t\tfncall.err = fmt.Errorf(\"could not restore registers: %v\", err)\n\t\t}\n\t\tif err := setPC(thread, pc); err != nil {\n\t\t\tfncall.err = fmt.Errorf(\"could not restore PC: %v\", err)\n\t\t}\n\t\tif err := setSP(thread, sp); err != nil {\n\t\t\tfncall.err = fmt.Errorf(\"could not restore SP: %v\", err)\n\t\t}\n\t\tfncallLog(\"stepping thread %d\", thread.ThreadID())\n\t\tif err := stepInstructionOut(callScope.callCtx.grp, p, thread, fncall.debugCallName, fncall.debugCallName); err != nil {\n\t\t\tfncall.err = fmt.Errorf(\"could not step out of %s: %v\", fncall.debugCallName, err)\n\t\t}\n\t\tif bi.Arch.Name == \"amd64\" {\n\t\t\t// The tail of debugCallV2 corrupts the state of RFLAGS, we must restore\n\t\t\t// it one extra time after stepping out of it.\n\t\t\t// See https://github.com/go-delve/delve/issues/2985 and\n\t\t\t// TestCallInjectionFlagCorruption\n\t\t\trflags := bi.Arch.RegistersToDwarfRegisters(0, fncall.savedRegs).Uint64Val(regnum.AMD64_Rflags)\n\t\t\terr := thread.SetReg(regnum.AMD64_Rflags, op.DwarfRegisterFromUint64(rflags))","sourceCodeStart":845,"sourceCodeEnd":881,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/fncall.go#L845-L881","documentation":"At the end of the function-call protocol the runtime asks Delve to restore all registers (except PC/SP) it saved before the call. This error is raised when thread.RestoreRegisters fails on the target thread, meaning the debuggee's register state could not be put back, leaving the thread in a possibly corrupted state.","triggerScenarios":"funcCallStep handles debugCallRegRestoreRegisters (16) and the backend's RestoreRegisters call returns an error (thread died, ptrace failure, unsupported register set).","commonSituations":"The debugged process exited or was killed mid-call; native backend ptrace failures on Linux; gdbserial connection dropped during the call; core-dump backend which cannot write registers.","solutions":["Check the target process is still alive before issuing 'call'","Reconnect / restart the debug session — the thread state may be unrecoverable","Retry the call on a stable backend (native instead of core)","Update OS/kernel or Delve if RestoreRegisters fails persistently on your platform"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"// Before 'call', verify the process is alive:\n// if dbg.Target().Process().Exited() { restart session }","typeGuard":null,"tryCatchPattern":"err := dbg.CallFunction(scope, fn, args)\nif err != nil && strings.Contains(err.Error(), \"could not restore registers\") {\n\t// thread state may be corrupted: restart the debug session\n}\n","preventionTips":["Avoid killing/terminating the process mid-call","Use the native backend rather than core dumps for call injection","Keep ptrace permissions correct (yama/ptrace_scope)","Treat any register-restore failure as session-fatal and restart"],"tags":["go","fncall","registers","ptrace"],"backgroundTag":"register-restore-failed","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}