When I try to print with string formatting, like I do when debugging in C, I get an error of conversion:
(gdb) printf "%s\n", "hello world"
Value can't be converted to integer.
Expected:
(gdb) printf "%s\n", "hello world"
$2 = "hello world"
Diagnostic info:
$ rust-gdb -v
GNU gdb (GDB) 7.12.1
.....
When using printf
, it expects the expression to be either a number or a pointer. Pulled from Commands for Controlled Output
printf template, expressions…
The expressions are separated by commas and may be either numbers or pointers
If I had checked the type of "hello world"
with gdb's ptype
command, I would have noticed that it's an object and not a number or a pointer.
(gdb) ptype "hello world"
type = struct &str {
data_ptr: u8 *,
length: usize,
}
To resolve this, change the argument to the string's property called data_ptr
.
(gdb) ptype "hello world".data_ptr
type = u8 *
(gdb) p "hello world".data_ptr
$14 = (u8 *) 0x101100080 "hello world\000"
Returning data_ptr
should work because it's a pointer ( u8 *
) and it points to an address that is the start of the string.
(gdb) printf "%s\n", "hello world".data_ptr
hello world
Be aware not to mix up with print
as this wouldn't work .
(gdb) print "%s\n", "hello world".data_ptr
Could not convert character to `UTF-8' character set
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.