在 gdb 会话中调用 malloc 失败
我正在尝试调试 C 程序,gdb 告诉我某个函数的第 329 行存在段错误。所以我为该函数设置了一个断点,并尝试单步执行它。然而,每当我到达第 68 行时,我都会收到 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 的调用在程序的其他执行期间显然会成功。当然,我有:
#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));
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
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>.
Here is the source code:
// 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));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以安全地忽略这一点。 gdb 抱怨它没有 malloc 的源代码 - 几乎可以肯定您不想单步执行源代码。
两个简单的解决方案:
使用
next
而不是step - 它不会下降到函数如果您已经不小心
单步
进入了某个函数,请使用finish
运行到该函数的 return 语句。另一种方法是:
您也可以在出现段错误之前稍微中断一下,而不是单步执行整个代码。
break
在特定行上放置断点来实现此目的(例如break foo.c:320
在 foo.c 的第 320 行处中断。break
在特定函数上中断(例如break foo
将在foo() 的顶部中断
函数)。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.
Two easy solutions:
Use
next
instead of step - it won't descend into functionsIf you've accidentally
step
ed into a function already, usefinish
to run to the return statement of the function.And an alternative approach:
You could also break a bit before the segfault, rather than stepping through the whole code.
break <source file>:<line num>
(for examplebreak foo.c:320
to break on line 320 of foo.c).break <function name>
(for examplebreak foo
will break at the top of thefoo()
function).