Chapter 5. Debugging

To debug is human. To fix it is divine...

Well, the human part is what this section is about. The divine part is up to you!

When a program does not behave in the way it is supposed to, we say the program contains a bug or bugs. This does not refer to compilation errors — those are simply errors and they are relatively easy to clear, because the compiler tells you where the problems are. On the other hand, bugs are errors that happen during the execution of the program and they can be hard (sometimes very hard!) to detect.

Any program that you think is bug-free is never completely bug-free. All we can try to do is to reduce the number of bugs contained in the program. The process of removing bugs is known as debugging, and the tool that is used for debugging is called the debugger. Anjuta provides a very user-friendly and powerful debugging environment (actually, a GUI wrapper over gdb, a powerful command line debugging tool and standard on Linux).

A debugger tracks and traces the execution of the program and provides various views of information needed to study the execution of the program.

Note

The debugger has been heavily changed since anjuta 2.0.2. Some features have been dropped but will be reenabled, a few have been added.

5.1. Start and stop

There are two ways to start the debugger:

5.1.1. Running an executable

Choose the menu item Debug->Run Target… to display a dialog box where you can select the executable that you want to debug, the command line parameters and if you want a terminal or not. The popup menu of the target combo box is already filled with all executables of the current project, so you can easily select one of them. But it is not mandatory, you can select any other executable. The debugger accepts libtool executable, I mean script wrapping the real executable generated by libtool. When you have set everything you can click on Execute to start the debugger, load your program and stop at its beginning.

Tip

In order to better user the debugger, it is recommended to debug program with debugging information (-g for gcc) and no optimization (-O0 for gcc).

5.1.2. Attaching to a Process

It is also possible to attach to a running process for debugging. Choose the menu item Debug->Attach to Process …. A list of all the process running on the system will appear.

Figure 5-1. Attach to Process dialog

Select the process to attach to and click OK.

Note

It is currently not possible to load symbol information for the attached process.

5.1.3. Restarting an executable

After running a executable at least one time, you can choose the menu item Debug->Restart Target to restart your program. It is just a shortcut that will stop the debugger if it is currently running and restart it with the last executable.

5.1.4. Stopping the Debugger

Choose the menu item Debug->Stop Debugger to stop the debugger. This option will kill the program which is being debugged. If the debugger has been attached to a running process, it will just detach from it without killing it. You will get a confirmation box if a program is currently attached or running.