How can I dump the code (and not the objects / stack) of a running Ruby process?

I just wrote a scraper process. This is a separate file program - it loads ActiveRecord, creates a pair of models, defines a pair of scrambling functions and then starts in a loop.

Unfortunately, I messed up the git command and now the file itself is gone.

However, I have one instance of the scraper that is currently working. I sent SIGSTOP to prevent possible errors.

How can I connect to a process and make it dump code so that I can copy it back to a .rb file?

I do not need its stack trace or other random objects in RAM; they are completely unimportant, as they are written to be killed at any time without loss. I just want not to rewrite this damn thing.

https://github.com/ileitch/hijack it seems that it will allow me to attach a debugger to the running process, which is a good first step. But if I do, then what?

FWIW, I was able to restore ~ ​​60 lines using puts Readline::HISTORY.to_a.reverse.uniq.reverse from the irb console in which I played with the code. But I would prefer everything if I can.

Thanks!

ETA: https://github.com/ngty/sourcify is another possible link.

ETA2: ... phew, Dropbox caught him. You should have thought to check it out earlier. > & L;

So, no longer critical, but still curious if this can be done.

+4
source share
2 answers

One thing to do is grab a copy of the kernel and save it for future analysis, although how to do it depends on your OS.

On Linux, the memory allocated for the current process is accessible through /proc/###/mem , where ### represents the process PID.

I'm not sure if the Ruby source code is actually saved, it was compiled into bytecode before it is executed, but you can get its version using ruby2ruby gem.

+1
source

All you have to do is find the name of the file associated with the ruby ​​program, and then read the contents of this file using File.read(file_name)

-one
source

Source: https://habr.com/ru/post/1391959/


All Articles