Ensure text mode when calling debug functions
Ensure text mode when calling debug functions

Summary:
Something seems to be changing stderr into binary mode,
so when the `traceIO` is called, the C code that ultimately
calls `vfprintf` is using a binary mode handle.

This causes newlines not to be encoded properly.

The patch ensures we're in text mode when writing the debug
messages (% interleaving as it's not thread safe at all) and restores
the previous mode when done.

I'm slightly concerned about the performance implications of writing
large dumps out in text mode, but I think the current behavior is not
intended as I cannot see any of the printing code setting the mode of
the std handles.

Test Plan: ./validate

Reviewers: austin, bgamari, erikd, simonmar

Reviewed By: bgamari

Subscribers: rwbarton, thomie

GHC Trac Issues: #14188

Differential Revision: https://phabricator.haskell.org/D4018

ezyang/ghc,ezyang/ghc,sdiehl/ghc,shlevy/ghc,shlevy/ghc,shlevy/ghc,shlevy/ghc,sdiehl/ghc,sdiehl/ghc,shlevy/ghc,shlevy/ghc,shlevy/ghc,sdiehl/ghc,ezyang/ghc,ezyang/ghc,sdiehl/ghc,ezyang/ghc,ezyang/ghc,ezyang/ghc,sdiehl/ghc,sdiehl/ghc