I'm looking to be able to have the debugger break when it reaches a particular string match. As an example, I might have something like this:
Foo myObj = [self gimmeObj];
myObj
might have a property called name
. I want the debugger to stop on the assignment when
[myObj.name isEqualToString:@"Bar"];
How can I set my conditional breakpoint in Xcode to do that?
To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.
Use conditional breakpoints to conditionally stop program execution. Breakpoints normally stop the execution every time a certain line or function is reached. However, using the condition keyword, a breakpoint will only be activated if a certain condition is true.
What are breakpoints in Xcode? A breakpoint can be placed at a certain line of code and pauses your app during execution in order to inspect the state of your app at that point. Breakpoints can be set at any time, before and while your app is running.
You can set a conditional break point in Xcode by setting the breakpoint normally, then control-click on it and select Edit Breakpoint (choose Run -> Show -> Breakpoints).
In the breakpoint entry, there is a Condition column.
Now, there are several issues to keep in mind for the condition. Firstly, gdb does not understand dot syntax, so instead of myObj.name, you must use [myObj name] (unless name is an ivar).
Next, as with most expressions in gdb, you must tell it the type of return result, namely "BOOL". So set a condition like:
(BOOL)[[myObj name] isEqualToString:@"Bar"]
Often it is actually easier to just do this in code by temporarily adding code like:
if ( [myObj.name isEqualToString:@"Bar"] ) { NSLog( @"here" ); }
and then setting the break point on the NSLog. Then your condition can be arbitrarily complex without having to worry about what gdb can and can't parse.
Here is how you do using XCode lldb conditional breakpoints.
First, double click the breakpoint (or right-click edit breakpoint
), you can see a dialogue popup.
Updated 2021-04-22 for Xcode 12:
Here is what these options means:
Here is a summary. For the above example in the image, it means that when the variable testedString
is equal to "Testing"
, break here. If I add ignore time to 1, then it will ignore the first time when testedString
is equal to "Testing"
and break at the second time the condition is met.
For actions, when you press add actions, there will be a list of choices. Usually what I do is to use the Debugger Command
po
to print variables that I need to check and I believe that there are better ways using the actions than I do.
It seems that you have to recompile and run the app if you change the conditions at runtime
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With