Add memory leak detection to audioserver

adb shell dumpsys media.audio_flinger --unreachable

(userdebug and eng builds)

Bug: 28049120
Change-Id: I72ac624bd5b5482aac44b9f3d8429f657f715cb4
diff --git a/services/audioflinger/Android.mk b/services/audioflinger/Android.mk
index 6700f6e..4f826e5 100644
--- a/services/audioflinger/Android.mk
+++ b/services/audioflinger/Android.mk
@@ -34,6 +34,7 @@
 LOCAL_C_INCLUDES := \
     $(TOPDIR)frameworks/av/services/audiopolicy \
     $(TOPDIR)external/sonic \
+    libcore/include \
     $(call include-path-for, audio-effects) \
     $(call include-path-for, audio-utils)
 
@@ -54,7 +55,8 @@
     libpowermanager \
     libserviceutility \
     libsonic \
-    libmediautils
+    libmediautils \
+    libmemunreachable
 
 LOCAL_STATIC_LIBRARIES := \
     libcpustats \
diff --git a/services/audioflinger/AudioFlinger.cpp b/services/audioflinger/AudioFlinger.cpp
index 208dc8b..1b69f8c 100644
--- a/services/audioflinger/AudioFlinger.cpp
+++ b/services/audioflinger/AudioFlinger.cpp
@@ -31,6 +31,7 @@
 #include <utils/Log.h>
 #include <utils/Trace.h>
 #include <binder/Parcel.h>
+#include <memunreachable/memunreachable.h>
 #include <utils/String16.h>
 #include <utils/threads.h>
 #include <utils/Atomic.h>
@@ -462,6 +463,21 @@
                 binder->dump(fd, args);
             }
         }
+
+        // check for optional arguments
+        bool unreachableMemory = false;
+        for (const auto &arg : args) {
+            if (arg == String16("--unreachable")) {
+                unreachableMemory = true;
+            }
+        }
+
+        if (unreachableMemory) {
+            dprintf(fd, "\nDumping unreachable memory:\n");
+            // TODO - should limit be an argument parameter?
+            std::string s = GetUnreachableMemoryString(true /* contents */, 10000 /* limit */);
+            write(fd, s.c_str(), s.size());
+        }
     }
     return NO_ERROR;
 }