Merge "storaged: move proto file to /data/misc_ce/0/storaged"
diff --git a/base/Android.bp b/base/Android.bp
index 0fd00ea..f4a8411 100644
--- a/base/Android.bp
+++ b/base/Android.bp
@@ -64,16 +64,20 @@
     target: {
         android: {
             srcs: [
-                "errors_unix.cpp",
                 "properties.cpp",
-                "chrono_utils.cpp",
             ],
-            cppflags: ["-Wexit-time-destructors"],
             sanitize: {
                 misc_undefined: ["integer"],
             },
 
         },
+        linux: {
+            srcs: [
+                "chrono_utils.cpp",
+                "errors_unix.cpp",
+            ],
+            cppflags: ["-Wexit-time-destructors"],
+        },
         darwin: {
             srcs: [
                 "chrono_utils.cpp",
@@ -82,20 +86,8 @@
             cppflags: ["-Wexit-time-destructors"],
         },
         linux_bionic: {
-            srcs: [
-                "chrono_utils.cpp",
-                "errors_unix.cpp",
-            ],
-            cppflags: ["-Wexit-time-destructors"],
             enabled: true,
         },
-        linux_glibc: {
-            srcs: [
-                "chrono_utils.cpp",
-                "errors_unix.cpp",
-            ],
-            cppflags: ["-Wexit-time-destructors"],
-        },
         windows: {
             srcs: [
                 "errors_windows.cpp",
@@ -127,15 +119,12 @@
     ],
     target: {
         android: {
-            srcs: [
-                "chrono_utils_test.cpp",
-                "properties_test.cpp"
-            ],
+            srcs: ["properties_test.cpp"],
             sanitize: {
                 misc_undefined: ["integer"],
             },
         },
-        linux_glibc: {
+        linux: {
             srcs: ["chrono_utils_test.cpp"],
         },
         windows: {
diff --git a/bootstat/bootstat.cpp b/bootstat/bootstat.cpp
index f8eaa1c..2270133 100644
--- a/bootstat/bootstat.cpp
+++ b/bootstat/bootstat.cpp
@@ -212,6 +212,17 @@
     {"hard,hw_reset", 72},
     {"shutdown,suspend", 73},    // Suspend to RAM
     {"shutdown,hibernate", 74},  // Suspend to DISK
+    {"power_on_key", 75},
+    {"reboot_by_key", 76},
+    {"wdt_by_pass_pwk", 77},
+    {"reboot_longkey", 78},
+    {"powerkey", 79},
+    {"usb", 80},
+    {"wdt", 81},
+    {"tool_by_pass_pwk", 82},
+    {"2sec_reboot", 83},
+    {"reboot,by_key", 84},
+    {"reboot,longkey", 85},
 };
 
 // Converts a string value representing the reason the system booted to an
@@ -508,7 +519,6 @@
           android::base::ReadFdToString(fileno(fp), &content);
         }
         android_logcat_pclose(&ctx, fp);
-        android_logcat_destroy(&ctx);
         static const char logcat_battery[] = "W/healthd (    0): battery l=";
         const char* match = logcat_battery;
 
diff --git a/init/builtins.cpp b/init/builtins.cpp
index 9be274b..950a551 100644
--- a/init/builtins.cpp
+++ b/init/builtins.cpp
@@ -1033,7 +1033,7 @@
         {"verity_load_state",       {0,     0,    {false,  do_verity_load_state}}},
         {"verity_update_state",     {0,     0,    {false,  do_verity_update_state}}},
         {"wait",                    {1,     2,    {true,   do_wait}}},
-        {"wait_for_prop",           {2,     2,    {true,   do_wait_for_prop}}},
+        {"wait_for_prop",           {2,     2,    {false,  do_wait_for_prop}}},
         {"write",                   {2,     2,    {true,   do_write}}},
     };
     // clang-format on
diff --git a/init/subcontext.h b/init/subcontext.h
index ac77e08..e920034 100644
--- a/init/subcontext.h
+++ b/init/subcontext.h
@@ -35,7 +35,7 @@
 class Subcontext {
   public:
     Subcontext(std::string path_prefix, std::string context)
-        : path_prefix_(path_prefix), context_(std::move(context)) {
+        : path_prefix_(std::move(path_prefix)), context_(std::move(context)) {
         Fork();
     }
 
diff --git a/libbacktrace/Android.bp b/libbacktrace/Android.bp
index 2f20684..b2c0c0f 100644
--- a/libbacktrace/Android.bp
+++ b/libbacktrace/Android.bp
@@ -65,7 +65,7 @@
 
 cc_library {
     name: "libbacktrace",
-    vendor_available: true,
+    vendor_available: false,
     vndk: {
         enabled: true,
         support_system_process: true,
@@ -83,20 +83,7 @@
         darwin: {
             enabled: true,
         },
-        linux_glibc: {
-            srcs: libbacktrace_sources,
-
-            shared_libs: [
-                "libbase",
-                "liblog",
-                "libunwind",
-                "libunwindstack",
-            ],
-
-            static_libs: ["libcutils"],
-        },
-        linux_bionic: {
-            enabled: true,
+        linux: {
             srcs: libbacktrace_sources,
 
             shared_libs: [
@@ -109,16 +96,7 @@
             static_libs: ["libcutils"],
         },
         android: {
-            srcs: libbacktrace_sources,
-
-            shared_libs: [
-                "libbase",
-                "liblog",
-                "libunwind",
-                "libunwindstack",
-            ],
-
-            static_libs: ["libasync_safe", "libcutils"],
+            static_libs: ["libasync_safe"],
         },
     },
     whole_static_libs: ["libdemangle"],
@@ -135,13 +113,7 @@
     srcs: ["backtrace_testlib.cpp"],
 
     target: {
-        linux_glibc: {
-            shared_libs: [
-                "libunwind",
-                "libunwindstack",
-            ],
-        },
-        android: {
+        linux: {
             shared_libs: [
                 "libunwind",
                 "libunwindstack",
diff --git a/libbacktrace/Backtrace.cpp b/libbacktrace/Backtrace.cpp
index 81f5e32..afe518c 100644
--- a/libbacktrace/Backtrace.cpp
+++ b/libbacktrace/Backtrace.cpp
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include <assert.h>
 #include <inttypes.h>
 #include <stdint.h>
 #include <stdlib.h>
@@ -30,9 +31,10 @@
 #include <demangle.h>
 
 #include "BacktraceLog.h"
-#include "thread_utils.h"
 #include "UnwindCurrent.h"
 #include "UnwindPtrace.h"
+#include "UnwindStack.h"
+#include "thread_utils.h"
 
 using android::base::StringPrintf;
 
@@ -140,6 +142,34 @@
   }
 }
 
+Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
+  if (pid == BACKTRACE_CURRENT_PROCESS) {
+    pid = getpid();
+    if (tid == BACKTRACE_CURRENT_THREAD) {
+      tid = gettid();
+    }
+  } else if (tid == BACKTRACE_CURRENT_THREAD) {
+    tid = pid;
+  }
+
+  if (map == nullptr) {
+// This would cause the wrong type of map object to be created, so disallow.
+#if defined(__ANDROID__)
+    __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
+              "Backtrace::CreateNew() must be called with a real map pointer.");
+#else
+    BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
+    abort();
+#endif
+  }
+
+  if (pid == getpid()) {
+    return new UnwindStackCurrent(pid, tid, map);
+  } else {
+    return new UnwindStackPtrace(pid, tid, map);
+  }
+}
+
 std::string Backtrace::GetErrorString(BacktraceUnwindError error) {
   switch (error) {
   case BACKTRACE_UNWIND_NO_ERROR:
diff --git a/libbacktrace/UnwindStack.cpp b/libbacktrace/UnwindStack.cpp
index 41153ce..73bfdec 100644
--- a/libbacktrace/UnwindStack.cpp
+++ b/libbacktrace/UnwindStack.cpp
@@ -15,7 +15,6 @@
  */
 
 #define _GNU_SOURCE 1
-#include <assert.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
@@ -93,7 +92,7 @@
     back_frame->pc = frame->pc;
     back_frame->sp = frame->sp;
 
-    back_frame->func_name = frame->function_name;
+    back_frame->func_name = demangle(frame->function_name.c_str());
     back_frame->func_offset = frame->function_offset;
 
     back_frame->map.name = frame->map_name;
@@ -149,31 +148,3 @@
   error_ = BACKTRACE_UNWIND_NO_ERROR;
   return ::Unwind(regs.get(), GetMap(), &frames_, num_ignore_frames);
 }
-
-Backtrace* Backtrace::CreateNew(pid_t pid, pid_t tid, BacktraceMap* map) {
-  if (pid == BACKTRACE_CURRENT_PROCESS) {
-    pid = getpid();
-    if (tid == BACKTRACE_CURRENT_THREAD) {
-      tid = gettid();
-    }
-  } else if (tid == BACKTRACE_CURRENT_THREAD) {
-    tid = pid;
-  }
-
-  if (map == nullptr) {
-// This would cause the wrong type of map object to be created, so disallow.
-#if defined(__ANDROID__)
-    __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__,
-              "Backtrace::CreateNew() must be called with a real map pointer.");
-#else
-    BACK_LOGE("Backtrace::CreateNew() must be called with a real map pointer.");
-    abort();
-#endif
-  }
-
-  if (pid == getpid()) {
-    return new UnwindStackCurrent(pid, tid, map);
-  } else {
-    return new UnwindStackPtrace(pid, tid, map);
-  }
-}
diff --git a/liblog/include/log/log_event_list.h b/liblog/include/log/log_event_list.h
index bb1ce34..4d24c68 100644
--- a/liblog/include/log/log_event_list.h
+++ b/liblog/include/log/log_event_list.h
@@ -108,6 +108,12 @@
 android_log_list_element android_log_read_next(android_log_context ctx);
 android_log_list_element android_log_peek_next(android_log_context ctx);
 
+/**
+ * Convert a writer context to a reader context. Useful for testing.
+ * Returns an error if ctx is already a reader.
+ */
+int android_log_writer_to_reader(android_log_context ctx);
+
 /* Finished with reader or writer context */
 int android_log_destroy(android_log_context* ctx);
 
@@ -122,6 +128,7 @@
  private:
   android_log_context ctx;
   int ret;
+  int tag_;
 
   android_log_event_list(const android_log_event_list&) = delete;
   void operator=(const android_log_event_list&) = delete;
@@ -129,11 +136,16 @@
  public:
   explicit android_log_event_list(int tag) : ret(0) {
     ctx = create_android_logger(static_cast<uint32_t>(tag));
+    tag_ = tag;
   }
+
   explicit android_log_event_list(log_msg& log_msg) : ret(0) {
-    ctx = create_android_log_parser(log_msg.msg() + sizeof(uint32_t),
+    const char* buf = log_msg.msg();
+    ctx = create_android_log_parser(buf + sizeof(uint32_t),
                                     log_msg.entry.len - sizeof(uint32_t));
+    tag_ = buf[0] | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
   }
+
   ~android_log_event_list() {
     android_log_destroy(&ctx);
   }
@@ -149,6 +161,10 @@
     return ctx;
   }
 
+  android_log_context context() const {
+    return ctx;
+  }
+
   /* return errors or transmit status */
   int status() const {
     return ret;
@@ -159,12 +175,17 @@
     if (retval < 0) ret = retval;
     return ret;
   }
+
   int end() {
     int retval = android_log_write_list_end(ctx);
     if (retval < 0) ret = retval;
     return ret;
   }
 
+  uint32_t tag() {
+    return tag_;
+  }
+
   android_log_event_list& operator<<(int32_t value) {
     int retval = android_log_write_int32(ctx, value);
     if (retval < 0) ret = retval;
@@ -296,6 +317,10 @@
     return ret >= 0;
   }
 
+  int convert_to_reader() {
+    return android_log_writer_to_reader(ctx);
+  }
+
   android_log_list_element read() {
     return android_log_read_next(ctx);
   }
diff --git a/liblog/log_event_list.c b/liblog/log_event_list.c
index a59cb87..f6e13db 100644
--- a/liblog/log_event_list.c
+++ b/liblog/log_event_list.c
@@ -565,3 +565,26 @@
 android_log_peek_next(android_log_context ctx) {
   return android_log_read_next_internal(ctx, 1);
 }
+
+LIBLOG_ABI_PUBLIC int android_log_writer_to_reader(android_log_context ctx) {
+  android_log_context_internal* context;
+
+  context = (android_log_context_internal*)ctx;
+
+  if (!context || context->read_write_flag != kAndroidLoggerWrite) {
+    return -EBADF;
+  }
+
+  context->len = context->pos;
+  context->storage[1] =
+      context
+          ->count[0];  // What does this do?!?! It's copied from the write func
+  context->pos = 0;
+  memset(context->count, 0, sizeof(context->count));
+  memset(context->list, 0, sizeof(context->list));
+  context->list_nest_depth = 0;
+  context->read_write_flag = kAndroidLoggerRead;
+  context->list_stop = false;
+
+  return 0;
+}
diff --git a/libutils/Android.bp b/libutils/Android.bp
index 1bf5a64..b70845b 100644
--- a/libutils/Android.bp
+++ b/libutils/Android.bp
@@ -97,8 +97,6 @@
     target: {
         android: {
             srcs: [
-                "Looper.cpp",
-                "ProcessCallStack.cpp",
                 "Trace.cpp",
             ],
 
@@ -115,6 +113,12 @@
                 misc_undefined: ["integer"],
             },
         },
+        linux: {
+            srcs: [
+                "Looper.cpp",
+                "ProcessCallStack.cpp",
+            ],
+        },
 
         host: {
             cflags: ["-DLIBUTILS_NATIVE=1"],
@@ -124,18 +128,8 @@
             },
         },
 
-        linux_glibc: {
-            srcs: [
-                "Looper.cpp",
-                "ProcessCallStack.cpp",
-            ],
-        },
         linux_bionic: {
             enabled: true,
-            srcs: [
-                "Looper.cpp",
-                "ProcessCallStack.cpp",
-            ],
         },
 
         darwin: {
diff --git a/libutils/tests/Android.bp b/libutils/tests/Android.bp
index 6911fc5..a891fca 100644
--- a/libutils/tests/Android.bp
+++ b/libutils/tests/Android.bp
@@ -34,8 +34,6 @@
     target: {
         android: {
             srcs: [
-                "Looper_test.cpp",
-                "RefBase_test.cpp",
                 "SystemClock_test.cpp",
             ],
             shared_libs: [
@@ -46,7 +44,7 @@
                 "libbase",
             ],
         },
-        linux_glibc: {
+        linux: {
             srcs: [
                 "Looper_test.cpp",
                 "RefBase_test.cpp",