# PDI-2024: Symbolic debugging of a program.
Just because a program has no compilation errors does not mean that it works correctly. In case of malfunction, the program must be debugged to find the problem. Symbolic debugging allows to execute the program in a controlled way, step by step, stopping its execution and visualizing the value of the variables as we go along the program flow.
It is recommended to review the concepts of *breakpoint*, *step-over*, *step-into* and *continue*, in addition to the debugging procedures described in [https://hackmd.io/@parraman/prog-lab-gfie-practica-0#Depurando-programas](https://hackmd.io/@parraman/prog-lab-gfie-practica-0#Depurando-programas).
### Debug Configurations
In order to be able to debug symbolically, it is necessary to define a debug configuration in eclipse. This is done through the **Run -> Debug Configurations** option.

In the previous image you can see the ``DEBUG_P0`` configuration created in the ``GDB Hardware Debugging`` section and that refers to the P0 project. Using the options above the configuration explorer on the left it is possible to manage new configurations or delete the ones that are no longer used.
The ```main``` tab simply contains the name of the ```P0``` project and the ```Debug/P0``` binary to be debugged. If you want to debug another program, the corresponding configuration should be created by changing the name of the project.

The ```Debugger``` tab contains the debugger to be used, in this case ```riscv-gaisler-elf-gdb```. This debugger will connect to the SPIKE simulator via a ```socket``` connection, on port 3333 and both are on the same ```localhost``` computer.
## debugP0 script
Ensure that the ```./debugP0``` script contains the following commands:
```
gnome-terminal -- picsimlab
sleep 1
gnome-terminal -- spike -H --isa=RV32IM --rbb-port=9824 -m0x40000000:0x100000,0xFFFF0000:0x10000 ./Debug/P0
gnome-terminal -- openocd -f spike.cfg
```
## Debugging the example program
Before debugging, it is necessary to compile and generate the corresponding binary.
Once the binary has been generated, type ```./debugP0``` in the terminal window opened in the previous section.
Like ```./runP0```, ```./debugP0``` is a script whose contents can be viewed and edited. In subsequent practices we will modify this script to adapt it to the new practice projects.

When executing this script four new command windows will be opened. Two corresponds to the SPIKE simulator in debug mode and that waits for the program to be sent from eclipse, in this window the messages that are printed from the code will be shown.
```
Listening for remote bitbang connection on port 9824.
```
The other window corresponds to a program called ```openocd``` which acts as a bridge between SPIKE and eclipse. We will not have to interact with it.
Next we proceed to launch the debug configuration ```DEBUG_P0```. Make sure that the selected configuration is  and click the icon .
The following image shows the debugging perspective. By default a breakpoint is active at the beginning of the ```main``` function.

At this point the debugger waits for user commands to advance the execution of the program. The main commands for debugging are:
1. Enable/disable breakpoints: ```Toggle Breakpoint```
2. Continue execution: ```Continue```
3. Execute a function completely: ```Step Over```
4. Enter to debug a function: ```Step into```
All these commands are available through the **Run** menu but are also accessible through the  icons. Review the debugging concepts in [https://hackmd.io/@parraman/prog-lab-gfie-practica-0#Depurando-programas](https://hackmd.io/@parraman/prog-lab-gfie-practica-0#Depurando-programas).
### Run the program completely and return to the compilation perspective.
If you press ``Continue`` at this point, the program will run completely, the debugger windows will close and you will not see the message printed on the screen.
To return to the compilation perspective of the program click  in the upper right corner. This option is also available in the menu **Window --> Perspective --> Open Perspective --> C/C++**.
### Duplicate Debug configuration and modify it to debug another project
By using mouse right button over ```DEBUG_P0``` is posible to make a copy of the debug configuration. After that, rename it and change all project names to the new one.
