GDB conditional break on function parameter

前端 未结 3 1346
一生所求
一生所求 2021-02-19 07:44

I\'m wanting to set a breakpoint on a function parameter if it is greater than a certain value. Dummy code below:

int main(void)
{
    uint64_t num = 123456;
            


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-02-19 07:57

    If break foo if arg1 == 14 doesn't work for some reason (I've encountered some functions/binaries where it does, and where it doesn't), you can try to substitute it with commands.

    commands allows you to set some gdb commands that will be run each time breakpoint is hit. To achieve desired effect - a conditional breakpoint - you can do the following:

    (gdb) b foo
    Breakpoint 1 at 0x400633: file test.c, line 6.
    (gdb) commands 1
    Type commands for breakpoint(s) 1, one per line.
    End with a line saying just "end".
    >silent
    >if arg1 != 14
     >cont
     >end
    >end
    

    The execution will stop at breakpoint only if arg1 == 14.

    The only drawback is that silent suppresses typical "breakpoint hit" message. You can remove silent, but then gdb will print the message even if breakpoint is skipped by commands script, which is undesireable if breakpoint is hit very often.

    You can add some custom notification inside command script, though.

提交回复
热议问题