popen and i/o redirection question

Donn Cave donn at u.washington.edu
Thu Mar 30 22:51:29 CEST 2000


Quoth "Charles G Waldman" <cgw at alum.mit.edu>:

| I am using the popen2 module to start an instance of "gdb".
| Then I send some gdb commands to set a few breakpoints and 
| start a program.
|
| Once the program starts, the standard output and standard error
| from that program come back to the top-level Python process
| through the same pipes as gdb's stdout and stderr.
|
| What I'd like to be able to do is to have the I/O to the debugging
| target on different pipes than the I/O to gdb itself, but I haven't
| been able to figure out quite how to do this.

Luckily, the "run" command in gdb supports redirection, it looks
just like the Bourne shell to me.

I'm going to write this and not check it.  Something along these
lines might work:

    class G:
        def __init__(self):
            pass
        def spawn(self, file):
            dw0, dw1 = os.pipe()
            aw0, aw1 = os.pipe()
            dr0, dr1 = os.pipe()
            ar0, ar1 = os.pipe()
            self.dfd = (dw0, dr1)
            self.afd = (aw0, ar1)
            self.pid = os.fork()
            if self.pid:
                os.close(dw1)
                os.close(aw1)
                os.close(dr0)
                os.close(ar0)
                os.write(self.dfd[1], 'break main\n')
                os.write(self.dfd[1], 'run <&%d >&%d 2>&1\n' % (ar0, aw1))
            else:
                try:
                    os.close(dw0)
                    os.close(aw0)
                    os.close(dr1)
                    os.close(ar1)
                    os.dup2(dr0, 0)
                    os.close(dr0)
                    os.dup2(dw1, 1)
                    os.close(dw1)
                    os.dup2(1, 2)
                    os.execve('/usr/local/bin/gdb', ('gdb', file), os.environ)
                except:
                    os._exit(117)

gdb = G()
gdb.spawn('a.out')
os.write(gdb.dfd[1], 'cont\n')
hello = os.read(gdb.afd[0], 2048)
print 'app says', repr(hello)
os.write(gdb.dfd[1], 'quit\n')
print 'gdb says', repr(os.read(gdb.dfd[0], 2048))

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list