Minor OatFile ownership and --oat-fd= argument formatting

Change-Id: Idd2e6aca574b473222d75a0fab1fe7538c6a787b
diff --git a/src/class_linker.cc b/src/class_linker.cc
index b385895..1355cbf 100644
--- a/src/class_linker.cc
+++ b/src/class_linker.cc
@@ -589,7 +589,7 @@
   const char* dex_file_option = dex_file_option_string.c_str();
 
   std::string oat_fd_option_string("--oat-fd=");
-  oat_fd_option_string += oat_fd;
+  StringAppendF(&oat_fd_option_string, "%d", oat_fd);
   const char* oat_fd_option = oat_fd_option_string.c_str();
 
   std::string oat_name_option_string("--oat-name=");
@@ -629,6 +629,16 @@
   return true;
 }
 
+void ClassLinker::RegisterOatFile(const OatFile& oat_file) {
+  MutexLock mu(dex_lock_);
+  RegisterOatFileLocked(oat_file);
+}
+
+void ClassLinker::RegisterOatFileLocked(const OatFile& oat_file) {
+  dex_lock_.AssertHeld();
+  oat_files_.push_back(&oat_file);
+}
+
 OatFile* ClassLinker::OpenOat(const Space* space) {
   MutexLock mu(dex_lock_);
   const Runtime* runtime = Runtime::Current();
@@ -648,12 +658,12 @@
   uint32_t oat_checksum = oat_file->GetOatHeader().GetChecksum();
   uint32_t image_oat_checksum = image_header.GetOatChecksum();
   if (oat_checksum != image_oat_checksum) {
-    LOG(ERROR) << "Failed to match oat filechecksum " << std::hex << oat_checksum
+    LOG(ERROR) << "Failed to match oat file checksum " << std::hex << oat_checksum
                << " to expected oat checksum " << std::hex << oat_checksum
                << " in image";
     return NULL;
   }
-  oat_files_.push_back(oat_file);
+  RegisterOatFileLocked(*oat_file);
   VLOG(startup) << "ClassLinker::OpenOat exiting";
   return oat_file;
 }
@@ -717,19 +727,24 @@
 
 const OatFile* ClassLinker::FindOatFileForDexFile(const DexFile& dex_file) {
   MutexLock mu(dex_lock_);
-  const OatFile* oat_file = FindOpenedOatFileForDexFile(dex_file);
-  if (oat_file != NULL) {
-    return oat_file;
+  const OatFile* open_oat_file = FindOpenedOatFileForDexFile(dex_file);
+  if (open_oat_file != NULL) {
+    return open_oat_file;
   }
 
   std::string oat_filename(OatFile::DexFilenameToOatFilename(dex_file.GetLocation()));
+  open_oat_file = FindOpenedOatFileFromOatLocation(oat_filename);
+  if (open_oat_file != NULL) {
+    return open_oat_file;
+  }
+
   while (true) {
-    oat_file = FindOatFileFromOatLocation(oat_filename);
-    if (oat_file != NULL) {
+    UniquePtr<const OatFile> oat_file(FindOatFileFromOatLocation(oat_filename));
+    if (oat_file.get() != NULL) {
       const OatFile::OatDexFile* oat_dex_file = oat_file->GetOatDexFile(dex_file.GetLocation());
       if (dex_file.GetHeader().checksum_ == oat_dex_file->GetDexFileChecksum()) {
-        oat_files_.push_back(oat_file);
-        return oat_file;
+        RegisterOatFileLocked(*oat_file.get());
+        return oat_file.release();
       }
       LOG(WARNING) << ".oat file " << oat_file->GetLocation()
                    << " checksum mismatch with " << dex_file.GetLocation() << " --- regenerating";
@@ -796,12 +811,7 @@
 }
 
 const OatFile* ClassLinker::FindOatFileFromOatLocation(const std::string& oat_location) {
-  const OatFile* oat_file = FindOpenedOatFileFromOatLocation(oat_location);
-  if (oat_file != NULL) {
-    return oat_file;
-  }
-
-  oat_file = OatFile::Open(oat_location, "", NULL);
+  const OatFile* oat_file = OatFile::Open(oat_location, "", NULL);
   if (oat_file == NULL) {
     if (oat_location.empty() || oat_location[0] != '/') {
       LOG(ERROR) << "Failed to open oat file from " << oat_location;
@@ -822,7 +832,6 @@
   }
 
   CHECK(oat_file != NULL) << oat_location;
-  oat_files_.push_back(oat_file);
   return oat_file;
 }
 
diff --git a/src/class_linker.h b/src/class_linker.h
index 2c94a93..7d94e95 100644
--- a/src/class_linker.h
+++ b/src/class_linker.h
@@ -230,6 +230,8 @@
   void RegisterDexFile(const DexFile& dex_file);
   void RegisterDexFile(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
 
+  void RegisterOatFile(const OatFile& oat_file);
+
   const std::vector<const DexFile*>& GetBootClassPath() {
     return boot_class_path_;
   }
@@ -337,6 +339,7 @@
 
   void RegisterDexFileLocked(const DexFile& dex_file, SirtRef<DexCache>& dex_cache);
   bool IsDexFileRegisteredLocked(const DexFile& dex_file) const;
+  void RegisterOatFileLocked(const OatFile& oat_file);
 
   bool InitializeClass(Class* klass, bool can_run_clinit);
   bool WaitForInitializeClass(Class* klass, Thread* self, ObjectLock& lock);
diff --git a/src/dex2oat.cc b/src/dex2oat.cc
index 35494cb..2781aae 100644
--- a/src/dex2oat.cc
+++ b/src/dex2oat.cc
@@ -387,7 +387,7 @@
     } else if (option.starts_with("--zip-fd=")) {
       const char* zip_fd_str = option.substr(strlen("--zip-fd=")).data();
       if (!parse_int(zip_fd_str, &zip_fd)) {
-        fprintf(stderr, "could not parse --zip-fd argument %s as integer\n", zip_fd_str);
+        fprintf(stderr, "could not parse --zip-fd argument '%s' as an integer\n", zip_fd_str);
         usage();
       }
     } else if (option.starts_with("--zip-name=")) {
@@ -397,7 +397,7 @@
     } else if (option.starts_with("--oat-fd=")) {
       const char* oat_fd_str = option.substr(strlen("--oat-fd=")).data();
       if (!parse_int(oat_fd_str, &oat_fd)) {
-        fprintf(stderr, "could not parse --oat-fd argument %s as integer\n", oat_fd_str);
+        fprintf(stderr, "could not parse --oat-fd argument '%s' as an integer\n", oat_fd_str);
         usage();
       }
     } else if (option.starts_with("--oat-name=")) {
diff --git a/src/oatdump.cc b/src/oatdump.cc
index a664852..dd46896 100644
--- a/src/oatdump.cc
+++ b/src/oatdump.cc
@@ -274,6 +274,7 @@
     os << "\n";
     os << std::flush;
 
+    class_linker->RegisterOatFile(*oat_file);
     OatDump::Dump(oat_location, host_prefix, os, *oat_file);
   }