繁体   English   中英

附加到另一个进程时如何在gdb中设置断点

[英]How to set breakpoint in gdb when attach to another process

我有一个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 < input ,它就可以工作。

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

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

有一些解决问题的方法,有些经过了时间的考验,而有些则相对更具实验性。

  • 如果要调试的程序(在gdb术语中为“劣等”)是长时间运行的(例如,GUI或某种类型的服务器),那么最简单的方法是仅运行脚本,等待劣等开始,然后附加到它。 您可以使用PID进行附加,既可以使用gdb -p PID进行attach PID ,也可以在gdb提示符下使用attach 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帮助程序项目中编写了一个变体。

那里有一个称为preattach的新命令。 它所做的是使用的SystemTap看一个exec指定的程序的; 然后在gdb附加到该程序时暂停该程序。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM