2

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.

bsky
  • 19,326
  • 49
  • 155
  • 270

2 Answers2

2

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
Mark Plotnick
  • 9,598
  • 1
  • 24
  • 40
  • Note for future me, as pointed out [here](https://stackoverflow.com/questions/33400844/how-do-i-set-the-stack-pointer-from-gdb-using-jlink-and-a-cortex-m4/35663966#comment54623762_33400844), ada mode requires assignment to be `:=`. – Vser Sep 27 '21 at 12:57
1

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
Community
  • 1
  • 1