-->
Previous Table of Contents Next


Debugging C++ Applications

A very important part of developing C++ programs is being able to debug them efficiently. The GNU debug application that was introduced in Chapter 26 can also be used to debug C++ applications. This section describes some of the differences between debugging C applications and debugging C++ applications.

The basic gdb commands that were introduced in Chapter 26 are listed again for your convenience in Table 27.2.

Table 27.2. Basic gdb commands.

Command Description

file Loads the executable file that is to be debugged.
kill Terminates the program you are currently debugging.
list Lists sections of the source code used to generate the executable file.
next Advances one line of source code in the current function, without stepping into other functions.
step Advances one line of source code in the current function, and does step into other functions.
run Executes the program that is currently being debugged.
quit Terminates gdb.
watch Enables you to examine the value of a program variable whenever the value changes.
break Sets a breakpoint in the code; this causes the execution of the program to be suspended whenever this point is reached.
make This command enables you to remake the executable program without quitting gdb or using another window.
shell Enables you to execute UNIX shell commands without leaving gdb.

From the programmer’s perspective, you have more details to be aware of when debugging C++ code than when you are debugging C code. This is because of the C++ features such as virtual functions and exception handling. gdb has added features to support debugging both of these C++ specific features.

Debugging Virtual Functions

As described in the “Polymorphism” section of this chapter, virtual functions are C++’s way of implementing polymorphism. This means that there may be more than one function in a program with the same name. The only way to tell these functions apart is by their signatures. The signature of a function is composed of the types of all the arguments to the function. For example, a function with the following prototype has a signature of int,real:


void func(int, real);

You can see how this could cause the gdb a few problems. For example, if you define a class that has a virtual function called calculate, and two objects with different definitions for this function are created, how do you set a breakpoint to trigger on this function? You set breakpoints in C by specifying the function name as an argument to the gdb break command, as follows:


(gdb) break calculate

This does not work in the case of a virtual function because the debugger is not able to tell which calculate you want the breakpoint to be set on. gdb was extended in a few ways so that it could handle virtual functions. The first way to solve the problem is to enter the function name by specifying its prototype, as well. This is done in the following way:


break ’calculate (float)’

This gives gdb enough information to determine which function the breakpoint was meant for. A second solution that gdb supports is using a breakpoint menu. Breakpoint menus allow you to specify the function name of a function. If there is more than one function definition for that function, it gives you a menu of choices. The first choice in the menu is to abort the break command. The second choice is to set a breakpoint on all the functions that the break command matches. The remaining choices correspond to each function that matches the break command. The following code shows an example of a breakpoint menu:


 (gdb) break shape::calculate

[0] cancel

[1] all

[2] file: shapes.C: line number: 153

[3] file: shapes.C: line number: 207

[4] file: shapes.C: line number: 247

> 2 3

Breakpoint 1 at 0xb234: file shapes.C, line 153

Breakpoint 2 at 0xa435: file shapes.C, line 207

Multiple breakpoints were set

Use the “delete” command to delete unwanted breakpoints

(gdb)

Debugging Exception Handlers

Exceptions are errors that occur within your program. Exception handlers are pieces of code that are written to handle errors and potential errors. For example, if you were writing a C program and calling the malloc function to get a block of memory, you would typically check malloc’s return code to make sure the memory allocation was successful. If C supported exception handling, you could specify a function that would receive or catch exceptions, and the malloc function would send or throw an exception to your function if one occurred.

The gdb added two new commands to support C++ exception handling: the catch command and the catch info command. The catch command is used to set a breakpoint in active exception handlers. The syntax of this command is as follows:


catch exceptions



exceptions is a list of the exceptions to catch.



The catch info command is used to display all the active exception

 handlers.


Previous Table of Contents Next