Provide method to dump backtrace heap data.

For non-zygote spawned processes, we might want to dump the backtrace
data. Provide a method to send a signal to a process and then dump the
data to a file.

Adds a method to dump the backtrace data on exit.

Update documentation and explain format of heap dump data.

Test: Ran unit tests, enabled new options and used them.
Change-Id: Ie2fa706694160731afe02c1382b037d06df1d069
diff --git a/libc/malloc_debug/BacktraceData.h b/libc/malloc_debug/BacktraceData.h
index 6dee505..c8234dc 100644
--- a/libc/malloc_debug/BacktraceData.h
+++ b/libc/malloc_debug/BacktraceData.h
@@ -31,6 +31,8 @@
 
 #include <stdint.h>
 
+#include <atomic>
+
 #include <private/bionic_macros.h>
 
 #include "OptionData.h"
@@ -47,13 +49,21 @@
 
   inline size_t alloc_offset() { return alloc_offset_; }
 
-  bool enabled() { return enabled_; }
-  void set_enabled(bool enabled) { enabled_ = enabled; }
+  bool ShouldBacktrace() { return enabled_ == 1; }
+  void ToggleBacktraceEnabled() { enabled_.fetch_xor(1); }
+
+  void EnableDumping() { dump_ = true; }
+  bool ShouldDumpAndReset() {
+    bool expected = true;
+    return dump_.compare_exchange_strong(expected, false);
+  }
 
  private:
   size_t alloc_offset_ = 0;
 
-  volatile bool enabled_ = false;
+  std::atomic_uint8_t enabled_;
+
+  std::atomic_bool dump_;
 
   DISALLOW_COPY_AND_ASSIGN(BacktraceData);
 };