Don't reset the_trace_ before flushing per-thread buffers

When we stop tracing, we set the_trace_ to nullptr and maintain a copy
of a local trace to flush the trace to file. The thread buffers were
cleared later as a part of flushing the trace to file. However, if a
thread gets destroyed after setting the_trace_ to nullptr but before we
clear the thread buffer it causes problems since we expect the_trace_
to be valid to flush any data from the buffer. This used to work fine
before we used per-thread buffers. This CL changes it so we reset
the_trace_ to nullptr only after clearing thread buffers. We do this in
a suspend all scope to avoid any races with stopping the trace and
thread getting destroyed.

This CL also moves resetting sampling_thread_ to a later point where we
have acquired trace_lock_ to be consistent.

Bug: 259258187
Test: art/test.py
Change-Id: I8f2e59bad1508e26ee43302ee1d619a35bb2f660
2 files changed