iovisor / bcc

BCC - Tools for BPF-based Linux IO analysis, networking, monitoring, and more
Apache License 2.0
20.25k stars 3.84k forks source link

Stream Redirection Disables All Output #905

Open pnovotnak opened 7 years ago

pnovotnak commented 7 years ago

Issue

When standard output is redirected, the Python programs output nothing.

Details, Steps to Reproduce

I see this issue in all the executables I've tried (though haven't tested very many).

sudo tcpconnect -t  # works as expected
sudo tcpconnect -t | tee tcpconnect.txt  # not a peep

Seems odd... Source code shows output is happening via print() statements, and I don't see anything obvious in sudo strace -o strace.txt biolatency | tee /dev/null output (attached).

python -c 'print("test")' | tee /dev/null  # works as expected

Is this expected behavior?

I don't see anything obvious when tracing with strace.

Versions

strace-redirected.txt strace-normal.txt

redirected-normal.diff.txt

brendangregg commented 7 years ago

It's output-buffered. One way to disable it is:

sudo stdbuf -oL tcpconnect -t | tee tcpconnect.txt

Another way (among others) is to edit the first line to be: #!/usr/bin/python -u

Should we change the tools to do this by default? Hm. For some tools probably.

openjny commented 3 years ago

Hi, I'm a new-commer to eBPF and hit the same problem as @pnovotnak mentioned here. Though I tried the above workarounds, however, stdbuf doesn't work for me.

# This works as expected
python3 -u exitsnoop.py > exitsnoop.txt

# This didn't work: nothing will be recorded in exitsnoop.txt.
stdbuf -oL exitsnoop.py > exitsnoop.txt

Does anyone know a clue for understanding this differences? Thanks.