c - Mac OS X Mavericks - gdb - 附加到进程时无法设置断点

标签 c macos debugging gdb attach-to-process

我最近从 Mac OS X 10.7 升级到 10.9。由于 OS X 不再支持 gdb,我通过 macports 安装了 GNU gdb。在能够使用它之前,我必须按照描述对其进行代码设计 here . 现在我可以使用 gdb 作为调试器,但是在附加到进程时我在设置断点时遇到了问题。我会为你做一个例子。 我拿了这个示例 C 代码

#include <unistd.h>
#include <stdio.h>

void f() {
    printf("f()\n");
}

int main() {
    printf("sleeping 30 seconds...\n");
    sleep(30);
    printf("invoking f()\n");
    f();
}

并编译它

gcc -g a.c

如果我现在尝试调试

gdb a.out

并在 gdb 中运行它,结果如下(如预期的那样)

GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin13.0.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /private/tmp/a.out...Reading symbols from /private/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
(gdb) break f
Breakpoint 1 at 0x100000ee0: file a.c, line 5.
(gdb) r
Starting program: /private/tmp/a.out
sleeping 30 seconds...
invoking f()

Breakpoint 1, f () at a.c:5
5               printf("f()\n");
(gdb) c
Continuing.
f()
[Inferior 1 (process 78776) exited with code 012]
(gdb)

所以我所做的是打开 gdb,在函数 f() 处设置断点,运行程序,然后在 f() 内停止时简单地发出继续命令。 如果我现在做同样的事情但附加到已经运行的进程,我不能设置断点,并且在发出 continue 命令使进程在附加后继续时我也会收到错误。这是结果:

GNU gdb (GDB) 7.6
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin13.0.0".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
(gdb) attach 78794
Attaching to process 78794
Reading symbols from /private/tmp/a.out...Reading symbols from /private/tmp/a.out.dSYM/Contents/Resources/DWARF/a.out...done.
done.
0x00007fff8516da3a in ?? ()
(gdb) break f
Cannot access memory at address 0x100000edc
(gdb) c
Continuing.
warning: Mach error at "darwin-nat.c:726" in function "void darwin_resume_thread(struct inferior *, darwin_thread_t *, int, int)": (os/kern) failure (0x5)
[Inferior 1 (process 78794) exited normally]
(gdb)

因此,首先,在附加时,gdb 不会提供有关代码中当前位置的任何信息(?? () 中的 0x00007fff8516da3a),这可能没问题,因为我很可能在进程处于 sleep 状态时停止了该进程() 函数。

但是,当我尝试在 f() 处设置断点时,出现“无法访问内存”错误,断点显然没有设置。事实上,当我发出 continue 命令时,该过程只是照常进行并终止而不会在 f() 处停止。

此外,在尝试继续时,我还会收到“Mach error at [...] (os/kern) failure (0x5)”。即使使用

设置断点也不起作用
(gdb) break a.c:5
Cannot access memory at address 0x100000edc

我也尝试在网络和 stackoverflow 上进行搜索,但没有找到该问题的答案。 感谢您的帮助。

最佳答案

我找到了解决问题的(不是很满意)解决方案。使用 MacPorts,我安装了苹果版的 GDB:

sudo port install gdb-apple

此版本的 GDB 能够附加到进程并正确设置断点,甚至在 Eclipse CDT 中也是如此。我发现的唯一缺陷是,在 Eclipse 中,我需要在附加到进程之前设置断点。如果我尝试在已经附加的情况下设置断点,则该过程将失败。我不知道为什么。

我希望这个答案可以帮助到别人,如果你们中的任何人找到更好的解决方案,我将很高兴知道。

关于c - Mac OS X Mavericks - gdb - 附加到进程时无法设置断点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21477094/

相关文章:

android - 我的 Android 设备未在 Android Studio 上显示

jquery - 隔离 jQuery FF4 问题

c - 将 config.txt 数据保存在双向链表中

php 扩展 >> 错误

C 列表段错误

linux - grep -xf 与预期输出不匹配,可能是一个错误

c - 在 C 中获取 GetTickCount() 时遇到问题

macos - 每次启动Matlab时如何设置一个文件夹为当前文件夹?

ios - 图形上下文的概念与文件句柄非常相似吗? (在 iOS 和其他系统上)

c++ - 在移动的 lambda 中捕获和调试对局部变量引用的无效使用