繁体   English   中英

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

[英]How can I print with formatting when debugging a Rust program with GDB?

当我尝试使用字符串格式进行打印时,就像在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 ,它期望表达式是数字或指针。 受控输出命令中拉出

printf模板,表达式......

表达式用逗号分隔,可以是数字或指针

如果我用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM