TUTORIAL - Preparing to Debug In Linux With GDB



Hello Programmers! 
Live long and prosper! 🖖

As you may know, computers are more advanced today than in the past years, and we can prove it just by looking at what today's computers can do. For example, computers now can process 3-dimensional graphics for gaming, store extensive data size for databases, and debug larger applications for programming. Visual Studio (VS) is an IDE that became very popular for debugging applications because it helps programmers to deal with thousands of lines of code very easily. In Linux, there is also a very popular and powerful debugger that is being used in the past 34 years ago, and it is called The GNU Project Debugger, or simply GDB.

If you are a professional or/and hobbyist that works within Linux distributions and you are looking for a tutorial that shows in step-by-step how to debug codes using GDB, this article is for you! 


Without further ado, let's dive into our step-by-step for debugging with GDB.

STEP ZERO
WHAT GDB SUPPORTS

The GDB works with the following languages:
- Ada
- Assembly
- C
- C++
- D
- Fortran
- Go
- Objective-C
- OpenCL
- Modula-2
- Pascal
- Rust

In this tutorial, I am using a C++ source code.


STEP 1
PREPARE YOUR CODE

It seems logical, but you need to remember that GDB needs the executable file of your source code. Since my code is in C++, the compiler that I am using is the "g++". The next image shows a simple code that I have typed in Vi, which I encourage you to copy and use along with this tutorial.

 

STEP 2
PLAN YOUR DEBUGGING

In most cases, when developers need to deal with hundreds of lines of code, it is preferable to create an execution plan to remember what should be focused more while debugging. First, start drawing some flowcharts. Second, decide where breakpoints should be over the code. Last, follow along with the flowchart and the GDB, understand the variables and the semantics developed. For example, Take a look at the flowchart below of our application “myHello.cpp” 

 

By looking at the above image, can you tell if some part of the application could have semantic errors? If you guessed that we should place a breakpoint in the part where the program checks if “i < 3” you are absolutely right! The reason for that is common to see semantic errors related to an infinite loop. Indeed, programmers should plan before coding


STEP 3
PREPARE YOUR EXECUTABLE FILE

Once you have your application ready to go, it is necessary to generate its executable to let the GDB use it for debugging. The compiler in Unix-like systems for C++ language is the "g++" (for the C language is the "GCC"), and below you can see the command to be typed, in our case with g++. The name of my application is "myHello.cpp"

g++ -g myHello.cpp



The command above compiles your C++ code and generates the executable file, and the option “-g” enables built-in debugging support, which is what GDB needs to work properly. The executable generated has a default name “a.out,” but if you want to give it a name while compiling the code, you need to add the option “-o” followed by some name, like the one below:


g++ -g myHello.cpp -o myHello.out



In the image below you can check out the executable file generated, wherein green is the executable file:

 


STEP 4
ACCESSING THE GDB

Once you have your executable file ready, it is time to open it with the debugger, and to accomplish that, we just simply need to use the command “gdb” followed by the name of your executable file that you generated:


gdb myHello.out


The following image is what you should expect to see on your screen after entering the above command:
 

We can see some messages about the gdb license and warranty, and in the last line it is written “(gdb)”, which means that the debugger is waiting for us to enter some commands.


USING THE GDB COMMANDS

Now that we have access to the GDB, and our application is ready to interact with the debugger, let's understand how to use some GDB commands.

In the next tutorial, we will talk about the commands used to let you debug your c/c++ code with GDB


To Be Continued ...



Thanks for the support❤️


REFERENCES:

1.    GNU Project: https://www.gnu.org/
3.    White-box Testing: https://en.wikipedia.org/wiki/White-box_testing  








Comments