GDB - 在单独的窗口中显示目标应用程序的输出
我正在使用 GDB 来调试一些 C 应用程序。我当前所做的是加载目标应用程序,在第 30 行设置断点并运行它。
我想让 GDB 在新的终端窗口中显示我自己的应用程序的输出,同时我仍然能够通过 GDB 终端窗口控制断点处理,但我似乎找不到合适的开关。有什么办法让 GDB 在它自己的窗口中显示我的程序的输出吗?
I'm using GDB to debug some of my C applications. What I currently do is load the target application, set a breakpoint on line 30 and run it.
I would like to make GDB display the output of my own application in a new terminal window while I'm still able to control the breakpoint handling via the GDB terminal window, but I couldn't seem to find a proper switch. Is there any way making GDB display my program's output in its own window?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
对于想知道如何使用 GDB tty 命令的人,这里有一个简短的描述...
在输出窗口中运行
tty
命令。这将显示底层控制台正在使用的 tty 的名称。$tty
/dev/pts/4
打开另一个控制台窗口并在此处启动 GDB。我们将其称为 GDB 窗口。
现在使用上面获得的tty文件名在GDB中运行tty命令,然后开始调试过程。
(gdb) tty /dev/pts/4
(gdb) run
现在您应该能够在输出窗口中单独看到程序输出。
注意:GDB
set new-console on
命令在 Linux 上不起作用!它只能在 Windows 上运行。在 Linux 上使用上述 tty 方法。For people wondering how to use the GDB tty command here is a short description...
Run the
tty
command in the output window. This will show the name of the tty being used by the underlying console.$ tty
/dev/pts/4
Open another console window and start GDB here. Let us call this the GDB window.
Now run the tty command in GDB using the tty file name obtained above and then start the debugging process.
(gdb) tty /dev/pts/4
(gdb) run
Now you should be able to see the program output separately in the output window.
Note: The GDB
set new-console on
command does not work on Linux! It is meant to be run on windows only. Use the tty method described above on Linux.您可以使用
set new-console on
来完成此操作,如此处所示。You can use
set new-console on
to accomplish this as shown here.另一种方法是使用 gdbserver 启动目标程序(假设您可以使用它)。然后您可以将在单独窗口中启动的 GDB 连接到 gdbserver。
GNU gdbserver 文档
从窗口 A:
从窗口 B:
Another way to do this would be to start your target program with gdbserver (assuming it is available to you). Then you can connect GDB started in a separate window to gdbserver.
GNU gdbserver documentation
From window A:
From window B:
我知道的最好方法是将程序的输出重定向到一个文件,然后在另一个终端中
tail -f
该文件。重定向是通过run > 完成的。文件名
,如GDB 文档中所述。The best way I know is to redirect the output of the program to a file, and then
tail -f
that file in another terminal. Redirection is done withrun > filename
, as documented in the GDB documentation.GDB 的 tty 命令确实可以工作,但它不适用于交互式程序,例如您想调试 bash。即使对于非交互式程序,您也会得到以下结果:
我编写了一个小程序来解决这两个问题:
假设您将该程序保存到
idleterm.c
。编译它:要使用它,请启动一个终端窗口,然后在该窗口上运行
idleterm
。它将打印要附加到的 tty 的名称:复制该 tty 路径并将其粘贴到第二个窗口中的 gdb 会话中:
第一个窗口中将出现 bash 提示符。所有需要特殊 TTY 行为的 bash 交互都将正常工作,包括
^C
、^Z
等。idleterm
传递所有 键盘输入到正在调试的子进程,包括^C和^Z。因此,为了停止idleterm,您必须从单独的窗口中终止它。这就是idleterm
打印其 pid 的原因。复制 pid,然后将其粘贴到 Kill 命令中:如果系统上只有一个
idleterm
实例运行,您当然可以只使用killall
。GDB's
tty
command does work, but it doesn't work well with interactive programs, like if you wanted to debug bash. Even for non-interactive programs, you get the following:I wrote a small program to fix both of these issues:
Suppose you save the program to
idleterm.c
. To compile it:To use it, start one terminal window, and on that window, run
idleterm
. It will print the name of the tty to attach to:Copy that tty path and paste it into a gdb session in a second window:
A bash prompt will appear in the first window. All of the interactions with bash requiring special TTY behaviour will work normally, including
^C
,^Z
, etc.idleterm
passes all keyboard input through to the child process being debugged, including ^C and ^Z. So in order to stop idleterm, you'll have to kill it from a separate window. This is whyidleterm
prints its pid. Copy the pid, then paste it into the kill command:If there's only one instance of
idleterm
running on the system, you can of course just usekillall
.只需使用 tty 命令即可。如果您希望程序的输出重定向到 /dev/pts/5 ,请输入:
Just use
tty
command. If you want output of your program redirected to /dev/pts/5 type:对于 Mac 上的 lldb,以下命令在新的终端窗口中运行程序,而调试器从原始窗口进行控制:
这将程序作为指定 tty(终端窗口)中的进程运行:
With lldb on Mac the following runs the program in w new terminal window while the debugger controls from the original window:
This runs the program as a process in specified tty (terminal window):