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

    Joined:
    Mar 27, 2009
    #1
    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:

    Code:
    -(void)methodA
    {
    BOOL bob = (some value);
    UIImage *someImage = ...;
    [imageView setImage:someImage];
    }
    
    -(void)methodB
    {
    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

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
  3. moonman239 thread starter macrumors 68000

    Joined:
    Mar 27, 2009
    #3
    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

    Joined:
    Sep 9, 2015
    #4
    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

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    in the method:
    Code:
    [imageView setImage:someImage];
    if (bob) {
      NSLog(@"...");
    }
    
    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.

    -Lee

    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

    GeeYouEye

    Joined:
    Dec 9, 2001
    Location:
    State of Denial
    #6
    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