Watch points on memory address

21,893

Omit the "dereferencing operator" * when setting the watch point in lldb, just pass the address:

watchpoint set expression -- 0x123456
# short form:
w s e -- 0x123456

sets a watchpoint at the memory location 0x123456. Optionally you can set the number of bytes to watch with --size. Example in short form:

w s e -s 2 -- 0x123456

You can also set a watchpoint on a variable:

watchpoint set variable <variable>
# short form:
w s v <variable>

Example: With the following code and a breakpoint set at the second line:

int x = 2;
x = 5;

I did this in the Xcode debugger console:

(lldb) p &x
(int *) $0 = 0xbfffcbd8
(lldb) w s e -- 0xbfffcbd8
Watchpoint created: Watchpoint 1: addr = 0xbfffcbd8 size = 4 state = enabled type = w
    new value: 2
(lldb) n

Watchpoint 1 hit:
old value: 2
new value: 5
(lldb)

More simply, I could have set the watchpoint with

(lldb) w s v x
Watchpoint created: Watchpoint 1: addr = 0x7fff5fbff7dc size = 4 state = enabled type = w
    declare @ '/Users/martin/Documents/tmpprojects/watcher/watcher/main.c:16'
    watchpoint spec = 'x'
Share:
21,893

Related videos on Youtube

user3001909
Author by

user3001909

Updated on January 15, 2020

Comments

  • user3001909
    user3001909 over 4 years

    With the new change from gdb to lldb , I can't find a way how to set watch points on some memory addresses .

    In gdb I used this

    watch -location *0x123456
    

    Doing the same in lldb

    w s e *0x123456
    

    Isn't working for me . So what can I use to run the same command in lldb ?

  • user3001909
    user3001909 over 10 years
    I get this : error expression evaluation of address to watch failed , expression evaluated
  • Martin R
    Martin R over 10 years
    @user3001909: Strange, I had tested this. - I have added an example.
  • user3001909
    user3001909 over 10 years
    I tried your example, lldb said use of undeclared identifier 'x' . Then did w s e -- 0xOFFSET and it worked . watchpoint created : Watchpoint 1: addr = 0xOFFSET size = 4 state ... new value :10 . Thank you !
  • Joey Carson
    Joey Carson over 9 years
    I've gotten this same error since I upgraded to Yosemite and had to update Xcode as well. How can you state the range of bytes to watch from that address?
  • Tylerc230
    Tylerc230 over 8 years
    Yes the -x doesn't seem to be recognized in xcode 7.X
  • Martin R
    Martin R over 8 years
    @Tylerc230: It seems to be --size or -s now. Answer updated.