Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Is there a way to get a core dump (or something similar) for a process without actually killing the processes? I have a multithreaded python process running on an embedded system. And I want to be able to get a snapshot of the process under normal conditions (ie with the other processes required to be running), but I don't have enough memory to connect gdb (or run it under gdb) without the python process being the only one running.

I hope this question makes sense.

share|improve this question
    
If this is only while you're debugging, have you considered something crazy like swap on an NFS file or a network block device? –  Gilles Apr 12 '11 at 19:18
add comment

2 Answers 2

up vote 4 down vote accepted

The usual trick is to have something (possibly a signal like SIGUSR1) trigger the program to fork(), then the child calls abort() to make itself dump core.

from os import fork, abort
(...)
def onUSR1(sig, frame):
    if os.fork == 0:
        os.abort

and during initialization

from signal import signal, SIGUSR1
from wherever import onUSR1
(...)
signal.signal(signal.SIGUSR1, wherever.onUSR1)

Used this way, fork won't consume much extra memory because almost all of the address space will be shared (which is also why this works for generating the core dump).

Once upon a time this trick was used with a program called undump to generate an executable from a core dump to save an image after complex initialization; emacs used to do this to generate a preloaded image from temacs.

share|improve this answer
2  
Interesting. I wish there was a way to do this without modifying the running code (ie being able to do it without setup), but I doubt that's possible. This should be good enough for what we need. –  Falmarri Apr 21 '11 at 21:09
add comment

You could try using gcore. Is that an option for you?

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.