Merge "Properly handle empty map after read-only map."
diff --git a/debuggerd/libdebuggerd/test/tombstone_test.cpp b/debuggerd/libdebuggerd/test/tombstone_test.cpp
index 9dea7ac..b33adf3 100644
--- a/debuggerd/libdebuggerd/test/tombstone_test.cpp
+++ b/debuggerd/libdebuggerd/test/tombstone_test.cpp
@@ -359,467 +359,3 @@
   dump_timestamp(&log_, 0);
   ASSERT_STREQ("Timestamp: 1970-01-01 00:00:00+0000\n", amfd_data_.c_str());
 }
-
-class MemoryPattern : public unwindstack::Memory {
- public:
-  MemoryPattern() = default;
-  virtual ~MemoryPattern() = default;
-
-  size_t Read(uint64_t, void* dst, size_t size) override {
-    uint8_t* data = reinterpret_cast<uint8_t*>(dst);
-    for (size_t i = 0; i < size; i++) {
-      data[i] = (i % 0xff);
-    }
-    return size;
-  }
-};
-
-TEST_F(TombstoneTest, dump_stack_single_frame) {
-  std::vector<unwindstack::FrameData> frames;
-  unwindstack::Maps maps;
-  MemoryPattern memory;
-
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
-  dump_stack(&log_, frames, &maps, &memory);
-
-  std::string contents;
-  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
-  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));
-
-  std::string expected =
-#if defined(__LP64__)
-      "         0000000000001f80  0706050403020100\n"
-      "         0000000000001f88  0f0e0d0c0b0a0908\n"
-      "         0000000000001f90  1716151413121110\n"
-      "         0000000000001f98  1f1e1d1c1b1a1918\n"
-      "         0000000000001fa0  2726252423222120\n"
-      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
-      "         0000000000001fb0  3736353433323130\n"
-      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
-      "         0000000000001fc0  4746454443424140\n"
-      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
-      "         0000000000001fd0  5756555453525150\n"
-      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
-      "         0000000000001fe0  6766656463626160\n"
-      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
-      "         0000000000001ff0  7776757473727170\n"
-      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
-      "    #00  0000000000002000  0706050403020100\n"
-      "         0000000000002008  0f0e0d0c0b0a0908\n"
-      "         0000000000002010  1716151413121110\n"
-      "         0000000000002018  1f1e1d1c1b1a1918\n"
-      "         0000000000002020  2726252423222120\n"
-      "         0000000000002028  2f2e2d2c2b2a2928\n"
-      "         0000000000002030  3736353433323130\n"
-      "         0000000000002038  3f3e3d3c3b3a3938\n"
-      "         0000000000002040  4746454443424140\n"
-      "         0000000000002048  4f4e4d4c4b4a4948\n"
-      "         0000000000002050  5756555453525150\n"
-      "         0000000000002058  5f5e5d5c5b5a5958\n"
-      "         0000000000002060  6766656463626160\n"
-      "         0000000000002068  6f6e6d6c6b6a6968\n"
-      "         0000000000002070  7776757473727170\n"
-      "         0000000000002078  7f7e7d7c7b7a7978\n";
-#else
-      "         00001fc0  03020100\n"
-      "         00001fc4  07060504\n"
-      "         00001fc8  0b0a0908\n"
-      "         00001fcc  0f0e0d0c\n"
-      "         00001fd0  13121110\n"
-      "         00001fd4  17161514\n"
-      "         00001fd8  1b1a1918\n"
-      "         00001fdc  1f1e1d1c\n"
-      "         00001fe0  23222120\n"
-      "         00001fe4  27262524\n"
-      "         00001fe8  2b2a2928\n"
-      "         00001fec  2f2e2d2c\n"
-      "         00001ff0  33323130\n"
-      "         00001ff4  37363534\n"
-      "         00001ff8  3b3a3938\n"
-      "         00001ffc  3f3e3d3c\n"
-      "    #00  00002000  03020100\n"
-      "         00002004  07060504\n"
-      "         00002008  0b0a0908\n"
-      "         0000200c  0f0e0d0c\n"
-      "         00002010  13121110\n"
-      "         00002014  17161514\n"
-      "         00002018  1b1a1918\n"
-      "         0000201c  1f1e1d1c\n"
-      "         00002020  23222120\n"
-      "         00002024  27262524\n"
-      "         00002028  2b2a2928\n"
-      "         0000202c  2f2e2d2c\n"
-      "         00002030  33323130\n"
-      "         00002034  37363534\n"
-      "         00002038  3b3a3938\n"
-      "         0000203c  3f3e3d3c\n";
-#endif
-  EXPECT_EQ(expected, contents);
-}
-
-TEST_F(TombstoneTest, dump_stack_multiple_frames_same_sp) {
-  std::vector<unwindstack::FrameData> frames;
-  unwindstack::Maps maps;
-  MemoryPattern memory;
-
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2000});
-  dump_stack(&log_, frames, &maps, &memory);
-
-  std::string contents;
-  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
-  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));
-
-  std::string expected =
-#if defined(__LP64__)
-      "         0000000000001f80  0706050403020100\n"
-      "         0000000000001f88  0f0e0d0c0b0a0908\n"
-      "         0000000000001f90  1716151413121110\n"
-      "         0000000000001f98  1f1e1d1c1b1a1918\n"
-      "         0000000000001fa0  2726252423222120\n"
-      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
-      "         0000000000001fb0  3736353433323130\n"
-      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
-      "         0000000000001fc0  4746454443424140\n"
-      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
-      "         0000000000001fd0  5756555453525150\n"
-      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
-      "         0000000000001fe0  6766656463626160\n"
-      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
-      "         0000000000001ff0  7776757473727170\n"
-      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
-      "    #00  0000000000002000  0706050403020100\n"
-      "         ................  ................\n"
-      "    #01  0000000000002000  0706050403020100\n"
-      "         0000000000002008  0f0e0d0c0b0a0908\n"
-      "         0000000000002010  1716151413121110\n"
-      "         0000000000002018  1f1e1d1c1b1a1918\n"
-      "         0000000000002020  2726252423222120\n"
-      "         0000000000002028  2f2e2d2c2b2a2928\n"
-      "         0000000000002030  3736353433323130\n"
-      "         0000000000002038  3f3e3d3c3b3a3938\n"
-      "         0000000000002040  4746454443424140\n"
-      "         0000000000002048  4f4e4d4c4b4a4948\n"
-      "         0000000000002050  5756555453525150\n"
-      "         0000000000002058  5f5e5d5c5b5a5958\n"
-      "         0000000000002060  6766656463626160\n"
-      "         0000000000002068  6f6e6d6c6b6a6968\n"
-      "         0000000000002070  7776757473727170\n"
-      "         0000000000002078  7f7e7d7c7b7a7978\n";
-#else
-      "         00001fc0  03020100\n"
-      "         00001fc4  07060504\n"
-      "         00001fc8  0b0a0908\n"
-      "         00001fcc  0f0e0d0c\n"
-      "         00001fd0  13121110\n"
-      "         00001fd4  17161514\n"
-      "         00001fd8  1b1a1918\n"
-      "         00001fdc  1f1e1d1c\n"
-      "         00001fe0  23222120\n"
-      "         00001fe4  27262524\n"
-      "         00001fe8  2b2a2928\n"
-      "         00001fec  2f2e2d2c\n"
-      "         00001ff0  33323130\n"
-      "         00001ff4  37363534\n"
-      "         00001ff8  3b3a3938\n"
-      "         00001ffc  3f3e3d3c\n"
-      "    #00  00002000  03020100\n"
-      "         ........  ........\n"
-      "    #01  00002000  03020100\n"
-      "         00002004  07060504\n"
-      "         00002008  0b0a0908\n"
-      "         0000200c  0f0e0d0c\n"
-      "         00002010  13121110\n"
-      "         00002014  17161514\n"
-      "         00002018  1b1a1918\n"
-      "         0000201c  1f1e1d1c\n"
-      "         00002020  23222120\n"
-      "         00002024  27262524\n"
-      "         00002028  2b2a2928\n"
-      "         0000202c  2f2e2d2c\n"
-      "         00002030  33323130\n"
-      "         00002034  37363534\n"
-      "         00002038  3b3a3938\n"
-      "         0000203c  3f3e3d3c\n";
-#endif
-  EXPECT_EQ(expected, contents);
-}
-
-TEST_F(TombstoneTest, dump_stack_multiple_frames) {
-  std::vector<unwindstack::FrameData> frames;
-  unwindstack::Maps maps;
-  MemoryPattern memory;
-
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2010});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2100});
-  dump_stack(&log_, frames, &maps, &memory);
-
-  std::string contents;
-  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
-  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));
-
-  std::string expected =
-#if defined(__LP64__)
-      "         0000000000001f80  0706050403020100\n"
-      "         0000000000001f88  0f0e0d0c0b0a0908\n"
-      "         0000000000001f90  1716151413121110\n"
-      "         0000000000001f98  1f1e1d1c1b1a1918\n"
-      "         0000000000001fa0  2726252423222120\n"
-      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
-      "         0000000000001fb0  3736353433323130\n"
-      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
-      "         0000000000001fc0  4746454443424140\n"
-      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
-      "         0000000000001fd0  5756555453525150\n"
-      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
-      "         0000000000001fe0  6766656463626160\n"
-      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
-      "         0000000000001ff0  7776757473727170\n"
-      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
-      "    #00  0000000000002000  0706050403020100\n"
-      "         0000000000002008  0f0e0d0c0b0a0908\n"
-      "    #01  0000000000002010  0706050403020100\n"
-      "         0000000000002018  0f0e0d0c0b0a0908\n"
-      "         0000000000002020  1716151413121110\n"
-      "         0000000000002028  1f1e1d1c1b1a1918\n"
-      "         0000000000002030  2726252423222120\n"
-      "         0000000000002038  2f2e2d2c2b2a2928\n"
-      "         0000000000002040  3736353433323130\n"
-      "         0000000000002048  3f3e3d3c3b3a3938\n"
-      "         0000000000002050  4746454443424140\n"
-      "         0000000000002058  4f4e4d4c4b4a4948\n"
-      "         0000000000002060  5756555453525150\n"
-      "         0000000000002068  5f5e5d5c5b5a5958\n"
-      "         0000000000002070  6766656463626160\n"
-      "         0000000000002078  6f6e6d6c6b6a6968\n"
-      "         0000000000002080  7776757473727170\n"
-      "         0000000000002088  7f7e7d7c7b7a7978\n"
-      "         ................  ................\n"
-      "    #02  0000000000002100  0706050403020100\n"
-      "         0000000000002108  0f0e0d0c0b0a0908\n"
-      "         0000000000002110  1716151413121110\n"
-      "         0000000000002118  1f1e1d1c1b1a1918\n"
-      "         0000000000002120  2726252423222120\n"
-      "         0000000000002128  2f2e2d2c2b2a2928\n"
-      "         0000000000002130  3736353433323130\n"
-      "         0000000000002138  3f3e3d3c3b3a3938\n"
-      "         0000000000002140  4746454443424140\n"
-      "         0000000000002148  4f4e4d4c4b4a4948\n"
-      "         0000000000002150  5756555453525150\n"
-      "         0000000000002158  5f5e5d5c5b5a5958\n"
-      "         0000000000002160  6766656463626160\n"
-      "         0000000000002168  6f6e6d6c6b6a6968\n"
-      "         0000000000002170  7776757473727170\n"
-      "         0000000000002178  7f7e7d7c7b7a7978\n";
-#else
-      "         00001fc0  03020100\n"
-      "         00001fc4  07060504\n"
-      "         00001fc8  0b0a0908\n"
-      "         00001fcc  0f0e0d0c\n"
-      "         00001fd0  13121110\n"
-      "         00001fd4  17161514\n"
-      "         00001fd8  1b1a1918\n"
-      "         00001fdc  1f1e1d1c\n"
-      "         00001fe0  23222120\n"
-      "         00001fe4  27262524\n"
-      "         00001fe8  2b2a2928\n"
-      "         00001fec  2f2e2d2c\n"
-      "         00001ff0  33323130\n"
-      "         00001ff4  37363534\n"
-      "         00001ff8  3b3a3938\n"
-      "         00001ffc  3f3e3d3c\n"
-      "    #00  00002000  03020100\n"
-      "         00002004  07060504\n"
-      "         00002008  0b0a0908\n"
-      "         0000200c  0f0e0d0c\n"
-      "    #01  00002010  03020100\n"
-      "         00002014  07060504\n"
-      "         00002018  0b0a0908\n"
-      "         0000201c  0f0e0d0c\n"
-      "         00002020  13121110\n"
-      "         00002024  17161514\n"
-      "         00002028  1b1a1918\n"
-      "         0000202c  1f1e1d1c\n"
-      "         00002030  23222120\n"
-      "         00002034  27262524\n"
-      "         00002038  2b2a2928\n"
-      "         0000203c  2f2e2d2c\n"
-      "         00002040  33323130\n"
-      "         00002044  37363534\n"
-      "         00002048  3b3a3938\n"
-      "         0000204c  3f3e3d3c\n"
-      "         ........  ........\n"
-      "    #02  00002100  03020100\n"
-      "         00002104  07060504\n"
-      "         00002108  0b0a0908\n"
-      "         0000210c  0f0e0d0c\n"
-      "         00002110  13121110\n"
-      "         00002114  17161514\n"
-      "         00002118  1b1a1918\n"
-      "         0000211c  1f1e1d1c\n"
-      "         00002120  23222120\n"
-      "         00002124  27262524\n"
-      "         00002128  2b2a2928\n"
-      "         0000212c  2f2e2d2c\n"
-      "         00002130  33323130\n"
-      "         00002134  37363534\n"
-      "         00002138  3b3a3938\n"
-      "         0000213c  3f3e3d3c\n";
-#endif
-  EXPECT_EQ(expected, contents);
-}
-
-TEST_F(TombstoneTest, dump_stack_multiple_frames_disjoint_frames) {
-  std::vector<unwindstack::FrameData> frames;
-  unwindstack::Maps maps;
-  MemoryPattern memory;
-
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1000, .pc = 0x301000, .sp = 0x2000});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x2010});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x1000});
-  frames.push_back(
-      unwindstack::FrameData{.num = 0, .rel_pc = 0x1400, .pc = 0x301400, .sp = 0x1030});
-  dump_stack(&log_, frames, &maps, &memory);
-
-  std::string contents;
-  ASSERT_TRUE(lseek(log_.tfd, 0, SEEK_SET) == 0);
-  ASSERT_TRUE(android::base::ReadFdToString(log_.tfd, &contents));
-
-  std::string expected =
-#if defined(__LP64__)
-      "         0000000000001f80  0706050403020100\n"
-      "         0000000000001f88  0f0e0d0c0b0a0908\n"
-      "         0000000000001f90  1716151413121110\n"
-      "         0000000000001f98  1f1e1d1c1b1a1918\n"
-      "         0000000000001fa0  2726252423222120\n"
-      "         0000000000001fa8  2f2e2d2c2b2a2928\n"
-      "         0000000000001fb0  3736353433323130\n"
-      "         0000000000001fb8  3f3e3d3c3b3a3938\n"
-      "         0000000000001fc0  4746454443424140\n"
-      "         0000000000001fc8  4f4e4d4c4b4a4948\n"
-      "         0000000000001fd0  5756555453525150\n"
-      "         0000000000001fd8  5f5e5d5c5b5a5958\n"
-      "         0000000000001fe0  6766656463626160\n"
-      "         0000000000001fe8  6f6e6d6c6b6a6968\n"
-      "         0000000000001ff0  7776757473727170\n"
-      "         0000000000001ff8  7f7e7d7c7b7a7978\n"
-      "    #00  0000000000002000  0706050403020100\n"
-      "         0000000000002008  0f0e0d0c0b0a0908\n"
-      "    #01  0000000000002010  0706050403020100\n"
-      "         0000000000002018  0f0e0d0c0b0a0908\n"
-      "         0000000000002020  1716151413121110\n"
-      "         0000000000002028  1f1e1d1c1b1a1918\n"
-      "         0000000000002030  2726252423222120\n"
-      "         0000000000002038  2f2e2d2c2b2a2928\n"
-      "         0000000000002040  3736353433323130\n"
-      "         0000000000002048  3f3e3d3c3b3a3938\n"
-      "         0000000000002050  4746454443424140\n"
-      "         0000000000002058  4f4e4d4c4b4a4948\n"
-      "         0000000000002060  5756555453525150\n"
-      "         0000000000002068  5f5e5d5c5b5a5958\n"
-      "         0000000000002070  6766656463626160\n"
-      "         0000000000002078  6f6e6d6c6b6a6968\n"
-      "         0000000000002080  7776757473727170\n"
-      "         0000000000002088  7f7e7d7c7b7a7978\n"
-      "         ................  ................\n"
-      "    #02  0000000000001000  0706050403020100\n"
-      "         0000000000001008  0f0e0d0c0b0a0908\n"
-      "         0000000000001010  1716151413121110\n"
-      "         0000000000001018  1f1e1d1c1b1a1918\n"
-      "         0000000000001020  2726252423222120\n"
-      "         0000000000001028  2f2e2d2c2b2a2928\n"
-      "    #03  0000000000001030  0706050403020100\n"
-      "         0000000000001038  0f0e0d0c0b0a0908\n"
-      "         0000000000001040  1716151413121110\n"
-      "         0000000000001048  1f1e1d1c1b1a1918\n"
-      "         0000000000001050  2726252423222120\n"
-      "         0000000000001058  2f2e2d2c2b2a2928\n"
-      "         0000000000001060  3736353433323130\n"
-      "         0000000000001068  3f3e3d3c3b3a3938\n"
-      "         0000000000001070  4746454443424140\n"
-      "         0000000000001078  4f4e4d4c4b4a4948\n"
-      "         0000000000001080  5756555453525150\n"
-      "         0000000000001088  5f5e5d5c5b5a5958\n"
-      "         0000000000001090  6766656463626160\n"
-      "         0000000000001098  6f6e6d6c6b6a6968\n"
-      "         00000000000010a0  7776757473727170\n"
-      "         00000000000010a8  7f7e7d7c7b7a7978\n";
-#else
-      "         00001fc0  03020100\n"
-      "         00001fc4  07060504\n"
-      "         00001fc8  0b0a0908\n"
-      "         00001fcc  0f0e0d0c\n"
-      "         00001fd0  13121110\n"
-      "         00001fd4  17161514\n"
-      "         00001fd8  1b1a1918\n"
-      "         00001fdc  1f1e1d1c\n"
-      "         00001fe0  23222120\n"
-      "         00001fe4  27262524\n"
-      "         00001fe8  2b2a2928\n"
-      "         00001fec  2f2e2d2c\n"
-      "         00001ff0  33323130\n"
-      "         00001ff4  37363534\n"
-      "         00001ff8  3b3a3938\n"
-      "         00001ffc  3f3e3d3c\n"
-      "    #00  00002000  03020100\n"
-      "         00002004  07060504\n"
-      "         00002008  0b0a0908\n"
-      "         0000200c  0f0e0d0c\n"
-      "    #01  00002010  03020100\n"
-      "         00002014  07060504\n"
-      "         00002018  0b0a0908\n"
-      "         0000201c  0f0e0d0c\n"
-      "         00002020  13121110\n"
-      "         00002024  17161514\n"
-      "         00002028  1b1a1918\n"
-      "         0000202c  1f1e1d1c\n"
-      "         00002030  23222120\n"
-      "         00002034  27262524\n"
-      "         00002038  2b2a2928\n"
-      "         0000203c  2f2e2d2c\n"
-      "         00002040  33323130\n"
-      "         00002044  37363534\n"
-      "         00002048  3b3a3938\n"
-      "         0000204c  3f3e3d3c\n"
-      "         ........  ........\n"
-      "    #02  00001000  03020100\n"
-      "         00001004  07060504\n"
-      "         00001008  0b0a0908\n"
-      "         0000100c  0f0e0d0c\n"
-      "         00001010  13121110\n"
-      "         00001014  17161514\n"
-      "         00001018  1b1a1918\n"
-      "         0000101c  1f1e1d1c\n"
-      "         00001020  23222120\n"
-      "         00001024  27262524\n"
-      "         00001028  2b2a2928\n"
-      "         0000102c  2f2e2d2c\n"
-      "    #03  00001030  03020100\n"
-      "         00001034  07060504\n"
-      "         00001038  0b0a0908\n"
-      "         0000103c  0f0e0d0c\n"
-      "         00001040  13121110\n"
-      "         00001044  17161514\n"
-      "         00001048  1b1a1918\n"
-      "         0000104c  1f1e1d1c\n"
-      "         00001050  23222120\n"
-      "         00001054  27262524\n"
-      "         00001058  2b2a2928\n"
-      "         0000105c  2f2e2d2c\n"
-      "         00001060  33323130\n"
-      "         00001064  37363534\n"
-      "         00001068  3b3a3938\n"
-      "         0000106c  3f3e3d3c\n";
-#endif
-  EXPECT_EQ(expected, contents);
-}
diff --git a/debuggerd/libdebuggerd/tombstone.cpp b/debuggerd/libdebuggerd/tombstone.cpp
index b64e260..4e7f35c 100644
--- a/debuggerd/libdebuggerd/tombstone.cpp
+++ b/debuggerd/libdebuggerd/tombstone.cpp
@@ -188,106 +188,6 @@
   _LOG(log, logtype::HEADER, "uid: %d\n", thread_info.uid);
 }
 
