Add safe downcast methods to Session.
Flush trace output stream in emergency_debug().

This can help with debugging.