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

时间:2017-04-25 02:41:19

标签: formatting gdb rust

当我尝试使用字符串格式进行打印时,就像我在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
.....

1 个答案:

答案 0 :(得分:3)

使用printf时,它希望表达式为数字或a 指针。从Commands for Controlled Output

拉出来
  

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

     

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

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

(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