C and C++ SoftBench User's Guide

Using SoftBench Debugger
Debugging Optimized Code
Chapter 7 227
Debugging Optimized Code
Source-level debugging of unoptimized code is relatively easy because
you have a simple correspondence between source code statements and
the assembly code instructions into which they are translated. Also,
program variables are stored in memory and are therefore easy to access.
Optimization performs a series of transformations on the object code in
order to make the program run faster. In effect, optimization
transforms a program into a different program. The executable
program you debug is actually not the same program as the source
program. In addition, program variables may be stored in registers
instead of memory and are therefore more difficult to access.
Ordinarily, you first compile and debug your program without
optimization. All or nearly all of the bugs in your program show up in the
unoptimized version. After eliminating all the bugs that you can find,
turn on optimization (compile with -O). If the program behaves
incorrectly, scan the source code for the most common kinds of bugs that
appear for the first time in optimized code:
Uninitialized variables
Out-of-bounds array references
Variable references based on the assumption that two variables are
adjacent in memory
SoftBench CodeAdvisor may assist you with some of these categories of
problems. Others require more extensive code examination.
These kinds of problems, however, are often very difficult to find by
examining the source code. If you cannot determine the reason for the
program's misbehavior, you need to debug the optimized code. For
tutorial and task-oriented information on how to debug optimized code
using the debugger, see SoftBench Online Help.