72

I know I can use jump to set the program counter to a specific line and so I can skip one or more lines (or execute some lines again). Can I easily just skip the next line without having to enter line numbers?

This would be very convenient to "comment out" something at run time.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Ortwin Gentz
  • 52,648
  • 24
  • 135
  • 213
  • 2
    Related: more general stuff about using `jump` (e.g. that it's only safe inside the current function, and only if you compiled with `-O0`): https://stackoverflow.com/questions/4116632/is-it-possible-to-jump-skip-in-gdb-debugger/46043760#46043760 – Peter Cordes Sep 04 '17 at 20:38

3 Answers3

88
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.

Craig Ringer
  • 307,061
  • 76
  • 688
  • 778
laalto
  • 150,114
  • 66
  • 286
  • 303
28

I have the following in my .gdbinit config file:

define skip
    tbreak +1
    jump +1
end

So just type skip in gdb to skip a line.

gospes
  • 3,819
  • 1
  • 28
  • 31
  • How to parametrize it to "skip(N)" ? – p2rkw Sep 13 '16 at 17:32
  • 7
    @p2rkw. You can replace '1' with $arg0, as explained here: https://sourceware.org/gdb/onlinedocs/gdb/Define.html. Be aware that I would not use the name 'skip' anymore, because it became a GDB function (allowing to skip source files while stepping through your code). – gospes Mar 12 '17 at 09:03
  • @Trass3r True. I mention this in the comment above. – gospes Sep 15 '20 at 07:50
5

To Skip Any Numbers of Lines during Execution:

[Current Position -- in GDB] Line N
.......... // Lines To Skip
..........
..........
[Line To Execute - After Jumping] Line M

Put a Breakpoint on Line M:

gdb$b M

Jump To Line M:

gdb$jump M
Sandeep Singh
  • 4,941
  • 8
  • 36
  • 56