Salahuddin
Salahuddin

Reputation: 1719

GDB - how can a breakpoint disable itself the best way?

If I have a breakpoint that has the number 8 for example. If I want this breakpoint to be disabled after it is hit, I write the following in commands 8:

disable 8
end

If I delete a breakpoint whose number is less than 8, then saved the breakpoints in a file before exiting gdb, the reopen gdb and restore the breakpoints file, the number of breakpoint 8 will change and its commands will disable the wrong breakpoint.

Is there a variable that I can use so that a breakpoint's command can disable itself? I mean some variable like $bpnum

Upvotes: 3

Views: 4161

Answers (1)

Omry
Omry

Reputation: 316

If you're okay with the breakpoint being deleted, rather than disabled, you have tbreak (temporary break point), which erases the breakpoint as soon as it's hit. For catchpoints (catch ...), tcatch provides similar behavior.

If you want it to be disabled, you can use conditional breakpoints, with debugger convenience variables. Following on gdb breakpoint conditions, you could do:

set $foo = 0
break 5
condition $foo++ < 1

Or alternatively, through commands:

break <line_num>
set $n $bpnum
commands
disable $n
end

Unfortunately, neither solution scales, unless you're willing to set breakpoints in a specific order. In that case, you could simply do:

break <line_num>
commands
disable 1
end

Where 1 is the first breakpoint, and it would be 2 for the second breakpoint, etc.

Upvotes: 4

Related Questions