-static void dump_stack_segment(log_t* log, unwindstack::Maps* maps, unwindstack::Memory* memory,
-                               uint64_t* sp, size_t words, int label) {
-  // Read the data all at once.
-  word_t stack_data[words];
-
-  // TODO: Do we need to word align this for crashes caused by a misaligned sp?
-  //       The process_vm_readv implementation of Memory should handle this appropriately?
-  size_t bytes_read = memory->Read(*sp, stack_data, sizeof(word_t) * words);
-  words = bytes_read / sizeof(word_t);
-  std::string line;
-  for (size_t i = 0; i < words; i++) {
-    line = "    ";
-    if (i == 0 && label >= 0) {
-      // Print the label once.
-      line += StringPrintf("#%02d  ", label);
-    } else {
-      line += "     ";
-    }
-    line += StringPrintf("%" PRIPTR "  %" PRIPTR, *sp, static_cast<uint64_t>(stack_data[i]));
-
-    unwindstack::MapInfo* map_info = maps->Find(stack_data[i]);
-    if (map_info != nullptr && !map_info->name.empty()) {
-      line += "  " + map_info->name;
-      std::string func_name;
-      uint64_t func_offset = 0;
-      if (map_info->GetFunctionName(stack_data[i], &func_name, &func_offset)) {
-        line += " (" + func_name;
-        if (func_offset) {
-          line += StringPrintf("+%" PRIu64, func_offset);
-        }
-        line += ')';
-      }
-    }
-    _LOG(log, logtype::STACK, "%s\n", line.c_str());
-
-    *sp += sizeof(word_t);
-  }
-}
-
-static void dump_stack(log_t* log, const std::vector<unwindstack::FrameData>& frames,
-                       unwindstack::Maps* maps, unwindstack::Memory* memory) {
-  size_t first = 0, last;
-  for (size_t i = 0; i < frames.size(); i++) {
-    if (frames[i].sp) {
-      if (!first) {
-        first = i+1;
-      }
-      last = i;
-    }
-  }
-
-  if (!first) {
-    return;
-  }
-  first--;
-
-  // Dump a few words before the first frame.
-  uint64_t sp = frames[first].sp - STACK_WORDS * sizeof(word_t);
-  dump_stack_segment(log, maps, memory, &sp, STACK_WORDS, -1);
-
-#if defined(__LP64__)
-  static constexpr const char delimiter[] = "         ................  ................\n";
-#else
-  static constexpr const char delimiter[] = "         ........  ........\n";
-#endif
-
-  // Dump a few words from all successive frames.
-  for (size_t i = first; i <= last; i++) {
-    auto* frame = &frames[i];
-    if (sp != frame->sp) {
-      _LOG(log, logtype::STACK, delimiter);
-      sp = frame->sp;
-    }
-    if (i != last) {
-      // Print stack data up to the stack from the next frame.
-      size_t words;
-      uint64_t next_sp = frames[i + 1].sp;
-      if (next_sp < sp) {
-        // The next frame is probably using a completely different stack,
-        // so dump the max from this stack.
-        words = STACK_WORDS;
-      } else {
-        words = (next_sp - sp) / sizeof(word_t);
-        if (words == 0) {
-          // The sp is the same as the next frame, print at least
-          // one line for this frame.
-          words = 1;
-        } else if (words > STACK_WORDS) {
-          words = STACK_WORDS;
-        }
-      }
-      dump_stack_segment(log, maps, memory, &sp, words, i);
-    } else {
-      // Print some number of words past the last stack frame since we
-      // don't know how large the stack is.
-      dump_stack_segment(log, maps, memory, &sp, STACK_WORDS, i);
-    }
-  }
-}
-
 static std::string get_addr_string(uint64_t addr) {
   std::string addr_str;
 #if defined(__LP64__)
@@ -499,9 +399,6 @@
   } else {
     _LOG(log, logtype::BACKTRACE, "\nbacktrace:\n");
     log_backtrace(log, unwinder, "    ");
-
-    _LOG(log, logtype::STACK, "\nstack:\n");
-    dump_stack(log, unwinder->frames(), unwinder->GetMaps(), unwinder->GetProcessMemory().get());
   }
 
   if (primary_thread) {
diff --git a/fastboot/Android.mk b/fastboot/Android.mk
index 17ec392..fd009e7 100644
--- a/fastboot/Android.mk
+++ b/fastboot/Android.mk
@@ -18,9 +18,9 @@
 # Package fastboot-related executables.
 #
 
-my_dist_files := $(HOST_OUT_EXECUTABLES)/mke2fs
-my_dist_files += $(HOST_OUT_EXECUTABLES)/e2fsdroid
-my_dist_files += $(HOST_OUT_EXECUTABLES)/make_f2fs
-my_dist_files += $(HOST_OUT_EXECUTABLES)/sload_f2fs
+my_dist_files := $(SOONG_HOST_OUT_EXECUTABLES)/mke2fs
+my_dist_files += $(SOONG_HOST_OUT_EXECUTABLES)/e2fsdroid
+my_dist_files += $(SOONG_HOST_OUT_EXECUTABLES)/make_f2fs
+my_dist_files += $(SOONG_HOST_OUT_EXECUTABLES)/sload_f2fs
 $(call dist-for-goals,dist_files sdk win_sdk,$(my_dist_files))
 my_dist_files :=
diff --git a/fastboot/constants.h b/fastboot/constants.h
index 5a554a0..aefd448 100644
--- a/fastboot/constants.h
+++ b/fastboot/constants.h
@@ -47,6 +47,8 @@
 #define FB_VAR_VERSION "version"
 #define FB_VAR_VERSION_BOOTLOADER "version-bootloader"
 #define FB_VAR_VERSION_BASEBAND "version-baseband"
+#define FB_VAR_VERSION_OS "version-os"
+#define FB_VAR_VERSION_VNDK "version-vndk"
 #define FB_VAR_PRODUCT "product"
 #define FB_VAR_SERIALNO "serialno"
 #define FB_VAR_SECURE "secure"
@@ -69,3 +71,9 @@
 #define FB_VAR_SUPER_PARTITION_NAME "super-partition-name"
 #define FB_VAR_SNAPSHOT_UPDATE_STATUS "snapshot-update-status"
 #define FB_VAR_CPU_ABI "cpu-abi"
+#define FB_VAR_SYSTEM_FINGERPRINT "system-fingerprint"
+#define FB_VAR_VENDOR_FINGERPRINT "vendor-fingerprint"
+#define FB_VAR_DYNAMIC_PARTITION "dynamic-partition"
+#define FB_VAR_FIRST_API_LEVEL "first-api-level"
+#define FB_VAR_SECURITY_PATCH_LEVEL "security-patch-level"
+#define FB_VAR_TREBLE_ENABLED "treble-enabled"
diff --git a/fastboot/device/commands.cpp b/fastboot/device/commands.cpp
index b7263d9..2c9dec9 100644
--- a/fastboot/device/commands.cpp
+++ b/fastboot/device/commands.cpp
@@ -106,6 +106,8 @@
             {FB_VAR_VERSION, {GetVersion, nullptr}},
             {FB_VAR_VERSION_BOOTLOADER, {GetBootloaderVersion, nullptr}},
             {FB_VAR_VERSION_BASEBAND, {GetBasebandVersion, nullptr}},
+            {FB_VAR_VERSION_OS, {GetOsVersion, nullptr}},
+            {FB_VAR_VERSION_VNDK, {GetVndkVersion, nullptr}},
             {FB_VAR_PRODUCT, {GetProduct, nullptr}},
             {FB_VAR_SERIALNO, {GetSerial, nullptr}},
             {FB_VAR_VARIANT, {GetVariant, nullptr}},
@@ -127,7 +129,13 @@
             {FB_VAR_HW_REVISION, {GetHardwareRevision, nullptr}},
             {FB_VAR_SUPER_PARTITION_NAME, {GetSuperPartitionName, nullptr}},
             {FB_VAR_SNAPSHOT_UPDATE_STATUS, {GetSnapshotUpdateStatus, nullptr}},
-            {FB_VAR_CPU_ABI, {GetCpuAbi, nullptr}}};
+            {FB_VAR_CPU_ABI, {GetCpuAbi, nullptr}},
+            {FB_VAR_SYSTEM_FINGERPRINT, {GetSystemFingerprint, nullptr}},
+            {FB_VAR_VENDOR_FINGERPRINT, {GetVendorFingerprint, nullptr}},
+            {FB_VAR_DYNAMIC_PARTITION, {GetDynamicPartition, nullptr}},
+            {FB_VAR_FIRST_API_LEVEL, {GetFirstApiLevel, nullptr}},
+            {FB_VAR_SECURITY_PATCH_LEVEL, {GetSecurityPatchLevel, nullptr}},
+            {FB_VAR_TREBLE_ENABLED, {GetTrebleEnabled, nullptr}}};
 
     if (args.size() < 2) {
         return device->WriteFail("Missing argument");
diff --git a/fastboot/device/variables.cpp b/fastboot/device/variables.cpp
index 10eac01..e7d8bc3 100644
--- a/fastboot/device/variables.cpp
+++ b/fastboot/device/variables.cpp
@@ -62,6 +62,18 @@
     return true;
 }
 
