Add Malloc Debug feature for CTS/STS tests.

Bug:65559061
Change-Id: I0e632404b3042c25f66dfab8b64849d17ef6e1ab
diff --git a/hostsidetests/security/src/android/security/cts/AdbUtils.java b/hostsidetests/security/src/android/security/cts/AdbUtils.java
index f44870a..77a8f7b 100644
--- a/hostsidetests/security/src/android/security/cts/AdbUtils.java
+++ b/hostsidetests/security/src/android/security/cts/AdbUtils.java
@@ -21,6 +21,7 @@
 import com.android.tradefed.device.DeviceNotAvailableException;
 import com.android.tradefed.device.ITestDevice;
 import com.android.tradefed.testtype.DeviceTestCase;
+import com.android.tradefed.log.LogUtil.CLog;
 
 import android.platform.test.annotations.RootPermissionTest;
 
@@ -89,6 +90,33 @@
     }
 
     /**
+     * Enables malloc debug on a given process.
+     *
+     * @param processName the name of the process to run with libc malloc debug
+     * @param device the device to use
+     * @return true if enabling malloc debug succeeded
+     */
+    public static boolean enableLibcMallocDebug(String processName, ITestDevice device) throws Exception {
+        device.executeShellCommand("setprop libc.debug.malloc.program " + processName);
+        device.executeShellCommand("setprop libc.debug.malloc.options \"backtrace guard\"");
+        String cmdOut = device.executeShellCommand("ps | fgrep " + processName);
+        Scanner s = new Scanner(cmdOut);
+        if(!s.hasNextInt()) {
+            CLog.w("Could not find pid for process: " + processName);
+            return false;
+        }
+
+        String result = device.executeShellCommand("kill -9 " + s.nextInt());
+        if(!result.equals("")) {
+            CLog.w("Could not restart process: " + processName);
+            return false;
+        }
+
+        TimeUnit.SECONDS.sleep(1);
+        return true;
+    }
+
+    /**
      * Pushes and installs an apk to the selected device
      *
      * @param pathToApk a string path to apk from the /res folder