A debugger is a computer program that is used to find errors and debug other 'target’ programs. It basically allows you to see what is going on ‘inside’ the target program when it crashes or doesn’t run the way you expect it to.
As the debugger is a valuable tool in a developer's toolkit, its use should always be considered when other debugging methods do not suffice. For instance, while error logs and print statements are quicker, they might not be efficient in spotting 'less-obvious' errors - call stack, file handling, logic errors to name a few. It’s also a real pain to remove all the print and log statements that has been added during a debugging session.
Since our design team's code base is large and it is likely that you are not aware of everything going on under the hood, being able to go slowly and perform error-checking will enable you to have an easier time completing your work. Therefore, it is in your best interests to learn the basics and be somewhat comfortable with using a debugger.
GDB stands for GNU debugger. It is a command-line debugger that runs on Unix-like systems and works on various programming languages, including C. Here is its official website: https://www.sourceware.org/gdb/
GDB operates on binary/executables, so all programs to be debugged in gdb
must be compiled with the GNU compiler, commonly known as gcc
.
Since gcc
is required, make sure that it is installed on your system.
If you are using Linux, then the odds are it has already been installed on your machine. You can use gdb -v
to check the version or which gdb
to check where it is available on your environment. If not, then with whatever package manager you are using (the following example is done in Ubuntu with apt
), type the following commands in your terminal:
sudo apt update
sudo apt install gdb
If the above does not work: https://www.gdbtutorial.com/tutorial/how-install-gdb
Since GDB is not native on Windows, you would need to have a Linux toolchain like MinGW or Cygwin installed to be able to run GDB on the terminal.
You can then follow this link to install and run GDB on an executable on Windows:
Check if it is already installed by doing gdb --version
. Type the following command in your terminal if it is not:
brew install gdb