在GDB中将字符数组打印到文件

13

我有一个大约有100k条目的字符数组。当我在函数中使用print *buffer时,它似乎会被截断得过早。是否有什么办法可以延长gdb将打印到控制台的字符数量?如果不行,我能把这个变量打印到文件中吗?我尝试使用dump命令,但似乎无法得出正确的结果。


从您的描述来看,您的数组可能实际上是一个指针。在执行该命令时,您是否指定了要打印的长度? - Carl Norum
它被作为**缓冲区从调用函数传递到该函数中。 - Derek
3
我觉得在代码中定义执行数据转储的函数,并从gdb中调用它们要容易得多。(将定义内容用 #ifdef DEBUG 包裹起来) - William Pursell
如果你在调试生产代码,这样做并不容易。;-) - mpontillo
“dump”命令对我来说是正确的解决方案。你尝试了哪些“dump”的参数?我在下面发布了对我有效的示例。 - mpontillo
显示剩余2条评论
4个回答

27

我认为你想要类似这样的东西:

(gdb) dump binary memory ~/file.bin 0x100390f4c (0x100390f4c + 940)

使用dump命令有些笨拙。它需要一个起始地址和一个结束地址,以及指示要转储的内容的表达式(如果您喜欢,可以使用 value 而不是 memory 来指定一个表达式,但有时我更愿意具体说明)。但是,似乎(如上面我已经测试过的那样),您仍然可以使用表达式,因为我已经根据我想要转储的起始地址加上我想要的字节数来指定了上面的结束地址。

您也可以像这样做(传递导致指针值的表达式,而不是指针本身的值):

(gdb) dump binary memory ~/file.bin buf (buf + len)

欲了解更多信息,请查看此处的文档


13
(gdb) help x
Examine memory: x/FMT ADDRESS.
ADDRESS is an expression for the memory address to examine.
FMT is a repeat count followed by a format letter and a size letter.
Format letters are o(octal), x(hex), d(decimal), u(unsigned decimal),
  t(binary), f(float), a(address), i(instruction), c(char) and s(string).
Size letters are b(byte), h(halfword), w(word), g(giant, 8 bytes).
The specified number of objects of the specified size are printed
according to the format.

Defaults for format and size letters are those previously used.
Default count is 1.  Default address is following last thing printed
with this command or "print".
(gdb) x/8b array
0xbffd7670:     0       0       0       0       0       0       0       0
(gdb) x/16b array
0xbffd7670:     0       0       0       0       0       0       0       0
0xbffd7678:     0       0       0       0       0       0       0       0
(gdb) x/128b array
0xbffd7670:     0       0       0       0       0       0       0       0
0xbffd7678:     0       0       0       0       0       0       0       0
0xbffd7680:     0       0       0       0       0       0       0       0
0xbffd7688:     0       0       0       0       0       0       0       0
0xbffd7690:     0       0       0       0       0       0       0       0
0xbffd7698:     0       0       0       0       0       0       0       0
0xbffd76a0:     0       0       0       0       0       0       0       0
0xbffd76a8:     0       0       0       0       0       0       0       0
0xbffd76b0:     0       0       0       0       0       0       0       0
0xbffd76b8:     0       0       0       0       0       0       0       0
0xbffd76c0:     0       0       0       0       0       0       0       0
0xbffd76c8:     0       0       0       0       0       0       0       0
0xbffd76d0:     0       0       0       0       0       0       0       0
0xbffd76d8:     0       0       0       0       0       0       0       0
0xbffd76e0:     0       0       0       0       0       0       0       0
0xbffd76e8:     0       0       0       0       0       0       0       0
(gdb)

如果您希望打印ASCII字符符号,则使用x/<size>c
(gdb) set logging file ~/gdb_dump.txt
(gdb) set logging on
Copying output to /home/mminich/gdb_dump.txt.
(gdb) x/26c array
0xbfff4b20:     97 'a'  98 'b'  99 'c'  100 'd' 101 'e' 102 'f' 103 'g' 104 'h'
0xbfff4b28:     105 'i' 106 'j' 107 'k' 108 'l' 109 'm' 110 'n' 111 'o' 112 'p'
0xbfff4b30:     113 'q' 114 'r' 115 's' 116 't' 117 'u' 118 'v' 119 'w' 120 'x'
0xbfff4b38:     121 'y' 122 'z'
(gdb) set logging off
Done logging to /home/mminich/gdb_dump.txt.
(gdb)

顺便说一句,我完全同意William Pursell在你的问题下发表的评论:“我发现在代码中定义执行数据转储的函数并从gdb中调用它们要容易得多。(将这些定义包装在#ifdef DEBUG之内)”


1
把它打印到文件里怎么样? - Derek

3

要在控制台中打印无限字符,请使用

set print elements 0

3
我个人使用嵌入式Python来转储数据。例如:
(gdb) pi open("output_data.log","w").write(gdb.execute("print myarray@100000",to_string=True))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接