Archive for September, 2010

Capture input/output of a process with gdb

Tuesday, September 14th, 2010

My tip of day. When you lose control of a process (for example with a process launched with disown or, another example, when you close your SSH terminal with “unsynced” mutt), thanks gdb.

$ tty
/dev/pts/13
$ gdb --pid=`pidof mutt`
(gdb) call close(0)
(gdb) call close(1)
(gdb) call close(2)
(gdb) call open("/dev/pts/13", 2, 0)
(gdb) call dup(0)
(gdb) call dup(0)
(gdb) detach
Detaching from program: /usr/bin/mutt-org, process 2932

Thanks to this comment on superuser.com for the original idea.

Edit: correction on dup() calls. Thanks to Peter Pentchev for his explanation in comments.