You can't next or cont from breakpoint command lists, but you can write a "stop event handler" in Python then resume inferior execution from there. See the self-contained example below:
buggy.c
#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
i = atoi(getenv("i"));
if (i > 0) {
i++;
} else {
i--;
}
printf("i: %d\n", i);
return 0;
}
next-after-break-on-conditional.gdb
set confirm 0
set python print-stack full
python import gdb
file buggy
break 8
python
conditional_bpnum = int(gdb.parse_and_eval('$bpnum'))
def stop_handler(event):
if not isinstance(event, gdb.BreakpointEvent):
return
if conditional_bpnum not in set(x.number for x in event.breakpoints):
return
gdb.write('i: %d\n' % (gdb.parse_and_eval('i'),))
gdb.execute('next')
gdb.write('GDB: incrementing "i" from debugger\n')
gdb.execute('set variable i = i+1')
gdb.execute('continue')
gdb.events.stop.connect(stop_handler)
end
run
quit
Sample Session
$ gcc -Os -g3 buggy.c -o buggy
$ i=0 gdb -q -x next-after-break-on-conditional.gdb
Breakpoint 1 at 0x4004e3: file buggy.c, line 8.
Breakpoint 1, main () at buggy.c:9
9 i++;
i: 0
11 i--;
GDB: incrementing "i" from debugger
i: 1
[Inferior 1 (process 7405) exited normally]
Implementation Notes
stop_handler() will be called whenever GDB stops so you MUST test that GDB stopped for the specific breakpoint before issuing commands.
If I compile with -O3, I'd get the dreaded "value has been optimized out" error for i and set variable i = i+1 would fail. So watch out for this as usual. (gcc-4.9.2, gdb-7.8.2 on Fedora 21, x86-64)
References
if(i > 0) { i++; } else { i--; }In this case, if I want to break just after the i++ instruction, and I set the breakpoint at this line, the "real" breakpoint will be put at the "i-- line" (which is in the else block). Then, if the condition is satisfied, the "i-- line" will never be reached and so my breakpoint will never be hit.