bsky
bsky

Reputation: 20222

Gdb conditional regex break

Does gdb allow conditional regex breaks?

I have a source file timer.c, an int64_t ticks, and a function timer_ticks() which returns it. Neither

rbreak timer.c:. if ticks >= 24

nor

rbreak timer.c:. if ticks_ticks() >= 24

place any breakpoints.

If I remove however either the regex part or the conditional part, the breakpoints are set.

Upvotes: 2

Views: 638

Answers (2)

Mark Plotnick
Mark Plotnick

Reputation: 10271

Here's a way to get it done. It takes a couple of steps, and requires some visual inspection of gdb's output.

First, run the rbreak command and note the breakpoint numbers it sets.

(gdb) rbreak f.c:.
Breakpoint 1 at 0x80486a7: file f.c, line 41.
int f();
Breakpoint 2 at 0x80486ac: file f.c, line 42.
int g();
Breakpoint 3 at 0x80486b1: file f.c, line 43.
int h();
Breakpoint 4 at 0x8048569: file f.c, line 8.
int main(int, char **);

Now, loop through that range of breakpoints and use the cond command to add the condition to each:

(gdb) set $i = 1
(gdb) while ($i <= 4)
 >cond $i ticks >= 24
 >set $i = $i + 1
 >end
(gdb) info breakpoints
Num     Type           Disp Enb Address    What
1       breakpoint     keep y   0x080486a7 in f at f.c:41
        stop only if ticks >= 24
2       breakpoint     keep y   0x080486ac in g at f.c:42
        stop only if ticks >= 24
3       breakpoint     keep y   0x080486b1 in h at f.c:43
        stop only if ticks >= 24
4       breakpoint     keep y   0x08048569 in main at f.c:8
        stop only if ticks >= 24

Upvotes: 2

user934691
user934691

Reputation:

Unfortunately, no, it doesn't.

However, there is workaround.

You want to break every function from the file conditionally, don't you?

If yes, you can use this answer as start point and then create conditional breaks.

So, first step: get a list of functions in the file:

nm a.out | grep ' T ' | addr2line  -fe a.out |
   grep -B1 'foo\.cpp' | grep -v 'foo\.cpp' > funclist

Next: create a gdb script that creates breaks:

sed 's/^/break /' funclist | sed 's/$/ if ticks>=24/' > stop-in-foo.gdb

And finally, import script in gdb:

(gdb): source stop-in-foo.gdb

Upvotes: 1

Related Questions