Snap for 8270536 from b16c613bf220073e40200ffe876fdabc30751be4 to tm-release

Change-Id: I760a6c87f8045f9233ce02fcac613ca779de07d6
diff --git a/libdmabufinfo/dmabufinfo.cpp b/libdmabufinfo/dmabufinfo.cpp
index 422fc6e..cb639c1 100644
--- a/libdmabufinfo/dmabufinfo.cpp
+++ b/libdmabufinfo/dmabufinfo.cpp
@@ -302,5 +302,36 @@
     return true;
 }
 
+bool ReadDmaBufs(std::vector<DmaBuffer>* bufs) {
+    bufs->clear();
+
+    std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir("/proc"), closedir);
+    if (!dir) {
+        LOG(ERROR) << "Failed to open /proc directory";
+        bufs->clear();
+        return false;
+    }
+
+    struct dirent* dent;
+    while ((dent = readdir(dir.get()))) {
+        if (dent->d_type != DT_DIR) continue;
+
+        int pid = atoi(dent->d_name);
+        if (pid == 0) {
+            continue;
+        }
+
+        if (!ReadDmaBufFdRefs(pid, bufs)) {
+            LOG(ERROR) << "Failed to read dmabuf fd references for pid " << pid;
+        }
+
+        if (!ReadDmaBufMapRefs(pid, bufs)) {
+            LOG(ERROR) << "Failed to read dmabuf map references for pid " << pid;
+        }
+    }
+
+    return true;
+}
+
 }  // namespace dmabufinfo
 }  // namespace android
diff --git a/libdmabufinfo/include/dmabufinfo/dmabufinfo.h b/libdmabufinfo/include/dmabufinfo/dmabufinfo.h
index 93736df..bb4aff1 100644
--- a/libdmabufinfo/include/dmabufinfo/dmabufinfo.h
+++ b/libdmabufinfo/include/dmabufinfo/dmabufinfo.h
@@ -127,5 +127,10 @@
 bool ReadDmaBufPss(int pid, uint64_t* pss, const std::string& procfs_path = "/proc",
                    const std::string& dmabuf_sysfs_path = "/sys/kernel/dmabuf/buffers");
 
+// Writes DmaBuffer info into an existing vector (which will be cleared first.)
+// Will include all DmaBuffers, whether thay are retained or mapped.
+// Returns true on success, otherwise false.
+bool ReadDmaBufs(std::vector<DmaBuffer>* bufs);
+
 }  // namespace dmabufinfo
 }  // namespace android
diff --git a/libdmabufinfo/tools/dmabuf_dump.cpp b/libdmabufinfo/tools/dmabuf_dump.cpp
index 682ab08..65ebd08 100644
--- a/libdmabufinfo/tools/dmabuf_dump.cpp
+++ b/libdmabufinfo/tools/dmabuf_dump.cpp
@@ -179,41 +179,6 @@
            total_size / 1024, kernel_rss / 1024, total_rss / 1024, total_pss / 1024);
 }
 
-static bool ReadDmaBufs(std::vector<DmaBuffer>* bufs) {
-    bufs->clear();
-
-    if (!ReadDmaBufInfo(bufs)) {
-        printf("debugfs entry for dmabuf not available, using /proc/<pid>/fdinfo instead\n");
-    }
-
-    std::unique_ptr<DIR, int (*)(DIR*)> dir(opendir("/proc"), closedir);
-    if (!dir) {
-        fprintf(stderr, "Failed to open /proc directory\n");
-        bufs->clear();
-        return false;
-    }
-
-    struct dirent* dent;
-    while ((dent = readdir(dir.get()))) {
-        if (dent->d_type != DT_DIR) continue;
-
-        int pid = atoi(dent->d_name);
-        if (pid == 0) {
-            continue;
-        }
-
-        if (!ReadDmaBufFdRefs(pid, bufs)) {
-            fprintf(stderr, "Failed to read dmabuf fd references for pid %d\n", pid);
-        }
-
-        if (!ReadDmaBufMapRefs(pid, bufs)) {
-            fprintf(stderr, "Failed to read dmabuf map references for pid %d\n", pid);
-        }
-    }
-
-    return true;
-}
-
 static void DumpDmabufSysfsStats() {
     android::dmabufinfo::DmabufSysfsStats stats;
 
@@ -296,7 +261,10 @@
             exit(EXIT_FAILURE);
         }
     } else {
-        if (!ReadDmaBufs(&bufs)) exit(EXIT_FAILURE);
+        if (!ReadDmaBufs(&bufs)) {
+            fprintf(stderr, "Failed to ReadDmaBufs, check logcat for info\n");
+            exit(EXIT_FAILURE);
+        }
     }
 
     // Show the old dmabuf table, inode x process