0

How do I get my programm to display the backtrace / caller when killed ?
I have an issue with an infinite loop in a gem that isn't mine and need to know where the issue is to report it

def hello
  puts 'hello'
end

def test
  while true
    sleep 2
    hello
  end
end

test

In this exemple, when a kill signal is sent to the programm, I wish to know what the programm what doing ( display the caller )
Currently all I get displayed is "Killed" on the output

3 Answers3

1

I guess you need to catch a signal, Did you try

https://gist.github.com/sauloperez/6592971

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141
1

It is not possible to catch sigkill signals as they are sent to the kernel and not the process : https://major.io/2010/03/18/sigterm-vs-sigkill/

you can, however, use a "parent" process that watches the "children" and will react accordingly SIGKILL signal Handler

This means that I cannot display the backtrace, I will probably have to use a log file or something like that.

I suppose anti-virus have a different way of working to avoid the issue of being killed

  • Speaking for linux. Only the user with real or effective user ID of the process or root can send a signal to a process. So every thing is allright for your anti-virus software. – slowjack2k May 30 '17 at 14:45
1

Taken from https://robots.thoughtbot.com/using-gdb-to-inspect-a-running-ruby-process

As a last resort you can use gdb to connect to your running ruby process gdb </path/to/ruby> <PID>

# inside ~/.gdbinit
define redirect_stdout
  call rb_eval_string("$_old_stdout, $stdout = $stdout,
    File.open('/tmp/ruby-debug.' + Process.pid.to_s, 'a'); $stdout.sync = true")
end

define ruby_eval
  call(rb_p(rb_eval_string_protect($arg0,(int*)0)))
end
slowjack2k
  • 2,566
  • 1
  • 15
  • 23