I enter the next command in gdb and jump to line 100, then to 97, then to 100, then to 97, then to 100, then to 103. I don’t understand why you can do it from 97 to 100. At 98 and 99, new variables are announced, but why skobs 97 and 100, and the same picture with 102 and 104 I do not understand. What is the problem?
1 answer
A single C / C ++ string can consist of several assembler commands. Each assembly instruction (if the -g option is used) is assigned a link to the file and line number. In the process of optimizations, the compiler can change the order of assembler instructions, including by mixing instructions from different lines (and even files, if there were inline functions). Most likely in gdb, the next command stops as soon as a new range has started (file + line). If the ranges are partially shuffled, then the impression of jumps in the source code is created. In other words, using "-g" together with "-O2" does not provide a simple way to bind current instructions to the sources. Normally, such a binding only works on "-g -O0".
PS In addition to the next command, there are also nexti / stepi and disas commands, but these are commands for step-by-step execution of assembler instructions.
-O0 -g(that wouldn’t be optimized and debug info) - KoVadim