3

在某些调试器中,这称为对变量“设置陷阱”。我想要做的是在任何更改对象的语句上触发断点。或更改对象的属性。

我有一个 NSMutableDictionary ,它添加了一个值/键,但我找不到任何可以这样做的语句。

4

2 回答 2

7

您可以设置一个观察点(从这里):

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
于 2012-09-09T19:01:13.853 回答
3

观察点用于跟踪对内存中地址的写入(默认行为)。如果您知道一个对象在内存中的位置(您有一个指向它的指针),并且您知道您关心的对象的偏移量,那么这就是观察点的用途。例如,在一个简单的 C 示例中,如果您有:

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;
}

一旦你在一个断点variable.a,做:

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

修改后程序会暂停variable.c。(我没有费心输入完整的“watch set variable”命令)。

例如,对于像这样的复杂对象NSMutableDictionary,我认为观察点不会满足您的需求。您需要知道NSMutableDictionary对象布局的实现细节才能知道要设置观察点的内存中的哪个字(或多个字)。

于 2012-09-23T10:18:39.237 回答