Bug 31401 - Error when placing watchpoint rust debug build
Summary: Error when placing watchpoint rust debug build
Status: RESOLVED INVALID
Alias: None
Product: gdb
Classification: Unclassified
Component: rust (show other bugs)
Version: 15.1
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2024-02-19 14:20 UTC by Joao Luca
Modified: 2024-02-21 16:00 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joao Luca 2024-02-19 14:20:29 UTC
When trying to place a watchpoint on a rust binary build with `cargo build`

it throws an error:
```
Breakpoint 1, 0x000055555555c8b0 in main.rs:1 ()
(gdb) watch *0x123456789
Attempt to take contents of a non-pointer value.
(gdb) watch *(char*)0x123456789
unexpected token
```

if you put it between quote it seems to at least place the watchpoint but its unclear if its working or not
```
fn main() {
    let mut a = 123;
    a = 321;
    a = 555;
    a = 222;
}

Breakpoint 1, example::main () at src/main.rs:2
2           let mut a = 123;
(gdb) next
3           a = 321;
(gdb) p &a
$1 = (*mut i32) 0x7fffffffd214
(gdb) watch "0x7fffffffd214"
Watchpoint 2: "0x7fffffffd214"
(gdb) watch a
Hardware watchpoint 3: a
(gdb) next

Watchpoint 2: "0x7fffffffd214"

Old value = "0x7fffffffd214"
New value = "0x7fffffffd214"

Watchpoint 2: "0x7fffffffd214"

Old value = "0x7fffffffd214"
New value = "0x7fffffffd214"

Watchpoint 2: "0x7fffffffd214"

Old value = "0x7fffffffd214"
New value = "0x7fffffffd214"
example::main () at src/main.rs:4
4           a = 555;
```

also using -l/-location doesn't seem to make any difference
```
(gdb) watch -l *0x123456789
Attempt to take contents of a non-pointer value.
(gdb) watch -location *0x123456789
Attempt to take contents of a non-pointer value.
(gdb) 
```

Took the examples from: https://sourceware.org/gdb/current/onlinedocs/gdb.html/Set-Watchpoints.html#Set-Watchpoints

How to reproduce:
```
mkdir example
cd example
cargo init .
cargo build
cargo build --release
gdb -nh -nx -ex 'b main.rs:1' -ex 'run' -ex 'watch *0x12345678' --batch target/debug/example
gdb -nh -nx -ex 'b main.rs:1' -ex 'run' -ex 'watch *0x12345678' --batch target/release/example
```

Both of the examples above work normally in the release build.

Tested on gdb 13.1 and on master
Rust versions 1.64, 1.66
Comment 1 Tom Tromey 2024-02-20 14:37:06 UTC
Hi.  Thanks for the bug report.

(gdb) watch *0x123456789
Attempt to take contents of a non-pointer value.

Here you're attempting to dereference just some
random int.  gdb used to allow this in the old
days (maybe it still does in C mode, not sure).
However you really need a typed pointer here.
Or you can just watch an expression -- best is
with "-location", like:

(gdb) watch -location a


(gdb) watch *(char*)0x123456789
unexpected token

This one is C syntax, not Rust.


(gdb) watch "0x7fffffffd214"
Watchpoint 2: "0x7fffffffd214"

This one watches that particular string, which is
not really what you want.  Perhaps this should be
an error, except strings are coerced to memory and
so gdb thinks this might actually change at some point.


> Both of the examples above work normally in the release build.

This probably works because gdb doesn't see any Rust debug info
and so defaults to the "minimal" language, which is basically
the same as C.

Anyway I don't think there's a bug here.  I'm going to close
this report.
Comment 2 Joao Luca 2024-02-21 16:00:34 UTC
Hi, thanks for the answer.
For some reason i assumed that i could do everything with C syntax.

Anyway, after looking at the rust testcases i realize that i could accomplish what i wanted with:
`watch *(0x7fffffffd4cc as *mut [u8; 4])`

Completely forgot about the `as` keyword.