Merge "adb unittest for win32 handle inheritance"
diff --git a/crash_reporter/crash_reporter.cc b/crash_reporter/crash_reporter.cc
index 72eeeda..7872f7b 100644
--- a/crash_reporter/crash_reporter.cc
+++ b/crash_reporter/crash_reporter.cc
@@ -20,6 +20,7 @@
 #include <vector>
 
 #include <base/files/file_util.h>
+#include <base/guid.h>
 #include <base/logging.h>
 #include <base/strings/string_split.h>
 #include <base/strings/string_util.h>
@@ -41,6 +42,7 @@
 static const char kKernelCrashDetected[] = "/var/run/kernel-crash-detected";
 static const char kUncleanShutdownDetected[] =
     "/var/run/unclean-shutdown-detected";
+static const char kGUIDFileName[] = "/data/misc/crash_reporter/guid";
 
 // Enumeration of kinds of crashes to be used in the CrashCounter histogram.
 enum CrashKinds {
@@ -122,6 +124,21 @@
                       const bool clean_shutdown) {
   CHECK(!clean_shutdown) << "Incompatible options";
 
+  // Try to read the GUID from kGUIDFileName.  If the file doesn't exist, is
+  // blank, or the read fails, generate a new GUID and write it to the file.
+  std::string guid;
+  base::FilePath filepath(kGUIDFileName);
+  if (!base::ReadFileToString(filepath, &guid) || guid.empty()) {
+    guid = base::GenerateGUID();
+    // If we can't read or write the file, log an error.  However it is not
+    // a fatal error, as the crash server will assign a random GUID based
+    // on a hash of the IP address if one is not provided in the report.
+    if (base::WriteFile(filepath, guid.c_str(), guid.size()) <= 0) {
+      LOG(ERROR) << "Could not write guid " << guid << " to file "
+                 << filepath.value();
+    }
+  }
+
   bool was_kernel_crash = false;
   bool was_unclean_shutdown = false;
   kernel_collector->Enable();
diff --git a/crash_reporter/crash_sender b/crash_reporter/crash_sender
index d0d6772..29a1229 100755
--- a/crash_reporter/crash_sender
+++ b/crash_reporter/crash_sender
@@ -22,9 +22,8 @@
 # Base directory that contains any crash reporter state files.
 CRASH_STATE_DIR="/data/misc/crash_reporter"
 
-# File whose existence implies crash reports may be sent, and whose
-# contents includes our machine's anonymized guid.
-CONSENT_ID="/data/misc/metrics/enabled"
+# File containing crash_reporter's anonymized guid.
+GUID_FILE="${CRASH_STATE_DIR}/guid"
 
 # Crash sender lock in case the sender is already running.
 CRASH_SENDER_LOCK="${CRASH_STATE_DIR}/lock/crash_sender"
@@ -393,7 +392,7 @@
 
   # Need to strip dashes ourselves as Chrome preserves it in the file
   # nowadays.  This is also what the Chrome breakpad client does.
-  guid=$(tr -d '-' < "${CONSENT_ID}")
+  guid=$(tr -d '-' < "${GUID_FILE}")
 
   local error_type="$(get_key_value "${meta_path}" "error_type")"
   [ "${error_type}" = "undefined" ] && error_type=
diff --git a/liblog/Android.bp b/liblog/Android.bp
new file mode 100644
index 0000000..ee394fd
--- /dev/null
+++ b/liblog/Android.bp
@@ -0,0 +1,76 @@
+//
+// Copyright (C) 2008-2014 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.
+//
+
+liblog_host_sources = [
+    "logd_write.c",
+    "log_event_write.c",
+    "fake_log_device.c",
+    //"event.logtags",
+]
+liblog_target_sources = [
+    "logd_write.c",
+    "log_event_write.c",
+    "event_tag_map.c",
+    "log_time.cpp",
+    "log_is_loggable.c",
+    "logprint.c",
+    "log_read.c",
+]
+
+// Shared and static library for host and device
+// ========================================================
+cc_library {
+    name: "liblog",
+    host_supported: true,
+
+    target: {
+        host: {
+            srcs: liblog_host_sources,
+            cflags: ["-DFAKE_LOG_DEVICE=1"],
+        },
+        android: {
+            srcs: liblog_target_sources,
+            // AddressSanitizer runtime library depends on liblog.
+            sanitize: ["never"],
+        },
+        android_arm: {
+            // TODO: This is to work around b/19059885. Remove after root cause is fixed
+            ldflags: ["-Wl,--hash-style=both"],
+        },
+        windows: {
+            srcs: ["uio.c"],
+        },
+        not_windows: {
+            srcs: ["event_tag_map.c"],
+        },
+        linux: {
+            host_ldlibs: ["-lrt"],
+        },
+    },
+
+    cflags: [
+        "-Werror",
+        // This is what we want to do:
+        //  liblog_cflags := $(shell \
+        //   sed -n \
+        //       's/^\([0-9]*\)[ \t]*liblog[ \t].*/-DLIBLOG_LOG_TAG=\1/p' \
+        //       $(LOCAL_PATH)/event.logtags)
+        // so make sure we do not regret hard-coding it as follows:
+        "-DLIBLOG_LOG_TAG=1005",
+    ],
+    compile_multilib: "both",
+    stl: "none",
+}
diff --git a/metricsd/metrics_daemon.cc b/metricsd/metrics_daemon.cc
index de7f2ea..b0e4b2f 100644
--- a/metricsd/metrics_daemon.cc
+++ b/metricsd/metrics_daemon.cc
@@ -241,7 +241,7 @@
   daily_active_use_.reset(
       new PersistentInteger("Platform.DailyUseTime"));
   version_cumulative_active_use_.reset(
-      new PersistentInteger("Platform.CumulativeDailyUseTime"));
+      new PersistentInteger("Platform.CumulativeUseTime"));
   version_cumulative_cpu_use_.reset(
       new PersistentInteger("Platform.CumulativeCpuTime"));
 
