【问题标题】:Print character array to file in GDB将字符数组打印到 GDB 中的文件
【发布时间】:2019-01-18 23:52:50
【问题描述】:

我有一个包含大约 100k 个条目的字符数组。好像在我的功能中 print *buffer 它被过早地切断了。我能做些什么来延长 gdb 将打印到控制台的字符数量吗?如果没有,我可以将此变量打印到文件中吗?我尝试使用转储命令,但似乎想不出正确的方法。

【问题讨论】:

  • 从您的描述看来,您的数组实际上可能是一个指针。您在发出该命令时是否指定了要打印的长度?
  • 好吧,它作为**缓冲区从调用函数传递到函数中。
  • 那么 GDB 应该如何知道它有多长?
  • 我发现在执行数据转储的代码中定义函数要容易得多,并且只需从 gdb 调用它们。 (将定义包装在#ifdef DEBUG 中)
  • 如果您正在调试生产代码,这并不容易。 ;-)

标签: c gdb


【解决方案1】:

我想你想要这样的东西:

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

dump 命令使用起来有点尴尬。它需要一个开始和结束地址,以及指示转储内容的表达式(您可以使用value 而不是memory 来指定一个表达式,如果这对您有用,但有时我宁愿具体一点。)但是看来(正如我在上面测试过的)无论如何你都可以使用表达式,因为我已经根据我想要转储的起始地址以及我想要的字节数指定了上面的结束地址。

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

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

有关详细信息,请参阅documentation here

【讨论】:

    【解决方案2】:
    (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 中) "

    【讨论】:

    • 如何将其打印到文件中?
    【解决方案3】:

    要将无限字符打印到控制台,请使用

    set print elements 0
    

    【讨论】:

      【解决方案4】:

      我个人使用嵌入式python进行数据转储。例如:

      (gdb) pi open("output_data.log","w").write(gdb.execute("print myarray@100000",to_string=True))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-07
        • 1970-01-01
        • 2011-07-10
        • 1970-01-01
        • 2011-02-18
        相关资源
        最近更新 更多