如何使 C++ 调试语句出现在 gdb 控制台中
How to make C++ debug statements appear in the gdb console
我正在尝试在 Ubuntu 18.04 上调试 gdb 运行 中的应用程序。
在部分代码中,我可以设置断点并成功调试问题。
但在其他部分,触发断点会导致进程退出。
有什么方法可以让我在 gdb 控制台中显示 debut 语句吗?
我目前使用 gdb 附加到进程,然后从那个点开始调试。
I have code (std::cout) that sends statements to standard out but they are not showing up in the gdb console.
你也不应该期望他们这样做。
应用程序启动时,其 std::cout
消息将发送到文件描述符 1 (stdout
)。这可以是启动应用程序的终端 window,或者如果重定向输出则为文件。它也可以是管道或 /dev/null
.
GDB 不会 "steal" 那个输出(如果有,调试作为另一个程序通过管道的输入源的程序会更难)。
您的第一个任务应该是确定输出的去向。在 Linux 上,这通常与 ls -l /proc/$pid/fds/1
一样简单(将 $pid
替换为您正在调试的进程的实际进程 ID)。
另一个复杂的问题是 stdout
可以完全缓冲(如果它进入文件、管道或套接字),并且在命中断点时可能不会 flush
ed .
P.S。从理论上讲,您可以通过 运行 以下 GDB 命令 "steal" 从任何地方输出到当前终端的输出:
(gdb) print open("/dev/tty", 2, 0) # open new fd in the inferior process
# going to current terminal.
# This will print something, e.g. 5
# Now make stdout go to that newly-opened fd
(gdb) call dup2($whatever_last_command_printed, 1)
但我不推荐这样做,因为它会以意想不到的方式干扰程序。
我正在尝试在 Ubuntu 18.04 上调试 gdb 运行 中的应用程序。
在部分代码中,我可以设置断点并成功调试问题。
但在其他部分,触发断点会导致进程退出。
有什么方法可以让我在 gdb 控制台中显示 debut 语句吗?
我目前使用 gdb 附加到进程,然后从那个点开始调试。
I have code (std::cout) that sends statements to standard out but they are not showing up in the gdb console.
你也不应该期望他们这样做。
应用程序启动时,其 std::cout
消息将发送到文件描述符 1 (stdout
)。这可以是启动应用程序的终端 window,或者如果重定向输出则为文件。它也可以是管道或 /dev/null
.
GDB 不会 "steal" 那个输出(如果有,调试作为另一个程序通过管道的输入源的程序会更难)。
您的第一个任务应该是确定输出的去向。在 Linux 上,这通常与 ls -l /proc/$pid/fds/1
一样简单(将 $pid
替换为您正在调试的进程的实际进程 ID)。
另一个复杂的问题是 stdout
可以完全缓冲(如果它进入文件、管道或套接字),并且在命中断点时可能不会 flush
ed .
P.S。从理论上讲,您可以通过 运行 以下 GDB 命令 "steal" 从任何地方输出到当前终端的输出:
(gdb) print open("/dev/tty", 2, 0) # open new fd in the inferior process
# going to current terminal.
# This will print something, e.g. 5
# Now make stdout go to that newly-opened fd
(gdb) call dup2($whatever_last_command_printed, 1)
但我不推荐这样做,因为它会以意想不到的方式干扰程序。