在 gdb 会话中调用 malloc 失败 [英] Call to malloc failing in gdb session
问题描述
我正在尝试调试 C 程序,而 gdb 告诉我某个函数的第 329 行存在段错误.因此,我为该函数设置了一个断点,并尝试逐步完成它.但是,每当我点击第 68 行时,我都会从 gdb 收到此投诉:
I am trying to debug a C program and gdb is telling me there is a segfault on line 329 of a certain function. So I set a break point for that function and I am trying to step through it. However, whenever I hit line 68 I get this complaint from gdb:
(gdb) step
68 next_bb = (basic_block *)malloc(sizeof(basic_block));
(gdb) step
*__GI___libc_malloc (bytes=40) at malloc.c:3621
3621 malloc.c: No such file or directory.
in malloc.c
我不知道这是什么意思.该程序在除一组输入之外的所有输入上都能完美运行,因此对 malloc 的调用在程序的其他执行期间显然成功.当然,我有:
I don't know what this means. The program runs perfectly on all but one set of inputs so this call to malloc clearly succeeds during other executions of the program. And, of course, I have:
#include <stdlib.h>.
这里是源代码:
// Block currently being built.
basic_block *next_bb = NULL;
// Traverse the list of instructions in the procedure.
while (curr_instr != NULL)
{
simple_op opcode = curr_instr->opcode;
// If we are not currently building a basic_block then we must start a new one.
// A new block can be started with any kind of instruction.
if (!in_block)
{
// Create a new basic_block.
next_bb = (basic_block *)malloc(sizeof(basic_block));
推荐答案
你可以放心地忽略这个.gdb 抱怨它没有 malloc 的源代码 - 几乎可以肯定你不想单步执行源代码.
You can safely ignore this. gdb is complaining that it doesn't have the source for malloc - and it's almost certain you don't want to step through the source.
两个简单的解决方案:
使用
next
而不是 step - 它不会下降到函数中
Use
next
instead of step - it won't descend into functions
如果你已经不小心step
进入了一个函数,使用finish
运行到函数的return语句.
If you've accidentally step
ed into a function already, use finish
to run to the return statement of the function.
还有一种替代方法:
您也可以在段错误之前中断一点,而不是单步执行整个代码.
You could also break a bit before the segfault, rather than stepping through the whole code.
- 您可以通过使用
break <source file>:<line num>
在特定行设置断点来实现(例如break foo.c:320
在 foo.c) 的第 320 行中断. - 或者您可以使用
break <function name>
中断特定函数(例如break foo
将在foo() 的顶部中断
函数).
- You can do this by putting a breakpoint on a particular line with
break <source file>:<line num>
(for examplebreak foo.c:320
to break on line 320 of foo.c). - Or you can break on a particular function with
break <function name>
(for examplebreak foo
will break at the top of thefoo()
function).
这篇关于在 gdb 会话中调用 malloc 失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!