SETTING BREAKPOINTS: CONDITIONAL

GDB's breakpoint can be set based on a condition to pause program execution only when a specified logical condition is true, rather than stopping every time the breakpoint location is reached. This is useful when a line of code is executed multiple times (such as inside a loop), but you are only interested in analyzing the program state under certain circumstances. Conditional breakpoints work by attaching a Boolean expression to the breakpoint, and GDB evaluates that expression each time the breakpoint is hit. If the condition evaluates to true, execution stops; otherwise, the program continues running normally. This technique is especially powerful for filtering out irrelevant iterations or for debugging programs with large datasets.

SYNTAX

//SYNTAX
break <location> if <condition>

//EXAMPLE
break main if x == 5

 * This sets a breakpoint at the start of main, but the debugger will only halt 
   execution if the variable x equals 5 at that moment.
gef>  disas loopFib
 Dump of assembler code for function loopFib:
 ..SNIP...
 0x0000000000401012 <+9>:	js     0x401009  

gef>  b *loopFib+9 if $rbx > 10
 Breakpoint 2 at 0x401012
gef>  c
 ───────────────────────────────────────────────────────────────────────────────────── registers ────
 $rax   : 0x8               
 $rbx   : 0xd               
 $eflags: [zero carry PARITY adjust sign trap INTERRUPT direction overflow resume virtualx86 identification]
 ─────────────────────────────────────────────────────────────────────────────────── code:x86:64 ────
      0x401009 <loopFib+0>      add    rax, rbx
      0x40100c <loopFib+3>      xchg   rbx, rax
      0x40100e <loopFib+5>      cmp    rbx, 0xa
  →   0x401012 <loopFib+9>      js     0x401009 <loopFib>	NOT taken [Reason: !(S)]
  
  
  

Last updated