为什么Ruby只在我的系统上抛出Segmentation故障,而且只在这个Rails应用程序中?

我不确定如何正确调试这个,但尝试了一些不同的方法,这些方法已经扼杀了时间,但没有解决问题。 我办公室中至少有4个人可以在相同的机器上执行此代码,并且安装了相同版本的ruby和rubygems且没有错误。

这是我正在执行的代码:

status = Open4::popen4( "swfmill simple stdin stdout" ) do | pid, stdin, stdout, stderr | stdin.write( config ) stdin.close bytes = stdout.read errors = stderr.read end 

我在这条线路上遇到“总线错误”:

 errors = stderr.read 

如果我评论该行,我会在此行中收到“Segementation Fault”:

 bytes = stdout.read 

我显然使用Open4库并执行外部进程。 这个外部进程在直接从终端执行时非常有效,并且当这个Ruby代码在这个特定的Rails应用程序之外运行时,即使在不同的,更简单的Rails应用程序中也是如此。

要重现错误,我只需要执行我的特定测试方法,如下所示:

 ruby test/unit/swf_generator_test.rb --name test_get_bytes 

我在配备Intel Core 2 Duo 2.53 GHz和8GB RAM的MacBook Pro上运行Snow Leopard Mac OS X 10.6.2(10C540)。

 ruby -v && gem -v ruby 1.8.7 (2009-06-12 patchlevel 174) [i686-darwin10.2.0] 1.3.5 

我在GDB中执行了这段代码,发现我的libxml和libxslt库都安装不正确。 我能够安装这两个库,现在GDB不再告诉我任何我认为有用的东西。 以下是GDB的当前输出:

 gdb ruby GNU gdb 6.3.50-20050815 (Apple version gdb-1344) (Fri Jul 3 01:19:56 UTC 2009) Copyright 2004 Free Software Foundation, Inc. GDB is free software, covered by the GNU General Public License, and you are welcome to change it and/or distribute copies of it under certain conditions. Type "show copying" to see the conditions. There is absolutely no warranty for GDB. Type "show warranty" for details. This GDB was configured as "x86_64-apple-darwin"...Reading symbols for shared libraries .... done (gdb) run test/unit/swf_generator_test.rb --name test_get_bytes Starting program: /usr/local/bin/ruby test/unit/swf_generator_test.rb --name test_get_bytes Reading symbols for shared libraries +++... done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries .. done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries ... done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries .. done Reading symbols for shared libraries . done Reading symbols for shared libraries .. done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries .... done Reading symbols for shared libraries . done Reading symbols for shared libraries . done Reading symbols for shared libraries ... done Loaded suite test/unit/swf_generator_test Started Reading symbols for shared libraries .. done Program received signal EXC_BAD_ACCESS, Could not access memory. Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff81973e90 0x0000000100045bf1 in gc_mark () (gdb) 

在更新了我的libxml和libxslt库之后,我还重建/重新安装了所有gem。 我这样做是通过运行:

 sudo gem pristine --all 

