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).
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…