The observed behaviour is usually a result of compiler optimisations being active. Optimisations might result in the binary code not following completely the structure of the source - it still gives the same result, but the compiler rearranges the operations in such a way as to be more efficiently executed. Also, some functions might get inlined. As a result the correspondence between range of instructions and source lines in the debug information becomes useless.
When doing source level debugging, always make sure that optimisation is turned off using the -O0
flag for most compilers or by not giving any optimisation flags for Sun/Oracle compilers. Note that some compiler options might result in elevated optimisation levels. For example, enabling OpenMP support in Sun/Oracle Studio with -xopenmp
automatically raises the optimisation level to -xO3
. Instead, -xopenmp=noopt
should be used and no optimisation level specified explicitly. Also, some compilers optimise by default, for example Intel's compiler.