【问题标题】:Cannot set breakpoint at malloc()无法在 malloc() 处设置断点
【发布时间】:2014-03-30 05:17:48
【问题描述】:

代码示例(foo.c)

int main(){
 int *x=(int*)malloc(sizeof(int));  // break here
 *x=10;
 free(x);
 return 0;
}

我想在 malloc 上休息一下。这是我尝试的方法:

# gcc -g foo.c -o bar
# gdb bar
(gdb) b main
Breakpoint 1 at 0x80484cf: file src.c, line 7.
(gdb) r
Breakpoint 1, main () at src.c:7
(gdb) b malloc
Breakpoint 2 at 0x550944
(gdb) c
Program exited normally.

我的系统规格是:

  • 操作系统:CentOS 5.5
  • gcc:gcc (GCC) 4.1.2 20080704(红帽 4.1.2-52)
  • gdb:GNU gdb (GDB) Red Hat Enterprise Linux (7.0.1-42.el5)

请告诉我哪里出错了!!

【问题讨论】:

  • 找到malloc所在的行,在GDB里面输入break line_number
  • 我在调用 malloc 的行号处设置了一个断点,它似乎运行良好。
  • 这里(Fedora 20,gcc-4.8.2)它询问我是否在加载库时启用断点,并且工作正常。
  • 也许它被优化了?试试 -O0

标签: c linux gcc gdb malloc


【解决方案1】:

我无法可靠地重现该错误,但在 Linux 上,您可以尝试使用 __libc_malloc 而不是 malloc 进行中断。

【讨论】:

    【解决方案2】:

    已解决
    弄清楚了, 实际上断点设置在ld-linux.so而不是libc.so,可以使用:

    • b __malloc
    • b __libc_malloc

    感谢您的调查!!

    虽然一个新问题是: 如何让 gdb 仅在特定库中设置断点(我知道可以为文件完成)!!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-19
      • 2014-09-22
      • 2010-10-14
      • 1970-01-01
      • 2011-12-21
      • 2015-02-08
      相关资源
      最近更新 更多