【问题标题】:How to set breakpoint in gdb when attach to another process附加到另一个进程时如何在gdb中设置断点
【发布时间】:2016-07-27 20:24:59
【问题描述】:

我有一个 C 程序,它编写了一个非常复杂的脚本来运行它。我需要使用gdb 调试这个程序。我尝试运行脚本并将 gdb 附加到它的进程,但是我无法设置我需要的断点:

$ gdb median.o 27944
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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 median.o...done.
Attaching to program: median.o, process 27944
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fc376e9cbfa in ?? ()
(gdb) break median.c:10
Cannot access memory at address 0x40059d

我也试过这个:

$gdb -p 28303
GNU gdb (Ubuntu 7.7.1-0ubuntu5~14.04.2) 7.7.1
Copyright (C) 2014 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-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://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".
Attaching to process 28303
Reading symbols from /bin/dash...(no debugging symbols found)...done.
Reading symbols from /lib/x86_64-linux-gnu/libc.so.6...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/libc-2.19.so...done.
done.
Loaded symbols for /lib/x86_64-linux-gnu/libc.so.6
Reading symbols from /lib64/ld-linux-x86-64.so.2...Reading symbols from /usr/lib/debug//lib/x86_64-linux-gnu/ld-2.19.so...done.
done.
Loaded symbols for /lib64/ld-linux-x86-64.so.2
0x00007fe918e50bfa in wait4 () at ../sysdeps/unix/syscall-template.S:81
81  ../sysdeps/unix/syscall-template.S: No such file or directory.
(gdb) break median.c:10
No source file named median.c.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (median.c:10) pending.
(gdb) continue
Continuing.
Please enter 3 numbers separated by spaces > 6 is the median
[Inferior 1 (process 28303) exited normally]

所以它会继续,不会在断点处停止。值得一提的是,如果我从 gdb 本身调用run median.o &lt; input,它可以工作。

如何在脚本运行的 C 程序上设置断点?

【问题讨论】:

  • 我知道这与this 主题有关,但它并没有解决我的问题
  • 你不需要给gdb传递可执行文件的名字,而不是目标文件吗?
  • @immibis 或者只是gdb -p [pid]
  • 它无法识别我的可执行文件:not in executable format: File format not recognized
  • @afsafzal 你运行了什么命令?

标签: c debugging gdb


【解决方案1】:

这是人们使用 gdb 时遇到的一个经典问题。它是如此常见,以至于您会认为它会有一个方便的名字!

这个问题有一些解决方案,一些经过时间考验,一些相对更具实验性。

    1234563 ,等劣势启动,再附加上去。您可以使用 PID 附加,在 gdb 提示符下使用 gdb -p PIDattach PID
  • 如果程序是短暂的,那么另一个经典的方法是在程序启动的早期添加对sleep的调用;比如说main 的第一行。然后,继续执行attach 计划。

这些是经典的方式。但现在让我们谈谈更有趣的事情。

gdb 有一个多下级模式,它可以一次调试多个进程。这种模式,IME,仍然有点脆弱,但我已经取得了一些成功。

首先将 gdb 置于正确的模式:

set detach-on-fork off
set non-stop off
set pagination off

(如果您有旧的 gdb,您还需要 set target-async on)。

现在您可以调试 shell,类似于:

$ gdb --args /bin/sh /path/to/my/script
(gdb) [... set the mode as above ...]
(gdb) break some_function_in_my_inferior

现在run 应该启动脚本,并自动将 gdb 附加到创建的每个子进程;最终停在断点处。

还有一种方法。很久以前,有一个内核补丁可以添加“全局断点”,还有一个 gdb 补丁可以使用此功能。据我所知,这些都没有合并。但是,我在 gdb helpers 项目中写了一个变体。

那里有一个名为preattach 的新命令。它的作用是使用 SystemTap 监视指定程序的exec;然后它会在 gdb 附加到它时暂停这个程序。

【讨论】:

  • 谢谢汤姆。这很有帮助。在这里修改源代码不是一个选项,所以我不能在main 中添加sleep()。第二个建议似乎更有趣。我试过了,似乎 gdb 已附加到子进程但它仍然存在,即使我没有设置任何断点:(gdb) run Starting program: /bin/sh test-median.sh [New process 28576] Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/libc-2.19.so...done. Reading symbols from /usr/lib/debug/lib/x86_64-linux-gnu/ld-2.19.so...done.
  • 我终于找到了让它工作的方法。因此,除了 Tom 建议的之外,我还需要切换下级并将命令从 gdb 发送到子进程。
  • @afsafzal 你能详细说明你做了什么吗?我似乎被困在同一个地方 (gdb) run 就像你在第一条评论中一样。
猜你喜欢
  • 2014-02-23
  • 1970-01-01
  • 1970-01-01
  • 2012-12-31
  • 2015-09-22
  • 2015-09-21
  • 2011-06-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多