@@ -444,7 +444,7 @@
   UpdateStats(TimeTicks::Now(), Time::Now());
 
   // Reports the active use time since the last crash and resets it.
-  SendCrashIntervalSample(user_crash_interval_);
+  SendAndResetCrashIntervalSample(user_crash_interval_);
 
   any_crashes_daily_count_->Add(1);
   any_crashes_weekly_count_->Add(1);
@@ -457,7 +457,7 @@
   UpdateStats(TimeTicks::Now(), Time::Now());
 
   // Reports the active use time since the last crash and resets it.
-  SendCrashIntervalSample(kernel_crash_interval_);
+  SendAndResetCrashIntervalSample(kernel_crash_interval_);
 
   any_crashes_daily_count_->Add(1);
   any_crashes_weekly_count_->Add(1);
@@ -472,7 +472,7 @@
   UpdateStats(TimeTicks::Now(), Time::Now());
 
   // Reports the active use time since the last crash and resets it.
-  SendCrashIntervalSample(unclean_shutdown_interval_);
+  SendAndResetCrashIntervalSample(unclean_shutdown_interval_);
 
   unclean_shutdowns_daily_count_->Add(1);
   unclean_shutdowns_weekly_count_->Add(1);
@@ -1033,7 +1033,7 @@
   int64_t active_use_seconds = version_cumulative_active_use_->Get();
   if (active_use_seconds > 0) {
     SendSample(version_cumulative_active_use_->Name(),
-               active_use_seconds / 1000,  // stat is in seconds
+               active_use_seconds,
                1,                          // device may be used very little...
                8 * 1000 * 1000,            // ... or a lot (about 90 days)
                100);
@@ -1046,7 +1046,7 @@
   }
 }
 
