{"record":{"id":"1ba9179ade4d845c","repo":"go-delve/delve","slug":"can-not-write-registers","errorCode":null,"errorMessage":"can not write registers","messagePattern":"can not write registers","errorType":"error_code","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"pkg/proc/mem.go","lineNumber":195,"sourceCode":"\nfunc (mem *compositeMemory) ReadMemory(data []byte, addr uint64) (int, error) {\n\taddr -= mem.base\n\tif addr >= uint64(len(mem.data)) || addr+uint64(len(data)) > uint64(len(mem.data)) {\n\t\treturn 0, errors.New(\"read out of bounds\")\n\t}\n\tcopy(data, mem.data[addr:addr+uint64(len(data))])\n\treturn len(data), nil\n}\n\nfunc (mem *compositeMemory) WriteMemory(addr uint64, data []byte) (int, error) {\n\taddr -= mem.base\n\tif addr >= uint64(len(mem.data)) || addr+uint64(len(data)) > uint64(len(mem.data)) {\n\t\treturn 0, errors.New(\"write out of bounds\")\n\t}\n\tif mem.regs.ChangeFunc == nil {\n\t\tfor _, piece := range mem.pieces {\n\t\t\tif piece.Kind == op.RegPiece {\n\t\t\t\treturn 0, errors.New(\"can not write registers\")\n\t\t\t}\n\t\t}\n\t}\n\n\tcopy(mem.data[addr:], data)\n\n\tcurAddr := uint64(0)\n\tdonesz := 0\n\tfor _, piece := range mem.pieces {\n\t\tif curAddr < (addr+uint64(len(data))) && addr < (curAddr+uint64(piece.Size)) {\n\t\t\t// changed memory interval overlaps current piece\n\t\t\tpieceMem := mem.data[curAddr : curAddr+uint64(piece.Size)]\n\n\t\t\tswitch piece.Kind {\n\t\t\tcase op.RegPiece:\n\t\t\t\toldReg := mem.regs.Reg(piece.Val)\n\t\t\t\tnewReg := op.DwarfRegisterFromBytes(pieceMem)\n\t\t\t\terr := mem.regs.ChangeFunc(piece.Val, oldReg.Overwrite(newReg))","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/go-delve/delve/blob/a23773e6c31361e43246bc43a424ee009679b174/pkg/proc/mem.go#L177-L213","documentation":"After bounds checking, compositeMemory.WriteMemory inspects its pieces; if any piece is a register piece (op.RegPiece) and mem.regs.ChangeFunc is nil, there is no mechanism to write values back into CPU registers, so it returns 'can not write registers'. This protects against silently writing only the cached slice while the live value stays in a register.","triggerScenarios":"Assigning to a variable whose value is (partially) held in CPU registers via compositeMemory when the register set has no ChangeFunc — e.g. the 'set' command on an argument or local variable kept in a register, common in optimized code and on architectures/backends without register-write support (e.g. core dump targets).","commonSituations":"Setting variables in optimized binaries where parameters are register-allocated; attempting writes while debugging a core dump (registers are read-only); backends lacking register modification support.","solutions":["Rebuild the target with -gcflags=\"all=-N -l\" so the variable spills to a stack slot, then set it","Set a different, memory-located variable or restructure code (copy the value to a stack variable) for debugging","Avoid variable writes in core-dump sessions; instead, edit code or use live debugging with a backend supporting register writes"],"exampleFix":"// before\n$ go build main.go   # i kept in a register\n(dlv) set i = 10     // can not write registers\n// after\n$ go build -gcflags=\"all=-N -l\" main.go\n(dlv) set i = 10","handlingStrategy":"fallback","validationCode":"// Detect register-backed variables before writing:\n//   (dlv) print &i\n// If the address is not a normal stack address (or the build is optimized\n// or you are in a core dump), the write will hit registers and fail.","typeGuard":null,"tryCatchPattern":"err := setVariable(\"i\", \"10\")\nif err != nil && strings.Contains(err.Error(), \"can not write registers\") {\n    log.Printf(\"variable held in registers; rebuild with -gcflags='all=-N -l' to set it\")\n    return fallbackSkipWrite()\n}","preventionTips":["Build debug binaries with -gcflags=\"all=-N -l\" so locals spill to memory","Do not attempt variable writes in core-dump sessions","Assign to stack variables (copies) instead of register-kept parameters"],"tags":["go","registers","optimized-code","debugger"],"backgroundTag":"register-write-unsupported","analyzedSha":"a23773e6c31361e43246bc43a424ee009679b174","analyzedAt":"2026-08-31T15:12:45.221Z","schemaVersion":2},"datasetVersion":"2026-08-31T19:17:28.585Z"}