Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have a breakpoint get triggered if an instance variable in the class has its value changed?

Say I have a variable, self.position, how do I get Xcode to break whenever it changes its value (a number of methods could change it).

like image 269
Doug Smith Avatar asked Aug 09 '13 15:08

Doug Smith


People also ask

How do you set a conditional breakpoint?

To set a conditional breakpoint, activate the context menu in the source pane, on the line where you want the breakpoint, and select “Add Conditional Breakpoint”. You'll then see a textbox where you can enter the expression. Press Return to finish.

How do I add a conditional breakpoint in Visual Studio?

Right-click the breakpoint symbol and select Conditions (or press Alt + F9, C). Or hover over the breakpoint symbol, select the Settings icon, and then select Conditions in the Breakpoint Settings window.

How do you break a variable change in Visual Studio?

In fact, you can now right-click a variable name in the Local window and then select Break When Value Changes (see Figure 58). Visual Studio will automatically track your variable, and it will break the application execution when the variable value changes.

How do you use breakpoints?

Add breakpoints to your projectAdd a breakpoint by clicking the gutter next to the line number you want to pause at. A dot will appear next to the line number, and the line will be highlighted.


1 Answers

For conditional breaking:

  1. Cmd+option click the breakpoint
  2. Add a break condition like so:

enter image description here

For breaking on every occasion the value has changed:

  1. Implement trivial setter (and getter for the sake of clean code).
  2. Add breakpoint to setter.

If you want to see who invoked the setter - just look at the next line in the stack trace (viewDidLoad in my example):

enter image description here

Update:

Adding a watchpoint

  1. Break anywhere so that the (lldb) prompt shows up in the console
  2. Type in watchpoint set variable _position (replace _position with an iVar you want to watch)
  3. Make a note of the assigned watchpoint number to your newly created watchpoint.
  4. Modify the watchpoint for conditional breaking: watchpoint modify -c "_position < 0.5" 1 where the expression in quotes is the condition and the number at the end is the watchpoint number you noted in #3.
  5. Continue running. You'll break whenever the value matches the condition and you'll be able to inspect the stack frame to understand where the call came from.
like image 79
Stavash Avatar answered Sep 24 '22 12:09

Stavash