【发布时间】:2021-11-11 07:10:03
【问题描述】:
我只是想了解如何动态重定向 linux 进程输入/输出/错误(0/1/2),我在堆栈溢出中找到了答案 How to redirect output of an already running process
如您所见,指南要求我通过 gdb 附加到进程并在文件描述符上调用 close() 并创建新的。不幸的是 cat (测试应用程序)没有使用调试信息构建,gdb 抱怨没有关闭这样的符号。我尝试的其他系统命令也是如此。
我需要做什么才能访问 gdb 中的这些符号 此外,如果您能够提供替代方法来重定向已运行进程的输出,而无需安装外来工具或使用 gdb,那就太好了。谢谢
编辑: gdb 会话
gdb -p 6836 /bin/cat
GNU gdb (GDB) 11.1
Copyright (C) 2021 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
<http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute
it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<https://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /bin/cat...
(No debugging symbols found in /bin/cat)
Attaching to program: /usr/bin/cat, process 6836
ptrace: No such process.
(gdb) p close(1)
No symbol table is loaded. Use the "file" command.
(gdb) file /bin/cat
Reading symbols from /bin/cat...
(No debugging symbols found in /bin/cat)
(gdb) p close(1)
No symbol table is loaded. Use the "file" command.
(gdb)
【问题讨论】:
-
“不幸的是 cat(测试应用程序)没有使用调试信息构建,gdb 抱怨没有关闭这样的符号。” -- 你的
cat是静态链接的吗?很可能你做错了什么。显示完整 GDB 会话可能有助于回答您的问题。 -
完成,我添加了 gdb 会话。我不知道我的 cat 二进制文件是如何链接的。我希望通过动态链接,因为它是许多发行版的常见做法。作为参考,我使用 manjaro 如果这对你有什么影响的话。我不知道他们如何链接他们的常用工具
标签: linux gdb command-line-interface