From: barabba Date: 2007-04-11T04:25:11+09:00 Subject: Re: Ruby C extension crash: how to create a stack trace? On Apr 9, 11:44 pm, Ryan Davis wrote: > On Apr 6, 2007, at 11:20 , barabba wrote: > > > Hi, > > > I am using ruby to test and develop my legacy C code. I packed it > > inside a ruby extension as dll under windows. I am using visual > > studio 6 to debug my extension. > > The process works very well, but when I make a mistake in my C code > > ruby abort without stack trace and it is difficult for me to find the > > reason. > > I haven't done any MS development in over a decade (happily), so > maybe I'm just missing something. This is how I get a BT on OSX/ > FreeBSD when I'm debugging my extensions: > > % gdb ruby > > r my_script.rb my_args > ... go boom > > bt > .. backtrace with hopefully useful information > > Can you not do that from the MS debugger??? Yes it is fast the same. MSVC start and run ruby using my_script.rb as argument. The problem is the "backtrace", it is only one line, because ruby don't crash, just exit. I have solved my problem directly in the C part using Structured Exception Handling(SEH) of win32. Now my extension looks like: void foo_thread_handler() { __try{ while(1) { // a thread task mystruct* pCrash = 0; // null pointer usage pCrash->len = 555; // BOOM do_realstuff(); // do real stuff // call a function inside ruby to get back to the scheduler rb_something() } } __except ( EXCEPTION_EXECUTE_HANDLER ) { printf("Boom recognized\n"); } } This solution works only under MSVC compiler, but it is ok for me. It is still not clear why trap("SEGV"){"my seg fault handler";exit} doesn't work on my system. It seam that the exception is caught, but the C code is recalled at the same place where the error is generated causing an infinite loop (may be an EXCEPTION_CONTINUE_EXECUTION is used in the trap handler?).