【问题标题】:gdb print to filegdb 打印到文件
【发布时间】:2013-02-18 16:04:16
【问题描述】:

我将我的临时字符串打印到标准输出:

printf "%s", nodeToString(myNode)

但我想将此字符串打印到文件中。

我尝试了here 的解决方案,但 printf 结果仍会输出到标准输出。

编辑:澄清 cIph3r 的答案。

这是我在命令行上尝试过的:

$ gdb 

(gdb) attach 23053

(gdb) printf "%s", nodeToString(myNode)  // This works and outputs to screen

(gdb) run printf "%s", nodeToString(myNode) > outputfile  // I get this warning
The program being debugged has been started already.
Start it from the beginning? (y or n) 

【问题讨论】:

    标签: c debugging redirect gdb printf


    【解决方案1】:

    gdb 可以像 bash 一样执行流水线操作 但我只知道如何从命令行使用 gdb。

    首先,运行 gdb 并告诉它应该调试什么:

    gdb ./prog
    

    然后在gdb中,你可以用run运行程序

    run 
    

    在这里你也可以使用 argumetns 运行它:

    run argv1 argv2
    

    但您也可以使用 bash-pipeline 命令运行它:

    run > outputfile
    

    这是一样的

    ./prog > outputfile
    

    但是,如果你想输出 gdb-output,比如

    (gdb) print "test"
    $s1 = "test"
    

    这不是正确的方法。你这样做:

    (gdb) set logging on
    

    但请记住,当您要调试的程序正在运行时不要这样做

    如果所有这些都不起作用,您可以使用 tee 从 gdb 捕获标准输出并将其重定向到文件:

    gdb ./prog | tee output.log
    

    那么 gdb-output 也保存在 output.log 中(但都是标准输出)

    退出 gdb 后,有一个名为 output.log 的文件名包含您在 gdb 中看到的所有内容

    【讨论】:

    • 我不知道如何做到这一点,我从 emacs 运行 gdb,并在 gdb shell 中使用附加命令添加程序进行调试。你能说得具体一点吗?
    • 好的,我误解了你的问题,以为 printf 是要调试的代码
    • 确保在程序未运行时设置登录
    • but keep in mind not to do this when the program you want to debug is running 为什么会这样?我就是这样做的,得到了​​我需要的输出。 gdb 7.9
    猜你喜欢
    • 1970-01-01
    • 2011-08-21
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    • 2012-11-01
    • 2023-02-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多