2012-09-09 30 views
3

在一些調試器中,這被稱爲變量上的「設置陷阱」。我想要做的是在任何改變對象的語句上觸發一個斷點。或者更改對象的屬性。如何找出Xcode 4/lldb中的一個對象(或一個簡單的變量)是如何改變的?

我有一個NSMutableDictionary獲取值/鍵添加到它,但我找不到任何可能做到這一點的聲明。

+0

它在gdb和lldb中被稱爲* watchpoint * - 請參閱聯機幫助以瞭解如何設置和使用觀察點。 –

回答

5

您可以設置一個觀察點(從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 
+0

你引用的報道確實不清楚。你給的鏈接有很多幫助,但是沒有解釋或者如何看對象的例子(只有簡單的變量)。經過大量不同形式的watch命令的試驗後,我發現我可以看到一個NSString:'watch set exp -w write - &testString',但我找不到任何可靠地觀察NSMutableDictionary的watch命令語法,它是原來的問題。 – RobertL

+0

@RobertL我不認爲除了內置的幫助外,其他文檔都不在lldb上。 – trojanfoe

+0

是的,這似乎是真的。內置的幫助似乎只是爲了提醒你命令的語法,但不能解釋任何事情。我想最終會開發出教程式的文檔。在此之前,我們需要嘗試或等待。另外,我發現lldb接受的一些監視語句實際上會在運行時導致崩潰。這可能會隨着時間的推移而改善(要麼被拒絕,要麼不會崩潰)。 – RobertL

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對象佈局的實現細節,才能知道要設置觀察點的內存中的哪個單詞(或多個單詞)。

相關問題