Change bash script to C++ implementation

Bug: 287884860
Test: adb shell dumpsys android.hardware.dumpstate.IDumpstateDevice/default dump_memory
Change-Id: Ifd6e7b19d1aaa8ca4e9d5b50902d4283dee9dffb
diff --git a/soc/Android.bp b/soc/Android.bp
index e913154..43e9202 100644
--- a/soc/Android.bp
+++ b/soc/Android.bp
@@ -2,11 +2,21 @@
     default_applicable_licenses: ["Android-Apache-2.0"],
 }
 
-sh_binary {
-    name: "dump_memory.sh",
-    src: "dump_memory.sh",
+cc_binary {
+    name: "dump_memory",
+    srcs: ["dump_memory.cpp"],
+    cflags: [
+        "-Wall",
+        "-Wextra",
+        "-Werror",
+    ],
+    shared_libs: [
+        "libbase",
+	"libdump",
+	"liblog",
+    ],
     vendor: true,
-    sub_dir: "dump",
+    relative_install_path: "dump",
 }
 
 cc_binary {
diff --git a/soc/dump_memory.cpp b/soc/dump_memory.cpp
new file mode 100644
index 0000000..1528f73
--- /dev/null
+++ b/soc/dump_memory.cpp
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2022 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.
+ */
+#include <dump/pixel_dump.h>
+
+#include <android-base/file.h>
+#include <stdio.h>
+#include <log/log.h>
+
+char* concat(char* result, const char* one, const char* two){
+  strcpy(result, one);
+  strcat(result, two);
+  return result;
+}
+
+void iterate(const char* path){
+    dirent *entry, *entry2;
+    char result[100], base[100];
+
+    std::unique_ptr<DIR, decltype(&closedir)> ion(opendir(path), closedir);
+    if (!ion) {
+        ALOGE("Fail To Open Dir %s", path);
+        return;
+    }
+    while ((entry = readdir(ion.get())) != nullptr) {
+        if(entry->d_name[0] == '.') {
+            continue;
+        }
+        strcpy(base, path);
+        strcat(base, entry->d_name);
+        strcat(base, "/");
+        std::unique_ptr<DIR, decltype(&closedir)> ion2(opendir(base), closedir);
+        if (!ion2) {
+            ALOGE("Fail To Open Dir %s\n", base);
+            return;
+        }
+        while ((entry2 = readdir(ion2.get())) != nullptr) {
+            if(entry2->d_name[0] == '.') {
+                continue;
+            }
+            dumpFileContent(entry2->d_name, concat(result, base, entry2->d_name));
+        }
+    }
+    return;
+}
+
+// Dump memory.
+int main() {
+    dirent *entry;
+    char result[100];
+
+    printf("------ ION HEAPS  ------\n");
+    iterate("/d/ion/");
+
+    dumpFileContent("dmabuf info", "/d/dma_buf/bufinfo");
+    dumpFileContent("Page Pinner - longterm pin", "/sys/kernel/debug/page_pinner/buffer");
+
+    printf("------ CMA info  ------\n");
+    std::unique_ptr<DIR, decltype(&closedir)> cmadebug(opendir("/sys/kernel/debug/cma/"), closedir);
+    if (!cmadebug) {
+        ALOGE("Fail To Open Dir /sys/kernel/debug/cma/");
+    } else {
+        while ((entry = readdir(cmadebug.get())) != nullptr) {
+            if(entry->d_name[0] == '.') {
+                continue;
+            }
+            dumpFileContent("count", concat(result, concat(result, "/sys/kernel/debug/cma/", entry->d_name), "/count"));
+            dumpFileContent("used", concat(result, concat(result, "/sys/kernel/debug/cma/", entry->d_name), "/used"));
+            dumpFileContent("bitmap", concat(result, concat(result, "/sys/kernel/debug/cma/", entry->d_name), "/bitmap"));
+        }
+    }
+
+    printf("------ Pixel CMA stat  ------\n");
+    iterate("/sys/kernel/pixel_stat/mm/cma/");
+
+    dumpFileContent("Pixel Trace", "/sys/kernel/tracing/instances/pixel/trace");
+    return 0;
+}
+
diff --git a/soc/dump_memory.sh b/soc/dump_memory.sh
deleted file mode 100644
index 6135dea..0000000
--- a/soc/dump_memory.sh
+++ /dev/null
@@ -1,46 +0,0 @@
-#!/vendor/bin/sh
-echo "------ ION HEAPS ------"
-for d in $(ls -d /d/ion/*)
-do
-  if [ -f $d ]; then
-    echo --- $d
-    cat $d
-  else
-    for f in $(ls $d)
-    do
-      echo --- $d/$f
-      cat $d/$f
-    done
-  fi
-done
-
-echo "------ dmabuf info ------"
-cat "/d/dma_buf/bufinfo"
-
-echo "------ Page Pinner - longterm pin ------"
-cat "/sys/kernel/debug/page_pinner/buffer"
-
-echo "------ CMA info ------"
-for d in $(ls -d /sys/kernel/debug/cma/*)
-do
-  echo --- $d
-  echo --- count; cat $d/count;
-  echo --- used; cat $d/used;
-  echo --- bitmap; cat $d/bitmap;
-done
-
-echo "------ Pixel CMA stat ------"
-for d in $(ls -d /sys/kernel/pixel_stat/mm/cma/*); do
-  if [ -f $d ]; then
-    echo --- $d
-    cat $d
-  else
-    for f in $(ls $d); do
-      echo --- $d/$f
-      cat $d/$f
-    done
-  fi
-done
-
-echo "------ Pixel Trace ------"
-cat "/sys/kernel/tracing/instances/pixel/trace"
diff --git a/soc/sepolicy/soc/file_contexts b/soc/sepolicy/soc/file_contexts
index 23a91e5..5adc605 100644
--- a/soc/sepolicy/soc/file_contexts
+++ b/soc/sepolicy/soc/file_contexts
@@ -1,3 +1,3 @@
 /vendor/bin/dump/dump_soc           u:object_r:dump_soc_exec:s0
-/vendor/bin/dump/dump_memory\.sh    u:object_r:dump_memory_exec:s0
+/vendor/bin/dump/dump_memory        u:object_r:dump_memory_exec:s0
 
diff --git a/soc/soc.mk b/soc/soc.mk
index d08d99d..8ec9412 100644
--- a/soc/soc.mk
+++ b/soc/soc.mk
@@ -1,5 +1,5 @@
 BOARD_VENDOR_SEPOLICY_DIRS += device/google/gs-common/soc/sepolicy/soc
 
 PRODUCT_PACKAGES += dump_soc
-PRODUCT_PACKAGES_DEBUG += dump_memory.sh
+PRODUCT_PACKAGES_DEBUG += dump_memory