Module: DebugSocket
- Defined in:
- lib/debug_socket.rb,
lib/debug_socket/version.rb
Constant Summary collapse
- VERSION =
"0.1.4".freeze
Class Method Summary collapse
Class Method Details
.backtrace ⇒ Object
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/debug_socket.rb', line 62 def self.backtrace pid = Process.pid "#{Time.now.utc.iso8601} #{$PROGRAM_NAME}\n" + Thread.list.map do |thread| output = "#{Time.now.utc.iso8601} pid=#{pid} thread.object_id=#{thread.object_id} thread.status=#{thread.status}" backtrace = thread.backtrace || [] output << "\n#{backtrace.join("\n")}" if backtrace output end.join("\n\n") end |
.logger ⇒ Object
13 14 15 16 17 |
# File 'lib/debug_socket.rb', line 13 def self.logger return @logger if defined?(@logger) require "logger" @logger = Logger.new(STDOUT) end |
.logger=(logger) ⇒ Object
9 10 11 |
# File 'lib/debug_socket.rb', line 9 def self.logger=(logger) @logger = logger end |
.start(path) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/debug_socket.rb', line 19 def self.start(path) pid = Process.pid raise "debug socket thread already running for this process" if @thread && @pid == pid @pid = pid # make sure socket is only accessible to the process owner old_mask = File.umask(0o0177) @path = path.to_s server = UNIXServer.new(@path) @thread = Thread.new do loop do begin socket = server.accept input = socket.read logger.warn("debug-socket-command=#{input.inspect}") if logger socket.puts(eval(input)) # rubocop:disable Security/Eval rescue Exception => e # rubocop:disable Lint/RescueException next unless logger logger.error("debug-socket-error=#{e.inspect} backtrace=#{e.backtrace.inspect}") ensure socket.close end end end logger.error("Debug socket listening on #{@path}") if logger @thread ensure File.umask(old_mask) if old_mask end |
.stop ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/debug_socket.rb', line 54 def self.stop @thread.kill if @thread File.unlink(@path) if @path && File.exist?(@path) @thread = nil @pid = nil @path = nil end |