Write profile to a temporary file and rename it on close

The code to rename the file on close was added by c868425e2325cc76f0d, the code
to create it with the correct name was not.
Ensure the trace file is always closed when the interpreter is destroyed
Always increment the ordinal when re-opening a closed trace file
Add an explicit MY_CXT parameter for reopen_output_file
Re-open the output file in the parent on fork/thread spawn

This ensures any eval text used by the child is written in a completed file
ready for processing.