最后,这是我的控制台日志条目,用于此特定exception:

 Process: ruby [5059] Path: /usr/local/bin/ruby Identifier: ruby Version: ??? (???) Code Type: X86-64 (Native) Parent Process: bash [5050] Date/Time: 2010-01-21 11:31:45.468 -0800 OS Version: Mac OS X 10.6.2 (10C540) Report Version: 6 Exception Type: EXC_BAD_ACCESS (SIGABRT) Exception Codes: KERN_INVALID_ADDRESS at 0x000000000000000e Crashed Thread: 0 Dispatch queue: com.apple.main-thread Application Specific Information: abort() called Thread 0 Crashed: Dispatch queue: com.apple.main-thread 0 libSystem.B.dylib 0x00007fff819befe6 __kill + 10 1 libSystem.B.dylib 0x00007fff81a5fe32 abort + 83 2 libruby.dylib 0x000000010001b887 rb_check_type + 0 3 libruby.dylib 0x00000001000a5427 sigsegv + 84 4 libSystem.B.dylib 0x00007fff819d0eaa _sigtramp + 26 5 ??? 0x00007fff5fbd7410 0 + 140734799639568 6 libruby.dylib 0x0000000100045a50 mark_tbl + 44 7 libruby.dylib 0x0000000100045a71 rb_mark_tbl + 31 8 libruby.dylib 0x0000000100059286 mark_load_arg + 40 9 libruby.dylib 0x000000010004635f gc_mark_children + 1687 10 libruby.dylib 0x0000000100045c74 gc_mark + 221 11 libruby.dylib 0x0000000100045882 mark_locations_array + 61 12 libruby.dylib 0x00000001000459fc rb_gc_mark_locations + 59 13 libruby.dylib 0x0000000100047620 garbage_collect + 389 14 libruby.dylib 0x0000000100044c2d ruby_xmalloc + 107 15 libruby.dylib 0x0000000100033a76 scope_dup + 100 16 libruby.dylib 0x000000010002ab7d rb_yield_0 + 2646 17 libruby.dylib 0x0000000100034dbd proc_invoke + 1020 18 libruby.dylib 0x0000000100034f73 rb_proc_call + 39 19 libruby.dylib 0x000000010002e061 call_cfunc + 252 20 libruby.dylib 0x000000010002d16a rb_call0 + 1548 21 libruby.dylib 0x000000010002ebca rb_call + 711 22 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 23 libruby.dylib 0x00000001000251d0 rb_eval + 6859 24 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 25 libruby.dylib 0x000000010002ebca rb_call + 711 26 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 27 libruby.dylib 0x00000001000249fc rb_eval + 4855 28 libruby.dylib 0x000000010002716e rb_eval + 14953 29 libruby.dylib 0x000000010002a8f8 rb_yield_0 + 2001 30 libruby.dylib 0x0000000100034dbd proc_invoke + 1020 31 libruby.dylib 0x0000000100034f73 rb_proc_call + 39 32 libruby.dylib 0x000000010002e061 call_cfunc + 252 33 libruby.dylib 0x000000010002d16a rb_call0 + 1548 34 libruby.dylib 0x000000010002ebca rb_call + 711 35 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 36 libruby.dylib 0x00000001000251d0 rb_eval + 6859 37 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 38 libruby.dylib 0x000000010002ebca rb_call + 711 39 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 40 libruby.dylib 0x00000001000249fc rb_eval + 4855 41 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 42 libruby.dylib 0x000000010002ebca rb_call + 711 43 libruby.dylib 0x00000001000260fb rb_eval + 10742 44 libruby.dylib 0x0000000100025617 rb_eval + 7954 45 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 46 libruby.dylib 0x000000010002ebca rb_call + 711 47 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 48 libruby.dylib 0x00000001000270e5 rb_eval + 14816 49 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 50 libruby.dylib 0x000000010002ebca rb_call + 711 51 libruby.dylib 0x000000010002ed88 rb_f_send + 170 52 libruby.dylib 0x000000010002e080 call_cfunc + 283 53 libruby.dylib 0x000000010002d16a rb_call0 + 1548 54 libruby.dylib 0x000000010002ebca rb_call + 711 55 libruby.dylib 0x00000001000260fb rb_eval + 10742 56 libruby.dylib 0x0000000100024f5a rb_eval + 6229 57 libruby.dylib 0x00000001000251d0 rb_eval + 6859 58 libruby.dylib 0x00000001000251d0 rb_eval + 6859 59 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 60 libruby.dylib 0x000000010002ebca rb_call + 711 61 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 62 libruby.dylib 0x0000000100035764 block_pass + 460 63 libruby.dylib 0x000000010002473a rb_eval + 4149 64 libruby.dylib 0x000000010002a8f8 rb_yield_0 + 2001 65 libruby.dylib 0x000000010002ad2f rb_yield + 42 66 libruby.dylib 0x00000001000061e4 rb_ary_each + 100 67 libruby.dylib 0x000000010002e098 call_cfunc + 307 68 libruby.dylib 0x000000010002d16a rb_call0 + 1548 69 libruby.dylib 0x000000010002ebca rb_call + 711 70 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 71 libruby.dylib 0x00000001000249fc rb_eval + 4855 72 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 73 libruby.dylib 0x000000010002ebca rb_call + 711 74 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 75 libruby.dylib 0x0000000100035764 block_pass + 460 76 libruby.dylib 0x000000010002473a rb_eval + 4149 77 libruby.dylib 0x000000010002a8f8 rb_yield_0 + 2001 78 libruby.dylib 0x000000010002ad2f rb_yield + 42 79 libruby.dylib 0x00000001000061e4 rb_ary_each + 100 80 libruby.dylib 0x000000010002e098 call_cfunc + 307 81 libruby.dylib 0x000000010002d16a rb_call0 + 1548 82 libruby.dylib 0x000000010002ebca rb_call + 711 83 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 84 libruby.dylib 0x00000001000249fc rb_eval + 4855 85 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 86 libruby.dylib 0x000000010002ebca rb_call + 711 87 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 88 libruby.dylib 0x00000001000249fc rb_eval + 4855 89 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 90 libruby.dylib 0x000000010002ebca rb_call + 711 91 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 92 libruby.dylib 0x0000000100025617 rb_eval + 7954 93 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 94 libruby.dylib 0x000000010002ebca rb_call + 711 95 libruby.dylib 0x000000010002614c rb_eval + 10823 96 libruby.dylib 0x0000000100025617 rb_eval + 7954 97 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 98 libruby.dylib 0x000000010002ebca rb_call + 711 99 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 100 libruby.dylib 0x0000000100025617 rb_eval + 7954 101 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 102 libruby.dylib 0x000000010002ebca rb_call + 711 103 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 104 libruby.dylib 0x0000000100025b16 rb_eval + 9233 105 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 106 libruby.dylib 0x000000010002ebca rb_call + 711 107 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 108 libruby.dylib 0x000000010002dc1b rb_call0 + 4285 109 libruby.dylib 0x000000010002ebca rb_call + 711 110 libruby.dylib 0x0000000100025dc4 rb_eval + 9919 111 libruby.dylib 0x0000000100025f54 rb_eval + 10319 112 libruby.dylib 0x000000010002a8f8 rb_yield_0 + 2001 113 libruby.dylib 0x0000000100034dbd proc_invoke + 1020 114 libruby.dylib 0x0000000100032ba5 call_end_proc + 230 115 libruby.dylib 0x0000000100032eea rb_exec_end_proc + 559 116 libruby.dylib 0x000000010002051c ruby_finalize_0 + 170 117 libruby.dylib 0x00000001000205ab ruby_cleanup + 57 118 libruby.dylib 0x00000001000208c8 ruby_stop + 19 119 libruby.dylib 0x0000000100020937 compile_error + 0 120 ruby 0x0000000100000ebe dyld_stub_exit + 0 121 ruby 0x0000000100000e74 start + 52 Thread 0 crashed with X86 Thread State (64-bit): rax: 0x0000000000000000 rbx: 0x00007fff5fbd7318 rcx: 0x00007fff5fbd6a68 rdx: 0x0000000000000000 rdi: 0x00000000000013c3 rsi: 0x0000000000000006 rbp: 0x00007fff5fbd6a80 rsp: 0x00007fff5fbd6a68 r8: 0x0000000000000002 r9: 0x0000000000000000 r10: 0x00007fff819bb026 r11: 0x0000000000000202 r12: 0x0000000000000000 r13: 0x0000000000000000 r14: 0x0000000000000000 r15: 0x0000000000000000 rip: 0x00007fff819befe6 rfl: 0x0000000000000202 cr2: 0x00007fff702e71f0 Binary Images: 0x100000000 - 0x100000fff +ruby ??? (???)  /usr/local/bin/ruby 0x100003000 - 0x1000fefef +libruby.dylib ??? (???)  /usr/local/lib/libruby.dylib 0x1001e1000 - 0x1001e4fff +thread.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/thread.bundle 0x1001e8000 - 0x1001e9ff7 +etc.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/etc.bundle 0x1001ed000 - 0x1001f1ff7 +stringio.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/stringio.bundle 0x1001f5000 - 0x1001f9ff7 +strscan.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/strscan.bundle 0x1001fd000 - 0x1001fdfff +fcntl.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/fcntl.bundle 0x1004b0000 - 0x1004d0fff +syck.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/syck.bundle 0x1004d8000 - 0x1004e5ff7 +bigdecimal.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/bigdecimal.bundle 0x1004e9000 - 0x1004eaff7 +fast_xs.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/hpricot-0.8.2/lib/fast_xs.bundle 0x1004ed000 - 0x1004f5ff7 +zlib.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/zlib.bundle 0x1004fa000 - 0x1004fafff +md5.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/digest/md5.bundle 0x1004fd000 - 0x1004fdfff +sha1.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/digest/sha1.bundle 0x10073d000 - 0x10076efff +nkf.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/nkf.bundle 0x100781000 - 0x10078bfff +socket.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/socket.bundle 0x100790000 - 0x100791fff +digest.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/digest.bundle 0x100795000 - 0x1007a1fff +bluecloth_ext.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/bluecloth-2.0.5/lib/bluecloth_ext.bundle 0x1007a6000 - 0x1007e2ff7 +openssl.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/openssl.bundle 0x10123a000 - 0x1012abfff +redcloth_scan.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/RedCloth-4.2.2/lib/redcloth_scan.bundle 0x1012af000 - 0x1012b2ff7 +cparse.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/racc/cparse.bundle 0x1012b5000 - 0x1012b7ff7 +iconv.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/iconv.bundle 0x1012bb000 - 0x1012bffff +parser.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/json-1.1.3/ext/json/ext/parser.bundle 0x1012c3000 - 0x1012c7ff7 +generator.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/json-1.1.3/ext/json/ext/generator.bundle 0x1012cb000 - 0x1012dafff +nokogiri.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/nokogiri-1.4.1/lib/nokogiri/nokogiri.bundle 0x1012eb000 - 0x1012f2ff7 +ruby_debug.bundle ??? (???)  /Users/lbayes/.gem/ruby/1.8/gems/ruby-debug-base-0.10.3/lib/ruby_debug.bundle 0x1012f7000 - 0x1012f9fff +trace_nums.bundle ??? (???)  /Users/lbayes/.gem/ruby/1.8/gems/linecache-0.43/ext/trace_nums.bundle 0x101802000 - 0x101815fe7 +libexslt.0.dylib ??? (???)  /usr/local/lib/libexslt.0.dylib 0x10181b000 - 0x10185bff7 +libxslt.1.dylib ??? (???)  /usr/local/lib/libxslt.1.dylib 0x101867000 - 0x101869ff7 +readline.bundle ??? (???)  /usr/local/lib/ruby/1.8/i686-darwin10.0.0/readline.bundle 0x10186d000 - 0x101889ff7 libedit.2.dylib ??? (???)  /usr/lib/libedit.2.dylib 0x101898000 - 0x1018a6fff +sqlite3_api.bundle ??? (???)  /usr/local/lib/ruby/gems/1.8/gems/sqlite3-ruby-1.2.5/lib/sqlite3_api.bundle 0x103900000 - 0x103a83fe7 +libxml2.2.dylib ??? (???)  /usr/local/lib/libxml2.2.dylib 0x7fff5fc00000 - 0x7fff5fc3bdef dyld 132.1 (???)  /usr/lib/dyld 0x7fff808d1000 - 0x7fff808d2ff7 com.apple.TrustEvaluationAgent 1.1 (1)  /System/Library/PrivateFrameworks/TrustEvaluationAgent.framework/Versions/A/TrustEvaluationAgent 0x7fff81877000 - 0x7fff8196ffe7 libiconv.2.dylib ??? (???)  /usr/lib/libiconv.2.dylib 0x7fff81970000 - 0x7fff81b2eff7 libSystem.B.dylib ??? (???)  /usr/lib/libSystem.B.dylib 0x7fff833f5000 - 0x7fff834aefff libsqlite3.dylib ??? (???)  /usr/lib/libsqlite3.dylib 0x7fff85363000 - 0x7fff853e0fef libstdc++.6.dylib ??? (???)  /usr/lib/libstdc++.6.dylib 0x7fff853e1000 - 0x7fff853f2fef libz.1.dylib ??? (???)  /usr/lib/libz.1.dylib 0x7fff85cf9000 - 0x7fff85d38fef libncurses.5.4.dylib ??? (???)  /usr/lib/libncurses.5.4.dylib 0x7fff86662000 - 0x7fff86783fe7 libcrypto.0.9.8.dylib ??? (???)  /usr/lib/libcrypto.0.9.8.dylib 0x7fff87f25000 - 0x7fff87f5ffff libssl.0.9.8.dylib ??? (???)  /usr/lib/libssl.0.9.8.dylib 0x7fff886ad000 - 0x7fff886f9fff libauto.dylib ??? (???)  /usr/lib/libauto.dylib 0x7fff88894000 - 0x7fff8894afff libobjc.A.dylib ??? (???)  /usr/lib/libobjc.A.dylib 0x7fff88c99000 - 0x7fff88c9dff7 libmathCommon.A.dylib ??? (???)  /usr/lib/system/libmathCommon.A.dylib 0x7fffffe00000 - 0x7fffffe01fff libSystem.B.dylib ??? (???)  /usr/lib/libSystem.B.dylib 

好的,我已经在我的机器上修复了这个问题。 我不确定为什么,但将ruby-debug gem降级到版本0.10.0解决了我的问题。

具体修复程序正在运行以下命令:

 sudo gem uninstall ruby-debug sudo gem install ruby-debug --version 0.10.0 

我自己也遇到过类似的问题。

它通常是由具有本机编译位的gem创建的,它正在加载为不同体系结构编译的库。 (听起来你已经走过这条路了。)

不幸的是,这还包括编译为通用二进制文件的库。

在我的情况下,问题是由于Ruby加载了错误版本的OpenSSL引起的。 也许这个问题对你来说很相似。 基于上面的答案,听起来像ruby-debug导致问题。 它可以链接到错误的库吗?

以下是有关帮助我的问题的一些链接:

  • openssl,eventmachine和ruby 1.8.7(以及ree,也是)总线错误
  • gitjour,dnssd总线错误
  • (更多)RVM和MacPorts Hell
  • SSL! 中止陷阱! 哦天呐!

我没有解决方案,但我已经找到了重现此问题的方法。

今天安装ruby-debug-0.10.3 gem后,我的Rails 2.1.1测试调用了’stderr.read’

 Open4::popen4( "FOO" ) do | pid, stdin, stdout, stderr | stderr.read end 

块已经开始抛出相同的'[BUG] Bus Error ruby​​ 1.8.7(2009-06-12 patchlevel 174)[i686-darwin10.2.0]’你得到的。

如果我’sudo gem uninstall ruby​​-debug’,测试运行并通过。 如果我再次’sudo gem install ruby​​-debug’,测试就会抛出。

我在Snow Leopard上运行了一个Rails 2.1.1项目,安装了Ruby 1.8.7(2009-06-12 patchlevel 174)[i686-darwin10.2.0]。

为64位重建ruby-debug gem并不能解决这个问题。

听起来这些是硬件错误而不是软件错误。 检查您的机器,尤其是内存。

听起来你有错误的gem版本,或者它与MacOS不兼容。 来自RDoc :

Linux,Unix,BSD等用户需要unix包。 Ara的Open4 gem是一种依赖。 Ruby One-Click安装程序(OCI)的用户希望选择适合他们的Ruby版本的gem。 这里真正的问题是编译器版本。 由于VC ++编译器或库中的某些不兼容性,错误的版本将是SEGFAULT