gpt4 book ai didi

c - gdb 在 malloc 中卡住

转载 作者:太空狗 更新时间:2023-10-29 11:03:38 25 4
gpt4 key购买 nike

假设我有这样的 C 程序:

#include <stdlib.h>
#include <stdbool.h>

int main()
{
while (true) {
void *p = malloc(1000);
free(p);
}
return 0;
}

然后我用 gdb 附加到它,就像这样 gdb a.out PIDgdb 成功附加到它,但我尝试做类似 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 选项执行命令)。

最佳答案

你被卡住的原因可能是你的程序持有一个锁,printf 也需要它。当你尝试获取它两次时 - 你失败了。

一个可能的 WA 是当中断你的程序以调用 printf 时,就在你调用之前,键入 finish - 它会导致当前函数完成并返回到主框架。这将确保在您调用 printf 之前锁是空闲的。

关于c - gdb 在 malloc 中卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39124817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com