5

In some debuggers this is called "setting a trap" on a variable. What I want to do is trigger a breakpoint on any statement that changes the object. Or changes a property of the object.

I have an NSMutableDictionary that gets a value/key added to it but I can't find any statement that could be doing that.

Jolta
  • 2,620
  • 1
  • 29
  • 42
RobertL
  • 14,214
  • 11
  • 38
  • 44
  • It's called a *watchpoint* in gdb and lldb - see the online help for how to set and use watchpoints. – Paul R Sep 09 '12 at 18:50

2 Answers2

8

You can set a watchpoint (from here):

Set a watchpoint on a variable when it is written to.
(lldb) watchpoint set variable -w write global_var
(lldb) watch set var -w write global_var
(gdb) watch global_var
Set a watchpoint on a memory location when it is written into. The size of the region to watch for defaults to the pointer size if no '-x byte_size' is specified. This command takes raw input, evaluated as an expression returning an unsigned integer pointing to the start of the region, after the '--' option terminator.
(lldb) watchpoint set expression -w write -- my_ptr
(lldb) watch set exp -w write -- my_ptr
(gdb) watch -location g_char_ptr
Set a condition on a watchpoint.
(lldb) watch set var -w write global
(lldb) watchpoint modify -c '(global==5)'
(lldb) c
...
(lldb) bt
* thread #1: tid = 0x1c03, 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16, stop reason = watchpoint 1
frame #0: 0x0000000100000ef5 a.out`modify + 21 at main.cpp:16
frame #1: 0x0000000100000eac a.out`main + 108 at main.cpp:25
frame #2: 0x00007fff8ac9c7e1 libdyld.dylib`start + 1
(lldb) frame var global
(int32_t) global = 5
List all watchpoints.
(lldb) watchpoint list
(lldb) watch l
(gdb) info break
Delete a watchpoint.
(lldb) watchpoint delete 1
(lldb) watch del 1
(gdb) delete 1
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • The writeup you quote is really unclear. The link you give helps a lot but it has no explanation or example of how to watch an object (only simple variables). After a lot of experimenting with different forms of the watch command I find that I can watch an NSString with: `watch set exp -w write -- &testString` but I can't find any watch command syntax that watches an NSMutableDictionary reliably, which was the original question. – RobertL Sep 11 '12 at 03:22
  • @RobertL I don't think there is much documentation on lldb other than the built-in help. – trojanfoe Sep 11 '12 at 07:00
  • Yes that seems to be true. And the built-in help seems intended just to remind you of the command syntax but not to explain anything. I guess tutorial-style documentation will be developed eventually. Until then we need to experiment or wait. Also, I've found that some watch statements that are accepted by lldb actually cause a crash at run time. That will probably improve with time too (either be rejected or not crash). – RobertL Sep 11 '12 at 17:41
4

Watchpoints are used to track a write to an address in memory (the default behavior). If you know where an object is in memory (you have a pointer to it), and you know the offset into the object that you care about, that's what watchpoints are for. For instance, in a simple C example, if you have:

struct info
{
   int a;
   int b;
   int c;
};

int main()
{
   struct info variable = {5, 10, 20};
   variable.a += 5;  // put a breakpoint on this line, run to the breakpoint
   variable.b += 5;
   variable.c += 5;
   return variable.a + variable.b + variable.c;
}

Once you're at a breakpoint on variable.a, do:

(lldb) wa se va variable.c
(lldb) continue

And the program will pause when variable.c has been modified. (I didn't bother to type out the full "watch set variable" command).

With a complex object like an NSMutableDictionary, for instance, I don't think watchpoints will do what you need. You would need to know the implementation details of the NSMutableDictionary object layout to know which word (or words) of memory to set a watchpoint.

Toribio
  • 3,963
  • 3
  • 34
  • 48
Jason Molenda
  • 14,835
  • 1
  • 59
  • 61