我试图使用GDB调试一个简单的停止和复制垃圾回收器(用C编写). GC通过处理SIGBUS工作.我在我的SIGBUS信号处理程序的顶部设置了一个断点.我告诉GDB将SIGBUS传递给我的程序.但是,它似乎没有起作用.
以下程序(内联解释)显示了我的问题的本质:
#include <stdio.h> #include <sys/mman.h> #include <assert.h> #include <signal.h> #define HEAP_SIZE 4096 unsigned long int *heap; void gc(int n) { signal(SIGBUS,SIG_DFL); // just for debugging printf("GC TIME\n"); } int main () { // Allocate twice the required heap size (two semi-spaces) heap = mmap(NULL,HEAP_SIZE * 2,PROT_READ | PROT_WRITE,MAP_ANON | MAP_SHARED,-1,0); assert (heap != MAP_Failed); // 2nd semi-space is unreadable. Using "bump-pointer allocation",a SIGBUS // tells us we are out of space and need to GC. void *guard = mmap(heap + HEAP_SIZE,HEAP_SIZE,PROT_NONE,MAP_ANON | MAP_SHARED | MAP_FIXED,0); assert (guard != MAP_Failed); signal(SIGBUS,gc); heap[HEAP_SIZE] = 90; // pretend we are out of heap space return 0; }
我在Mac OS X 10.6上编译并运行程序,并获得我期望的输出:
$gcc debug.c $./a.out GC TIME Bus error
我想使用GDB运行和调试这个程序.特别是,我想在gc函数中设置一个断点(真的是gc信号处理程序).当然,我需要告诉GDB也不要停止SIGBUS:
$gdb ./a.out GNU gdb 6.3.50-20050815 (Apple version gdb-1346) (Fri Sep 18 20:40:51 UTC 2009) ... snip ... (gdb) handle SIGSEGV SIGBUS nostop noprint Signal Stop Print Pass to program Description SIGBUS No No Yes Bus error SIGSEGV No No Yes Segmentation fault (gdb) break gc Breakpoint 1 at 0x100000d6f
但是,我们从来没有达到断点:
(gdb) run Starting program: /snip/a.out Reading symbols for shared libraries +. done Program received signal EXC_BAD_ACCESS,Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x0000000100029000 0x0000000100000e83 in main () (gdb)
显然,不调用信号处理程序(GC TIME未打印).此外,我们仍然在main(),在错误的mov:
0x0000000100000e83 <main+247>: movq $0x5a,(%rax)
有任何想法吗?
谢谢.
解决方法
相同的代码(修改为处理SIGSEGV)也可以像在Linux上的GDB一样工作;它可能是OS X或GDB到该平台的端口的错误.
Googling发现broken OS X behavior就像您一直在10.1上一样,使用一种解决方法(在运行程序之前设置inferior-bind-exception-port).
(有一个similar bug on Windows.)