Tough there are already very good information available on how to use picoprobe for debugging the Raspberry Pico, I will write down a short tutorial. I do this mostly for myself because I rarely use the Pico and always struggle days before I make this run.
Run OpenOcd from the OpenOcd install folder using
sudo ./src/openocd -s tcl -f interface/picoprobe.cfg -f target/rp2040.cfg
Use gdb in the folder where your debug *.elf file is stored.
gdb binary_to_debug.elf
(gdb) target extended-remote localhost:3333
(gdb) load
Programm your Pico using OpenOcd
sudo openocd -s tcl -f interface/picoprobe.cfg -f target/rp2040.cfg -c "program path/to/your/binary.elf verify reset exit"
The Raspberry Foundation already provides a good documentation for getting started with the Pico. In Appendix A they describe how to install and wire picoprobe. Follow the instructions.
If you have not used OpenOcd and gdb with the pico, you are used to copying *.ul2 files to the pico. Those files do not contain the information necessary to debug the programm. Hence you need to compile a *.elf file, which provides all necessary information. Please make sure to either use option -g if you comple directly with gcc or to use the Debug Build Type in your CmakeLists.txt file
set(CMAKE_BUILD_TYPE Debug)
Only if you used one of those setting you will be able to debug your project. The tools will not show error messages, but it'll be hard to find the error. Alternatively you can ad the build type as argument to your cmake call
cmake -DCMAKE_BUILD_TYPE=Debug
OpenOcd is the programm that builds the bridge between your development machine and the pico to be debugged.
To run it, you need a interface description, which is installed in your pico-folder under openocd/src/interface/picoprobe.cfg.
In addition, you need a file defining the target controller. In our case it's openocd/src/target/rp2040.cfg. Both files must be passed
with the "-f" option.
You can either pass the full filepath to the two files to OpenOcd or you tell OpenOcd where to search for the files using the "-s" option
followed by a path, like -s tcl.
cd ~/pico/openocd/src
sudo openocd -s tcl -f interface/picoprobe.cfg -f target/rp2040.cfg
If running this command, the output should look similar to this:
If your output looks more like this
GDB, the GNU Debugger, is the programm that you will use to debug your pico executable.
Navigate to the directory in which your built files are stored and run gdb.
If you type in gdb in you command prompt and execute it, you will see a gdb prompt.
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "arm-linux-gnueabihf".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)
If you debug a programm locally on your machine you start gdb with the programm to debug, like
gdb programm_to_debug.elf
But we want to debug a programm running on a Raspberry Pico. Hence we need to tell gdb to use OpenOcd as a connection interface to the Pico.
target extended-remote localhost:3333
If you are using an object oriented programming lanuage like C++, you can enable the printing of objects by enabling the object option
set print object on
Now, load the binary including the debug information
load
GDB will show you some details of your application while loading it, like
(gdb) load
Loading section .boot2, size 0x100 lma 0x10000000
Loading section .text, size 0x39d48 lma 0x10000100
Loading section .rodata, size 0x728c lma 0x10039e48
Loading section .ARM.extab, size 0x26cc lma 0x100410d4
Loading section .ARM.exidx, size 0x1250 lma 0x100437a0
Loading section .binary_info, size 0x24 lma 0x100449f0
Loading section .data, size 0x1c0c lma 0x10044a14
Start address 0x100001e8, load size 288288
Transfer rate: 27 KB/sec, 13104 bytes/write.
If you now run the list command in gdb, gdb will prompt the first code lines of your application code. This means you are ready to debug your application using gdb.
You can use OpenOcd to flash the Raspberry Pico. Using OpenOcd for flashing is very handy as it does not require to press Pico's button nor to disconnect the USB connection. All you have to do is to add several commands to your call of OpenOcd usinf the -c option.
sudo ./src/openocd -s tcl -f interface/picoprobe.cfg -f target/rp2040.cfg -c "program path/to/your/binary.elf verify reset exit"
What this does
This commands OpenOcd to programm your Pico using your binary.elf file.
After flashing OpenOcd will verify the written binary, then reset the Pico and exit.
If typing in the same commands every time you start gdb annoys you, you can put all the command you want gdb to execute into a textfile and run it using option -x. Given a file gdbcommands.txt containing
target remote localhost:3333
load
you can run gdb using this file on startup
gdb your_binary.elf -x gdbcommands.txt