我最近从Mac OS X 10.7升级到了10.9。由于OS X不再支持gdb,我通过macports安装了GNU gdb。在能够使用它之前,我必须像这里描述的那样对其进行代码签名。 现在我可以使用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()内部时,只需简单地发出继续命令即可。
如果我现在尝试附加到已经运行的进程上,我将无法设置断点,并且在发出继续命令以使进程在附加后继续运行时也会出现错误。以下是结果:
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 in ?? ())的任何信息,这可能是可以接受的,因为我很可能在sleep()函数内停止了进程。
但是,当我尝试在f()处设置断点时,我会收到“无法访问内存”的错误,并且明显未设置断点。实际上,当我给出继续命令时,进程会像平常一样继续运行并在不在f()处停止。
此外,在尝试继续操作时,我还会收到“Mach error at [...] (os/kern) failure (0x5)”错误。即使使用设置断点也无法正常工作。
(gdb) break a.c:5
Cannot access memory at address 0x100000edc
我试图在网上和stackoverflow上搜索,但是我没有找到解决这个问题的答案。非常感谢您的帮助。