Change to support new BacktraceMap.

Change-Id: I291313583dca2c8e1e946504c442f5810f0fb477
diff --git a/runtime/mem_map.cc b/runtime/mem_map.cc
index 39e838f..97b34ef 100644
--- a/runtime/mem_map.cc
+++ b/runtime/mem_map.cc
@@ -16,7 +16,7 @@
 
 #include "mem_map.h"
 
-#include <backtrace/backtrace.h>
+#include <backtrace/BacktraceMap.h>
 
 #include "base/stringprintf.h"
 #include "ScopedFd.h"
@@ -32,12 +32,16 @@
 
 #if !defined(NDEBUG)
 
-static std::ostream& operator<<(std::ostream& os, backtrace_map_info_t* rhs) {
-  for (backtrace_map_info_t* m = rhs; m != NULL; m = m->next) {
-    os << StringPrintf("0x%08x-0x%08x %c%c %s\n",
-                       static_cast<uint32_t>(m->start),
-                       static_cast<uint32_t>(m->end),
-                       m->is_readable ? 'r' : '-', m->is_executable ? 'x' : '-', m->name);
+static std::ostream& operator<<(
+    std::ostream& os,
+    std::pair<BacktraceMap::const_iterator, BacktraceMap::const_iterator> iters) {
+  for (BacktraceMap::const_iterator it = iters.first; it != iters.second; ++it) {
+    os << StringPrintf("0x%08x-0x%08x %c%c%c %s\n",
+                       static_cast<uint32_t>(it->start),
+                       static_cast<uint32_t>(it->end),
+                       (it->flags & PROT_READ) ? 'r' : '-',
+                       (it->flags & PROT_WRITE) ? 'w' : '-',
+                       (it->flags & PROT_EXEC) ? 'x' : '-', it->name.c_str());
   }
   return os;
 }
@@ -47,20 +51,24 @@
     return;
   }
 
-  uint32_t base = reinterpret_cast<size_t>(addr);
-  uint32_t limit = base + byte_count;
+  uintptr_t base = reinterpret_cast<uintptr_t>(addr);
+  uintptr_t limit = base + byte_count;
 
-  backtrace_map_info_t* map_info_list = backtrace_create_map_info_list(getpid());
-  for (backtrace_map_info_t* m = map_info_list; m != NULL; m = m->next) {
-    CHECK(!(base >= m->start && base < m->end)     // start of new within old
-        && !(limit > m->start && limit < m->end)   // end of new within old
-        && !(base <= m->start && limit > m->end))  // start/end of new includes all of old
+  BacktraceMap map(getpid());
+  if (!map.Build()) {
+    PLOG(WARNING) << "Failed to build process map";
+    return;
+  }
+  for (BacktraceMap::const_iterator it = map.begin(); it != map.end(); ++it) {
+    CHECK(!(base >= it->start && base < it->end)     // start of new within old
+        && !(limit > it->start && limit < it->end)   // end of new within old
+        && !(base <= it->start && limit > it->end))  // start/end of new includes all of old
         << StringPrintf("Requested region 0x%08x-0x%08x overlaps with existing map 0x%08x-0x%08x (%s)\n",
                         base, limit,
-                        static_cast<uint32_t>(m->start), static_cast<uint32_t>(m->end), m->name)
-        << map_info_list;
+                        static_cast<uint32_t>(it->start), static_cast<uint32_t>(it->end),
+                        it->name.c_str())
+        << std::make_pair(it, map.end());
   }
-  backtrace_destroy_map_info_list(map_info_list);
 }
 
 #else
diff --git a/runtime/utils.cc b/runtime/utils.cc
index 2ad4c4a..a293043 100644
--- a/runtime/utils.cc
+++ b/runtime/utils.cc
@@ -16,6 +16,7 @@
 
 #include "utils.h"
 
+#include <inttypes.h>
 #include <pthread.h>
 #include <sys/stat.h>
 #include <sys/syscall.h>
@@ -1035,17 +1036,17 @@
   return "";
 }
 
-static const char* CleanMapName(const char* map_name) {
-  if (map_name == NULL) {
+static std::string CleanMapName(const backtrace_map_t* map) {
+  if (map == NULL || map->name.empty()) {
     return "???";
   }
   // Turn "/usr/local/google/home/enh/clean-dalvik-dev/out/host/linux-x86/lib/libartd.so"
   // into "libartd.so".
-  const char* last_slash = strrchr(map_name, '/');
-  if (last_slash != NULL) {
-    map_name = last_slash + 1;
+  size_t last_slash = map->name.rfind('/');
+  if (last_slash == std::string::npos) {
+    return map->name;
   }
-  return map_name;
+  return map->name.substr(last_slash);
 }
 
 void DumpNativeStack(std::ostream& os, pid_t tid, const char* prefix, bool include_count) {
@@ -1058,25 +1059,24 @@
     return;
   }
 
-  for (size_t i = 0; i < backtrace->NumFrames(); ++i) {
+  for (Backtrace::const_iterator it = backtrace->begin();
+       it != backtrace->end(); ++it) {
     // We produce output like this:
     // ]    #00 unwind_backtrace_thread+536 [0x55d75bb8] (libbacktrace.so)
-    const backtrace_frame_data_t* frame = backtrace->GetFrame(i);
-
     os << prefix;
     if (include_count) {
-      os << StringPrintf("#%02zu ", i);
+      os << StringPrintf("#%02zu ", it->num);
     }
-    if (frame->func_name) {
-      os << frame->func_name;
+    if (!it->func_name.empty()) {
+      os << it->func_name;
     } else {
       os << "???";
     }
-    if (frame->func_offset != 0) {
-      os << "+" << frame->func_offset;
+    if (it->func_offset != 0) {
+      os << "+" << it->func_offset;
     }
-    os << StringPrintf(" [%p]", reinterpret_cast<void*>(frame->pc));
-    os << " (" << CleanMapName(frame->map_name) << ")\n";
+    os << StringPrintf(" [%p]", reinterpret_cast<void*>(it->pc));
+    os << " (" << CleanMapName(it->map) << ")\n";
   }
 }