【问题标题】:Invoke system commands in gdb for application with no debug symbols在 gdb 中为没有调试符号的应用程序调用系统命令
【发布时间】: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


【解决方案1】:

你的问题从这里开始:

Attaching to program: /usr/bin/cat, process 6836
ptrace: No such process.

这很可能会失败,因为您输入了错误的进程 ID,或者您的 /usr/bin/cat 因其他原因退出。

尝试这样做:

cat > /tmp/foo &
[1] 1084976   # Note the PID

gdb /usr/bin/cat -p 1084976  # use the PID printed by the shell

这是您应该看到的:

Attaching to program: /bin/cat, process 1084976

Program received signal SIGTTIN, Stopped (tty input).
0x00007f96c36178be in __GI___libc_read (fd=0, buf=0x7f96c36f6000, nbytes=131072) at ../sysdeps/unix/sysv/linux/read.c:26
26      ../sysdeps/unix/sysv/linux/read.c: No such file or directory.

(gdb) info shared
From                To                  Syms Read   Shared Object Library
0x00007f96c354f330  0x00007f96c3696df9  Yes         /lib/x86_64-linux-gnu/libc.so.6
0x00007f96c3718050  0x00007f96c3737cbe  Yes         /lib64/ld-linux-x86-64.so.2

您没有定义close 的原因是它在libc.so.6 中定义,但是您的GDB 没有从libc.so.6 加载符号因为它未能附加进程。

如果没有进程,即使在主可执行文件中定义了 close,您也无法使用 call

【讨论】:

  • 谢谢我确实写错了 pid 但我仍然有没有这样的符号的问题。我试图加载一些额外的符号(gdb)文件 /lib64/ld-linux-x86-64.so.2 从 /lib64/ld-linux-x86-64.so.2 读取符号...但它看起来像我的发行版使用高级优化构建所有内容,并且没有调试符号,我认为这很好。也许我需要另一种方法来做到这一点
猜你喜欢
  • 2018-03-09
  • 2016-02-02
  • 2013-04-04
  • 1970-01-01
  • 2023-03-06
  • 2013-05-04
  • 1970-01-01
  • 2012-02-20
  • 2016-10-18
相关资源
最近更新 更多