如何將信號發送到ruby進程,併爲轉儲所有堆棧的信號創建處理程序?
從http://le-huy.blogspot.com/2012/04/dump-backtrace-of-all-threads-in-ruby.html我們有這個例子:
require 'pp'
def backtrace_for_all_threads(signame)
File.open("/tmp/ruby_backtrace_#{Process.pid}.txt","a") do |f|
f.puts "--- got signal #{signame}, dump backtrace for all threads at #{Time.now}"
if Thread.current.respond_to?(:backtrace)
Thread.list.each do |t|
f.puts t.inspect
PP.pp(t.backtrace.delete_if {|frame| frame =~ /^#{File.expand_path(__FILE__)}/},
f) # remove frames resulting from calling this method
end
else
PP.pp(caller.delete_if {|frame| frame =~ /^#{File.expand_path(__FILE__)}/},
f) # remove frames resulting from calling this method
end
end
end
Signal.trap(29) do
backtrace_for_all_threads("INFO")
end
然後我們需要將信號發送到相應的進程:
ps afxw | grep ruby
kill -29 <pid>
ls -l /tmp/ruby*
vi /tmp/ruby_backtrace_...
重複在適當的採樣時間的信號。
'p caller'或'puts caller.inspect'會給你一個更好的輸出格式。 – 2012-12-09 18:36:46
'puts caller.join(「\ n」)'正常工作 – fotanus 2013-05-22 18:57:49
不需要'.join(「\ n」)' – Nakilon 2014-04-17 23:47:42