Saturday, July 7, 2012

gdb tutorial

Sometimes when you build a project in C++, it might become necessary that the code needs to be changed/added. In such cases, debugging the entire project becomes really very important because you need to ensure that the program flow is correct and no regression takes place.

In this post, we will debug the case study which we implemented in C++ demonstrating the employee register with the help of vectors. To know about how this case study is implemented, read the following posts:
1. Vector Operations - API
2. Vector Operations - Implementation
3. Vector Operations - Implementation part 2

We will use GNU C debugger in order to debug the case study. To start debugging of the case study, first we need to type the following command:

g++ -g "files.cpp"

which in our case transforms to be:

g++ -g EmployeeRegister.cpp Employee.cpp Display.cpp main.cpp

To start debugging, invoke gdb with the following command:

gdb a.out

To debug the entire case study, we will set a breakpoint in main.cpp from the beginning of main function. If you need to debug only a part of a huge project, you can specify that particular file with line number. The idea of this breakpoint is to run the project till that particular breakpoint and from there on, we can inspect or evaluate what each programming statement is actually doing.

break file.cpp:line_no

which in our case is:

break main.cpp:3

Now we want to start running the code line by line with the following command.

run

To go to the next line for debugging, execute the n command in gdb prompt. When you encounter any function and you want to get inside the function definition, use the s command. This is similar to the Step Into functionality in debugging.

In between when you want to check the value of any variable, use the following command:

p variable_name

To exit the GNU C debugger, use the q or quit command.

Now let us use all the above mentioned gdb commands in the case study.



Related Posts:

Vector Operations - API
Vector Operations - Implementation
Vector Operations - Implementation part 2


No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...