9

You can easily set a watchpoint in XCode by following these steps (btw if there is a simpler way, I'd like to know it...):
- Run your program
- Set a breakpoint in the code where your variable is used
- when reaching breakpoint, use a right click on the variable and select 'Watch "nameOfTheVariable"'
- Continue execution.

The only problem is that execution will stop every time the variable value changes. I would like XCode to continue the execution without stopping, i.e. merely display the value changes in the console output.
This feature seems available in command line mode, and although I initially wanted to avoid it, I posted a solution using that mode (see below), as it seems to be the only way to do what I want, i.e. continue execution while displaying variable changes.

DrMad
  • 490
  • 3
  • 12
  • I don't know how to do that but perhaps you could use KVO to an nslog when it changes? https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/KeyValueObserving/KeyValueObserving.html set up an observer on the thing you want to monitor, then use output method to nslog its current state – Ben Feb 18 '15 at 11:00
  • Yeah but that's not what I'm looking for. Plus this might be only available for iOS. I could also add DBG lines everywhere the variable is modified... – DrMad Feb 18 '15 at 11:26

2 Answers2

18

Well it seems that the only way to achieve this is to use the LLDB command line. So for those of you who, like me, had never used it here is a step-by-step (actually fairly easy) way to use it and watch variables without stopping execution:

  1. Set a breakpoint in Xcode (click to the left of your source line) where the variable you want to watch is used (in scope), and run your code until it reaches the breakpoint.
  2. In the console view (little window displayed at the bottom right where you can display console things) you should see a (lldb) prompt. This is where you enter the following commands:
    w s v stuff (or watchpoint set variable stuff) where stuff is the name of the variable you want to watch
    w c a (or watchpoint command add) to enter a script mode where you enter one command per line as follows after the '>'
    p stuff (or print stuff) to display the new stuff variable value
    c (or continue) to continue execution
    DONE to finish this little script (note the UPPERCASE characters!)

THAT'S IT ! You can remove your breakpoint and continue execution. From then on messages will be displayed in the console every time the variable "stuff" is updated, without stopping the execution of your code (it might slow it down a little of course, but that is usually not important).

DrMad
  • 490
  • 3
  • 12
  • In this case I would personally find it better, if you had used the long version of the lldb commands. Then it would be easier to understand, what this actually does. – Tim Bodeit Jun 18 '15 at 19:44
  • Well I thought I would show the shortest commands which will be what you use, but I agree it helps remember them to show the long version. So I edited my answer... :) – DrMad Jun 19 '15 at 07:08
  • Could you add like a example of what we would input if we wanted it to print something like `p self.m_someObj.m_someString`? It doesn't seems to be working for me, some errors comes up. I also tried the `->` instead of `.` to no avail. – CyberMew Jun 28 '16 at 06:45
  • @CyberMew should be the same for subfields : w s v self.m_someObj.m_someString and then p self.m_someObj.m_someString What kind of errors do you get ? They may be due to the fact that the variables you try to watch are not in the scope anymore. – DrMad Jun 28 '16 at 11:48
0

Watchpoint is just like a breakpoint which gets hit when the value of a variable which is being watched gets updated. To set it please follow below steps:

1.Set a breakpoint such that the variables view in the debugger shows the variable you want to watch. 2.Right click on the variable and select Watch "variable name". 3.This will stop the execution whenever the value of the variable changes.

The watchpoint will now start showing in the debug navigator. In order to remove it just drag is towards the editor and you are good to go.

PS : this is just a smarter version of implemention didset for a variable and setting and breakpoint inside it.

Pranav Gupta
  • 741
  • 9
  • 10