Merge "ART: Print maps directly to log"
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);
diff --git a/runtime/oat_file.cc b/runtime/oat_file.cc
index 3192e03..739d62c 100644
--- a/runtime/oat_file.cc
+++ b/runtime/oat_file.cc
@@ -171,10 +171,10 @@
     return false;
   }
   if (requested_base != NULL && begin_ != requested_base) {
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
     *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
-                              "oatdata=%p != expected=%p /proc/self/maps:\n",
+                              "oatdata=%p != expected=%p. See process maps in the log.",
                               begin_, requested_base);
-    ReadFileToString("/proc/self/maps", error_msg);
     return false;
   }
   end_ = reinterpret_cast<uint8_t*>(dlsym(dlopen_handle_, "oatlastword"));
@@ -209,10 +209,10 @@
     return false;
   }
   if (requested_base != NULL && begin_ != requested_base) {
+    PrintFileToLog("/proc/self/maps", LogSeverity::WARNING);
     *error_msg = StringPrintf("Failed to find oatdata symbol at expected address: "
-                              "oatdata=%p != expected=%p /proc/self/maps:\n",
+                              "oatdata=%p != expected=%p. See process maps in the log.",
                               begin_, requested_base);
-    ReadFileToString("/proc/self/maps", error_msg);
     return false;
   }
   end_ = elf_file_->FindDynamicSymbolAddress("oatlastword");
diff --git a/runtime/utils.cc b/runtime/utils.cc
index dea8f79..6afc373 100644
--- a/runtime/utils.cc
+++ b/runtime/utils.cc
@@ -133,14 +133,14 @@
 }
 
 bool ReadFileToString(const std::string& file_name, std::string* result) {
-  std::unique_ptr<File> file(new File);
-  if (!file->Open(file_name, O_RDONLY)) {
+  File file;
+  if (!file.Open(file_name, O_RDONLY)) {
     return false;
   }
 
   std::vector<char> buf(8 * KB);
   while (true) {
-    int64_t n = TEMP_FAILURE_RETRY(read(file->Fd(), &buf[0], buf.size()));
+    int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[0], buf.size()));
     if (n == -1) {
       return false;
     }
@@ -151,6 +151,59 @@
   }
 }
 
+bool PrintFileToLog(const std::string& file_name, LogSeverity level) {
+  File file;
+  if (!file.Open(file_name, O_RDONLY)) {
+    return false;
+  }
+
+  constexpr size_t kBufSize = 256;  // Small buffer. Avoid stack overflow and stack size warnings.
+  char buf[kBufSize + 1];           // +1 for terminator.
+  size_t filled_to = 0;
+  while (true) {
+    DCHECK_LT(filled_to, kBufSize);
+    int64_t n = TEMP_FAILURE_RETRY(read(file.Fd(), &buf[filled_to], kBufSize - filled_to));
+    if (n <= 0) {
+      // Print the rest of the buffer, if it exists.
+      if (filled_to > 0) {
+        buf[filled_to] = 0;
+        LOG(level) << buf;
+      }
+      return n == 0;
+    }
+    // Scan for '\n'.
+    size_t i = filled_to;
+    bool found_newline = false;
+    for (; i < filled_to + n; ++i) {
+      if (buf[i] == '\n') {
+        // Found a line break, that's something to print now.
+        buf[i] = 0;
+        LOG(level) << buf;
+        // Copy the rest to the front.
+        if (i + 1 < filled_to + n) {
+          memmove(&buf[0], &buf[i + 1], filled_to + n - i - 1);
+          filled_to = filled_to + n - i - 1;
+        } else {
+          filled_to = 0;
+        }
+        found_newline = true;
+        break;
+      }
+    }
+    if (found_newline) {
+      continue;
+    } else {
+      filled_to += n;
+      // Check if we must flush now.
+      if (filled_to == kBufSize) {
+        buf[kBufSize] = 0;
+        LOG(level) << buf;
+        filled_to = 0;
+      }
+    }
+  }
+}
+
 std::string GetIsoDate() {
   time_t now = time(NULL);
   tm tmbuf;
diff --git a/runtime/utils.h b/runtime/utils.h
index 3191e7d..698d686 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -410,6 +410,7 @@
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
 bool ReadFileToString(const std::string& file_name, std::string* result);
+bool PrintFileToLog(const std::string& file_name, LogSeverity level);
 
 // Returns the current date in ISO yyyy-mm-dd hh:mm:ss format.
 std::string GetIsoDate();