stdout/stderr in Instruments does not show output

I'm using Instruments to profile a C program that prints to stdout via printf but the program output does not appear in the Detail Area of the stdout/stderr instrument I added.

The program is launched by Instruments itself when I click the Record button and it appears to run correctly – I just can't see the stdout output.

I'm using Version 14.3.1 (14E300c) of Instruments on a MacBook Pro 16" (2019) running macOS 13.4 (22F66).

Replies

Hi Eurydice,

I suspect that this is due to the output you are writing to printf being buffered. stdout buffers output by default, meaning output will be written to a buffer first and once the buffer fills up it will actually be written out. It might be that your program write so little that the buffer never fills up and then when you stop the recording, Instruments kills the process without it ever actually writing the output.

When you run the same program in a Terminal, Terminal changes stdout to do line-by-line buffering, meaning the output will be written out once a line ends.

To quickly test whether this is the case for you, try changing to stderr instead. stderr should always be unbuffered (and the stdout/stderr instrument also captures stderr.

If it is indeed due to buffering of stdout, there is some more information about configuring the buffering behavior of your program under various conditions here: https://developer.apple.com/forums/thread/669842?answerId=706389022#706389022

The most promising option seems to be setting the STDBUF environment variable.

If this workaround works for you, could you please also file a radar on Instruments? Instruments already changes the buffering behavior for Foundation-based apps, but it could likely do a better job for generic C programs.

If changing the buffering behavior does NOT solve your problem, please report back here. As it's WWDC week you can also sign up for the Performance, power, and stability lab on Friday to have an engineer from the Instruments team take a look at the issue with you and either solve the issue or help you file a feedback report on Instruments. Note that signup closes on Thursday at 6PM California time.

  • Hi and thank you for your comprehensive response. I added fflush(stdout) after my printf statements and that resolved it, so I believe you were right about the buffering being the culprit here. Are there any downsides to using fflush(stdout) I'm unaware of vs. setting the STDBUF environment variable? Thanks again!

  • There are two downsides, but they might not be a big issue for you: 1. Having to add fflush(stdout) everywhere you do printf makes your code more complicated and harder to read. It's also easy to forget it in one of the places, whereas setting STDBUF changes the behavior globally for your whole process.

  • Also fflush changes the behavior of your process no matter what environment it runs in and there is a reason that buffering is the default: It's more efficient. E.g. when you pipe your output to a different process maybe bigger chunks are preferable. The environment variable allows you to change the buffering behaviour based on the current context you are running your program in. E.g. maybe in Instruments you want to turn off buffering completely but in regular use buffered output works better.

Add a Comment