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

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

问题描述

假设我有这样的C程序:

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

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

我附加到 gdb 像这样 gdb a.out PID gdb 成功附加到它,但我试图做一些类似于 call printf(bla bla bla) gdb 冻结,如果我按 Ctrl ^ C 我得到这个:



< (gdb)调用printf(bla bla bla)
^ C
编程接收到的信号SIGINT,中断。
__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:没有这样的文件或目录。
正在调试的程序在从GDB调用的函数中发出信号。
GDB保留在收到信号的帧中。
要改变这种行为,请使用set unwindonsignal on。
对包含函数
(malloc)的表达式的评估将被废弃。
当函数完成执行时,GDB将静静地停止。

我想这是因为我的 a.out 正在创建一个对象,并在 malloc.c 中获得了一个锁,并且在这一刻我连接了 gdb 并尝试使用 malloc 创建字符串bla bla bla。



我的问题是如何检测到我在 malloc.c 中并让我的程序完成这个执行?我需要做的不是在命令行中,而是使用某种类型的gdb脚本(我只能用 -ex gdb 内执行命令

解决方案

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



一个可能的WA是当你的程序打破 call printf ,在进行调用之前,输入 finish - 它会导致当前函数完成并返回到主框架。这将确保在您调用printf之前锁定是免费的。


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;
}

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.

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.

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).

解决方案

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.

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天全站免登陆