The first step towards adding a guardrail.

Added a malloc debug cmd to find memory leak and where
the most heap allocations are with backtrace.

More info: malloc_debug/README.md

The util class is borrowed from audio server.

Usage:

adb shell stop
adb shell setprop libc.debug.malloc.program statsd
adb shell setprop libc.debug.malloc.options backtrace
adb shell start
adb shell cmd stats meminfo

Also,

adb shell dumpsys meminfo [statsd_pid]

also gives you the overall summary of the memory usage.

Test: manual

Change-Id: I4eb4167466a748ea0c3d9fd39e45d9900bc00fc2
diff --git a/cmds/statsd/src/StatsService.cpp b/cmds/statsd/src/StatsService.cpp
index ef01ec7..0a5c526 100644
--- a/cmds/statsd/src/StatsService.cpp
+++ b/cmds/statsd/src/StatsService.cpp
@@ -17,10 +17,11 @@
 #define DEBUG true
 #include "Log.h"
 
-#include "android-base/stringprintf.h"
 #include "StatsService.h"
+#include "android-base/stringprintf.h"
 #include "config/ConfigKey.h"
 #include "config/ConfigManager.h"
+#include "guardrail/MemoryLeakTrackUtil.h"
 #include "storage/DropboxReader.h"
 
 #include <android-base/file.h>
@@ -226,6 +227,10 @@
         if (!args[0].compare(String8("clear-config"))) {
             return cmd_remove_config_files(out);
         }
+
+        if (!args[0].compare(String8("meminfo"))) {
+            return cmd_dump_memory_info(out);
+        }
     }
 
     print_cmd_help(out);
@@ -238,6 +243,15 @@
             "[timestamp_nsec_optional]\n");
     fprintf(out, "\n");
     fprintf(out, "\n");
+    fprintf(out, "usage: adb shell cmd stats meminfo\n");
+    fprintf(out, "\n");
+    fprintf(out, "  Prints the malloc debug information. You need to run the following first: \n");
+    fprintf(out, "   # adb shell stop\n");
+    fprintf(out, "   # adb shell setprop libc.debug.malloc.program statsd \n");
+    fprintf(out, "   # adb shell setprop libc.debug.malloc.options backtrace \n");
+    fprintf(out, "   # adb shell start\n");
+    fprintf(out, "\n");
+    fprintf(out, "\n");
     fprintf(out, "usage: adb shell cmd stats print-uid-map \n");
     fprintf(out, "\n");
     fprintf(out, "  Prints the UID, app name, version mapping.\n");
@@ -507,6 +521,13 @@
     return NO_ERROR;
 }
 
+status_t StatsService::cmd_dump_memory_info(FILE* out) {
+    std::string s = dumpMemInfo(100);
+    fprintf(out, "Memory Info\n");
+    fprintf(out, "%s", s.c_str());
+    return NO_ERROR;
+}
+
 Status StatsService::informAllUidData(const vector<int32_t>& uid, const vector<int32_t>& version,
                                       const vector<String16>& app) {
     if (DEBUG) ALOGD("StatsService::informAllUidData was called");