w09-basic-debugging
Debugging using GDB in VSCode
The overall objective of this task is to familiarize ourselves with the debugging process. We will use gdb
+ the functionalities of VScode
for debugging.
Loading the appropriate modules (this needs to be done only once)
Whenever we connect VScode
to the cluster Euler
it automatically loads the default modules present in the .bashrc
file in your home directory /cluster/home/your-username/.bashrc
. In order for VScode
to properly debug our code, we have to make sure that when it connects to Euler
, it loads the appropriate modules that are required for debugging. To do so, follow the steps:
- Open the
VScode
in your local machine and connect to the remote hosteuler
. - Open the terminal within
VScode
, navigate to your home folder by writingcd
on terminal and pressingEnter
. - Type the command
set_software_stack.sh new
in the terminal and pressEnter
. - In your home directory, locate the file
.bashrc
and open the.bashrc
file. - At the end of the
.bashrc
file, add the linemodule load gcc/8.2.0 python/3.8.5 cmake
. - Save and close the file.
Now, disconnect the VScode
connection to the Euler
(Close Remote Connection) and open the VScode
again. Doing so ensures that the modules load correctly every time you connect the VScode
to Euler
. You must do the above steps only once and never again.
We also need to add a few extensions to the VScode
- install
C++
,C++ Extension Pack
,CMake Tools
andCMake
extension inVScode
(Make sure that you install the extension in Euler as well with the optionInstall in SSH:euler.ethz.ch
)
Debugging the project
- open workspace on root (open your project folder) --
Ctrl+Shift+P
-- File: Open folder -- tehpc2024 - CMake: configure (You can access all these options through Command Palette, which can be opened from
View->Command Palette
orAlt+X
orCtrl+Shift+ P
). It might ask you to choose aGCC
version, please choose version8.2.0
. - CMake: Select Variant -> Debug (because we want to compile the project in debugging mode)
- CMake: Edit CMake Cache (UI) -> enable tests, examples, verbose ... and Save (by clicking on the check box)
- CMake: build
- CMake: Set debug target (Select the executable you want to debug. For this task, we choose
matrix_test
) - Open the file
matrix_test.cpp
in the pathtephc2024/tests/matrix_test.cpp
and set the breakpoints by clicking in front of the line (should appear as a red dot). - CMake : debug
Introduce breakpoints
- Introduce breakpoints to stop the debugging process at a specific point.
To inspect the variable values
- By default, variable values will be displaced in the RUN AND DEBUG section.
- You can also use
Debug Console
to print the values
To check values of a std::vector or custom data structure
- *(std::vector.M_impl._M_start)
- *(std::vector.M_impl._M_start+n) to get at the value at the nth location
- For example, if we ahve to check the values of our custom
Matrix mat
data structure. We can execute the following command. - *(mat.storage._M_impl._M_start)
- This way can be tedious to do therefore, we have
Verbose
to print the values