r/linux_programming • • Jul 07 '23

Binary dies on using tee

So I have this program written in C++ that outputs some statistics on standard op. I need to capture this in another file. For this I have been using the command: ./my_bin | tee abc.txt.

I recently got a new machine running Ubuntu 22.04 with kernel 5.15.0-75-generic but now my binary dies when I run it with tee, > works fine only tee causes the crash.

Running with GDB shows segfault in pthread_get_name_np but the same binary runs fine on another machine. I don't know how to debug this ? Can anyone help.

5 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/Proof-Fortune Jul 07 '23

Thanks, will try running the test suite with valgrind.

I don't understand how piping the output would make a difference in the program?If you have time can you elaborate or give me some resources to read about this?

2

u/nderflow Jul 07 '23

When stdout is a terminal the C language standard requires that the output be line buffered. When it is not interactive, the standard requires that it is fully buffered. The difference in the set-up of stdout will make a small change to the behaviour of the program inside the standard C library.

As soon as you change the memory allocation pattern of your program, the symptoms of any memory access bugs you have will appear, disappear or move around.

You could read https://www.pixelbeat.org/programming/stdio_buffering/#:~:text=Default%20Buffering%20modes%3A,stderr%20is%20unbuffered for some additional info.

If you're using a reasonably recent system you could use stdbuf to control this (e.g. to experiment and gather evidence, not as a workaround for your bug). See the manual page (man stdbuf) for details.