How to print a variable on a specific condition in gdb?

I want to print a variable in a function (which is called several times) to print with every function call.

Is it possible to do this automatically via gdb ?? Something like conditional printing ...

sort of..

void func()
{ 
    if( t == 0 ) 
       x = z+1;
    else
       x = p+2; 
} 

I want the variable to be printed when t == 0 and ignored otherwise ..

+5
source share
3 answers

This can be done using a combination of commands breakpoint, conditionand commands.

  • Set a breakpoint with breakpoint func
  • Make it conditional condition t == 0
  • Make a breakpoint print local variables using:

.

commands  
info locals  
end  

or specific variables:

commands  
print t
print z
print x  
end  
+6
source

x=z+1, 'command [breakpoint number], .

, .

if-else. U "break if condition" .

+3

you can actually break in a certain place provided.

eg. break sourcefile.c: 123 if x + y -foo (z) == 4. this will break on line 123 sourcefile.c if this expression evaluates to true. Then you can print any value you want (or continue until the next condition is satisfied)

0
source

All Articles