gdb - 使用管道进行调试 [英] gdb - debugging with pipe

查看:36
本文介绍了gdb - 使用管道进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个名为 blahret 的程序.我想调试通过 I/O 重定向从 ret 程序接收输入的 blah 程序.以下情况下如何使用gdb调试blah程序?

Say I have a two programs named blah and ret. I want to debug blah program which receives input from ret program via I/O redirection. How do I debug the blah program in the following case using gdb?

bash> ret | blah 

推荐答案

首先可以通过pid运行程序并调试.当然,这种解决方案并不涵盖所有情况.

At first, you may run the program and debug it by pid. This solution, of course, doesn't cover all cases.

另一种方法是使用 Linux 功能进行进程间通信.简而言之,您将 ret 的输出重定向到 FIFO 特殊文件(命名管道"),然后通过调试器从该 FIFO 读取.这是它是如何完成的.从 bash 运行:

Another approach is to use Linux capabilities for inter-process communication. In short, you redirect the output of ret to a FIFO special file ("named pipe") and then read from that FIFO via debugger. Here's how it's done. From bash, run:

mkfifo foo

这会在您的目录中创建一个特殊文件,用作命名管道.当您向该文件写入文本时(使用相同的语法 echo "Hello" >foo),写入程序将阻塞,直到有人从文件中读取数据(cat <foo,例如).在我们的例子中,一个 gdb 控制的进程将从这个文件中读取.

This creates a special file in your directory that will serve as a named pipe. When you write text to this file (using the same syntax echo "Hello" >foo), the writing program will block until someone reads the data from the file (cat <foo, for instance). In our case, a gdb-controlled process will read from this file.

创建 fifo 后,从 bash 运行:

After you created a fifo, run from bash:

ret > foo &   # ampersand because it may block as nobody is reading from foo
gdb blah

然后,在 gdb 提示符下,运行

Then, in gdb prompt, run

run <foo

并得到想要的效果.请注意,您不能从 fifo(以及通常的管道)读取数据两次:当您读取所有数据时,blah 进程终止,您应该重复命令写入到 foo (您可以从另一个 shell 窗口执行此操作).

And get the desired effect. Note that you can't read the data from the fifo (as well as from a usual pipe) twice: when you've read all the data, the blah process dies and you should repeat the command writing to foo (you may do it from the other shell window).

完成后,用rm foo删除fifo(或者放到系统重启后自动删除的目录,如/tmp).

When you're done, remove the fifo with rm foo (or place it into the directory where it will automatically be removed upon system restart, such as /tmp).

这篇关于gdb - 使用管道进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