Assume the profile file was created before saving.

bug: 26080105
Change-Id: I9969a4abd8533614922076551fcbae2cdf695525
diff --git a/runtime/jit/offline_profiling_info.cc b/runtime/jit/offline_profiling_info.cc
index 5dc0e45..3942b0b 100644
--- a/runtime/jit/offline_profiling_info.cc
+++ b/runtime/jit/offline_profiling_info.cc
@@ -77,9 +77,7 @@
       break;
     case READ_WRITE:
       // TODO(calin) allow the shared uid of the app to access the file.
-      fd = open(filename.c_str(),
-                    O_CREAT | O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC,
-                    S_IRUSR | S_IWUSR);
+      fd = open(filename.c_str(), O_WRONLY | O_TRUNC | O_NOFOLLOW | O_CLOEXEC);
       break;
   }
 
diff --git a/runtime/native/dalvik_system_VMRuntime.cc b/runtime/native/dalvik_system_VMRuntime.cc
index 4b24f82..da4a891 100644
--- a/runtime/native/dalvik_system_VMRuntime.cc
+++ b/runtime/native/dalvik_system_VMRuntime.cc
@@ -565,8 +565,8 @@
  */
 static void VMRuntime_registerAppInfo(JNIEnv* env,
                                       jclass clazz ATTRIBUTE_UNUSED,
-                                      jstring pkg_name,
-                                      jstring app_dir,
+                                      jstring profile_file,
+                                      jstring app_dir ATTRIBUTE_UNUSED,  // TODO: remove argument
                                       jobjectArray code_paths) {
   std::vector<std::string> code_paths_vec;
   int code_paths_length = env->GetArrayLength(code_paths);
@@ -577,13 +577,11 @@
     env->ReleaseStringUTFChars(code_path, raw_code_path);
   }
 
-  const char* raw_app_dir = env->GetStringUTFChars(app_dir, nullptr);
-  const char* raw_pkg_name = env->GetStringUTFChars(pkg_name, nullptr);
-  std::string profile_file = StringPrintf("%s/code_cache/%s.prof", raw_app_dir, raw_pkg_name);
-  env->ReleaseStringUTFChars(pkg_name, raw_pkg_name);
-  env->ReleaseStringUTFChars(app_dir, raw_app_dir);
+  const char* raw_profile_file = env->GetStringUTFChars(profile_file, nullptr);
+  std::string profile_file_str(raw_profile_file);
+  env->ReleaseStringUTFChars(profile_file, raw_profile_file);
 
-  Runtime::Current()->RegisterAppInfo(code_paths_vec, profile_file);
+  Runtime::Current()->RegisterAppInfo(code_paths_vec, profile_file_str);
 }
 
 static jboolean VMRuntime_isBootClassPathOnDisk(JNIEnv* env, jclass, jstring java_instruction_set) {
diff --git a/runtime/runtime.cc b/runtime/runtime.cc
index 5c72629..b7fdcdf 100644
--- a/runtime/runtime.cc
+++ b/runtime/runtime.cc
@@ -1685,13 +1685,29 @@
 
 void Runtime::RegisterAppInfo(const std::vector<std::string>& code_paths,
                               const std::string& profile_output_filename) {
-  VLOG(profiler) << "Register app with " << profile_output_filename_
-      << " " << Join(code_paths, ':');
-  DCHECK(!profile_output_filename.empty());
-  profile_output_filename_ = profile_output_filename;
-  if (jit_.get() != nullptr && !profile_output_filename.empty() && !code_paths.empty()) {
-    jit_->StartProfileSaver(profile_output_filename, code_paths);
+  if (jit_.get() == nullptr) {
+    // We are not JITing. Nothing to do.
+    return;
   }
+
+  VLOG(profiler) << "Register app with " << profile_output_filename
+      << " " << Join(code_paths, ':');
+
+  if (profile_output_filename.empty()) {
+    LOG(WARNING) << "JIT profile information will not be recorded: profile filename is empty.";
+    return;
+  }
+  if (!FileExists(profile_output_filename)) {
+    LOG(WARNING) << "JIT profile information will not be recorded: profile file does not exits.";
+    return;
+  }
+  if (code_paths.empty()) {
+    LOG(WARNING) << "JIT profile information will not be recorded: code paths is empty.";
+    return;
+  }
+
+  profile_output_filename_ = profile_output_filename;
+  jit_->StartProfileSaver(profile_output_filename, code_paths);
 }
 
 // Transaction support.
diff --git a/runtime/utils.cc b/runtime/utils.cc
index ff6b4c0..1e1c7e7 100644
--- a/runtime/utils.cc
+++ b/runtime/utils.cc
@@ -1446,6 +1446,11 @@
   return true;
 }
 
+bool FileExists(const std::string& filename) {
+  struct stat buffer;
+  return stat(filename.c_str(), &buffer) == 0;
+}
+
 std::string PrettyDescriptor(Primitive::Type type) {
   return PrettyDescriptor(Primitive::Descriptor(type));
 }
diff --git a/runtime/utils.h b/runtime/utils.h
index a07e74c..5ceb3b5 100644
--- a/runtime/utils.h
+++ b/runtime/utils.h
@@ -276,6 +276,9 @@
 // Wrapper on fork/execv to run a command in a subprocess.
 bool Exec(std::vector<std::string>& arg_vector, std::string* error_msg);
 
+// Returns true if the file exists.
+bool FileExists(const std::string& filename);
+
 class VoidFunctor {
  public:
   template <typename A>