A. K.
A. K.

Reputation: 38264

Ignore but count breakpoint hits in lldb/gdb

I have two breakpoints A and B. And I'd like to count how many instances of A occur before B is hit. The thing is A occurs quite a bit (>1000) so i can't manually continue and iterate. A can also occur after B so I can't run a program to completion to find out the hit counts. Is there an automatic way to do this?

Upvotes: 4

Views: 1267

Answers (2)

Employed Russian
Employed Russian

Reputation: 213937

Is there an automatic way to do this?

In GDB, I usually do this:

(gdb) ign 1 10000
(gdb) cont

When the second (B) breakpoint is hit, info break will say something like:

(gdb) info b
Num     Type           Disp Enb Address            What
1       breakpoint     keep y   0x0000555555555129 in a at t.c:1
        breakpoint already hit 1234 times                         <<<===
        ignore next 8766 hits
2       breakpoint     keep y   0x0000555555555130 in b at t.c:2
        breakpoint already hit 1 time

If 10000 turns out to not be enough, you could always increase it to 10000000 instead.

Upvotes: 2

Jim Ingham
Jim Ingham

Reputation: 27203

You can do this pretty easily with an auto-continue breakpoint at A and commands on breakpoint B. In the simplest approach, the breakpoint on A would look like:

break set <HoweverYouSpecifyA> --auto-continue 1 -N BreakpointA

Then the breakpoint on B would be:

break set <HoweverYouSpecifyB> -C "break list BreakpointA" -C "break disable BreakpointA" --one-shot

The break list BreakpointA output will show the hit count of A when you hit B, which is what you wanted to know. By disabling A when you hit B, the hit count for A will stay what it was when you hit B, so you should be able to check it at any point after that (till you rerun).

I like to use named breakpoints when I'm doing a little breakpoint two-step like this, otherwise you have to use the ID of the first breakpoint, and that can change from run to run depending on the order in which you set breakpoints.

I also made breakpoint B a one-shot since you're just using it to freeze the hit-count of A so it only needs to get hit the once. If it's more convenient you could also make B auto continue and then just read out the hit count of A when the program exits.

If you wanted to get fancier, you could use a Python callback for B instead, and get the hit count from A and report that however you want. It's more work but it is easier to control output formatting from Python...

Upvotes: 5

Related Questions