GDB Conditional Debugging


Adding a conditional GDB

 

If we want to debug the function at a specific condition (say when the function hits Nth iteration or ptr_var holds the address of 0x18ed70e8), we need to go with conditional GDB.

 

Condition is a boolean expression and can be made with breakpoint command.

 

(gdb) br function_add_1 if ptr_var == 0x18ed70e8                                      

 

Syntax :  breakpoint <function>  if <condition>


 

If there is an existing breakpoint condition, and later we found that it is getting hit for all the time but not hitting for the specific value,  we can add the condition by using the condition command.

 

(gdb) bt function_add_1                                                                              

(gdb) condition 1 (ptr_var == 0x18ed70e8)                                                 

 

Syntax :  condition <bnum> <condition>


Removing a conditional GDB

If the condition is set with condition command,  

(gdb) condition 1 (ptr_var == 0x18ed70e8)                                  

 

Removal is setting the condition with empty list.

(gdb) condition 1                                                                          

(gdb) end                                                                                     

 




Adding a command list

 

Executing a series of commands when the breakpoint hits, is a little restless activity if same API gets hit multiple times.  I recently learnt preparing a command list for each breakpoint will save a plenty of time.

 

commands [bnum]

... command-list ...

end

 


(gdb) commands 1                                                                                         

Type commands for breakpoint(s) 1, one per line.                                        

End with a line saying just "end".                                                                  

>bt                                                                                                                 

> set x = y+4                                                                                                  

>printf “x is %d\n”, x                                                                                     

>end                                                                                                               

 
Removing the command list


We need to provide the empty command list for the specific commands breakpoint.

(gdb) commands 1                           

>end                                                

 

Syntax: commands end

 

Now, you are good to debug the 'hang process'. Click here to debug with me. 

Comments