fflush(stdout) in c

hari picture hari · Oct 29, 2010 · Viewed 11.1k times · Source

Right when I am at fflush(stdout) and I break there in GDB, can I know what is there in stdout before I actually print it?

How can I know what is there in stdout at any point in time?

Answer

Frédéric Hamidi picture Frédéric Hamidi · Oct 29, 2010

If you allocate a buffer yourself and pass it to setvbuf, I suppose you can access it before a flush, since it's yours to begin with.

EDIT: Your comment made your intent more clear, but what you want won't be easy:

  1. Set up your own buffer as described above,
  2. Set a read watchpoint on stdout,
  3. Watch your program slow to a crawl.

From then on, gdb will break each time anything accesses stdout, and you can check your buffer for changes, weird output, etc.

That said, that's not an ideal solution at all. A far better approach would be using a logging-enabled output function everywhere in your code.