ART: Print maps directly to log

Do not read proc maps into a string before printing them later back
to the log. In low-memory situations this can cause a bad_alloc.

External bug: http://b.android.com/153990
Bug: 19494774

Change-Id: Ie63d8788afe8c9da65b30b2f89c50d3dbb820755
diff --git a/runtime/mem_map.cc b/runtime/mem_map.cc
index a722813..4b85469 100644
--- a/runtime/mem_map.cc
+++ b/runtime/mem_map.cc
@@ -152,11 +152,9 @@
       return true;
     }
   }
-  std::string maps;
-  ReadFileToString("/proc/self/maps", &maps);
+  PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
   *error_msg = StringPrintf("Requested region 0x%08" PRIxPTR "-0x%08" PRIxPTR " does not overlap "
-                            "any existing map:\n%s\n",
-                            begin, end, maps.c_str());
+                            "any existing map. See process maps in the log.", begin, end);
   return false;
 }
 
@@ -375,12 +373,11 @@
 #endif
 
   if (actual == MAP_FAILED) {
-    std::string maps;
-    ReadFileToString("/proc/self/maps", &maps);
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
 
-    *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s\n%s",
-                              expected_ptr, page_aligned_byte_count, prot, flags, fd.get(),
-                              strerror(saved_errno), maps.c_str());
+    *error_msg = StringPrintf("Failed anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0): %s. See process "
+                              "maps in the log.", expected_ptr, page_aligned_byte_count, prot,
+                              flags, fd.get(), strerror(saved_errno));
     return nullptr;
   }
   std::ostringstream check_map_request_error_msg;
@@ -435,14 +432,13 @@
   if (actual == MAP_FAILED) {
     auto saved_errno = errno;
 
-    std::string maps;
-    ReadFileToString("/proc/self/maps", &maps);
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
 
     *error_msg = StringPrintf("mmap(%p, %zd, 0x%x, 0x%x, %d, %" PRId64
-                              ") of file '%s' failed: %s\n%s",
+                              ") of file '%s' failed: %s. See process maps in the log.",
                               page_aligned_expected, page_aligned_byte_count, prot, flags, fd,
                               static_cast<int64_t>(page_aligned_offset), filename,
-                              strerror(saved_errno), maps.c_str());
+                              strerror(saved_errno));
     return nullptr;
   }
   std::ostringstream check_map_request_error_msg;
@@ -544,11 +540,9 @@
   // Unmap/map the tail region.
   int result = munmap(tail_base_begin, tail_base_size);
   if (result == -1) {
-    std::string maps;
-    ReadFileToString("/proc/self/maps", &maps);
-    *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'\n%s",
-                              tail_base_begin, tail_base_size, name_.c_str(),
-                              maps.c_str());
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
+    *error_msg = StringPrintf("munmap(%p, %zd) failed for '%s'. See process maps in the log.",
+                              tail_base_begin, tail_base_size, name_.c_str());
     return nullptr;
   }
   // Don't cause memory allocation between the munmap and the mmap
@@ -558,11 +552,10 @@
   uint8_t* actual = reinterpret_cast<uint8_t*>(mmap(tail_base_begin, tail_base_size, tail_prot,
                                               flags, fd.get(), 0));
   if (actual == MAP_FAILED) {
-    std::string maps;
-    ReadFileToString("/proc/self/maps", &maps);
-    *error_msg = StringPrintf("anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0) failed\n%s",
-                              tail_base_begin, tail_base_size, tail_prot, flags, fd.get(),
-                              maps.c_str());
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
+    *error_msg = StringPrintf("anonymous mmap(%p, %zd, 0x%x, 0x%x, %d, 0) failed. See process "
+                              "maps in the log.", tail_base_begin, tail_base_size, tail_prot, flags,
+                              fd.get());
     return nullptr;
   }
   return new MemMap(tail_name, actual, tail_size, actual, tail_base_size, tail_prot, false);