Fixes around flushing per-thread tracing buffer

We should never see a non-nullptr in the thread destructor. We should
flush the buffer in Thread::Destroy. This CL has the following fixes:
1. Update the Thread::~Thread to check that the per-thread method trace
   buffer is nullptr instead of handling it by deleting it.
2. Flush the buffer at a later point to avoid the possibility of any
   java code executing and re-allocating the buffer after we flushed the
   contents.
3. Remove the potential race between Trace::Stop and Thread::Destroy
   when flushing the trace buffer. We now take the tracing_lock_ lock
   earlier in Trace::Stop. Trace::Stop flushes all the buffers and
   resets the trace_ under the tracing_lock_. Thread::Destroy already
   takes the tracing_lock_ before flushing the buffer. So we make sure
   only one of them flushes the contents.
4. Also reset the buffer index when resetting the per-thread buffer.
   Otherwise JITed code could incorrectly assume there is a buffer
   allocated. JITed code avoids a nullptr check on the buffer to make it
   more efficient.

Bug:  326890292
Test: art/test.py --trace --stream
Change-Id: Ifebaa8498817a248689730ac3cfe7cc8a74d4e1a
2 files changed