Getting GDB to stop on a conditional breakpoint

One of the cool things about Xcode and GDB is that you can create custom breakpoints that could be set up by calling a function within your own project.

GDB supports C functionality in their conditional checks, but for me, I found this functionality to be particularly helpful when paired with convenience variables.

So, let’s say you have a string in your function and you want your breakpoint to only be triggered when the string matches a particular pattern. What you could do is the following:

  1. Set up a convenience variable and set that to the result of a strstr call
  2. Create another breakpoint where it breaks on the new convenience variable

In a breakpoint, set the following in the command:
set $myRes = (char*)strstr(myCharPtr, “My search string”)

For this particular breakpoint, you can actually set it to continue since you don’t want to stop every time GDB gets to that point.

Now in a new breakpoint, set the condition for that breakpoint to $myRes

One problem I see with this is that you might break on a line of code that doesn’t make sense if the same variable was modified by another thread.

In this case, you might want to reset the variable or have another condition to check the thread id too!

Leave a Reply

Your email address will not be published. Required fields are marked *