unknown node type 0, called on terminated object, SEGV など、GC のタイ
ミングに依存して起こる問題はなかなか修正するのが難しいので、GC の情報
をもっと記録するパッチを書いてみました。

* GC が何回起きたか
* GC されたオブジェクトが何回目の GC で回収されたか
* 最後の GC がどこで起きたか (要 gcc, platform にも依存?)

という情報を記録します。

また、called on terminated object が起きたら NotImplementedError じゃ
なくて即座に core を吐いてそれらの情報を保全し、ついでに SIGSEGV,
SIGBUS が来た時の signal handler は stack を微妙に壊してしまうことがあ
るので除去してあります。

GC の問題で悩んでいる人がいれば、試してみるといいかもしれません。
まぁ、このパッチ自体が問題を解決するわけじゃありませんが。

Index: eval.c
===================================================================
RCS file: /src/ruby/eval.c,v
retrieving revision 1.747
diff -u -p -r1.747 eval.c
--- eval.c	16 Dec 2004 15:01:48 -0000	1.747
+++ eval.c	23 Dec 2004 11:10:54 -0000
@@ -3865,7 +3865,7 @@ rb_eval(self, n)
 	break;
 
       default:
-	rb_bug("unknown node type %d", nd_type(node));
+	rb_bug("unknown node type %d (0x%lx)", nd_type(node), (long)node);
     }
   finish:
     CHECK_INTS;
@@ -5741,8 +5741,8 @@ rb_call(klass, recv, mid, argc, argv, sc
     struct cache_entry *ent;
 
     if (!klass) {
-	rb_raise(rb_eNotImpError, "method `%s' called on terminated object (0x%lx)",
-		 rb_id2name(mid), recv);
+	rb_bug("method `%s' called on terminated object (0x%lx)",
+               rb_id2name(mid), recv);
     }
     /* is it in the method cache? */
     ent = cache + EXPR1(klass, mid);
Index: gc.c
===================================================================
RCS file: /src/ruby/gc.c,v
retrieving revision 1.194
diff -u -p -r1.194 gc.c
--- gc.c	20 Dec 2004 13:55:00 -0000	1.194
+++ gc.c	23 Dec 2004 11:10:54 -0000
@@ -284,6 +284,7 @@ typedef struct RVALUE {
 	struct {
 	    unsigned long flags;	/* always 0 for freed obj */
 	    struct RVALUE *next;
+            long gc_count;
 	} free;
 	struct RBasic  basic;
 	struct RObject object;
@@ -308,6 +309,7 @@ typedef struct RVALUE {
 #endif
 } RVALUE;
 
+static long gc_count = 0;
 static RVALUE *freelist = 0;
 static RVALUE *deferred_final_list = 0;
 
@@ -370,6 +372,7 @@ add_heap()
     while (p < pend) {
 	p->as.free.flags = 0;
 	p->as.free.next = freelist;
+	p->as.free.gc_count = gc_count;
 	freelist = p;
 	p++;
     }
@@ -1004,6 +1007,7 @@ finalize_list(p)
 	if (!FL_TEST(p, FL_SINGLETON)) { /* not freeing page */
 	    p->as.free.flags = 0;
 	    p->as.free.next = freelist;
+            p->as.free.gc_count = gc_count;
 	    freelist = p;
 	}
 	p = tmp;
@@ -1038,6 +1042,7 @@ gc_sweep()
     unsigned long live = 0;
 
     mark_source_filename(ruby_sourcefile);
+    if (source_filenames)
     st_foreach(source_filenames, sweep_source_filename, 0);
 
     freelist = 0;
@@ -1057,6 +1062,7 @@ gc_sweep()
 		if (need_call_final && FL_TEST(p, FL_FINALIZE)) {
 		    p->as.free.flags = FL_MARK; /* remain marked */
 		    p->as.free.next = final_list;
+                    p->as.free.gc_count = gc_count;
 		    final_list = p;
 		}
 		else {
@@ -1113,6 +1119,7 @@ rb_gc_force_recycle(p)
 {
     RANY(p)->as.free.flags = 0;
     RANY(p)->as.free.next = freelist;
+    RANY(p)->as.free.gc_count = -1;
     freelist = RANY(p);
 }
 
@@ -1288,6 +1295,11 @@ int rb_setjmp (rb_jmp_buf);
 #endif /* __human68k__ or DJGPP */
 #endif /* __GNUC__ */
 
+#ifdef __GNUC__
+void *main_return_address;
+static void *last_gc_stacktrace[10];
+#endif
+
 static void
 garbage_collect()
 {
@@ -1310,6 +1322,25 @@ garbage_collect()
     if (during_gc) return;
     during_gc++;
 
+    gc_count++;
+    if (gc_count < 0)
+        gc_count = 0;
+
+#ifdef __GNUC__
+    memset(last_gc_stacktrace, 0, sizeof(last_gc_stacktrace));
+    if ((last_gc_stacktrace[0] = __builtin_return_address(0)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[1] = __builtin_return_address(1)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[2] = __builtin_return_address(2)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[3] = __builtin_return_address(3)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[4] = __builtin_return_address(4)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[5] = __builtin_return_address(5)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[6] = __builtin_return_address(6)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[7] = __builtin_return_address(7)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[8] = __builtin_return_address(8)) == main_return_address) goto gc_stacktrace_done;
+    if ((last_gc_stacktrace[9] = __builtin_return_address(9)) == main_return_address) goto gc_stacktrace_done;
+    gc_stacktrace_done: ;
+#endif
+
     init_mark_stack();
     
     /* mark frame stack */
Index: main.c
===================================================================
RCS file: /src/ruby/main.c,v
retrieving revision 1.13
diff -u -p -r1.13 main.c
--- main.c	23 Jun 2004 12:59:01 -0000	1.13
+++ main.c	23 Dec 2004 11:10:54 -0000
@@ -21,6 +21,10 @@
 static void objcdummyfunction( void ) { objc_msgSend(); }
 #endif
 
+#ifdef __GNUC__
+extern void *main_return_address;
+#endif
+
 int
 main(argc, argv, envp)
     int argc;
@@ -31,6 +35,10 @@ main(argc, argv, envp)
 #endif
 #if defined(__MACOS__) && defined(__MWERKS__)
     argc = ccommand(&argv);
+#endif
+
+#ifdef __GNUC__
+    main_return_address = __builtin_return_address(0);
 #endif
 
     ruby_init();
Index: signal.c
===================================================================
RCS file: /src/ruby/signal.c,v
retrieving revision 1.55
diff -u -p -r1.55 signal.c
--- signal.c	30 Nov 2004 17:28:16 -0000	1.55
+++ signal.c	23 Dec 2004 11:10:54 -0000
@@ -878,10 +878,10 @@ Init_signal()
 #endif
 
 #ifdef SIGBUS
-    install_sighandler(SIGBUS, sigbus);
+    /*install_sighandler(SIGBUS, sigbus);*/
 #endif
 #ifdef SIGSEGV
-    install_sighandler(SIGSEGV, sigsegv);
+    /*install_sighandler(SIGSEGV, sigsegv);*/
 #endif
 #ifdef SIGPIPE
     install_sighandler(SIGPIPE, sigpipe);

-- 
[田中 哲][たなか あきら][Tanaka Akira]