【发布时间】:2016-12-31 16:16:15
【问题描述】:
假设我有一些这样的 C 程序:
#include <stdlib.h>
#include <stdbool.h>
int main()
{
while (true) {
void *p = malloc(1000);
free(p);
}
return 0;
}
我用gdb 附加到它,就像gdb a.out PID 这样。 gdb 成功附加到它,但我尝试做类似call printf("bla bla bla") gdb 冻结,如果我按Ctrl^C 我得到这个:
(gdb) call printf("bla bla bla")
^C
Program received signal SIGINT, Interrupt.
__lll_lock_wait_private () at ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S:95
95 ../nptl/sysdeps/unix/sysv/linux/x86_64/lowlevellock.S: No such file or directory.
The program being debugged was signaled while in a function called from GDB.
GDB remains in the frame where the signal was received.
To change this behavior use "set unwindonsignal on".
Evaluation of the expression containing the function
(malloc) will be abandoned.
When the function is done executing, GDB will silently stop.
我想这是因为我的a.out 正在创建一个对象并在malloc.c 中获得了一个锁,此时我与gdb 连接并尝试使用malloc 创建字符串“bla bla bla”。
我的问题是如何检测到我在malloc.c 中并让我的程序完成此执行?我不需要在命令行中执行此操作,而是使用某种 gdb 脚本(我只能在 gdb 中使用 -ex 选项执行命令)。
【问题讨论】:
-
这对我有用...
printfcompleted。我尝试运行gdb ./sample.out并附加到正在运行的进程。 -
@IshayPeled 我认为你很幸运,
sample.out目前还没有执行malloc。您可以尝试重复多次。 -
从 gdb 调用函数是一种可疑的做法,尤其是从标准库中。一个原因是你刚刚经历的。在调试时,您希望程序处于静态状态以便您可以检查它,调用可能改变状态的函数对于调试来说适得其反。我建议找到一种不同的方法来解决您的问题。
-
@Art 但据我所知,在
gdb中调用一些函数是我可以在运行进程中注入一些代码的唯一方法。你知道更好的方法吗? -
@Dima 对 - 我尝试了 10 次,但最终还是被挂了。请参阅我的答案以了解可能的 WA