How do I break at a breakpoint if, and only if, a variable is declared and it meets a condition

Discussion in 'Mac Programming' started by moonman239, Sep 7, 2015.

  1. moonman239 macrumors 68000

    Mar 27, 2009
    I have a BOOL variable. Let's call it Bob. I have a symbolic breakpoint at -[UIImageView setImage], and I want my program to break if - and only if - a variable named Bob is declared and is true. That is, given a piece of code like this:

    BOOL bob = (some value);
    UIImage *someImage = ...;
    [imageView setImage:someImage];
    UIImage *someOtherImage = ...;
    [imageView setImage:someOtherImage];
    the program should break after setImage is called in methodA, and then only if bob = true. But the program should not break in methodB.

    Unfortunately, based on my experience, I predict that the following will actually happen:

    If method A is called, the debugger will see a variable named "bob", and make sure it is true; if "bob" is false, then the program will not be paused.
    If method B is called, the debugger will see that there is no variable named "bob" and will pause the program, since the debugger can't evaluate an undeclared variable.
  2. lloyddean, Sep 7, 2015
    Last edited by a moderator: Sep 8, 2015

    lloyddean macrumors 6502a

    May 10, 2009
    Des Moines, WA
  3. moonman239 thread starter macrumors 68000

    Mar 27, 2009
    I saw that one, but I don't see how that's going to help. Jeff uses a BOOL variable in the same method in which he sets up a breakpoint. In my case, I'm telling the debugger to break at setImage, and the debugger figures out where setImage is in my code. There are multiple classes in my code that cause UIImageView's setImage to be fired, and I don't want to add one piece of code per class just to make a conditional breakpoint work.
  4. derbs macrumors newbie

    Sep 9, 2015
    In the condition, have you tried "(BOOL)bob != nil && (BOOL)bob == YES" ...?
  5. lee1210, Sep 9, 2015
    Last edited: Sep 9, 2015

    lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    in the method:
    [imageView setImage:someImage];
    if (bob) {
    Then you can set a breakpoint on the NSLog. You can definitely set conditional breakpoints, but if you're having trouble, this should be an easy fix.


    EDIT: if you set a conditional breakpoint on the LINE with setImage when Bob is true rather than a method breakpoint that should work, too.
  6. GeeYouEye macrumors 68000


    Dec 9, 2001
    State of Denial
    The breakpoint on -setImage: actually breaks in the prologue of the implementation of the -setImage: method, not at the call site. It only appears like it does because you're calling a library function. Set a breakpoint on -methodA for a demonstration of this. It will break at the beginning of the method, not at the call site in whatever method calls it.

    AFAIK, there is no way to refer to variables declared one stack frame up (the calling function) in a conditional breakpoint. You have to set your breakpoints in each method with a bob variable (on the line with the -setImage: call is fine) and then make the breakpoint conditional. Note that conditional breakpoints are unreasonably slow (the condition test itself is slow, because it actually breaks, tests in the interpreter, and continues if false) so if you expect -methodA to be called a lot, it may be better to roughly follow @lee1210's advice, although you don't need a log; an empty statement (just the semicolon) is fine, and you can break on that line. You may want to put it before the -setImage: call though if you're interested in tracing though that.

Share This Page