By Coocoo4Cocoa

2009-06-12 19:34:30 8 Comments

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

[ isEqualToString:@"Bar"];

How can I set my conditional breakpoint in Xcode to do that?


@nuynait 2017-02-15 22:10:56

Here is how you do using XCode lldb conditional breakpoints.

First, double click the break point (or right click edit breakpoint), you can see a dialog popup.

enter image description here

Here is what those option means:

  1. Condition: The breakpoint will only fire under this condition.
  2. Ignore: The amount of times the condition needs to meet before fire the breakpoint
  3. Action: Action that runs after the breakpoint breaks.
  4. Options: Automatically continue after evaluating actions

Here is a summary. For the above example in image, it means that when the variable buildingId is equal to 13, break here. If I add ignore time to 1, then it will ignore the first time when buildingId is equal to 13 and break at the second time the condition is met.

For actions, when you press add actions, there will be a list of choice. 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 then I do.

It seems that you have to recompile and run the app if you change the conditions at runtime

@Z S 2017-12-12 10:43:48

Probably because the question was about stopping at breakpoint based on a string value [I wasn't the down-voter]

@andreskwan 2018-11-22 15:43:22

Thanks, quite helpful. This answer deserves more votes.

@lal 2017-02-08 19:23:57

At times when working with Frameworks (debug builds) and need to put a breakpoint in certain file/location that is hard to navigate or isn't exposed publically in framework under development. One option is to write a helper class to trigger conditional breakpoints & make step-in/step-out easier.

- (void)invokeFrameworkMethod {
    [DebugConditionalBreakPointHelper breakPointCondition:YES comment:@"from invokeFrameworkMethod."];

Header declaration in framework under development.

#import <Foundation/Foundation.h>

@interface DebugConditionalBreakPointHelper : NSObject
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment;

And implementation file:

#import "DebugConditionalBreakPointHelper.h"

@implementation DebugConditionalBreakPointHelper
+ (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment {
    if (enabled)
        NSLog(@"Triggerred Conditional Break Point. Comment: %@");

@Peter N Lewis 2009-06-13 02:53:11

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, 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 ( [ 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.

@Pål Brattberg 2011-05-27 15:45:02

Except that by altering your code you run the risk of forgetting to remove your logging or altering behaviour

@Peter N Lewis 2011-06-07 01:05:13

That's true. I often mitigate this by adding "NYI" (Not Yet Implemented) to the string, and then my pre-release check search for NYI will catch it.

@Wex 2012-07-09 11:51:39

To get this working I had to make (bool) uppercase as (BOOL), probably an LLDB thing.

@Chaosphere2112 2012-10-26 16:28:29

bool didn't work for me in GDB, I had to use BOOL or int– the difference is explained here

@ninjaneer 2013-01-03 08:05:31

I've found that putting it in code is a lot better. I had a for loop with a breakpoint stopping at a certain case of the for loop, it took 2 minutes before it finally stopped at the breakpoint. Setting it in code is a lot faster.

@Vaseltior 2013-01-31 13:14:42

Symbolic links in the debugger seems more appropriate.

@Almo 2013-11-20 16:32:46

You can't put it in code if you have a once-every 200 game bug which has finally come up, and now you need to do a conditional breakpoint. Stopping the program to alter the code is not an option.

@Gerard 2014-05-19 15:59:58

I found that I did not even have to add the NSLog placeholder in C++

@Arik Segal 2017-11-30 13:01:26

sleep(0) is a great ad-hoc break point (NSLog(@"") is more complex)

@Barry Wark 2009-06-12 19:59:25

If you mutate using the setter, you can add a symbolic breakpoint on -[MyObjClass setName:] either from the Debugger Console or from the Run->Manage Breakpoints->Add Symbolic Breakpoint menu in Xcode. If not (why not? you probably shouldn't be modifying the instance variable directly except in the designated initializer or dealloc) you can set a watchpoint in gdb (use the Debugger Console in Xcode once the debugger is running). This page explains how. I don't believe Xcode exposes a UI for setting watchpoints without using the Debugger Console.

@Adam Rosenfield 2009-06-12 19:38:38

I'm not sure if this will work, but you can try setting a breakpoint at that line of code, open up the debugger console (Cmd+Shift+R), and type

condition N (int)[[myObj name] isEqualToString:@"Bar"]

Where N is replaced by the number of the breakpoint (an integer).

Related Questions

Sponsored Content

1 Answered Questions

[SOLVED] How to download Xcode DMG or XIP file?

  • 2012-04-26 14:41:18
  • Proud Member
  • 1573497 View
  • 981 Score
  • 1 Answer
  • Tags:   xcode dmg

26 Answered Questions

[SOLVED] How do I sort an NSMutableArray with custom objects in it?

36 Answered Questions

[SOLVED] Reference - What does this error mean in PHP?

23 Answered Questions

[SOLVED] How do I check if a string contains another string in Objective-C?

10 Answered Questions

9 Answered Questions

[SOLVED] How to "add existing frameworks" in Xcode 4?

1 Answered Questions

Get NSException object from Exception breakpoint in XCode

1 Answered Questions

Sponsored Content