How to set a conditional breakpoint in Xcode based

2019-01-10 02:17发布

问题:

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?

回答1:

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.



回答2:

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.

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



回答3:

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).



回答4:

If you mutate myObj.name 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.



回答5:

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;
@end

And implementation file:

#import "DebugConditionalBreakPointHelper.h"

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