Add cmd to let statsd print all logs it received for debugging.

It only works on eng build. And all code is behind a build flag, so the
code will be stripped out in production builds.

Bug: 78239479
Test: manual
Change-Id: I20ee51822d18e6c77ca324a5327712cbed09593e
diff --git a/cmds/statsd/Android.mk b/cmds/statsd/Android.mk
index 7198bad..091268e 100644
--- a/cmds/statsd/Android.mk
+++ b/cmds/statsd/Android.mk
@@ -141,10 +141,12 @@
 
 LOCAL_MODULE_CLASS := EXECUTABLES
 
-# Enable sanitizer on eng builds
+# Enable sanitizer and allow very verbose printing on eng builds
 ifeq ($(TARGET_BUILD_VARIANT),eng)
     LOCAL_CLANG := true
     LOCAL_SANITIZE := address
+    LOCAL_CFLAGS += \
+        -DVERY_VERBOSE_PRINTING
 endif
 
 LOCAL_INIT_RC := statsd.rc
diff --git a/cmds/statsd/src/StatsLogProcessor.cpp b/cmds/statsd/src/StatsLogProcessor.cpp
index 986f2ef..173f8fd 100644
--- a/cmds/statsd/src/StatsLogProcessor.cpp
+++ b/cmds/statsd/src/StatsLogProcessor.cpp
@@ -164,6 +164,12 @@
 
 void StatsLogProcessor::OnLogEvent(LogEvent* event, bool reconnected) {
     std::lock_guard<std::mutex> lock(mMetricsMutex);
+
+#ifdef VERY_VERBOSE_PRINTING
+    if (mPrintAllLogs) {
+        ALOGI("%s", event->ToString().c_str());
+    }
+#endif
     const int64_t currentTimestampNs = event->GetElapsedTimestampNs();
 
     if (reconnected && mLastTimestampSeen != 0) {
diff --git a/cmds/statsd/src/StatsLogProcessor.h b/cmds/statsd/src/StatsLogProcessor.h
index c3c4663..17fa5cd 100644
--- a/cmds/statsd/src/StatsLogProcessor.h
+++ b/cmds/statsd/src/StatsLogProcessor.h
@@ -89,6 +89,13 @@
 
     int64_t getLastReportTimeNs(const ConfigKey& key);
 
+    inline void setPrintLogs(bool enabled) {
+#ifdef VERY_VERBOSE_PRINTING
+        std::lock_guard<std::mutex> lock(mMetricsMutex);
+        mPrintAllLogs = enabled;
+#endif
+    }
+
 private:
     // For testing only.
     inline sp<AlarmMonitor> getAnomalyAlarmMonitor() const {
@@ -164,6 +171,10 @@
 
     long mLastPullerCacheClearTimeSec = 0;
 
+#ifdef VERY_VERBOSE_PRINTING
+    bool mPrintAllLogs = false;
+#endif
+
     FRIEND_TEST(StatsLogProcessorTest, TestOutOfOrderLogs);
     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitByteSize);
     FRIEND_TEST(StatsLogProcessorTest, TestRateLimitBroadcast);
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index 0e7b4f9..0ca3caa 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -334,6 +334,10 @@
         if (!args[0].compare(String8("clear-puller-cache"))) {
             return cmd_clear_puller_cache(out);
         }
+
+        if (!args[0].compare(String8("print-logs"))) {
+            return cmd_print_logs(out, args);
+        }
     }
 
     print_cmd_help(out);
@@ -419,6 +423,9 @@
     fprintf(out, "\n");
     fprintf(out, "usage: adb shell cmd stats clear-puller-cache\n");
     fprintf(out, "  Clear cached puller data.\n");
+    fprintf(out, "\n");
+    fprintf(out, "usage: adb shell cmd stats print-logs\n");
+    fprintf(out, "      Only works on eng build\n");
 }
 
 status_t StatsService::cmd_trigger_broadcast(FILE* out, Vector<String8>& args) {
@@ -738,6 +745,22 @@
     }
 }
 
+status_t StatsService::cmd_print_logs(FILE* out, const Vector<String8>& args) {
+    IPCThreadState* ipc = IPCThreadState::self();
+    VLOG("StatsService::cmd_print_logs with Pid %i, Uid %i", ipc->getCallingPid(),
+         ipc->getCallingUid());
+    if (checkCallingPermission(String16(kPermissionDump))) {
+        bool enabled = true;
+        if (args.size() >= 2) {
+            enabled = atoi(args[1].c_str()) != 0;
+        }
+        mProcessor->setPrintLogs(enabled);
+        return NO_ERROR;
+    } else {
+        return PERMISSION_DENIED;
+    }
+}
+
 Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int64_t>& version,
                                       const vector<String16>& app) {
     ENFORCE_UID(AID_SYSTEM);
diff --git a/cmds/statsd/src/StatsService.h b/cmds/statsd/src/StatsService.h
index e409a71..70b0aed 100644
--- a/cmds/statsd/src/StatsService.h
+++ b/cmds/statsd/src/StatsService.h
@@ -221,6 +221,11 @@
     status_t cmd_clear_puller_cache(FILE* out);
 
     /**
+     * Print all stats logs received to logcat.
+     */
+    status_t cmd_print_logs(FILE* out, const Vector<String8>& args);
+
+    /**
      * Adds a configuration after checking permissions and obtaining UID from binder call.
      */
     bool addConfigurationChecked(int uid, int64_t key, const vector<uint8_t>& config);