+bool GetOsVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                  std::string* message) {
+    *message = android::base::GetProperty("ro.build.version.release", "");
+    return true;
+}
+
+bool GetVndkVersion(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                    std::string* message) {
+    *message = android::base::GetProperty("ro.vndk.version", "");
+    return true;
+}
+
 bool GetProduct(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
                 std::string* message) {
     *message = android::base::GetProperty("ro.product.device", "");
@@ -458,3 +470,42 @@
     *message = android::base::GetProperty("ro.product.cpu.abi", "");
     return true;
 }
+
+bool GetSystemFingerprint(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                          std::string* message) {
+    *message = android::base::GetProperty("ro.system.build.fingerprint", "");
+    if (message->empty()) {
+        *message = android::base::GetProperty("ro.build.fingerprint", "");
+    }
+    return true;
+}
+
+bool GetVendorFingerprint(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                          std::string* message) {
+    *message = android::base::GetProperty("ro.vendor.build.fingerprint", "");
+    return true;
+}
+
+bool GetDynamicPartition(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                         std::string* message) {
+    *message = android::base::GetProperty("ro.boot.dynamic_partitions", "");
+    return true;
+}
+
+bool GetFirstApiLevel(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                      std::string* message) {
+    *message = android::base::GetProperty("ro.product.first_api_level", "");
+    return true;
+}
+
+bool GetSecurityPatchLevel(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                           std::string* message) {
+    *message = android::base::GetProperty("ro.build.version.security_patch", "");
+    return true;
+}
+
+bool GetTrebleEnabled(FastbootDevice* /* device */, const std::vector<std::string>& /* args */,
+                      std::string* message) {
+    *message = android::base::GetProperty("ro.treble.enabled", "");
+    return true;
+}
diff --git a/fastboot/device/variables.h b/fastboot/device/variables.h
index 90840d6..c11e472 100644
--- a/fastboot/device/variables.h
+++ b/fastboot/device/variables.h
@@ -26,6 +26,10 @@
                           std::string* message);
 bool GetBasebandVersion(FastbootDevice* device, const std::vector<std::string>& args,
                         std::string* message);