-void MetricsDaemon::SendDailyUseSample(
+void MetricsDaemon::SendAndResetDailyUseSample(
     const scoped_ptr<PersistentInteger>& use) {
   SendSample(use->Name(),
              use->GetAndClear(),
@@ -1055,7 +1055,7 @@
              50);                      // number of buckets
 }
 
-void MetricsDaemon::SendCrashIntervalSample(
+void MetricsDaemon::SendAndResetCrashIntervalSample(
     const scoped_ptr<PersistentInteger>& interval) {
   SendSample(interval->Name(),
              interval->GetAndClear(),
@@ -1064,7 +1064,7 @@
              50);                      // number of buckets
 }
 
-void MetricsDaemon::SendCrashFrequencySample(
+void MetricsDaemon::SendAndResetCrashFrequencySample(
     const scoped_ptr<PersistentInteger>& frequency) {
   SendSample(frequency->Name(),
              frequency->GetAndClear(),
@@ -1097,21 +1097,20 @@
 
   if (daily_cycle_->Get() != day) {
     daily_cycle_->Set(day);
-    SendDailyUseSample(daily_active_use_);
-    SendDailyUseSample(version_cumulative_active_use_);
-    SendCrashFrequencySample(any_crashes_daily_count_);
-    SendCrashFrequencySample(user_crashes_daily_count_);
-    SendCrashFrequencySample(kernel_crashes_daily_count_);
-    SendCrashFrequencySample(unclean_shutdowns_daily_count_);
+    SendAndResetDailyUseSample(daily_active_use_);
+    SendAndResetCrashFrequencySample(any_crashes_daily_count_);
+    SendAndResetCrashFrequencySample(user_crashes_daily_count_);
+    SendAndResetCrashFrequencySample(kernel_crashes_daily_count_);
+    SendAndResetCrashFrequencySample(unclean_shutdowns_daily_count_);
     SendKernelCrashesCumulativeCountStats();
   }
 
   if (weekly_cycle_->Get() != week) {
     weekly_cycle_->Set(week);
-    SendCrashFrequencySample(any_crashes_weekly_count_);
-    SendCrashFrequencySample(user_crashes_weekly_count_);
-    SendCrashFrequencySample(kernel_crashes_weekly_count_);
-    SendCrashFrequencySample(unclean_shutdowns_weekly_count_);
+    SendAndResetCrashFrequencySample(any_crashes_weekly_count_);
+    SendAndResetCrashFrequencySample(user_crashes_weekly_count_);
+    SendAndResetCrashFrequencySample(kernel_crashes_weekly_count_);
+    SendAndResetCrashFrequencySample(unclean_shutdowns_weekly_count_);
   }
 }
 
diff --git a/metricsd/metrics_daemon.h b/metricsd/metrics_daemon.h
index b363c5e..fbb871e 100644
--- a/metricsd/metrics_daemon.h
+++ b/metricsd/metrics_daemon.h
@@ -171,15 +171,19 @@
   base::TimeDelta GetIncrementalCpuUse();
 
   // Sends a sample representing the number of seconds of active use
-  // for a 24-hour period.
-  void SendDailyUseSample(const scoped_ptr<PersistentInteger>& use);
+  // for a 24-hour period and reset |use|.
+  void SendAndResetDailyUseSample(
+      const scoped_ptr<PersistentInteger>& use);
 
   // Sends a sample representing a time interval between two crashes of the
-  // same type.
-  void SendCrashIntervalSample(const scoped_ptr<PersistentInteger>& interval);
+  // same type and reset |interval|.
+  void SendAndResetCrashIntervalSample(
+      const scoped_ptr<PersistentInteger>& interval);
 
-  // Sends a sample representing a frequency of crashes of some type.
-  void SendCrashFrequencySample(const scoped_ptr<PersistentInteger>& frequency);
+  // Sends a sample representing a frequency of crashes of some type and reset
+  // |frequency|.
+  void SendAndResetCrashFrequencySample(
+      const scoped_ptr<PersistentInteger>& frequency);
 
   // Initializes vm and disk stats reporting.
   void StatsReporterInit();