26

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
  • 331
  • 1
  • 4
  • 8

1 Answers1

51

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'
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • I get this : error expression evaluation of address to watch failed , expression evaluated – user3001909 Jan 11 '14 at 17:22
  • @user3001909: Strange, I had tested this. - I have added an example. – Martin R Jan 11 '14 at 17:30
  • 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 ! – user3001909 Jan 11 '14 at 17:39
  • 1
    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? – Joey Carson Jan 13 '15 at 17:30
  • Yes the `-x` doesn't seem to be recognized in xcode 7.X – Tylerc230 Jan 14 '16 at 19:38
  • 1
    @Tylerc230: It seems to be `--size` or `-s` now. Answer updated. – Martin R Jan 14 '16 at 19:51