How to Set a Conditional Breakpoint in gdb: Example

I frequently want to set a breakpoint that stops only when a zero-terminated C string has a specific value. Let’s say that I have already set breakpoint 14:

Breakpoint 14, mysql_parse (thd=0x197c6f8, rawbuf=0x19db5e8 "SHOW SLAVE STATUS", length=17, found_semicolon=0x44088ee0) at

The breakpoint just stopped with the variable rawbuf having the value “SHOW SLAVE STATUS”.  I’m not interested in having it stop until rawbuf has the value “FLUSH LOGS” (hundreds of calls later).

Here is how to make it stop when rawbuf has the value “FLUSH LOGS”. At the (gdb prompt) enter the command:

cond 14 strstr (rawbuf, "FLUSH LOGS")

Then type the command “c” and watch it run until breakpoint 14 is reached AND the rawbuf variable contains the [sub]string “FLUSH LOGS”.

Another String-Related Conditions

Make breakpoint 12 stop only if the variable opt->name matches the string “user”:

cond 14 strcmp (opt->name, “user”) == 0