gpt4 book ai didi

formatting - 使用 GDB 调试 Rust 程序时,如何使用格式打印?

转载 作者:行者123 更新时间:2023-11-29 08:14:21 26 4
gpt4 key购买 nike

当我尝试使用字符串格式进行打印时,就像我在 C 中调试时所做的那样,我得到了一个转换错误:

(gdb) printf "%s\n", "hello world"
Value can't be converted to integer.

预期:

(gdb) printf "%s\n", "hello world"
$2 = "hello world"

诊断信息:

$ rust-gdb -v
GNU gdb (GDB) 7.12.1
.....

最佳答案

当使用 printf 时,它期望表达式是一个数字或一个指针。拉自 Commands for Controlled Output

printf template, expressions…

The expressions are separated by commas and may be either numbers or pointers

如果我用 gdb 的 ptype 命令检查了 "hello world" 的类型,我会注意到它是一个对象而不是数字或指针。

(gdb) ptype "hello world"
type = struct &str {
data_ptr: u8 *,
length: usize,
}

要解决此问题,请将参数更改为名为 data_ptr 的字符串属性。

(gdb) ptype "hello world".data_ptr
type = u8 *

(gdb) p "hello world".data_ptr
$14 = (u8 *) 0x101100080 "hello world\000"

返回 data_ptr 应该有效,因为它是一个指针 (u8 *),它指向一个作为字符串开头的地址。

(gdb) printf "%s\n", "hello world".data_ptr
hello world

请注意不要与print 混淆,因为这行不通

(gdb) print "%s\n", "hello world".data_ptr
Could not convert character to `UTF-8' character set

关于formatting - 使用 GDB 调试 Rust 程序时,如何使用格式打印?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43600685/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com