How to set conditional breakpoint in Xcode based on object property?

I am looking to be able to debug a debugger when it reaches a specific string match. As an example, I might have something like this:

Foo myObj = [self gimmeObj]; 

myObj may 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 for this?

+74
debugging objective-c xcode breakpoints
Jun 12 '09 at 19:34
source share
5 answers

You can set a conditional breakpoint in Xcode by setting a breakpoint in normal mode, then right-clicking on it and select โ€œEdit breakpointโ€ (choose Run โ†’ Show โ†’ breakpoints).

The breakpoint entry has a Condition column.

Now there are a few issues that should be considered provided. Firstly, gdb does not understand the dot syntax, so instead of myObj.name you should use [myObj name] (if the name is not ivar).

Next, like most expressions in gdb, you must specify the type of the return result, namely "BOOL". So set a condition like this:

 (BOOL)[[myObj name] isEqualToString:@"Bar"] 

It is often actually actually easier to just do this in code by temporarily adding code, for example:

 if ( [myObj.name isEqualToString:@"Bar"] ) { NSLog( @"here" ); } 

and then set a breakpoint in NSLog. Then your condition can be arbitrarily complex without worrying about what gdb can and cannot analyze.

+158
Jun 13 '09 at 2:53
source share

I'm not sure if this will work, but you can try to set a breakpoint in this line of code, open 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 (integer).

+6
Jun 12 '09 at 19:38
source share

If you change myObj.name using setter, you can add a symbolic breakpoint to -[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 change the instance variable directly, except for the designated initializer or dealloc), you can set the watchpoint in gdb (use the debugger console in Xcode after starting the debugger). This page explains how to do this. I do not believe that Xcode provides a user interface for setting watchpoints without using the debugger console.

+2
Jun 12 '09 at 19:59
source share

Here's how you use it using XCode lldb conditional breakpoints.

First, double-click the edit breakpoint (or right-click edit breakpoint ), you will see a pop-up dialog box.

enter image description here

Here is what this option means:

  • Condition : A breakpoint will only fire under this condition.
  • Ignore : the number of times the condition must match before the breakpoint fire
  • Action : The action that takes place after breaking the breakpoint.
  • Parameters : automatic continuation after evaluating actions

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

For actions, when you click additional actions, a list will be selected. I usually use Debugger Command po to print variables that I need to check, and I believe that there are better ways to use the actions that I do.

It seems you need to recompile and run the application if you change the conditions at runtime

+1
Feb 15 '17 at 22:10
source share

While you are working with Frameworks (debug builds) and you need to set a breakpoint in a specific file / location that is difficult to orient or not publish as part of development. One option is to write a helper class to trigger conditional breakpoints and simplify I / O.

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

Title declaration as part of development.

 #import <Foundation/Foundation.h> @interface DebugConditionalBreakPointHelper : NSObject + (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment; @end 

And the implementation file:

 #import "DebugConditionalBreakPointHelper.h" @implementation DebugConditionalBreakPointHelper + (void)breakPointCondition:(BOOL)enabled comment:(NSString *)comment { if (enabled) { NSLog(@"Triggerred Conditional Break Point. Comment: %@"); } } @end 
0
Feb 08 '17 at 19:23
source share



All Articles