调试程序时如何阻止输出(使用gdb)

6

我有一个程序,想要在gdb中进行调试。

我能看到常规的程序输出吗?如何启用/禁用此输出,仅保留gdb消息。

4个回答

8

您可以在gdb内部重定向输出:

(gdb) run > somefile.txt

将标准输出重定向到 somefile.txt。您还可以指定终端以发送输出:

(gdb) tty /dev/ttyb

6

是的,您将看到程序的所有输出。

您可以通过将其发送至其他地方来禁用此功能。例如:

(gdb) run > /dev/null

2

忽略标准输出和标准错误

run &>/dev/null

类似于Bash语法。
在GDB 7.10上进行测试。

2

如果你只是想在不需要gdb输出的情况下查看程序的输出,那么这个脚本可能会有用。

#!/bin/bash
file=$1
delay=1 #seconds
lastTime=`stat --printf=%y "$file"`

while [ 1 ]
do
  thisTime=`stat --printf=%y "$file"`
  if [ "$thisTime" != "$lastTime" ]
  then
    clear
    cat "$file"
  fi
  lastTime="$thisTime"
  sleep $delay
done

lastTime="$thisTime" 等待 $delay 秒 完成


2
什么?gdb在哪里?如何使用它?我只想要我所要求的,用gdb启动程序,使用gdb,但不要看到程序的stderr和stdout。 - osgx

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