0

I'm a new person in world of python and I've a problem with buffering (but I think is not exactly python problem). I want write my own debugger under linux. I need redirect stdin, stdout and stderr to different descriptor, because I want show output in my debugger console.

So code is:

mylib = CDLL("libc.so.6")
self.pid = os.fork()

if (self.pid == 0):
        c = os.open("myfile", 1, 0)
        os.dup2(c,0)           
        os.dup2(c,1)
        os.dup2(c,2)
        mylib.ptrace(ptraceRequest.PTRACE_TRACEME, 0, 0, 0)

        os.execl("Fib.out", "Fib")
        sys.exit()
    else:
        print(self.pid)

The problem is in execl. I don't know why, but streams are buffered (or I think that buffering is source of my problem :) ). So my testing program Fib.out (it display first five value of fibonacci sequence with 5 sec break between values) is visible only after Fib.out is finished.

I changed third argument in os.open - documentations says that 0 as third argument means no buffering, but this don't work for me. When output is redirected on /dev/pts/12 - everything works fine. Its looks for me like the problem can be connected with default value of streams buffer.

In C we have 'setvbuf', but in python I can't find nothing what will be similar - I only can found information that I should use os.open with 0 or 1 value (as third argument)

Maybe it is wrong way of thinking about problem? Do you know any better idea to show program output in my own console?

Mariusz
  • 349
  • 2
  • 7
  • If Fib.out is written in python, does this help? [stackoverflow.com: python output buffering](http://stackoverflow.com/questions/107705/python-output-buffering). If it's written in some other language, let us know. If you can't modify fib.out, the solution is probably to open a pty and have the process read and write that. – Mark Plotnick Apr 14 '14 at 18:20
  • Hey, thanks for your comments, it was very inspiring and I found solution my problem. I've used pty lib https://docs.python.org/2/library/pty.html and I've changed os.fork on pty.fork. Now I can read/write by using fd which I got from pty.fork(). – Mariusz Apr 15 '14 at 10:47

0 Answers0