+bool GetOsVersion(FastbootDevice* device, const std::vector<std::string>& args,
+                  std::string* message);
+bool GetVndkVersion(FastbootDevice* device, const std::vector<std::string>& args,
+                    std::string* message);
 bool GetProduct(FastbootDevice* device, const std::vector<std::string>& args, std::string* message);
 bool GetSerial(FastbootDevice* device, const std::vector<std::string>& args, std::string* message);
 bool GetSecure(FastbootDevice* device, const std::vector<std::string>& args, std::string* message);
@@ -64,6 +68,18 @@
 bool GetSnapshotUpdateStatus(FastbootDevice* device, const std::vector<std::string>& args,
                              std::string* message);
 bool GetCpuAbi(FastbootDevice* device, const std::vector<std::string>& args, std::string* message);
+bool GetSystemFingerprint(FastbootDevice* device, const std::vector<std::string>& args,
+                          std::string* message);
+bool GetVendorFingerprint(FastbootDevice* device, const std::vector<std::string>& args,
+                          std::string* message);
+bool GetDynamicPartition(FastbootDevice* device, const std::vector<std::string>& args,
+                         std::string* message);
+bool GetFirstApiLevel(FastbootDevice* device, const std::vector<std::string>& args,
+                      std::string* message);
+bool GetSecurityPatchLevel(FastbootDevice* device, const std::vector<std::string>& args,
+                           std::string* message);
+bool GetTrebleEnabled(FastbootDevice* device, const std::vector<std::string>& args,
+                      std::string* message);
 
 // Helpers for getvar all.
 std::vector<std::vector<std::string>> GetAllPartitionArgsWithSlot(FastbootDevice* device);
