Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In XCode 6 how can you set a watchpoint without stopping execution?

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.

like image 440
DrMad Avatar asked Feb 18 '15 10:02

DrMad


People also ask

What is a watchpoint?

Similarly, a watchpoint is a type of breakpoint that indicates an area of memory associated with a data item that you want to watch, pausing your application when that memory is updated. These types of breakpoint are set on a data item, as opposed to a particular line of code.

How do I add a watchpoint in GDB?

Set a watchpoint that will break when watch expr is read by the program. Set a watchpoint that will break when expr is either read or written into by the program. This command prints a list of watchpoints, breakpoints, and catchpoints; it is the same as info break . GDB sets a hardware watchpoint if possible.

How to remove a break point in GDB?

With the clear command you can delete breakpoints according to where they are in your program. With the delete command you can delete individual breakpoints, watchpoints, or catchpoints by specifying their breakpoint numbers.


1 Answers

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

like image 129
DrMad Avatar answered Sep 24 '22 23:09

DrMad