Reputation: 1108
What I am looking for is something like this -
Set Breakpoint 1
Set Breakpoint 2
Disable Breakpoint 2
Set dbg_counter to 0
Increment dbg_counter everytime Breakpoint 1 is reached
If dbg_counter > 100:
Enable (once) Breakpoint 2
Set dbg_counter to 0
Please note that 'dbg_counter' is a variable that only the debugger is aware of (i.e. not part of the program being debugged).
Upvotes: 3
Views: 134
Reputation: 1108
Based on the information provided by @Thomas Padron-McCarthy about convenience variables I was able to come up with the following GDB command file to solve my problem -
break file.c:20
break file.c:35
disable 2
set $dbg_count = 0
commands 1
set $dbg_count += 1
if $dbg_count > 100
enable once 2
set $dbg_count = 0
end
end
run
Upvotes: 2
Reputation: 27632
From https://sourceware.org/gdb/current/onlinedocs/gdb/Convenience-Vars.html:
GDB provides convenience variables that you can use within GDB to hold on to a value and refer to it later. These variables exist entirely within GDB; they are not part of your program, and setting a convenience variable has no direct effect on further execution of your program.
A convenience variable can be used with a breakpoint condition to ignore a breakpoint a certain number of times. But there is an easier way to do that. From https://sourceware.org/gdb/current/onlinedocs/gdb/Conditions.html:
A special case of a breakpoint condition is to stop only when the breakpoint has been reached a certain number of times. This is so useful that there is a special way to do it, using the ignore count of the breakpoint. Every breakpoint has an ignore count, which is an integer. Most of the time, the ignore count is zero, and therefore has no effect. But if your program reaches a breakpoint whose ignore count is positive, then instead of stopping, it just decrements the ignore count by one and continues. As a result, if the ignore count value is n, the breakpoint does not stop the next n times your program reaches it.
Upvotes: 6