I got a strange issue with debugging a RISC-V program on a raspberry pi pico 2W. I can upload the program to the flash, but when i try to connect openocd it seems that the RISC-V cores are not active. I need to explicitly reboot into riscv mode to be able to use openocd and gdb.

Then when i connect to the program, the current instruction is way outside the program flash memory and when i examine the flash location, it just returns all zeroes, as if the program was never written. However, when i dump the program with picotool, then i _can_ see the program at the correct location. It just seems the riscv cores cannot access the flash memory.

I'm gonna try with a non-W pico 2 but if anyone can give some insights or hints i'd greatly appreciate it!

Its a classic case of RTFM! The first 4k of flash memory needs to contain a magic block of information that describes the executable. This is handled automagically for you when using the SDK, but it isnt obvious when building without the SDK.

So i had an ELF file, and to load that ELF file to the pico i used picotool, and it executed without complaints or warnings. I assumed that, when loading an ELF file where E stands for 'executable', the program would actually be executable.

But without that magic pattern it isnt! And not a single warning message that says "hey, this probably wont work, you might want to add a magic block to your program!" not during the coversion to UF2, not during directly loading, not during uploading the UF2. That certainly caused me some gray hairs..