go-delve/delve · error

wrong response length, expected %d got %d

Error message

wrong response length, expected %d got %d

What it means

This error is thrown by gdbConn.readRegister when the GDB remote stub returns a hex-encoded register value longer than the expected 2*len(data) hex characters, i.e. the target sent more bytes than the destination buffer can hold. Delve validates the response length before decoding to avoid overflowing the caller-provided byte slice. It indicates the remote stub answered a register-read ('p' packet) with an unexpected register size, usually because stub and client disagree on register width.

Source

Thrown at pkg/proc/gdbserial/gdbserver_conn.go:538

}

// readRegister executes 'p' (read register) command.
func (conn *gdbConn) readRegister(threadID string, regnum int, data []byte) error {
	if !conn.threadSuffixSupported {
		if err := conn.selectThread('g', threadID, "registers write"); err != nil {
			return err
		}
	}
	conn.outbuf.Reset()
	fmt.Fprintf(&conn.outbuf, "$p%x", regnum)
	conn.appendThreadSelector(threadID)
	resp, err := conn.exec(conn.outbuf.Bytes(), "register read")
	if err != nil {
		return err
	}

	if len(resp) > len(data)*2 {
		return fmt.Errorf("wrong response length, expected %d got %d", len(data)*2, len(resp))
	}

	for i := 0; i < len(resp); i += 2 {
		n, _ := strconv.ParseUint(string(resp[i:i+2]), 16, 8)
		data[i/2] = uint8(n)
	}

	return nil
}

// writeRegister executes 'P' (write register) command.
func (conn *gdbConn) writeRegister(threadID string, regnum int, data []byte) error {
	if !conn.threadSuffixSupported {
		if err := conn.selectThread('g', threadID, "registers write"); err != nil {
			return err
		}
	}
	conn.outbuf.Reset()

View on GitHub (pinned to a23773e6c3)

Solutions

  1. Update or replace the gdbserver/stub so it returns the exact number of bytes for the requested register
  2. Verify the target architecture matches the Delve build (GOARCH) — register sizes must agree
  3. Use the 'g' (read all registers) path or a stub that supports proper register enumeration
  4. Check Delve version and upgrade; register-width handling for exotic stubs is fixed over time

Example fix

// before: buffer sized for expected register, stub returns extra bytes
data := make([]byte, 8)
// stub replied with 10 bytes -> error

// after: pad/truncate the response before calling the lower layer, or request
// a register set whose width matches the stub, e.g. use ReadRegisters (g packet)
// which allocates from the reply length instead of a fixed buffer
Defensive patterns

Strategy: validation

Validate before calling

// Before relying on a register read against a gdbserial target, confirm the stub
// honors 'p' with the expected width:
resp, err := conn.exec([]byte("p" + hexRegNum), "register read")
if err != nil { return err }
if len(resp) != len(data)*2 {
    return fmt.Errorf("stub register width mismatch: got %d hex chars, want %d", len(resp), len(data)*2)
}

Try / catch

if err := conn.readRegister(regNum, data); err != nil {
    if strings.Contains(err.Error(), "wrong response length") {
        // fall back to bulk 'g' register read instead of per-register 'p'
        return conn.readAllRegisters()
    }
    return err
}

Prevention

When it happens

Trigger: Calling conn.readRegister (via thread registers access) against a gdbserial target whose stub returns more hex bytes than the register width Delve expects — e.g. the 'p' reply contains extra bytes or the stub ignores the register number and returns a different register.

Common situations: Debugging with a non-standard or buggy gdbserver/stub (openocd, custom RTOS stubs, QEMU with unusual register sets); architecture mismatches between Delve's expected register layout and the target's actual layout; connecting to stubs that don't implement 'p' correctly.

Related errors


AI-assisted analysis of go-delve/delve@a23773e6c3 (2026-08-31). Data as JSON: /api/errors/d33d4b31712a2ddc. Report an issue: GitHub.