Chapter 7. Debugging the Code and Solving the Error

We successfully developed a server-client program in the previous chapter. We also smoothly ran the program that we created. However, sometimes, we will face some problems when we run the application, such as receiving an unexpected result or the application crashing during runtime. In this situation, the debugging tool has the ability to help us to solve these problems. While discussing the debugging tool, in this chapter, we will cover the following topics:

  • Choosing the debugging tool for our use and keeping it simple and lightweight
  • Setting up the debugging tool and preparing the executable file to be debugged
  • Familiarizing with commands that are used in the debugging tool

Choosing a debugging tool

Many debugging tools around come with the Integrated Development Environment (IDE) of the programing language. For instance, Visual Studio has a debugging tool for C, C++, C#, and Visual Basic. Alternatively, you may have heard about CodeBlock and Bloodshed Dev-C++, which have their own debugging tools as well. However, if you remember what we discussed in Chapter 1, Simplifying Your Network Programming in C++, we decided not to use an IDE because its heavy load will not load much resource to our computer. We need a tool that is lightweight to develop our network application.

Our choice of tool is the GNU Debugger (GDB). GDB is a powerful debugging tool based on a command-line tool; this means that we don't need the complex Graphic User Interface (GUI). In other words, all we need is a keyboard, not even a mouse, so the system becomes lightweight as well.

There are four main things that GDB can do to help us solve the code problem, which are as follows:

  • Running our code line-by-line: When GDB runs our program, we can see which line is being executed at the moment
  • Stopping our code on a specific line: This is useful when we suspect that a certain line has caused an error
  • Examining the suspected line: When we successfully stop at the suspected line, we can continue to examine it, for example, by checking the value of the variable involved
  • Changing the value of the variable: If we find the unexpected variable value that has caused an error, we can replace the value at GDB runtime with our expected value to ensure that the change of the value will solve the problem

Installing a debugging tool

Fortunately, you will not need to install anything else if you followed all the steps related to the installation of MinGW-w64 in Chapter 1, Simplifying Your Network Programming in C++, because the installer package contains the GDB tool as well. What we need to do now is to run the GDB tool in our command console to check whether it runs properly.

In any active directory of our command prompt, type the following command:

gdb

We should get the following output in our console window:

C:CPP>gdb
GNU gdb (GDB) 7.8.1
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-w64-mingw32".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb)_

As we can see in the preceding output that we got on the console, we have the version 7.8.1 (this is not the latest version as we just obtained it from the MinGW-w64 installer package). In the last line, we also have (gdb) with a blinking cursor next to it; this means that GDB is ready to receive the command. However, for now, the command we need to know is quit (alternatively, we can use q as a shortcut) to exit the GDB. Just type q and press Enter, and you will come back to the command prompt.

Preparing a file for debugging

GDB needs at least one executable file to be debugged. For this purpose, we will go back to the previous chapter to borrow the source code from there. Do you remember that we created a game in Chapter 1, Simplifying Your Network Programming in C++, where we had to guess the random number that the computer was thinking of? If you remember, we have the source code, that we saved as rangen.cpp in the first chapter, and we have modified it by adding the Boost library, saving it as rangen_boost.cpp in Chapter 3, Introducing the Boost C++ Libraries. In the next section, we will use the rangen_boost.cpp source code to demonstrate the use of GDB. Also, for those who have forgotten the source code, I've rewritten it for you here:

/* rangen_boost.cpp */
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/uniform_int_distribution.hpp>
#include <iostream>

int main(void) {
  int guessNumber;
  std::cout << "Select number among 0 to 10: ";
  std::cin >> guessNumber;
  if(guessNumber < 0 || guessNumber > 10) {
    return 1;
  }
  boost::random::mt19937 rng;
  boost::random::uniform_int_distribution<> ten(0,10);
  int randomNumber = ten(rng);

  if(guessNumber == randomNumber) {
    std::cout << "Congratulation, " << guessNumber << " is your lucky number.
";
  }
  else {
    std::cout << "Sorry, I'm thinking about number " << randomNumber << "
"; 
  }
  return 0;
}

We will modify the compiling command in order for it to be used in GDB. We will use the -g option so that the executable file that is created will contain the debugging information and symbols that will be read by GDB. We will produce the rangen_boost_gdb.exe executable file from the rangen_boost.cpp file, which contains the debugging information and symbols using the following command:

g++ -Wall -ansi -I ../boost_1_58_0 rangen_boost.cpp -o rangen_boost_gdb -g

As we can see in the preceding command, we add the -g option in the compiling command in order to record the debugging information and symbols in the executable file. Now, we should have the file named rangen_boost_gdb.exe in our active directory. In the next section, we will debug it using GDB.

Tip

We are only able to debug the executable file that is compiled using the -g option. In other words, we will not be able to debug the executable file without having debugging information and symbols. Also, we cannot debug the source code file (*.cpp file) or header file (*.h file).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset