Gdb Is Jumping Over Lines

gdb is jumping over lines

The library you are stepping into has been built with optimization and debug symbols (most likely -g -O2, which is the default for Linux builds).

Debugging optimized code is somewhat hard, as control flow optimization causes the code to "jump around", some variables become "<optimized out>", etc.

You can rebuild the library with CXXFLAGS = -g -O0, or you can learn to debug with optimization on.

The latter is a very useful skill, as many times your program will only crash in optimized mode, and you'll have to debug it in that mode anyway.

Can I use gdb to skip a line without having to type line numbers?

jump +1

jumps to the next line line i.e. skipping the current line. You may also want to combine it with tbreak +1 to set a temporary breakpoint at the jump target.

See http://sourceware.org/gdb/current/onlinedocs/gdb/Specify-Location.html for more ways of expressing locations with gdb.

Note that without a breakpoint gdb is likely to continue execution normally instead of jumping. So if jumping doesn't seem to work, make sure you set a breakpoint at the destination.

gdb jumps previous lines after finish main

There was a similar gcc bug 49951 in the past which was later closed with the status RESOLVED FIXED. However it can be reproduced as of now with the current version of gcc (I tested with gcc 8.2.1). Other people also claim that the bug was not fixed, see https://gcc.gnu.org/bugzilla/show_bug.cgi?id=49951#c20.

As a workaround you can try to use clang instead of gcc, at least the bug was not reproduced on test example from gcc bug 49951 when building with clang. You can also downgrade gcc to version 4.4 but it is too old for now.

Why GDB jumps unpredictably between lines and prints variables as value optimized out?

To debug optimized code, learn assembly/machine language.

Use the GDB TUI mode. My copy of GDB enables it when I type the minus and Enter. Then type C-x 2 (that is hold down Control and press X, release both and then press 2). That will put it into split source and disassembly display. Then use stepi and nexti to move one machine instruction at a time. Use C-x o to switch between the TUI windows.

Download a PDF about your CPU's machine language and the function calling conventions. You will quickly learn to recognize what is being done with function arguments and return values.

You can display the value of a register by using a GDB command like p $eax

Is it possible to jump/skip in GDB debugger?

There seems to be a jump command which is exactly what you are looking for:

http://idlebox.net/2010/apidocs/gdb-7.0.zip/gdb_18.html#SEC163

Updated link:
http://web.archive.org/web/20140101193811/http://idlebox.net/2010/apidocs/gdb-7.0.zip/gdb_18.html#SEC163

Gdb jumping some parts of the assembly codes

If I type run I noticed that the program stops at the line 0x00000000004005d4 and not in the first line of the function 0x00000000004005cc as I expected.

Your expectation is incorrect.

Why is this happening?

Because when you set breakpoint via break getInput, GDB sets the breakpoint after function prolog. From documentation:

-function function
The value specifies the name of a function. Operations on function locations
unmodified by other options (such as -label or -line) refer to the line that
begins the body of the function. In C, for example, this is the line with the
open brace.

If you want to set breakpoint on the first instruction, use break *getInput instead.

Documentation here and here.



Related Topics



Leave a reply



Submit