Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDB conditional break on function parameter

Tags:

c

gdb

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;
    uint64_t x   = 847534;

    uint64_t other = (num*x) - (x/num);

    .... other stuff here (multithreaded stuff)

    calc(other);
}

void calc(uint64_t size)
{
    ...do some stuff with size
}

I've tried to set a breakpoint by:

(gdb) b calc if size == 852479

but it does not know what size is since it is a parameter I'm guessing. How would I break if the parameter equals a certain number. It is NOT an option to break on all the calls to this function because it gets called a billion times in the multithreaded environment.

like image 389
Nick.D Avatar asked Nov 12 '14 16:11

Nick.D


2 Answers

from the gdb prompt:

break "file.c":100 if (size=852479)

or

break "file.c":100 if (size>852479)

here i am assuming you want the conditional breakpoint on line 100 and your src file is file.c

i.e if you want to break on the line that calls calc, then that would be line 100 - modify as appropriate (you would also have to substitute size with other in this instance)

if you used a line no. that was one of the 1st statements in the calc function then you would stick with size

like image 53
bph Avatar answered Sep 20 '22 18:09

bph


Assuming x86-64 calling conventions on GNU/Linux platform you could examine %rdi (64-bit) register directly to check function's first parameter:

b calc if $rdi == 852479

This allows you to break on function calc even if you don't have debugging symbols loaded (thus no code listing, i.e. by list calc).

Note that this method would fail if function is inlined by optimizing compiler.

like image 37
Grzegorz Szpetkowski Avatar answered Sep 21 '22 18:09

Grzegorz Szpetkowski