views:

72

answers:

3

I am trying to isolate a nasty bug, which brings down my linux kernel. I am printing messages to stderr and stderr is redirected to a log file. Is there a way to disable buffering on the file access? When kernel hangs, I am losing the messages in the buffer.

A: 

You can force flushing the buffer, using fflush(stderr);

Didier Trosset
There are way to many files/libraries to enforce fflush. Also is not stderr already non-buffered? I thought the only issue was the file buffer.
l.thee.a
A: 

Actually, stderr is unbuffered by default but I think that's only in terms of the C runtime. We've solved this before with:

fflush (stderr); fsync (fileno (stderr));

(although we actually did it to stdout but the same rules apply - the actual fflush may not be necessary for stderr but it does no harm).

The fflush flushes the C runtime buffers to the OS, the fsync forces write to disk.

Keep in mind this may severely affect your performance.

paxdiablo
A: 

You can try to use setvbuf when starting your app

setvbuf(stderr, NULL, _IONBF, 0);

However, you will get read of the stdio buffer, but still have the "in kernel" buffer problem, that won't disappear unless you fsync. However may be tracking a kernel bug from userspace isn't the best way to go at it.

Can you use a serial console and get the output on another machine ? This way you could get both the oops and the stderr messages

shodanex