gdb 在 malloc 中冻结 [英] gdb freezes in malloc

查看:10
本文介绍了gdb 在 malloc 中冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些这样的 C 程序:

Suppose I have some C program like this:

#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 我明白了:

and I attach to it with gdb like this gdb a.out PID. gdb successfully attaches to it but that I try to do something like call printf("bla bla bla") gdb freezes and if I press Ctrl^C I get this:

(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".

I suppose that this happens because my a.out was creating an object and acquired a lock inside malloc.c and in this moment I connected with gdb and tried to create string "bla bla bla" using malloc.

我的问题是如何检测到我在 malloc.c 中并让我的程序完成此执行?我不需要在命令行中执行此操作,而是使用某种 gdb 脚本(我只能使用 -ex 选项在 gdb 中执行命令).

My question is how can I detect that I'm inside malloc.c and let my program finish this execution? I need to do it not inside command line but using some sort of gdb scripting (I only can execute commands inside gdb with -ex option).

推荐答案

你被冻结的原因可能是你的程序持有的锁,并且 printf 也需要这个锁.当您尝试获取它两次时 - 您失败了.

The reason you're froze is probably a lock that's being held by your program, and is also required by printf. When you try to aquire it twice - you fail.

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

A possible WA is when breaking your program to call printf, just before you make the call, type finish - it will cause the current function to complete and return to the main frame. This will ensure the lock is free before you call printf.

这篇关于gdb 在 malloc 中冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