# OpenOCD Failed to read memory

**URL:** <https://forums.sifive.com/t/openocd-failed-to-read-memory/5791>\
**Category:** HiFive Unmatched\
**Created:** [August 2, 2022, 7:27am UTC](https://forums.sifive.com/t/openocd-failed-to-read-memory/5791 "2022-08-02T07:27:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![chivy](https://avatars.discourse-cdn.com/v4/letter/c/7ba0ec/32.png) [@chivy](https://forums.sifive.com/u/chivy)\
**Post date:** [August 2, 2022, 7:27am UTC](https://forums.sifive.com/t/openocd-failed-to-read-memory/5791/1 "2022-08-02T07:27:38Z")

</div>

I use the OpenOCD to debug my kernel. The OpenOCD connect to board succeed, I target remote my board, Then use ‘file kernel.elf’, OpenOCD get the error messages:

```auto
Info : accepting 'gdb' connection on tcp/3333
Info : New GDB Connection: 1, Target riscv.cpu, state: halted
Warn : Prefer GDB command "target extended-remote 3333" instead of "target remote 3333"
Warn : Failed to read memory via program buffer.
Warn : Failed to read memory via abstract access.
Error: Target riscv.cpu: Failed to read memory (addr=0x49883ae0ce8bfd4e)
Error: progbuf=failed, sysbus=skipped (unsupported size), abstract=failed
Error: Target riscv.cpu: Failed to read memory (addr=0x49883ae0ce8bfd4e)
Error: progbuf=failed, sysbus=skipped (unsupported size), abstract=failed

```

I find the issue:

> <https://github.com/riscv/riscv-openocd/issues/559>
>
> During debugging I found that the frame pointer (x8/s0/fp), which is also used b…y the debugger for certain operations, was getting corrupted. I added some logging to the above file and see this:
> 
> \`\`\`
> Info : read\_memory:3560 Begin x8/s0/fp = 0x8013000
> Warn : Failed to read memory via program buffer.
> Warn : Failed to read memory via abstract access.
> Error: Target mpfs.hart0\_e51: Failed to read memory (addr=0x500000000000000)
> Error: progbuf=failed, sysbus=skipped (unsupported size), abstract=failed
> Info : read\_memory:3621 End (failure) x8/s0/fp = 0x500000000000000
> \`\`\`
> 
> So it seems that a failed memory read leaves the frame pointer corrupted with obvious concomitant negative impact on further execution of the program being debugged.
> 
> I guess that at least one code path fails to restore the x8/s0/fp register used by openocd to do some work. There are several places where it saves and restores the original value but in this case I guess that the restore never happens.
> 
> One obvious solution would be to save it at the start of read\_memory() and restore it at the end (success and failure cases?) but maybe this is not the appropriate place?
> 
> @timsifive - any suggestions before I submit a patch/PR? Thanks.
> 
> Thanks a lot
> Cheers
> Tommy
> 
> Edit: the reason for the attempted read of memory at 0x0500\_0000\_0000\_0000 is that the stack contains a pointer variable which has not been initialized yet and happens to assume that value on entry and (in Eclipse) when the Variables view is active or the user mouses over the variable in the Editor view the debugger attempts to read that memory.
> 
> Edit 2: I don't know if the same or a similar issue might also affect riscv-011.c?

And this issue had been solved by OpenOCD:

> <https://github.com/riscv/riscv-openocd/pull/560>
>
> Be sure to restore mstatus/s0 even if there is a failure during the
> operation.
> …
> 
> Fixes #559.
> 
> Change-Id: Ib86ca2c7455bad4a668f34703566060a782116db
> Signed-off-by: Tim Newsome \<tim@sifive.com\>

It seems that SiFive OpenOCD didn’t merge this branch, this issue repaired on Dec 15, 2020. SiFive OpenOCD only provide the bin file, the last time is on Dec 1, 2020.

---

<div class="post-metadata">

**Author:** ![RalphF](https://avatars.discourse-cdn.com/v4/letter/r/d26b3c/32.png) [@RalphF](https://forums.sifive.com/u/RalphF)\
**Post date:** [August 4, 2022, 6:13pm UTC](https://forums.sifive.com/t/openocd-failed-to-read-memory/5791/2 "2022-08-04T18:13:24Z")

</div>

Hi Chivy,

The problem you’re experiencing might be caused by the issue you identified, but we can’t be certain. We don’t have a newer version of OpenOCD available publicly, so we suggest rebuilding OpenOCD yourself from the following repo:

- [GitHub - riscv/riscv-openocd: Fork of OpenOCD that has RISC-V support](https://github.com/riscv/riscv-openocd)

If you are a SiFive IP customer, please email [support@sifive.com](mailto:support@sifive.com) from your employer’s email domain and we can get you a newer version of the tools.

Regards,  
Ralph