diff --git a/healthd/BatteryMonitor.cpp b/healthd/BatteryMonitor.cpp
index bdf4aac..7caf468 100644
--- a/healthd/BatteryMonitor.cpp
+++ b/healthd/BatteryMonitor.cpp
@@ -126,6 +126,26 @@
     return *ret;
 }
 
+BatteryCapacityLevel getBatteryCapacityLevel(const char* capacityLevel) {
+    static SysfsStringEnumMap<BatteryCapacityLevel> batteryCapacityLevelMap[] = {
+            {"Unknown", BatteryCapacityLevel::UNKNOWN},
+            {"Critical", BatteryCapacityLevel::CRITICAL},
+            {"Low", BatteryCapacityLevel::LOW},
+            {"Normal", BatteryCapacityLevel::NORMAL},
+            {"High", BatteryCapacityLevel::HIGH},
+            {"Full", BatteryCapacityLevel::FULL},
+            {NULL, BatteryCapacityLevel::UNKNOWN},
+    };
+
+    auto ret = mapSysfsString(capacityLevel, batteryCapacityLevelMap);
+    if (!ret) {
+        KLOG_WARNING(LOG_TAG, "Unknown battery capacity level '%s'\n", capacityLevel);
+        *ret = BatteryCapacityLevel::UNKNOWN;
+    }
+
+    return *ret;
+}
+
 BatteryHealth getBatteryHealth(const char* status) {
     static SysfsStringEnumMap<BatteryHealth> batteryHealthMap[] = {
             {"Unknown", BatteryHealth::UNKNOWN},
@@ -241,9 +261,10 @@
         mHealthInfo->legacy.batteryCurrentAverage =
                 getIntField(mHealthdConfig->batteryCurrentAvgPath);
 
-    // TODO(b/142260281): Retrieve these values correctly.
-    mHealthInfo->batteryCapacityLevel = BatteryCapacityLevel::UNKNOWN;
-    mHealthInfo->batteryChargeTimeToFullNowSeconds = 0;
+    if (!mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
+        mHealthInfo->batteryChargeTimeToFullNowSeconds =
+                getIntField(mHealthdConfig->batteryChargeTimeToFullNowPath);
+
     mHealthInfo->batteryFullCapacityUah = props.batteryFullCharge;
 
     props.batteryTemperature = mBatteryFixedTemperature ?
@@ -252,6 +273,9 @@
 
     std::string buf;
 
+    if (readFromFile(mHealthdConfig->batteryCapacityLevelPath, &buf) > 0)
+        mHealthInfo->batteryCapacityLevel = getBatteryCapacityLevel(buf.c_str());
+
     if (readFromFile(mHealthdConfig->batteryStatusPath, &buf) > 0)
         props.batteryStatus = getBatteryStatus(buf.c_str());
 
@@ -585,6 +609,19 @@
                         mHealthdConfig->batteryCycleCountPath = path;
                 }
 
+                if (mHealthdConfig->batteryCapacityLevelPath.isEmpty()) {
+                    path.clear();
+                    path.appendFormat("%s/%s/capacity_level", POWER_SUPPLY_SYSFS_PATH, name);
+                    if (access(path, R_OK) == 0) mHealthdConfig->batteryCapacityLevelPath = path;
+                }
+
+                if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty()) {
+                    path.clear();
+                    path.appendFormat("%s/%s/time_to_full_now", POWER_SUPPLY_SYSFS_PATH, name);
+                    if (access(path, R_OK) == 0)
+                        mHealthdConfig->batteryChargeTimeToFullNowPath = path;
+                }
+
                 if (mHealthdConfig->batteryCurrentAvgPath.isEmpty()) {
                     path.clear();
                     path.appendFormat("%s/%s/current_avg",
@@ -653,6 +690,10 @@
             KLOG_WARNING(LOG_TAG, "BatteryFullChargePath not found\n");
         if (mHealthdConfig->batteryCycleCountPath.isEmpty())
             KLOG_WARNING(LOG_TAG, "BatteryCycleCountPath not found\n");
+        if (mHealthdConfig->batteryCapacityLevelPath.isEmpty())
+            KLOG_WARNING(LOG_TAG, "batteryCapacityLevelPath not found\n");
+        if (mHealthdConfig->batteryChargeTimeToFullNowPath.isEmpty())
+            KLOG_WARNING(LOG_TAG, "batteryChargeTimeToFullNowPath. not found\n");
     }
 
     if (property_get("ro.boot.fake_battery", pval, NULL) > 0
diff --git a/healthd/include/healthd/healthd.h b/healthd/include/healthd/healthd.h
index a900071..8ffb114 100644
--- a/healthd/include/healthd/healthd.h
+++ b/healthd/include/healthd/healthd.h
@@ -69,6 +69,8 @@
     android::String8 batteryChargeCounterPath;
     android::String8 batteryFullChargePath;
     android::String8 batteryCycleCountPath;
+    android::String8 batteryCapacityLevelPath;
+    android::String8 batteryChargeTimeToFullNowPath;
 
     int (*energyCounter)(int64_t *);
     int boot_min_cap;
diff --git a/libstats/socket/Android.bp b/libstats/socket/Android.bp
index 3b6efbb..9fd9fbc 100644
--- a/libstats/socket/Android.bp
+++ b/libstats/socket/Android.bp
@@ -75,3 +75,22 @@
         "libgtest_prod",
     ],
 }
+
+cc_test {
+    name: "libstatssocket_test",
+    srcs: ["tests/stats_event_test.cpp"],
+    cflags: [
+        "-Wall",
+        "-Werror",
+    ],
+    static_libs: [
+        "libgmock",
+        "libstatssocket",
+    ],
+    shared_libs: [
+        "libcutils",
+        "liblog",
+        "libutils",
+    ],
+    test_suites: ["device_tests"],
+}
diff --git a/libstats/socket/tests/stats_event_test.cpp b/libstats/socket/tests/stats_event_test.cpp
new file mode 100644
index 0000000..cf0592c
--- /dev/null
+++ b/libstats/socket/tests/stats_event_test.cpp
@@ -0,0 +1,344 @@
+/*
+ * Copyright (C) 2019 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "stats_event.h"
+#include <gtest/gtest.h>
+#include <utils/SystemClock.h>
+
+using std::string;
+using std::vector;
+
+// Side-effect: this function moves the start of the buffer past the read value
+template <class T>
+T readNext(uint8_t** buffer) {
+    T value = *(T*)(*buffer);
+    *buffer += sizeof(T);
+    return value;
+}
+
+void checkTypeHeader(uint8_t** buffer, uint8_t typeId, uint8_t numAnnotations = 0) {
+    uint8_t typeHeader = (numAnnotations << 4) | typeId;
+    EXPECT_EQ(readNext<uint8_t>(buffer), typeHeader);
+}
+
+template <class T>
+void checkScalar(uint8_t** buffer, T expectedValue) {
+    EXPECT_EQ(readNext<T>(buffer), expectedValue);
+}
+
+void checkString(uint8_t** buffer, const string& expectedString) {
+    uint32_t size = readNext<uint32_t>(buffer);
+    string parsedString((char*)(*buffer), size);
+    EXPECT_EQ(parsedString, expectedString);
+    *buffer += size;  // move buffer past string we just read
+}
+
+void checkByteArray(uint8_t** buffer, const vector<uint8_t>& expectedByteArray) {
+    uint32_t size = readNext<uint32_t>(buffer);
+    vector<uint8_t> parsedByteArray(*buffer, *buffer + size);
+    EXPECT_EQ(parsedByteArray, expectedByteArray);
+    *buffer += size;  // move buffer past byte array we just read
+}
+
+template <class T>
+void checkAnnotation(uint8_t** buffer, uint8_t annotationId, uint8_t typeId, T annotationValue) {
+    EXPECT_EQ(readNext<uint8_t>(buffer), annotationId);
+    EXPECT_EQ(readNext<uint8_t>(buffer), typeId);
+    checkScalar<T>(buffer, annotationValue);
+}
+
+void checkMetadata(uint8_t** buffer, uint8_t numElements, int64_t startTime, int64_t endTime,
+                   uint32_t atomId) {
+    // All events start with OBJECT_TYPE id.
+    checkTypeHeader(buffer, OBJECT_TYPE);
+
+    // We increment by 2 because the number of elements listed in the
+    // serialization accounts for the timestamp and atom id as well.
+    checkScalar(buffer, static_cast<uint8_t>(numElements + 2));
+
+    // Check timestamp
+    checkTypeHeader(buffer, INT64_TYPE);
+    int64_t timestamp = readNext<int64_t>(buffer);
+    EXPECT_GE(timestamp, startTime);
+    EXPECT_LE(timestamp, endTime);
+
+    // Check atom id
+    checkTypeHeader(buffer, INT32_TYPE);
+    checkScalar(buffer, atomId);
+}
+
+TEST(StatsEventTest, TestScalars) {
+    uint32_t atomId = 100;
+    int32_t int32Value = -5;
+    int64_t int64Value = -2 * android::elapsedRealtimeNano();
+    float floatValue = 2.0;
+    bool boolValue = false;
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, atomId);
+    stats_event_write_int32(event, int32Value);
+    stats_event_write_int64(event, int64Value);
+    stats_event_write_float(event, floatValue);
+    stats_event_write_bool(event, boolValue);
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/4, startTime, endTime, atomId);
+
+    // check int32 element
+    checkTypeHeader(&buffer, INT32_TYPE);
+    checkScalar(&buffer, int32Value);
+
+    // check int64 element
+    checkTypeHeader(&buffer, INT64_TYPE);
+    checkScalar(&buffer, int64Value);
+
+    // check float element
+    checkTypeHeader(&buffer, FLOAT_TYPE);
+    checkScalar(&buffer, floatValue);
+
+    // check bool element
+    checkTypeHeader(&buffer, BOOL_TYPE);
+    checkScalar(&buffer, boolValue);
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestStrings) {
+    uint32_t atomId = 100;
+    string str = "test_string";
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, atomId);
+    stats_event_write_string8(event, str.c_str());
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/1, startTime, endTime, atomId);
+
+    checkTypeHeader(&buffer, STRING_TYPE);
+    checkString(&buffer, str);
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestByteArrays) {
+    uint32_t atomId = 100;
+    vector<uint8_t> message = {'b', 'y', 't', '\0', 'e', 's'};
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, atomId);
+    stats_event_write_byte_array(event, message.data(), message.size());
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/1, startTime, endTime, atomId);
+
+    checkTypeHeader(&buffer, BYTE_ARRAY_TYPE);
+    checkByteArray(&buffer, message);
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestAttributionChains) {
+    uint32_t atomId = 100;
+
+    uint8_t numNodes = 50;
+    uint32_t uids[numNodes];
+    vector<string> tags(numNodes);  // storage that cTag elements point to
+    const char* cTags[numNodes];
+    for (int i = 0; i < (int)numNodes; i++) {
+        uids[i] = i;
+        tags.push_back("test" + std::to_string(i));
+        cTags[i] = tags[i].c_str();
+    }
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, atomId);
+    stats_event_write_attribution_chain(event, uids, cTags, numNodes);
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/1, startTime, endTime, atomId);
+
+    checkTypeHeader(&buffer, ATTRIBUTION_CHAIN_TYPE);
+    checkScalar(&buffer, numNodes);
+    for (int i = 0; i < numNodes; i++) {
+        checkScalar(&buffer, uids[i]);
+        checkString(&buffer, tags[i]);
+    }
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestKeyValuePairs) {
+    uint32_t atomId = 100;
+
+    uint8_t numPairs = 4;
+    struct key_value_pair pairs[numPairs];
+    pairs[0] = {.key = 0, .valueType = INT32_TYPE, .int32Value = -1};
+    pairs[1] = {.key = 1, .valueType = INT64_TYPE, .int64Value = 0x123456789};
+    pairs[2] = {.key = 2, .valueType = FLOAT_TYPE, .floatValue = 5.5};
+    string str = "test_key_value_pair_string";
+    pairs[3] = {.key = 3, .valueType = STRING_TYPE, .stringValue = str.c_str()};
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, atomId);
+    stats_event_write_key_value_pairs(event, pairs, numPairs);
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/1, startTime, endTime, atomId);
+
+    checkTypeHeader(&buffer, KEY_VALUE_PAIRS_TYPE);
+    checkScalar(&buffer, numPairs);
+
+    // first pair
+    checkScalar(&buffer, pairs[0].key);
+    checkTypeHeader(&buffer, pairs[0].valueType);
+    checkScalar(&buffer, pairs[0].int32Value);
+
+    // second pair
+    checkScalar(&buffer, pairs[1].key);
+    checkTypeHeader(&buffer, pairs[1].valueType);
+    checkScalar(&buffer, pairs[1].int64Value);
+
+    // third pair
+    checkScalar(&buffer, pairs[2].key);
+    checkTypeHeader(&buffer, pairs[2].valueType);
+    checkScalar(&buffer, pairs[2].floatValue);
+
+    // fourth pair
+    checkScalar(&buffer, pairs[3].key);
+    checkTypeHeader(&buffer, pairs[3].valueType);
+    checkString(&buffer, str);
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestAnnotations) {
+    uint32_t atomId = 100;
+
+    // first element information
+    bool boolValue = false;
+    uint8_t boolAnnotation1Id = 1;
+    uint8_t boolAnnotation2Id = 2;
+    bool boolAnnotation1Value = true;
+    int32_t boolAnnotation2Value = 3;
+
+    // second element information
+    float floatValue = -5.0;
+    uint8_t floatAnnotation1Id = 3;
+    uint8_t floatAnnotation2Id = 4;
+    int32_t floatAnnotation1Value = 8;
+    bool floatAnnotation2Value = false;
+
+    int64_t startTime = android::elapsedRealtimeNano();
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, 100);
+    stats_event_write_bool(event, boolValue);
+    stats_event_add_bool_annotation(event, boolAnnotation1Id, boolAnnotation1Value);
+    stats_event_add_int32_annotation(event, boolAnnotation2Id, boolAnnotation2Value);
+    stats_event_write_float(event, floatValue);
+    stats_event_add_int32_annotation(event, floatAnnotation1Id, floatAnnotation1Value);
+    stats_event_add_bool_annotation(event, floatAnnotation2Id, floatAnnotation2Value);
+    stats_event_build(event);
+    int64_t endTime = android::elapsedRealtimeNano();
+
+    size_t bufferSize;
+    uint8_t* buffer = stats_event_get_buffer(event, &bufferSize);
+    uint8_t* bufferEnd = buffer + bufferSize;
+
+    checkMetadata(&buffer, /*numElements=*/2, startTime, endTime, atomId);
+
+    // check first element
+    checkTypeHeader(&buffer, BOOL_TYPE, /*numAnnotations=*/2);
+    checkScalar(&buffer, boolValue);
+    checkAnnotation(&buffer, boolAnnotation1Id, BOOL_TYPE, boolAnnotation1Value);
+    checkAnnotation(&buffer, boolAnnotation2Id, INT32_TYPE, boolAnnotation2Value);
+
+    // check second element
+    checkTypeHeader(&buffer, FLOAT_TYPE, /*numAnnotations=*/2);
+    checkScalar(&buffer, floatValue);
+    checkAnnotation(&buffer, floatAnnotation1Id, INT32_TYPE, floatAnnotation1Value);
+    checkAnnotation(&buffer, floatAnnotation2Id, BOOL_TYPE, floatAnnotation2Value);
+
+    EXPECT_EQ(buffer, bufferEnd);  // ensure that we have read the entire buffer
+    EXPECT_EQ(stats_event_get_errors(event), 0);
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestNoAtomIdError) {
+    struct stats_event* event = stats_event_obtain();
+    // Don't set the atom id in order to trigger the error.
+    stats_event_build(event);
+
+    uint32_t errors = stats_event_get_errors(event);
+    EXPECT_NE(errors | ERROR_NO_ATOM_ID, 0);
+
+    stats_event_release(event);
+}
+
+TEST(StatsEventTest, TestOverflowError) {
+    struct stats_event* event = stats_event_obtain();
+    stats_event_set_atom_id(event, 100);
+    // Add 1000 int32s to the event. Each int32 takes 5 bytes so this will
+    // overflow the 4068 byte buffer.
+    for (int i = 0; i < 1000; i++) {
+        stats_event_write_int32(event, 0);
+    }
+    stats_event_build(event);
+
+    uint32_t errors = stats_event_get_errors(event);
+    EXPECT_NE(errors | ERROR_OVERFLOW, 0);
+
+    stats_event_release(event);
+}
diff --git a/rootdir/Android.mk b/rootdir/Android.mk
index 5821379..a9d0ed0 100644
--- a/rootdir/Android.mk
+++ b/rootdir/Android.mk
@@ -67,6 +67,11 @@
   EXPORT_GLOBAL_GCOV_OPTIONS := export GCOV_PREFIX /data/misc/trace
 endif
 
+EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS :=
+ifeq ($(CLANG_COVERAGE),true)
+  EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS := export LLVM_PROFILE_FILE /data/misc/trace/clang-%p-%m.profraw
+endif
+
 # Put it here instead of in init.rc module definition,
 # because init.rc is conditionally included.
 #
@@ -147,6 +152,7 @@
 	$(hide) sed -i -e 's?%SYSTEMSERVERCLASSPATH%?$(PRODUCT_SYSTEM_SERVER_CLASSPATH)?g' $@
 	$(hide) sed -i -e 's?%EXPORT_GLOBAL_ASAN_OPTIONS%?$(EXPORT_GLOBAL_ASAN_OPTIONS)?g' $@
 	$(hide) sed -i -e 's?%EXPORT_GLOBAL_GCOV_OPTIONS%?$(EXPORT_GLOBAL_GCOV_OPTIONS)?g' $@
+	$(hide) sed -i -e 's?%EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS%?$(EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS)?g' $@
 	$(hide) sed -i -e 's?%EXPORT_GLOBAL_HWASAN_OPTIONS%?$(EXPORT_GLOBAL_HWASAN_OPTIONS)?g' $@
 
 # Append PLATFORM_VNDK_VERSION to base name.
diff --git a/rootdir/init.environ.rc.in b/rootdir/init.environ.rc.in
index 50005d9..fdaaf1a 100644
--- a/rootdir/init.environ.rc.in
+++ b/rootdir/init.environ.rc.in
@@ -15,4 +15,5 @@
     export SYSTEMSERVERCLASSPATH %SYSTEMSERVERCLASSPATH%
     %EXPORT_GLOBAL_ASAN_OPTIONS%
     %EXPORT_GLOBAL_GCOV_OPTIONS%
+    %EXPORT_GLOBAL_CLANG_COVERAGE_OPTIONS%
     %EXPORT_GLOBAL_HWASAN_OPTIONS%
diff --git a/rootdir/init.rc b/rootdir/init.rc
index 6a6169c..e575808 100644
--- a/rootdir/init.rc
+++ b/rootdir/init.rc
@@ -173,7 +173,7 @@
     mkdir /mnt/user/0/emulated/0 0755 root root
 
     # Prepare directories for pass through processes
-    mkdir /mnt/pass_through 0755 root root
+    mkdir /mnt/pass_through 0700 root root
     mkdir /mnt/pass_through/0 0755 root root
     mkdir /mnt/pass_through/0/self 0755 root root
     mkdir /mnt/pass_through/0/emulated 0755 root root