Merge "Cleanup JDWP event matching"
diff --git a/Android.mk b/Android.mk
index d11d011..370ea02 100644
--- a/Android.mk
+++ b/Android.mk
@@ -101,6 +101,7 @@
 include $(art_path)/dex2oat/Android.mk
 include $(art_path)/disassembler/Android.mk
 include $(art_path)/oatdump/Android.mk
+include $(art_path)/imgdiag/Android.mk
 include $(art_path)/patchoat/Android.mk
 include $(art_path)/dalvikvm/Android.mk
 include $(art_path)/tools/Android.mk
@@ -313,11 +314,7 @@
 
 # $(1): input jar or apk target location
 define declare-oat-target-target
-ifneq (,$(filter $(1),$(addprefix system/app/,$(addsuffix .apk,$(PRODUCT_DEX_PREOPT_PACKAGES_IN_DATA)))))
-OUT_OAT_FILE := $(call dalvik-cache-out,$(1)/classes.dex)
-else
 OUT_OAT_FILE := $(PRODUCT_OUT)/$(basename $(1)).odex
-endif
 
 ifeq ($(ONE_SHOT_MAKEFILE),)
 # ONE_SHOT_MAKEFILE is empty for a top level build and we don't want
diff --git a/build/Android.common.mk b/build/Android.common.mk
index 39e78fa..00962f5 100644
--- a/build/Android.common.mk
+++ b/build/Android.common.mk
@@ -14,8 +14,8 @@
 # limitations under the License.
 #
 
-ifndef ANDROID_COMMON_MK
-ANDROID_COMMON_MK = true
+ifndef ART_ANDROID_COMMON_MK
+ART_ANDROID_COMMON_MK = true
 
 ART_TARGET_SUPPORTED_ARCH := arm arm64 mips x86 x86_64
 ART_HOST_SUPPORTED_ARCH := x86 x86_64
@@ -81,4 +81,4 @@
   2ND_ART_HOST_OUT_SHARED_LIBRARIES := $(2ND_HOST_OUT_SHARED_LIBRARIES)
 endif
 
-endif # ANDROID_COMMON_MK
+endif # ART_ANDROID_COMMON_MK
diff --git a/build/Android.common_build.mk b/build/Android.common_build.mk
index 1a8752f..68f5ed7 100644
--- a/build/Android.common_build.mk
+++ b/build/Android.common_build.mk
@@ -14,10 +14,11 @@
 # limitations under the License.
 #
 
-ifndef ANDROID_COMMON_BUILD_MK
-ANDROID_COMMON_BUILD_MK = true
+ifndef ART_ANDROID_COMMON_BUILD_MK
+ART_ANDROID_COMMON_BUILD_MK = true
 
 include art/build/Android.common.mk
+include art/build/Android.common_utils.mk
 
 # These can be overridden via the environment or by editing to
 # enable/disable certain build configuration.
@@ -227,21 +228,16 @@
   art_target_non_debug_cflags += -Wframe-larger-than=1728
 endif
 
-# DALVIK_VM_LIB will be empty for VM-less builds. Avoid $(error) calls here because
-# LIBART_IMG_XXX variables won't be defined.
-ifneq ($(DALVIK_VM_LIB),)
-  ifndef LIBART_IMG_HOST_BASE_ADDRESS
-    $(error LIBART_IMG_HOST_BASE_ADDRESS unset)
-  endif
-  ART_HOST_CFLAGS += $(art_cflags) -DART_BASE_ADDRESS=$(LIBART_IMG_HOST_BASE_ADDRESS)
-  ART_HOST_CFLAGS += -DART_DEFAULT_INSTRUCTION_SET_FEATURES=default
-
-  ifndef LIBART_IMG_TARGET_BASE_ADDRESS
-    $(error LIBART_IMG_TARGET_BASE_ADDRESS unset)
-  endif
-  ART_TARGET_CFLAGS += $(art_cflags) \
-                       -DART_TARGET -DART_BASE_ADDRESS=$(LIBART_IMG_TARGET_BASE_ADDRESS)
+ifndef LIBART_IMG_HOST_BASE_ADDRESS
+  $(error LIBART_IMG_HOST_BASE_ADDRESS unset)
 endif
+ART_HOST_CFLAGS += $(art_cflags) -DART_BASE_ADDRESS=$(LIBART_IMG_HOST_BASE_ADDRESS)
+ART_HOST_CFLAGS += -DART_DEFAULT_INSTRUCTION_SET_FEATURES=default
+
+ifndef LIBART_IMG_TARGET_BASE_ADDRESS
+  $(error LIBART_IMG_TARGET_BASE_ADDRESS unset)
+endif
+ART_TARGET_CFLAGS += $(art_cflags) -DART_TARGET -DART_BASE_ADDRESS=$(LIBART_IMG_TARGET_BASE_ADDRESS)
 
 ART_HOST_NON_DEBUG_CFLAGS := $(art_host_non_debug_cflags)
 ART_TARGET_NON_DEBUG_CFLAGS := $(art_target_non_debug_cflags)
@@ -330,4 +326,4 @@
   ART_BUILD_DEBUG := true
 endif
 
-endif # ANDROID_COMMON_BUILD_MK
+endif # ART_ANDROID_COMMON_BUILD_MK
diff --git a/build/Android.common_path.mk b/build/Android.common_path.mk
index 281d189..e0c0b0c 100644
--- a/build/Android.common_path.mk
+++ b/build/Android.common_path.mk
@@ -14,8 +14,8 @@
 # limitations under the License.
 #
 
-ifndef ANDROID_COMMON_PATH_MK
-ANDROID_COMMON_PATH_MK := true
+ifndef ART_ANDROID_COMMON_PATH_MK
+ART_ANDROID_COMMON_PATH_MK := true
 
 include art/build/Android.common.mk
 
@@ -88,4 +88,4 @@
 
 HOST_CORE_DEX_FILES   := $(foreach jar,$(HOST_CORE_JARS),  $(call intermediates-dir-for,JAVA_LIBRARIES,$(jar),t,COMMON)/javalib.jar)
 TARGET_CORE_DEX_FILES := $(foreach jar,$(TARGET_CORE_JARS),$(call intermediates-dir-for,JAVA_LIBRARIES,$(jar), ,COMMON)/javalib.jar)
-endif # ANDROID_COMMON_PATH_MK
+endif # ART_ANDROID_COMMON_PATH_MK
diff --git a/build/Android.common_test.mk b/build/Android.common_test.mk
index 2493565..0ae42dd 100644
--- a/build/Android.common_test.mk
+++ b/build/Android.common_test.mk
@@ -14,8 +14,8 @@
 # limitations under the License.
 #
 
-ifndef ANDROID_COMMON_TEST_MK
-ANDROID_COMMON_TEST_MK = true
+ifndef ART_ANDROID_COMMON_TEST_MK
+ART_ANDROID_COMMON_TEST_MK = true
 
 include art/build/Android.common_path.mk
 
@@ -197,4 +197,4 @@
   endif
 endef
 
-endif # ANDROID_COMMON_TEST_MK
+endif # ART_ANDROID_COMMON_TEST_MK
diff --git a/build/Android.common_utils.mk b/build/Android.common_utils.mk
new file mode 100644
index 0000000..8069c3a
--- /dev/null
+++ b/build/Android.common_utils.mk
@@ -0,0 +1,26 @@
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+ifndef ART_ANDROID_COMMON_UTILS_MK
+ART_ANDROID_COMMON_UTILS_MK = true
+
+#
+# Convert a string into an uppercase string.
+#
+# $(1): a string which should be made uppercase
+art-string-to-uppercase = $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
+
+endif # ART_ANDROID_COMMON_UTILS_MK
diff --git a/build/Android.executable.mk b/build/Android.executable.mk
index be97e82..ef9e7cd 100644
--- a/build/Android.executable.mk
+++ b/build/Android.executable.mk
@@ -47,12 +47,13 @@
   art_target_or_host := $(5)
   art_ndebug_or_debug := $(6)
   art_multilib := $(7)
+  art_out_binary_name :=
 
   include $(CLEAR_VARS)
   LOCAL_CPP_EXTENSION := $(ART_CPP_EXTENSION)
   LOCAL_MODULE_TAGS := optional
   LOCAL_SRC_FILES := $$(art_source)
-  LOCAL_C_INCLUDES += $(ART_C_INCLUDES) art/runtime $$(art_c_includes)
+  LOCAL_C_INCLUDES += $(ART_C_INCLUDES) art/runtime art/cmdline $$(art_c_includes)
   LOCAL_SHARED_LIBRARIES += $$(art_shared_libraries)
   LOCAL_WHOLE_STATIC_LIBRARIES += libsigchain
 
@@ -91,32 +92,114 @@
   endif
 
   LOCAL_ADDITIONAL_DEPENDENCIES := art/build/Android.common_build.mk
+  LOCAL_ADDITIONAL_DEPENDENCIES += art/build/Android.common_utils.mk
   LOCAL_ADDITIONAL_DEPENDENCIES += art/build/Android.executable.mk
 
   ifeq ($$(art_target_or_host),target)
     LOCAL_MODULE_TARGET_ARCH := $(ART_SUPPORTED_ARCH)
   endif
 
-  # If multilib, need to provide stem.
   LOCAL_MULTILIB := $$(art_multilib)
+  art_out_binary_name := $$(LOCAL_MODULE)
+
+  # If multilib=both (potentially building both 32-bit and 64-bit), need to provide stem.
   ifeq ($$(art_multilib),both)
-    ifeq ($$(art_ndebug_or_debug),ndebug)
-      LOCAL_MODULE_STEM_32 := $$(art_executable)32
-      LOCAL_MODULE_STEM_64 := $$(art_executable)
-    else #debug
-      LOCAL_MODULE_STEM_32 := $$(art_executable)d32
-      LOCAL_MODULE_STEM_64 := $$(art_executable)d
+    # Set up a 32-bit/64-bit stem if we are building both binaries.
+    # In this case, the 32-bit binary has an additional 32-bit suffix.
+    LOCAL_MODULE_STEM_32 := $$(LOCAL_MODULE)32
+    LOCAL_MODULE_STEM_64 := $$(LOCAL_MODULE)
+
+    # Remember the binary names so we can add them to the global art executables list later.
+    art_out_binary_name := $$(LOCAL_MODULE_STEM_32) $$(LOCAL_MODULE_STEM_64)
+
+    # For single-architecture targets, remove any binary name suffixes.
+    ifeq ($$(art_target_or_host),target)
+      ifeq (,$(TARGET_2ND_ARCH))
+        LOCAL_MODULE_STEM_32 := $$(LOCAL_MODULE)
+        art_out_binary_name := $$(LOCAL_MODULE)
+      endif
+    endif
+
+    # For single-architecture hosts, remove any binary name suffixes.
+    ifeq ($$(art_target_or_host),host)
+      ifeq (,$(HOST_2ND_ARCH))
+        LOCAL_MODULE_STEM_32 := $$(LOCAL_MODULE)
+        art_out_binary_name := $$(LOCAL_MODULE)
+      endif
     endif
   endif
 
   include external/libcxx/libcxx.mk
   ifeq ($$(art_target_or_host),target)
     include $(BUILD_EXECUTABLE)
-    ART_TARGET_EXECUTABLES := $(ART_TARGET_EXECUTABLES) $(TARGET_OUT_EXECUTABLES)/$$(LOCAL_MODULE)
+    ART_TARGET_EXECUTABLES := $(ART_TARGET_EXECUTABLES) $$(foreach name,$$(art_out_binary_name),$(TARGET_OUT_EXECUTABLES)/$$(name))
   else # host
     LOCAL_IS_HOST_MODULE := true
     include $(BUILD_HOST_EXECUTABLE)
-    ART_HOST_EXECUTABLES := $(ART_HOST_EXECUTABLES) $(HOST_OUT_EXECUTABLES)/$$(LOCAL_MODULE)
+    ART_HOST_EXECUTABLES := $(ART_HOST_EXECUTABLES) $$(foreach name,$$(art_out_binary_name),$(HOST_OUT_EXECUTABLES)/$$(name))
   endif
 
+  # Clear out local variables now that we're done with them.
+  art_executable :=
+  art_source :=
+  art_shared_libraries :=
+  art_c_includes :=
+  art_target_or_host :=
+  art_ndebug_or_debug :=
+  art_multilib :=
+  art_out_binary_name :=
+
+endef
+
+#
+# Build many art executables from multiple variations (debug/ndebug, host/target, 32/64bit).
+# By default only either 32-bit or 64-bit is built (but not both -- see multilib arg).
+# All other variations are gated by ANDROID_BUILD_(TARGET|HOST)_[N]DEBUG.
+# The result must be eval-uated.
+#
+# $(1): executable name
+# $(2): source files
+# $(3): library dependencies (common); debug prefix is added on as necessary automatically.
+# $(4): library dependencies (target only)
+# $(5): library dependencies (host only)
+# $(6): extra include directories
+# $(7): multilib (default: empty), valid values: {,32,64,both})
+define build-art-multi-executable
+  $(foreach debug_flavor,ndebug debug,
+    $(foreach target_flavor,host target,
+      art-multi-binary-name := $(1)
+      art-multi-source-files := $(2)
+      art-multi-lib-dependencies := $(3)
+      art-multi-lib-dependencies-target := $(4)
+      art-multi-lib-dependencies-host := $(5)
+      art-multi-include-extra := $(6)
+      art-multi-multilib := $(7)
+
+      # Add either -host or -target specific lib dependencies to the lib dependencies.
+      art-multi-lib-dependencies += $$(art-multi-lib-dependencies-$(target_flavor))
+
+      # Replace libart- prefix with libartd- for debug flavor.
+      ifeq ($(debug_flavor),debug)
+        art-multi-lib-dependencies := $$(subst libart-,libartd-,$$(art-multi-lib-dependencies))
+      endif
+
+      # Build the env guard var name, e.g. ART_BUILD_HOST_NDEBUG.
+      art-multi-env-guard := $$(call art-string-to-uppercase,ART_BUILD_$(target_flavor)_$(debug_flavor))
+
+      # Build the art executable only if the corresponding env guard was set.
+      ifeq ($$($$(art-multi-env-guard)),true)
+        $$(eval $$(call build-art-executable,$$(art-multi-binary-name),$$(art-multi-source-files),$$(art-multi-lib-dependencies),$$(art-multi-include-extra),$(target_flavor),$(debug_flavor),$$(art-multi-multilib)))
+      endif
+
+      # Clear locals now they've served their purpose.
+      art-multi-binary-name :=
+      art-multi-source-files :=
+      art-multi-lib-dependencies :=
+      art-multi-lib-dependencies-target :=
+      art-multi-lib-dependencies-host :=
+      art-multi-include-extra :=
+      art-multi-multilib :=
+      art-multi-env-guard :=
+    )
+  )
 endef
diff --git a/build/Android.gtest.mk b/build/Android.gtest.mk
index 5567d15..4c19ba0 100644
--- a/build/Android.gtest.mk
+++ b/build/Android.gtest.mk
@@ -65,10 +65,15 @@
 # TODO: document why this is needed.
 ART_GTEST_proxy_test_HOST_DEPS := $(HOST_CORE_IMAGE_default_no-pic_64) $(HOST_CORE_IMAGE_default_no-pic_32)
 
+# The imgdiag test has dependencies on core.oat since it needs to load it during the test.
+ART_GTEST_imgdiag_test_HOST_DEPS := $(HOST_CORE_IMAGE_default_no-pic_64) $(HOST_CORE_IMAGE_default_no-pic_32)
+ART_GTEST_imgdiag_test_TARGET_DEPS := $(TARGET_CORE_IMAGE_default_no-pic_64) $(TARGET_CORE_IMAGE_default_no-pic_32)
+
 # The path for which all the source files are relative, not actually the current directory.
 LOCAL_PATH := art
 
 RUNTIME_GTEST_COMMON_SRC_FILES := \
+  imgdiag/imgdiag_test.cc \
   runtime/arch/arch_test.cc \
   runtime/arch/instruction_set_test.cc \
   runtime/arch/instruction_set_features_test.cc \
diff --git a/cmdline/cmdline.h b/cmdline/cmdline.h
new file mode 100644
index 0000000..c15594a
--- /dev/null
+++ b/cmdline/cmdline.h
@@ -0,0 +1,370 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ART_CMDLINE_CMDLINE_H_
+#define ART_CMDLINE_CMDLINE_H_
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <fstream>
+#include <iostream>
+#include <string>
+
+#include "runtime.h"
+#include "base/stringpiece.h"
+#include "noop_compiler_callbacks.h"
+#include "base/logging.h"
+
+#if !defined(NDEBUG)
+#define DBG_LOG LOG(INFO)
+#else
+#define DBG_LOG LOG(DEBUG)
+#endif
+
+namespace art {
+
+// TODO: Move to <runtime/utils.h> and remove all copies of this function.
+static bool LocationToFilename(const std::string& location, InstructionSet isa,
+                               std::string* filename) {
+  bool has_system = false;
+  bool has_cache = false;
+  // image_location = /system/framework/boot.art
+  // system_image_filename = /system/framework/<image_isa>/boot.art
+  std::string system_filename(GetSystemImageFilename(location.c_str(), isa));
+  if (OS::FileExists(system_filename.c_str())) {
+    has_system = true;
+  }
+
+  bool have_android_data = false;
+  bool dalvik_cache_exists = false;
+  bool is_global_cache = false;
+  std::string dalvik_cache;
+  GetDalvikCache(GetInstructionSetString(isa), false, &dalvik_cache,
+                 &have_android_data, &dalvik_cache_exists, &is_global_cache);
+
+  std::string cache_filename;
+  if (have_android_data && dalvik_cache_exists) {
+    // Always set output location even if it does not exist,
+    // so that the caller knows where to create the image.
+    //
+    // image_location = /system/framework/boot.art
+    // *image_filename = /data/dalvik-cache/<image_isa>/boot.art
+    std::string error_msg;
+    if (GetDalvikCacheFilename(location.c_str(), dalvik_cache.c_str(),
+                               &cache_filename, &error_msg)) {
+      has_cache = true;
+    }
+  }
+  if (has_system) {
+    *filename = system_filename;
+    return true;
+  } else if (has_cache) {
+    *filename = cache_filename;
+    return true;
+  } else {
+    return false;
+  }
+}
+
+static Runtime* StartRuntime(const char* boot_image_location,
+                             InstructionSet instruction_set) {
+  CHECK(boot_image_location != nullptr);
+
+  RuntimeOptions options;
+
+  // We are more like a compiler than a run-time. We don't want to execute code.
+  {
+    static NoopCompilerCallbacks callbacks;
+    options.push_back(std::make_pair("compilercallbacks", &callbacks));
+  }
+
+  // Boot image location.
+  {
+    std::string boot_image_option;
+    boot_image_option += "-Ximage:";
+    boot_image_option += boot_image_location;
+    options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
+  }
+
+  // Instruction set.
+  options.push_back(
+      std::make_pair("imageinstructionset",
+                     reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
+
+  if (!Runtime::Create(options, false)) {
+    fprintf(stderr, "Failed to create runtime\n");
+    return nullptr;
+  }
+
+  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
+  // give it away now and then switch to a more manageable ScopedObjectAccess.
+  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
+
+  return Runtime::Current();
+}
+
+struct CmdlineArgs {
+  enum ParseStatus {
+    kParseOk,               // Parse successful. Do not set the error message.
+    kParseUnknownArgument,  // Unknown argument. Do not set the error message.
+    kParseError,            // Parse ok, but failed elsewhere. Print the set error message.
+  };
+
+  bool Parse(int argc, char** argv) {
+    // Skip over argv[0].
+    argv++;
+    argc--;
+
+    if (argc == 0) {
+      fprintf(stderr, "No arguments specified\n");
+      PrintUsage();
+      return false;
+    }
+
+    std::string error_msg;
+    for (int i = 0; i < argc; i++) {
+      const StringPiece option(argv[i]);
+      if (option.starts_with("--boot-image=")) {
+        boot_image_location_ = option.substr(strlen("--boot-image=")).data();
+      } else if (option.starts_with("--instruction-set=")) {
+        StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
+        instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
+        if (instruction_set_ == kNone) {
+          fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
+          PrintUsage();
+          return false;
+        }
+      } else if (option.starts_with("--output=")) {
+        output_name_ = option.substr(strlen("--output=")).ToString();
+        const char* filename = output_name_.c_str();
+        out_.reset(new std::ofstream(filename));
+        if (!out_->good()) {
+          fprintf(stderr, "Failed to open output filename %s\n", filename);
+          PrintUsage();
+          return false;
+        }
+        os_ = out_.get();
+      } else {
+        ParseStatus parse_status = ParseCustom(option, &error_msg);
+
+        if (parse_status == kParseUnknownArgument) {
+          fprintf(stderr, "Unknown argument %s\n", option.data());
+        }
+
+        if (parse_status != kParseOk) {
+          fprintf(stderr, "%s\n", error_msg.c_str());
+          PrintUsage();
+          return false;
+        }
+      }
+    }
+
+    DBG_LOG << "will call parse checks";
+
+    {
+      ParseStatus checks_status = ParseChecks(&error_msg);
+      if (checks_status != kParseOk) {
+          fprintf(stderr, "%s\n", error_msg.c_str());
+          PrintUsage();
+          return false;
+      }
+    }
+
+    return true;
+  }
+
+  virtual std::string GetUsage() const {
+    std::string usage;
+
+    usage +=  // Required.
+        "  --boot-image=<file.art>: provide the image location for the boot class path.\n"
+        "      Do not include the arch as part of the name, it is added automatically.\n"
+        "      Example: --boot-image=/system/framework/boot.art\n"
+        "\n";
+    usage += StringPrintf(  // Optional.
+        "  --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
+        "      file based on the image location set.\n"
+        "      Example: --instruction-set=x86\n"
+        "      Default: %s\n"
+        "\n",
+        GetInstructionSetString(kRuntimeISA));
+    usage +=  // Optional.
+        "  --output=<file> may be used to send the output to a file.\n"
+        "      Example: --output=/tmp/oatdump.txt\n"
+        "\n";
+
+    return usage;
+  }
+
+  // Specified by --boot-image.
+  const char* boot_image_location_ = nullptr;
+  // Specified by --instruction-set.
+  InstructionSet instruction_set_ = kRuntimeISA;
+  // Specified by --output.
+  std::ostream* os_ = &std::cout;
+  std::unique_ptr<std::ofstream> out_;  // If something besides cout is used
+  std::string output_name_;
+
+  virtual ~CmdlineArgs() {}
+
+ protected:
+  virtual ParseStatus ParseCustom(const StringPiece& option, std::string* error_msg) {
+    UNUSED(option);
+    UNUSED(error_msg);
+
+    return kParseUnknownArgument;
+  }
+
+  virtual ParseStatus ParseChecks(std::string* error_msg) {
+    if (boot_image_location_ == nullptr) {
+      *error_msg = "--boot-image must be specified";
+      return kParseError;
+    }
+
+    DBG_LOG << "boot image location: " << boot_image_location_;
+
+    // Checks for --boot-image location.
+    {
+      std::string boot_image_location = boot_image_location_;
+      size_t file_name_idx = boot_image_location.rfind("/");
+      if (file_name_idx == std::string::npos) {  // Prevent a InsertIsaDirectory check failure.
+        *error_msg = "Boot image location must have a / in it";
+        return kParseError;
+      }
+
+      // Don't let image locations with the 'arch' in it through, since it's not a location.
+      // This prevents a common error "Could not create an image space..." when initing the Runtime.
+      if (file_name_idx != std::string::npos) {
+        std::string no_file_name = boot_image_location.substr(0, file_name_idx);
+        size_t ancestor_dirs_idx = no_file_name.rfind("/");
+
+        std::string parent_dir_name;
+        if (ancestor_dirs_idx != std::string::npos) {
+          parent_dir_name = no_file_name.substr(ancestor_dirs_idx + 1);
+        } else {
+          parent_dir_name = no_file_name;
+        }
+
+        DBG_LOG << "boot_image_location parent_dir_name was " << parent_dir_name;
+
+        if (GetInstructionSetFromString(parent_dir_name.c_str()) != kNone) {
+          *error_msg = "Do not specify the architecture as part of the boot image location";
+          return kParseError;
+        }
+      }
+
+      // Check that the boot image location points to a valid file name.
+      std::string file_name;
+      if (!LocationToFilename(boot_image_location, instruction_set_, &file_name)) {
+        *error_msg = StringPrintf("No corresponding file for location '%s' exists",
+                                  file_name.c_str());
+        return kParseError;
+      }
+
+      DBG_LOG << "boot_image_filename does exist: " << file_name;
+    }
+
+    return kParseOk;
+  }
+
+ private:
+  void PrintUsage() {
+    fprintf(stderr, "%s", GetUsage().c_str());
+  }
+};
+
+template <typename Args = CmdlineArgs>
+struct CmdlineMain {
+  int Main(int argc, char** argv) {
+    InitLogging(argv);
+    std::unique_ptr<Args> args = std::unique_ptr<Args>(CreateArguments());
+    args_ = args.get();
+
+    DBG_LOG << "Try to parse";
+
+    if (args_ == nullptr || !args_->Parse(argc, argv)) {
+      return EXIT_FAILURE;
+    }
+
+    std::unique_ptr<Runtime> runtime = CreateRuntime(args.get());
+    if (runtime == nullptr) {
+      return EXIT_FAILURE;
+    }
+
+    bool needs_runtime = NeedsRuntime();
+
+    if (needs_runtime) {
+      if (!ExecuteWithRuntime(runtime.get())) {
+        return EXIT_FAILURE;
+      }
+    } else {
+      if (!ExecuteWithoutRuntime()) {
+        return EXIT_FAILURE;
+      }
+    }
+
+    if (!ExecuteCommon()) {
+      return EXIT_FAILURE;
+    }
+
+    return EXIT_SUCCESS;
+  }
+
+  // Override this function to create your own arguments.
+  // Usually will want to return a subtype of CmdlineArgs.
+  virtual Args* CreateArguments() {
+    return new Args();
+  }
+
+  // Override this function to do something else with the runtime.
+  virtual bool ExecuteWithRuntime(Runtime* runtime) {
+    CHECK(runtime != nullptr);
+    // Do nothing
+    return true;
+  }
+
+  // Does the code execution need a runtime? Sometimes it doesn't.
+  virtual bool NeedsRuntime() {
+    return true;
+  }
+
+  // Do execution without having created a runtime.
+  virtual bool ExecuteWithoutRuntime() {
+    return true;
+  }
+
+  // Continue execution after ExecuteWith[out]Runtime
+  virtual bool ExecuteCommon() {
+    return true;
+  }
+
+  virtual ~CmdlineMain() {}
+
+ protected:
+  Args* args_ = nullptr;
+
+ private:
+  std::unique_ptr<Runtime> CreateRuntime(CmdlineArgs* args) {
+    CHECK(args != nullptr);
+
+    return std::unique_ptr<Runtime>(StartRuntime(args->boot_image_location_,
+                                                 args->instruction_set_));
+  }
+};
+}  // namespace art
+
+#endif  // ART_CMDLINE_CMDLINE_H_
diff --git a/compiler/compiler.h b/compiler/compiler.h
index 07e2fd6..d688ead 100644
--- a/compiler/compiler.h
+++ b/compiler/compiler.h
@@ -41,7 +41,7 @@
 
   static Compiler* Create(CompilerDriver* driver, Kind kind);
 
-  virtual void Init() const = 0;
+  virtual void Init() = 0;
 
   virtual void UnInit() const = 0;
 
diff --git a/compiler/dex/quick/quick_compiler.cc b/compiler/dex/quick/quick_compiler.cc
index c14e22e..102ce17 100644
--- a/compiler/dex/quick/quick_compiler.cc
+++ b/compiler/dex/quick/quick_compiler.cc
@@ -41,7 +41,7 @@
  public:
   explicit QuickCompiler(CompilerDriver* driver) : Compiler(driver, 100) {}
 
-  void Init() const OVERRIDE;
+  void Init() OVERRIDE;
 
   void UnInit() const OVERRIDE;
 
@@ -574,7 +574,7 @@
   cu.disable_opt |= kDisabledOptimizationsPerISA[cu.instruction_set];
 }
 
-void QuickCompiler::Init() const {
+void QuickCompiler::Init() {
   CHECK(GetCompilerDriver()->GetCompilerContext() == nullptr);
 }
 
diff --git a/compiler/dex/verification_results.cc b/compiler/dex/verification_results.cc
index 60d2406..4daed67 100644
--- a/compiler/dex/verification_results.cc
+++ b/compiler/dex/verification_results.cc
@@ -57,8 +57,8 @@
 
   const VerifiedMethod* verified_method = VerifiedMethod::Create(method_verifier, compile);
   if (verified_method == nullptr) {
-    DCHECK(method_verifier->HasFailures());
-    return false;
+    // Do not report an error to the verifier. We'll just punt this later.
+    return true;
   }
 
   WriterMutexLock mu(Thread::Current(), verified_methods_lock_);
diff --git a/compiler/dex/verified_method.cc b/compiler/dex/verified_method.cc
index d684bc9..93e9a51 100644
--- a/compiler/dex/verified_method.cc
+++ b/compiler/dex/verified_method.cc
@@ -49,7 +49,6 @@
   if (compile) {
     /* Generate a register map. */
     if (!verified_method->GenerateGcMap(method_verifier)) {
-      CHECK(method_verifier->HasFailures());
       return nullptr;  // Not a real failure, but a failure to encode.
     }
     if (kIsDebugBuild) {
@@ -83,17 +82,17 @@
   ComputeGcMapSizes(method_verifier, &num_entries, &ref_bitmap_bits, &pc_bits);
   // There's a single byte to encode the size of each bitmap.
   if (ref_bitmap_bits >= kBitsPerByte * 8192 /* 13-bit size */) {
-    // TODO: either a better GC map format or per method failures
-    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
-        << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers";
+    LOG(WARNING) << "Cannot encode GC map for method with " << ref_bitmap_bits << " registers: "
+                 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
+                                 *method_verifier->GetMethodReference().dex_file);
     return false;
   }
   size_t ref_bitmap_bytes = RoundUp(ref_bitmap_bits, kBitsPerByte) / kBitsPerByte;
   // There are 2 bytes to encode the number of entries.
   if (num_entries >= 65536) {
-    // TODO: Either a better GC map format or per method failures.
-    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
-        << "Cannot encode GC map for method with " << num_entries << " entries";
+    LOG(WARNING) << "Cannot encode GC map for method with " << num_entries << " entries: "
+                 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
+                                 *method_verifier->GetMethodReference().dex_file);
     return false;
   }
   size_t pc_bytes;
@@ -105,10 +104,10 @@
     format = verifier::kRegMapFormatCompact16;
     pc_bytes = 2;
   } else {
-    // TODO: Either a better GC map format or per method failures.
-    method_verifier->Fail(verifier::VERIFY_ERROR_BAD_CLASS_HARD)
-        << "Cannot encode GC map for method with "
-        << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2)";
+    LOG(WARNING) << "Cannot encode GC map for method with "
+                 << (1 << pc_bits) << " instructions (number is rounded up to nearest power of 2): "
+                 << PrettyMethod(method_verifier->GetMethodReference().dex_method_index,
+                                 *method_verifier->GetMethodReference().dex_file);
     return false;
   }
   size_t table_size = ((pc_bytes + ref_bitmap_bytes) * num_entries) + 4;
@@ -161,7 +160,7 @@
         }
       }
     } else {
-      DCHECK(reg_bitmap == NULL);
+      DCHECK(i >= 65536 || reg_bitmap == NULL);
     }
   }
 }
diff --git a/compiler/driver/compiler_driver.cc b/compiler/driver/compiler_driver.cc
index 051b310..cbb23c2 100644
--- a/compiler/driver/compiler_driver.cc
+++ b/compiler/driver/compiler_driver.cc
@@ -2120,8 +2120,12 @@
   } else if ((access_flags & kAccAbstract) != 0) {
     // Abstract methods don't have code.
   } else {
+    bool has_verified_method = verification_results_->GetVerifiedMethod(method_ref) != nullptr;
     bool compile = compilation_enabled &&
-                   verification_results_->IsCandidateForCompilation(method_ref, access_flags);
+                   // Basic checks, e.g., not <clinit>.
+                   verification_results_->IsCandidateForCompilation(method_ref, access_flags) &&
+                   // Did not fail to create VerifiedMethod metadata.
+                   has_verified_method;
     if (compile) {
       // NOTE: if compiler declines to compile this method, it will return nullptr.
       compiled_method = compiler_->Compile(code_item, access_flags, invoke_type, class_def_idx,
@@ -2129,10 +2133,12 @@
     }
     if (compiled_method == nullptr && dex_to_dex_compilation_level != kDontDexToDexCompile) {
       // TODO: add a command-line option to disable DEX-to-DEX compilation ?
+      // Do not optimize if a VerifiedMethod is missing. SafeCast elision, for example, relies on
+      // it.
       (*dex_to_dex_compiler_)(*this, code_item, access_flags,
                               invoke_type, class_def_idx,
                               method_idx, class_loader, dex_file,
-                              dex_to_dex_compilation_level);
+                              has_verified_method ? dex_to_dex_compilation_level : kRequired);
     }
   }
   if (kTimeCompileMethod) {
diff --git a/compiler/elf_writer_test.cc b/compiler/elf_writer_test.cc
index 7fabc30..fd3a912 100644
--- a/compiler/elf_writer_test.cc
+++ b/compiler/elf_writer_test.cc
@@ -51,14 +51,7 @@
 #else
 TEST_F(ElfWriterTest, dlsym) {
 #endif
-  std::string elf_location;
-  if (IsHost()) {
-    const char* host_dir = getenv("ANDROID_HOST_OUT");
-    CHECK(host_dir != NULL);
-    elf_location = StringPrintf("%s/framework/core.oat", host_dir);
-  } else {
-    elf_location = "/data/art-test/core.oat";
-  }
+  std::string elf_location = GetCoreOatLocation();
   std::string elf_filename = GetSystemImageFilename(elf_location.c_str(), kRuntimeISA);
   LOG(INFO) << "elf_filename=" << elf_filename;
 
diff --git a/compiler/optimizing/constant_folding_test.cc b/compiler/optimizing/constant_folding_test.cc
index cad6683..ed7e57b 100644
--- a/compiler/optimizing/constant_folding_test.cc
+++ b/compiler/optimizing/constant_folding_test.cc
@@ -47,9 +47,9 @@
 
   x86::CodeGeneratorX86 codegen(graph);
   HConstantFolding(graph).Run();
-  SSAChecker ssa_checker(&allocator, graph);
-  ssa_checker.Run();
-  ASSERT_TRUE(ssa_checker.IsValid());
+  SSAChecker ssa_checker_cf(&allocator, graph);
+  ssa_checker_cf.Run();
+  ASSERT_TRUE(ssa_checker_cf.IsValid());
 
   StringPrettyPrinter printer_after_cf(graph);
   printer_after_cf.VisitInsertionOrder();
@@ -59,8 +59,9 @@
   check_after_cf(graph);
 
   HDeadCodeElimination(graph).Run();
-  ssa_checker.Run();
-  ASSERT_TRUE(ssa_checker.IsValid());
+  SSAChecker ssa_checker_dce(&allocator, graph);
+  ssa_checker_dce.Run();
+  ASSERT_TRUE(ssa_checker_dce.IsValid());
 
   StringPrettyPrinter printer_after_dce(graph);
   printer_after_dce.VisitInsertionOrder();
diff --git a/compiler/optimizing/graph_checker.cc b/compiler/optimizing/graph_checker.cc
index 5d712fe..e55175f 100644
--- a/compiler/optimizing/graph_checker.cc
+++ b/compiler/optimizing/graph_checker.cc
@@ -16,9 +16,9 @@
 
 #include "graph_checker.h"
 
-#include <string>
 #include <map>
 #include <sstream>
+#include <string>
 
 #include "base/bit_vector-inl.h"
 
@@ -123,6 +123,14 @@
 }
 
 void GraphChecker::VisitInstruction(HInstruction* instruction) {
+  if (seen_ids_.IsBitSet(instruction->GetId())) {
+    std::stringstream error;
+    error << "Duplicate id in graph " << instruction->GetId() << ".";
+    errors_.push_back(error.str());
+  } else {
+    seen_ids_.SetBit(instruction->GetId());
+  }
+
   // Ensure `instruction` is associated with `current_block_`.
   if (instruction->GetBlock() != current_block_) {
     std::stringstream error;
diff --git a/compiler/optimizing/graph_checker.h b/compiler/optimizing/graph_checker.h
index b6c9f17..ba60cb9 100644
--- a/compiler/optimizing/graph_checker.h
+++ b/compiler/optimizing/graph_checker.h
@@ -30,7 +30,8 @@
                const char* dump_prefix = "art::GraphChecker: ")
     : HGraphDelegateVisitor(graph),
       allocator_(allocator),
-      dump_prefix_(dump_prefix) {}
+      dump_prefix_(dump_prefix),
+      seen_ids_(allocator, graph->GetCurrentInstructionId(), false) {}
 
   // Check the whole graph (in insertion order).
   virtual void Run() { VisitInsertionOrder(); }
@@ -68,6 +69,7 @@
  private:
   // String displayed before dumped errors.
   const char* const dump_prefix_;
+  ArenaBitVector seen_ids_;
 
   DISALLOW_COPY_AND_ASSIGN(GraphChecker);
 };
diff --git a/compiler/optimizing/graph_visualizer.cc b/compiler/optimizing/graph_visualizer.cc
index 5d1703e..b14b0a7 100644
--- a/compiler/optimizing/graph_visualizer.cc
+++ b/compiler/optimizing/graph_visualizer.cc
@@ -167,6 +167,15 @@
       }
       output_ << "]";
     }
+    if (instruction->IsIntConstant()) {
+      output_ << " " << instruction->AsIntConstant()->GetValue();
+    } else if (instruction->IsLongConstant()) {
+      output_ << " " << instruction->AsLongConstant()->GetValue();
+    } else if (instruction->IsFloatConstant()) {
+      output_ << " " << instruction->AsFloatConstant()->GetValue();
+    } else if (instruction->IsDoubleConstant()) {
+      output_ << " " << instruction->AsDoubleConstant()->GetValue();
+    }
     if (pass_name_ == kLivenessPassName && instruction->GetLifetimePosition() != kNoLifetime) {
       output_ << " (liveness: " << instruction->GetLifetimePosition();
       if (instruction->HasLiveInterval()) {
@@ -270,7 +279,7 @@
                                    const char* string_filter,
                                    const CodeGenerator& codegen,
                                    const char* method_name)
-    : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
+  : output_(output), graph_(graph), codegen_(codegen), is_enabled_(false) {
   if (output == nullptr) {
     return;
   }
@@ -279,7 +288,7 @@
   }
 
   is_enabled_ = true;
-  HGraphVisualizerPrinter printer(graph, *output_, "", codegen_);
+  HGraphVisualizerPrinter printer(graph_, *output_, "", codegen_);
   printer.StartTag("compilation");
   printer.PrintProperty("name", method_name);
   printer.PrintProperty("method", method_name);
@@ -287,12 +296,12 @@
   printer.EndTag("compilation");
 }
 
-void HGraphVisualizer::DumpGraph(const char* pass_name) const {
-  if (!is_enabled_) {
-    return;
+void HGraphVisualizer::DumpGraph(const char* pass_name, bool is_after_pass) const {
+  if (is_enabled_) {
+    std::string pass_desc = std::string(pass_name) + (is_after_pass ? " (after)" : " (before)");
+    HGraphVisualizerPrinter printer(graph_, *output_, pass_desc.c_str(), codegen_);
+    printer.Run();
   }
-  HGraphVisualizerPrinter printer(graph_, *output_, pass_name, codegen_);
-  printer.Run();
 }
 
 }  // namespace art
diff --git a/compiler/optimizing/graph_visualizer.h b/compiler/optimizing/graph_visualizer.h
index b5baed9..b90d15e 100644
--- a/compiler/optimizing/graph_visualizer.h
+++ b/compiler/optimizing/graph_visualizer.h
@@ -32,28 +32,18 @@
 static const char* kRegisterAllocatorPassName = "register";
 
 /**
- * If enabled, emits compilation information suitable for the c1visualizer tool
- * and IRHydra.
- * Currently only works if the compiler is single threaded.
+ * This class outputs the HGraph in the C1visualizer format.
+ * Note: Currently only works if the compiler is single threaded.
  */
 class HGraphVisualizer : public ValueObject {
  public:
-  /**
-   * If output is not null, and the method name of the dex compilation
-   * unit contains `string_filter`, the compilation information will be
-   * emitted.
-   */
   HGraphVisualizer(std::ostream* output,
                    HGraph* graph,
                    const char* string_filter,
                    const CodeGenerator& codegen,
                    const char* method_name);
 
-  /**
-   * If this visualizer is enabled, emit the compilation information
-   * in `output_`.
-   */
-  void DumpGraph(const char* pass_name) const;
+  void DumpGraph(const char* pass_name, bool is_after_pass = true) const;
 
  private:
   std::ostream* const output_;
diff --git a/compiler/optimizing/inliner.cc b/compiler/optimizing/inliner.cc
index 1de5b78..73eb521 100644
--- a/compiler/optimizing/inliner.cc
+++ b/compiler/optimizing/inliner.cc
@@ -200,6 +200,11 @@
   }
 
   callee_graph->InlineInto(graph_, invoke_instruction);
+
+  // Now that we have inlined the callee, we need to update the next
+  // instruction id of the caller, so that new instructions added
+  // after optimizations get a unique id.
+  graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId());
   VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, outer_dex_file);
   outer_stats_->RecordStat(kInlinedInvoke);
   return true;
diff --git a/compiler/optimizing/nodes.cc b/compiler/optimizing/nodes.cc
index fb941b5..4133cf6 100644
--- a/compiler/optimizing/nodes.cc
+++ b/compiler/optimizing/nodes.cc
@@ -750,13 +750,16 @@
     }
   }
 
-  // Finally, replace the invoke with the return value of the inlined graph.
+  // Replace the invoke with the return value of the inlined graph.
   if (last->IsReturn()) {
     invoke->ReplaceWith(last->InputAt(0));
     body->RemoveInstruction(last);
   } else {
     DCHECK(last->IsReturnVoid());
   }
+
+  // Finally remove the invoke from the caller.
+  invoke->GetBlock()->RemoveInstruction(invoke);
 }
 
 }  // namespace art
diff --git a/compiler/optimizing/optimizing_compiler.cc b/compiler/optimizing/optimizing_compiler.cc
index deebaf7..94751f8 100644
--- a/compiler/optimizing/optimizing_compiler.cc
+++ b/compiler/optimizing/optimizing_compiler.cc
@@ -68,13 +68,8 @@
 };
 
 /**
- * If set to true, generates a file suitable for the c1visualizer tool and IRHydra.
- */
-static bool kIsVisualizerEnabled = false;
-
-/**
  * Filter to apply to the visualizer. Methods whose name contain that filter will
- * be in the file.
+ * be dumped.
  */
 static const char* kStringFilter = "";
 
@@ -114,7 +109,7 @@
 
   void InitCompilationUnit(CompilationUnit& cu ATTRIBUTE_UNUSED) const OVERRIDE {}
 
-  void Init() const OVERRIDE {}
+  void Init() OVERRIDE;
 
   void UnInit() const OVERRIDE {}
 
@@ -136,8 +131,16 @@
     : Compiler(driver, kMaximumCompilationTimeBeforeWarning),
       run_optimizations_(
           driver->GetCompilerOptions().GetCompilerFilter() != CompilerOptions::kTime),
-      compilation_stats_() {
-  if (kIsVisualizerEnabled) {
+      compilation_stats_() {}
+
+void OptimizingCompiler::Init() {
+  // Enable C1visualizer output. Must be done in Init() because the compiler
+  // driver is not fully initialized when passed to the compiler's constructor.
+  CompilerDriver* driver = GetCompilerDriver();
+  if (driver->GetDumpPasses()) {
+    CHECK_EQ(driver->GetThreadCount(), 1U)
+      << "Graph visualizer requires the compiler to run single-threaded. "
+      << "Invoke the compiler with '-j1'.";
     visualizer_output_.reset(new std::ofstream("art.cfg"));
   }
 }
@@ -213,8 +216,9 @@
 
   for (size_t i = 0; i < arraysize(optimizations); ++i) {
     HOptimization* optimization = optimizations[i];
+    visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/false);
     optimization->Run();
-    visualizer.DumpGraph(optimization->GetPassName());
+    visualizer.DumpGraph(optimization->GetPassName(), /*is_after=*/true);
     optimization->Check();
   }
 }
diff --git a/compiler/optimizing/test/ConstantFolding.java b/compiler/optimizing/test/ConstantFolding.java
new file mode 100644
index 0000000..7fac5a9
--- /dev/null
+++ b/compiler/optimizing/test/ConstantFolding.java
@@ -0,0 +1,221 @@
+/*
+* Copyright (C) 2014 The Android Open Source Project
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*      http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
+
+public class ConstantFolding {
+
+  /**
+   * Tiny three-register program exercising int constant folding
+   * on negation.
+   */
+
+  // CHECK-START: int ConstantFolding.IntNegation() constant_folding (before)
+  // CHECK:   [[Const42:i[0-9]+]]  IntConstant 42
+  // CHECK:   [[Neg:i[0-9]+]]      Neg [ [[Const42]] ]
+  // CHECK:                        Return [ [[Neg]] ]
+
+  // CHECK-START: int ConstantFolding.IntNegation() constant_folding (after)
+  // CHECK:   [[ConstN42:i[0-9]+]] IntConstant -42
+  // CHECK:                        Return [ [[ConstN42]] ]
+
+  public static int IntNegation() {
+    int x, y;
+    x = 42;
+    y = -x;
+    return y;
+  }
+
+  /**
+   * Tiny three-register program exercising int constant folding
+   * on addition.
+   */
+
+  // CHECK-START: int ConstantFolding.IntAddition1() constant_folding (before)
+  // CHECK:   [[Const1:i[0-9]+]]   IntConstant 1
+  // CHECK:   [[Const2:i[0-9]+]]   IntConstant 2
+  // CHECK:   [[Add:i[0-9]+]]      Add [ [[Const1]] [[Const2]] ]
+  // CHECK:                        Return [ [[Add]] ]
+
+  // CHECK-START: int ConstantFolding.IntAddition1() constant_folding (after)
+  // CHECK:   [[Const3:i[0-9]+]]   IntConstant 3
+  // CHECK:                        Return [ [[Const3]] ]
+
+  public static int IntAddition1() {
+    int a, b, c;
+    a = 1;
+    b = 2;
+    c = a + b;
+    return c;
+  }
+
+ /**
+  * Small three-register program exercising int constant folding
+  * on addition.
+  */
+
+  // CHECK-START: int ConstantFolding.IntAddition2() constant_folding (before)
+  // CHECK:   [[Const1:i[0-9]+]]   IntConstant 1
+  // CHECK:   [[Const2:i[0-9]+]]   IntConstant 2
+  // CHECK:   [[Const5:i[0-9]+]]   IntConstant 5
+  // CHECK:   [[Const6:i[0-9]+]]   IntConstant 6
+  // CHECK:   [[Add1:i[0-9]+]]     Add [ [[Const1]] [[Const2]] ]
+  // CHECK:   [[Add2:i[0-9]+]]     Add [ [[Const5]] [[Const6]] ]
+  // CHECK:   [[Add3:i[0-9]+]]     Add [ [[Add1]] [[Add2]] ]
+  // CHECK:                        Return [ [[Add3]] ]
+
+  // CHECK-START: int ConstantFolding.IntAddition2() constant_folding (after)
+  // CHECK:   [[Const14:i[0-9]+]]  IntConstant 14
+  // CHECK:                        Return [ [[Const14]] ]
+
+  public static int IntAddition2() {
+    int a, b, c;
+    a = 1;
+    b = 2;
+    a += b;
+    b = 5;
+    c = 6;
+    b += c;
+    c = a + b;
+    return c;
+  }
+
+  /**
+   * Tiny three-register program exercising int constant folding
+   * on subtraction.
+   */
+
+  // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (before)
+  // CHECK:   [[Const5:i[0-9]+]]   IntConstant 5
+  // CHECK:   [[Const2:i[0-9]+]]   IntConstant 2
+  // CHECK:   [[Sub:i[0-9]+]]      Sub [ [[Const5]] [[Const2]] ]
+  // CHECK:                        Return [ [[Sub]] ]
+
+  // CHECK-START: int ConstantFolding.IntSubtraction() constant_folding (after)
+  // CHECK:   [[Const3:i[0-9]+]]   IntConstant 3
+  // CHECK:                        Return [ [[Const3]] ]
+
+  public static int IntSubtraction() {
+    int a, b, c;
+    a = 5;
+    b = 2;
+    c = a - b;
+    return c;
+  }
+
+  /**
+   * Tiny three-register program exercising long constant folding
+   * on addition.
+   */
+
+  // CHECK-START: long ConstantFolding.LongAddition() constant_folding (before)
+  // CHECK:   [[Const1:j[0-9]+]]   LongConstant 1
+  // CHECK:   [[Const2:j[0-9]+]]   LongConstant 2
+  // CHECK:   [[Add:j[0-9]+]]      Add [ [[Const1]] [[Const2]] ]
+  // CHECK:                        Return [ [[Add]] ]
+
+  // CHECK-START: long ConstantFolding.LongAddition() constant_folding (after)
+  // CHECK:   [[Const3:j[0-9]+]]   LongConstant 3
+  // CHECK:                        Return [ [[Const3]] ]
+
+  public static long LongAddition() {
+    long a, b, c;
+    a = 1L;
+    b = 2L;
+    c = a + b;
+    return c;
+  }
+
+  /**
+   * Tiny three-register program exercising long constant folding
+   * on subtraction.
+   */
+
+  // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (before)
+  // CHECK:   [[Const5:j[0-9]+]]   LongConstant 5
+  // CHECK:   [[Const2:j[0-9]+]]   LongConstant 2
+  // CHECK:   [[Sub:j[0-9]+]]      Sub [ [[Const5]] [[Const2]] ]
+  // CHECK:                        Return [ [[Sub]] ]
+
+  // CHECK-START: long ConstantFolding.LongSubtraction() constant_folding (after)
+  // CHECK:   [[Const3:j[0-9]+]]   LongConstant 3
+  // CHECK:                        Return [ [[Const3]] ]
+
+  public static long LongSubtraction() {
+    long a, b, c;
+    a = 5L;
+    b = 2L;
+    c = a - b;
+    return c;
+  }
+
+  /**
+   * Three-register program with a constant (static) condition.
+   */
+
+  // CHECK-START: int ConstantFolding.StaticCondition() constant_folding (before)
+  // CHECK:   [[Const5:i[0-9]+]]   IntConstant 5
+  // CHECK:   [[Const2:i[0-9]+]]   IntConstant 2
+  // CHECK:   [[Cond:z[0-9]+]]     GreaterThanOrEqual [ [[Const5]] [[Const2]] ]
+  // CHECK:                        If [ [[Cond]] ]
+
+  // CHECK-START: int ConstantFolding.StaticCondition() constant_folding (after)
+  // CHECK:   [[Const1:i[0-9]+]]   IntConstant 1
+  // CHECK:                        If [ [[Const1]] ]
+
+  public static int StaticCondition() {
+    int a, b, c;
+    a = 5;
+    b = 2;
+    if (a < b)
+      c = a + b;
+    else
+      c = a - b;
+    return c;
+  }
+
+  /**
+   * Four-variable program with jumps leading to the creation of many
+   * blocks.
+   *
+   * The intent of this test is to ensure that all constant expressions
+   * are actually evaluated at compile-time, thanks to the reverse
+   * (forward) post-order traversal of the the dominator tree.
+   */
+
+  // CHECK-START: int ConstantFolding.JumpsAndConditionals(boolean) constant_folding (before)
+  // CHECK:   [[Const5:i[0-9]+]]   IntConstant 5
+  // CHECK:   [[Const2:i[0-9]+]]   IntConstant 2
+  // CHECK:   [[Add:i[0-9]+]]      Add [ [[Const5]] [[Const2]] ]
+  // CHECK:   [[Phi:i[0-9]+]]      Phi [ [[Add]] [[Sub:i[0-9]+]] ]
+  // CHECK:                        Return [ [[Phi]] ]
+  // CHECK:   [[Sub]]              Sub [ [[Const5]] [[Const2]] ]
+
+  // CHECK-START: int ConstantFolding.JumpsAndConditionals(boolean) constant_folding (after)
+  // CHECK:   [[Const7:i[0-9]+]]   IntConstant 7
+  // CHECK:   [[Phi:i[0-9]+]]      Phi [ [[Const7]] [[Const3:i[0-9]+]] ]
+  // CHECK:                        Return [ [[Phi]] ]
+  // CHECK:   [[Const3]]           IntConstant 3
+
+  public static int JumpsAndConditionals(boolean cond) {
+    int a, b, c;
+    a = 5;
+    b = 2;
+    if (cond)
+      c = a + b;
+    else
+      c = a - b;
+    return c;
+  }
+}
diff --git a/disassembler/disassembler_x86.cc b/disassembler/disassembler_x86.cc
index b58f5fa..1a768c8 100644
--- a/disassembler/disassembler_x86.cc
+++ b/disassembler/disassembler_x86.cc
@@ -171,7 +171,7 @@
   std::ostringstream address;
   if (mod == 0 && rm == 5) {
     if (!supports_rex_) {  // Absolute address.
-      *address_bits = *reinterpret_cast<const uint32_t*>(**instr);
+      *address_bits = *reinterpret_cast<const uint32_t*>(*instr);
       address << StringPrintf("[0x%x]", *address_bits);
     } else {  // 64-bit RIP relative addressing.
       address << StringPrintf("[RIP + 0x%x]",  *reinterpret_cast<const uint32_t*>(*instr));
diff --git a/imgdiag/Android.mk b/imgdiag/Android.mk
new file mode 100644
index 0000000..d5d7c22
--- /dev/null
+++ b/imgdiag/Android.mk
@@ -0,0 +1,28 @@
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+LOCAL_PATH := $(call my-dir)
+
+include art/build/Android.executable.mk
+
+IMGDIAG_SRC_FILES := \
+	imgdiag.cc
+
+# Note that this tool needs to be built for both 32-bit and 64-bit since it requires
+# that the image it's analyzing be the same ISA as the runtime ISA.
+
+# Build variants {target,host} x {debug,ndebug} x {32,64}
+$(eval $(call build-art-multi-executable,imgdiag,$(IMGDIAG_SRC_FILES),libart-compiler libbacktrace,libcutils,libziparchive-host,art/compiler,both))
diff --git a/imgdiag/imgdiag.cc b/imgdiag/imgdiag.cc
new file mode 100644
index 0000000..9b57ecb
--- /dev/null
+++ b/imgdiag/imgdiag.cc
@@ -0,0 +1,951 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <fstream>
+#include <iostream>
+#include <string>
+#include <vector>
+#include <set>
+#include <map>
+
+#include "base/unix_file/fd_file.h"
+#include "base/stringprintf.h"
+#include "gc/space/image_space.h"
+#include "gc/heap.h"
+#include "mirror/class-inl.h"
+#include "mirror/object-inl.h"
+#include "mirror/art_method-inl.h"
+#include "image.h"
+#include "scoped_thread_state_change.h"
+#include "os.h"
+#include "gc_map.h"
+
+#include "cmdline.h"
+#include "backtrace/BacktraceMap.h"
+
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <signal.h>
+
+namespace art {
+
+class ImgDiagDumper {
+ public:
+  explicit ImgDiagDumper(std::ostream* os,
+                       const ImageHeader& image_header,
+                       const char* image_location,
+                       pid_t image_diff_pid)
+      : os_(os),
+        image_header_(image_header),
+        image_location_(image_location),
+        image_diff_pid_(image_diff_pid) {}
+
+  bool Dump() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::ostream& os = *os_;
+    os << "MAGIC: " << image_header_.GetMagic() << "\n\n";
+
+    os << "IMAGE BEGIN: " << reinterpret_cast<void*>(image_header_.GetImageBegin()) << "\n\n";
+
+    bool ret = true;
+    if (image_diff_pid_ >= 0) {
+      os << "IMAGE DIFF PID (" << image_diff_pid_ << "): ";
+      ret = DumpImageDiff(image_diff_pid_);
+      os << "\n\n";
+    } else {
+      os << "IMAGE DIFF PID: disabled\n\n";
+    }
+
+    os << std::flush;
+
+    return ret;
+  }
+
+ private:
+  static bool EndsWith(const std::string& str, const std::string& suffix) {
+    return str.size() >= suffix.size() &&
+           str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
+  }
+
+  // Return suffix of the file path after the last /. (e.g. /foo/bar -> bar, bar -> bar)
+  static std::string BaseName(const std::string& str) {
+    size_t idx = str.rfind("/");
+    if (idx == std::string::npos) {
+      return str;
+    }
+
+    return str.substr(idx + 1);
+  }
+
+  bool DumpImageDiff(pid_t image_diff_pid) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::ostream& os = *os_;
+
+    {
+      struct stat sts;
+      std::string proc_pid_str = StringPrintf("/proc/%ld", static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
+      if (stat(proc_pid_str.c_str(), &sts) == -1) {
+        os << "Process does not exist";
+        return false;
+      }
+    }
+
+    // Open /proc/$pid/maps to view memory maps
+    auto proc_maps = std::unique_ptr<BacktraceMap>(BacktraceMap::Create(image_diff_pid));
+    if (proc_maps == nullptr) {
+      os << "Could not read backtrace maps";
+      return false;
+    }
+
+    bool found_boot_map = false;
+    backtrace_map_t boot_map = backtrace_map_t();
+    // Find the memory map only for boot.art
+    for (const backtrace_map_t& map : *proc_maps) {
+      if (EndsWith(map.name, GetImageLocationBaseName())) {
+        if ((map.flags & PROT_WRITE) != 0) {
+          boot_map = map;
+          found_boot_map = true;
+          break;
+        }
+        // In actuality there's more than 1 map, but the second one is read-only.
+        // The one we care about is the write-able map.
+        // The readonly maps are guaranteed to be identical, so its not interesting to compare
+        // them.
+      }
+    }
+
+    if (!found_boot_map) {
+      os << "Could not find map for " << GetImageLocationBaseName();
+      return false;
+    }
+
+    // Future idea: diff against zygote so we can ignore the shared dirty pages.
+    return DumpImageDiffMap(image_diff_pid, boot_map);
+  }
+
+    // Look at /proc/$pid/mem and only diff the things from there
+  bool DumpImageDiffMap(pid_t image_diff_pid, const backtrace_map_t& boot_map)
+    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    std::ostream& os = *os_;
+    const size_t pointer_size = InstructionSetPointerSize(
+        Runtime::Current()->GetInstructionSet());
+
+    std::string file_name = StringPrintf("/proc/%ld/mem", static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
+
+    size_t boot_map_size = boot_map.end - boot_map.start;
+
+    // Open /proc/$pid/mem as a file
+    auto map_file = std::unique_ptr<File>(OS::OpenFileForReading(file_name.c_str()));
+    if (map_file == nullptr) {
+      os << "Failed to open " << file_name << " for reading";
+      return false;
+    }
+
+    // Memory-map /proc/$pid/mem subset from the boot map
+    CHECK(boot_map.end >= boot_map.start);
+
+    std::string error_msg;
+
+    // Walk the bytes and diff against our boot image
+    const ImageHeader& boot_image_header = GetBootImageHeader();
+
+    os << "\nObserving boot image header at address "
+       << reinterpret_cast<const void*>(&boot_image_header)
+       << "\n\n";
+
+    const uint8_t* image_begin_unaligned = boot_image_header.GetImageBegin();
+    const uint8_t* image_end_unaligned = image_begin_unaligned + boot_image_header.GetImageSize();
+
+    // Adjust range to nearest page
+    const uint8_t* image_begin = AlignDown(image_begin_unaligned, kPageSize);
+    const uint8_t* image_end = AlignUp(image_end_unaligned, kPageSize);
+
+    ptrdiff_t page_off_begin = boot_image_header.GetImageBegin() - image_begin;
+
+    if (reinterpret_cast<uintptr_t>(image_begin) > boot_map.start ||
+        reinterpret_cast<uintptr_t>(image_end) < boot_map.end) {
+      // Sanity check that we aren't trying to read a completely different boot image
+      os << "Remote boot map is out of range of local boot map: " <<
+        "local begin " << reinterpret_cast<const void*>(image_begin) <<
+        ", local end " << reinterpret_cast<const void*>(image_end) <<
+        ", remote begin " << reinterpret_cast<const void*>(boot_map.start) <<
+        ", remote end " << reinterpret_cast<const void*>(boot_map.end);
+      return false;
+      // If we wanted even more validation we could map the ImageHeader from the file
+    }
+
+    std::vector<uint8_t> remote_contents(boot_map_size);
+    if (!map_file->PreadFully(&remote_contents[0], boot_map_size, boot_map.start)) {
+      os << "Could not fully read file " << file_name;
+      return false;
+    }
+
+    std::string page_map_file_name = StringPrintf("/proc/%ld/pagemap",
+                                                  static_cast<long>(image_diff_pid));  // NOLINT [runtime/int]
+    auto page_map_file = std::unique_ptr<File>(OS::OpenFileForReading(page_map_file_name.c_str()));
+    if (page_map_file == nullptr) {
+      os << "Failed to open " << page_map_file_name << " for reading: " << strerror(errno);
+      return false;
+    }
+
+    // Not truly clean, mmap-ing boot.art again would be more pristine, but close enough
+    const char* clean_page_map_file_name = "/proc/self/pagemap";
+    auto clean_page_map_file = std::unique_ptr<File>(
+        OS::OpenFileForReading(clean_page_map_file_name));
+    if (clean_page_map_file == nullptr) {
+      os << "Failed to open " << clean_page_map_file_name << " for reading: " << strerror(errno);
+      return false;
+    }
+
+    auto kpage_flags_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpageflags"));
+    if (kpage_flags_file == nullptr) {
+      os << "Failed to open /proc/kpageflags for reading: " << strerror(errno);
+      return false;
+    }
+
+    auto kpage_count_file = std::unique_ptr<File>(OS::OpenFileForReading("/proc/kpagecount"));
+    if (kpage_count_file == nullptr) {
+      os << "Failed to open /proc/kpagecount for reading:" << strerror(errno);
+      return false;
+    }
+
+    std::set<size_t> dirty_page_set_remote;  // Set of the remote virtual page indices that are dirty
+    std::set<size_t> dirty_page_set_local;   // Set of the local virtual page indices that are dirty
+
+    size_t different_int32s = 0;
+    size_t different_bytes = 0;
+    size_t different_pages = 0;
+    size_t virtual_page_idx = 0;   // Virtual page number (for an absolute memory address)
+    size_t page_idx = 0;           // Page index relative to 0
+    size_t previous_page_idx = 0;  // Previous page index relative to 0
+    size_t dirty_pages = 0;
+    size_t private_pages = 0;
+    size_t private_dirty_pages = 0;
+
+    // Iterate through one page at a time. Boot map begin/end already implicitly aligned.
+    for (uintptr_t begin = boot_map.start; begin != boot_map.end; begin += kPageSize) {
+      ptrdiff_t offset = begin - boot_map.start;
+
+      // We treat the image header as part of the memory map for now
+      // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
+      // But it might still be interesting to see if any of the ImageHeader data mutated
+      const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
+      uint8_t* remote_ptr = &remote_contents[offset];
+
+      if (memcmp(local_ptr, remote_ptr, kPageSize) != 0) {
+        different_pages++;
+
+        // Count the number of 32-bit integers that are different.
+        for (size_t i = 0; i < kPageSize / sizeof(uint32_t); ++i) {
+          uint32_t* remote_ptr_int32 = reinterpret_cast<uint32_t*>(remote_ptr);
+          const uint32_t* local_ptr_int32 = reinterpret_cast<const uint32_t*>(local_ptr);
+
+          if (remote_ptr_int32[i] != local_ptr_int32[i]) {
+            different_int32s++;
+          }
+        }
+      }
+    }
+
+    // Iterate through one byte at a time.
+    for (uintptr_t begin = boot_map.start; begin != boot_map.end; ++begin) {
+      previous_page_idx = page_idx;
+      ptrdiff_t offset = begin - boot_map.start;
+
+      // We treat the image header as part of the memory map for now
+      // If we wanted to change this, we could pass base=start+sizeof(ImageHeader)
+      // But it might still be interesting to see if any of the ImageHeader data mutated
+      const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&boot_image_header) + offset;
+      uint8_t* remote_ptr = &remote_contents[offset];
+
+      virtual_page_idx = reinterpret_cast<uintptr_t>(local_ptr) / kPageSize;
+
+      // Calculate the page index, relative to the 0th page where the image begins
+      page_idx = (offset + page_off_begin) / kPageSize;
+      if (*local_ptr != *remote_ptr) {
+        // Track number of bytes that are different
+        different_bytes++;
+      }
+
+      // Independently count the # of dirty pages on the remote side
+      size_t remote_virtual_page_idx = begin / kPageSize;
+      if (previous_page_idx != page_idx) {
+        uint64_t page_count = 0xC0FFEE;
+        // TODO: virtual_page_idx needs to be from the same process
+        int dirtiness = (IsPageDirty(page_map_file.get(),        // Image-diff-pid procmap
+                                     clean_page_map_file.get(),  // Self procmap
+                                     kpage_flags_file.get(),
+                                     kpage_count_file.get(),
+                                     remote_virtual_page_idx,    // potentially "dirty" page
+                                     virtual_page_idx,           // true "clean" page
+                                     &page_count,
+                                     &error_msg));
+        if (dirtiness < 0) {
+          os << error_msg;
+          return false;
+        } else if (dirtiness > 0) {
+          dirty_pages++;
+          dirty_page_set_remote.insert(dirty_page_set_remote.end(), remote_virtual_page_idx);
+          dirty_page_set_local.insert(dirty_page_set_local.end(), virtual_page_idx);
+        }
+
+        bool is_dirty = dirtiness > 0;
+        bool is_private = page_count == 1;
+
+        if (page_count == 1) {
+          private_pages++;
+        }
+
+        if (is_dirty && is_private) {
+          private_dirty_pages++;
+        }
+      }
+    }
+
+    // Walk each object in the remote image space and compare it against ours
+    size_t different_objects = 0;
+    std::map<mirror::Class*, int /*count*/> dirty_object_class_map;
+    // Track only the byte-per-byte dirtiness (in bytes)
+    std::map<mirror::Class*, int /*byte_count*/> dirty_object_byte_count;
+    // Track the object-by-object dirtiness (in bytes)
+    std::map<mirror::Class*, int /*byte_count*/> dirty_object_size_in_bytes;
+    std::map<mirror::Class*, int /*count*/> clean_object_class_map;
+
+    std::map<mirror::Class*, std::string> class_to_descriptor_map;
+
+    std::map<off_t /* field offset */, int /* count */> art_method_field_dirty_count;
+    std::vector<mirror::ArtMethod*> art_method_dirty_objects;
+
+    std::map<off_t /* field offset */, int /* count */> class_field_dirty_count;
+    std::vector<mirror::Class*> class_dirty_objects;
+
+    // List of local objects that are clean, but located on dirty pages.
+    std::vector<mirror::Object*> false_dirty_objects;
+    std::map<mirror::Class*, int /*byte_count*/> false_dirty_byte_count;
+    std::map<mirror::Class*, int /*object_count*/> false_dirty_object_count;
+    std::map<mirror::Class*, std::vector<mirror::Object*>> false_dirty_objects_map;
+    size_t false_dirty_object_bytes = 0;
+
+    // Remote pointers to dirty objects
+    std::map<mirror::Class*, std::vector<mirror::Object*>> dirty_objects_by_class;
+    // Look up remote classes by their descriptor
+    std::map<std::string, mirror::Class*> remote_class_map;
+    // Look up local classes by their descriptor
+    std::map<std::string, mirror::Class*> local_class_map;
+
+    size_t dirty_object_bytes = 0;
+    {
+      const uint8_t* begin_image_ptr = image_begin_unaligned;
+      const uint8_t* end_image_ptr = image_end_unaligned;
+
+      const uint8_t* current = begin_image_ptr + RoundUp(sizeof(ImageHeader), kObjectAlignment);
+      while (reinterpret_cast<const uintptr_t>(current)
+             < reinterpret_cast<const uintptr_t>(end_image_ptr)) {
+        CHECK_ALIGNED(current, kObjectAlignment);
+        mirror::Object* obj = reinterpret_cast<mirror::Object*>(const_cast<uint8_t*>(current));
+
+        // Sanity check that we are reading a real object
+        CHECK(obj->GetClass() != nullptr) << "Image object at address " << obj << " has null class";
+        if (kUseBakerOrBrooksReadBarrier) {
+          obj->AssertReadBarrierPointer();
+        }
+
+        // Iterate every page this object belongs to
+        bool on_dirty_page = false;
+        size_t page_off = 0;
+        size_t current_page_idx;
+        uintptr_t object_address;
+        do {
+          object_address = reinterpret_cast<uintptr_t>(current);
+          current_page_idx = object_address / kPageSize + page_off;
+
+          if (dirty_page_set_local.find(current_page_idx) != dirty_page_set_local.end()) {
+            // This object is on a dirty page
+            on_dirty_page = true;
+          }
+
+          page_off++;
+        } while ((current_page_idx * kPageSize) <
+                 RoundUp(object_address + obj->SizeOf(), kObjectAlignment));
+
+        mirror::Class* klass = obj->GetClass();
+
+        bool different_object = false;
+
+        // Check against the other object and see if they are different
+        ptrdiff_t offset = current - begin_image_ptr;
+        const uint8_t* current_remote = &remote_contents[offset];
+        mirror::Object* remote_obj = reinterpret_cast<mirror::Object*>(
+            const_cast<uint8_t*>(current_remote));
+        if (memcmp(current, current_remote, obj->SizeOf()) != 0) {
+          different_objects++;
+          dirty_object_bytes += obj->SizeOf();
+
+          ++dirty_object_class_map[klass];
+
+          // Go byte-by-byte and figure out what exactly got dirtied
+          size_t dirty_byte_count_per_object = 0;
+          for (size_t i = 0; i < obj->SizeOf(); ++i) {
+            if (current[i] != current_remote[i]) {
+              dirty_byte_count_per_object++;
+            }
+          }
+          dirty_object_byte_count[klass] += dirty_byte_count_per_object;
+          dirty_object_size_in_bytes[klass] += obj->SizeOf();
+
+          different_object = true;
+
+          dirty_objects_by_class[klass].push_back(remote_obj);
+        } else {
+          ++clean_object_class_map[klass];
+        }
+
+        std::string descriptor = GetClassDescriptor(klass);
+        if (different_object) {
+          if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
+            // this is a "Class"
+            mirror::Class* obj_as_class  = reinterpret_cast<mirror::Class*>(remote_obj);
+
+            // print the fields that are dirty
+            for (size_t i = 0; i < obj->SizeOf(); ++i) {
+              if (current[i] != current_remote[i]) {
+                class_field_dirty_count[i]++;
+              }
+            }
+
+            class_dirty_objects.push_back(obj_as_class);
+          } else if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
+            // this is an ArtMethod
+            mirror::ArtMethod* art_method = reinterpret_cast<mirror::ArtMethod*>(remote_obj);
+
+            // print the fields that are dirty
+            for (size_t i = 0; i < obj->SizeOf(); ++i) {
+              if (current[i] != current_remote[i]) {
+                art_method_field_dirty_count[i]++;
+              }
+            }
+
+            art_method_dirty_objects.push_back(art_method);
+          }
+        } else if (on_dirty_page) {
+          // This object was either never mutated or got mutated back to the same value.
+          // TODO: Do I want to distinguish a "different" vs a "dirty" page here?
+          false_dirty_objects.push_back(obj);
+          false_dirty_objects_map[klass].push_back(obj);
+          false_dirty_object_bytes += obj->SizeOf();
+          false_dirty_byte_count[obj->GetClass()] += obj->SizeOf();
+          false_dirty_object_count[obj->GetClass()] += 1;
+        }
+
+        if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
+          local_class_map[descriptor] = reinterpret_cast<mirror::Class*>(obj);
+          remote_class_map[descriptor] = reinterpret_cast<mirror::Class*>(remote_obj);
+        }
+
+        // Unconditionally store the class descriptor in case we need it later
+        class_to_descriptor_map[klass] = descriptor;
+        current += RoundUp(obj->SizeOf(), kObjectAlignment);
+      }
+    }
+
+    // Looking at only dirty pages, figure out how many of those bytes belong to dirty objects.
+    float true_dirtied_percent = dirty_object_bytes * 1.0f / (dirty_pages * kPageSize);
+    size_t false_dirty_pages = dirty_pages - different_pages;
+
+    os << "Mapping at [" << reinterpret_cast<void*>(boot_map.start) << ", "
+       << reinterpret_cast<void*>(boot_map.end) << ") had: \n  "
+       << different_bytes << " differing bytes, \n  "
+       << different_int32s << " differing int32s, \n  "
+       << different_objects << " different objects, \n  "
+       << dirty_object_bytes << " different object [bytes], \n  "
+       << false_dirty_objects.size() << " false dirty objects,\n  "
+       << false_dirty_object_bytes << " false dirty object [bytes], \n  "
+       << true_dirtied_percent << " different objects-vs-total in a dirty page;\n  "
+       << different_pages << " different pages; \n  "
+       << dirty_pages << " pages are dirty; \n  "
+       << false_dirty_pages << " pages are false dirty; \n  "
+       << private_pages << " pages are private; \n  "
+       << private_dirty_pages << " pages are Private_Dirty\n  "
+       << "";
+
+    // vector of pairs (int count, Class*)
+    auto dirty_object_class_values = SortByValueDesc(dirty_object_class_map);
+    auto clean_object_class_values = SortByValueDesc(clean_object_class_map);
+
+    os << "\n" << "  Dirty object count by class:\n";
+    for (const auto& vk_pair : dirty_object_class_values) {
+      int dirty_object_count = vk_pair.first;
+      mirror::Class* klass = vk_pair.second;
+      int object_sizes = dirty_object_size_in_bytes[klass];
+      float avg_dirty_bytes_per_class = dirty_object_byte_count[klass] * 1.0f / object_sizes;
+      float avg_object_size = object_sizes * 1.0f / dirty_object_count;
+      const std::string& descriptor = class_to_descriptor_map[klass];
+      os << "    " << PrettyClass(klass) << " ("
+         << "objects: " << dirty_object_count << ", "
+         << "avg dirty bytes: " << avg_dirty_bytes_per_class << ", "
+         << "avg object size: " << avg_object_size << ", "
+         << "class descriptor: '" << descriptor << "'"
+         << ")\n";
+
+      constexpr size_t kMaxAddressPrint = 5;
+      if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
+        os << "      sample object addresses: ";
+        for (size_t i = 0; i < art_method_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
+          auto art_method = art_method_dirty_objects[i];
+
+          os << reinterpret_cast<void*>(art_method) << ", ";
+        }
+        os << "\n";
+
+        os << "      dirty byte +offset:count list = ";
+        auto art_method_field_dirty_count_sorted = SortByValueDesc(art_method_field_dirty_count);
+        for (auto pair : art_method_field_dirty_count_sorted) {
+          off_t offset = pair.second;
+          int count = pair.first;
+
+          os << "+" << offset << ":" << count << ", ";
+        }
+
+        os << "\n";
+
+        os << "      field contents:\n";
+        const auto& dirty_objects_list = dirty_objects_by_class[klass];
+        for (mirror::Object* obj : dirty_objects_list) {
+          // remote method
+          auto art_method = reinterpret_cast<mirror::ArtMethod*>(obj);
+
+          // remote class
+          mirror::Class* remote_declaring_class =
+            FixUpRemotePointer(art_method->GetDeclaringClass(), remote_contents, boot_map);
+
+          // local class
+          mirror::Class* declaring_class =
+            RemoteContentsPointerToLocal(remote_declaring_class,
+                                         remote_contents,
+                                         boot_image_header);
+
+          os << "        " << reinterpret_cast<void*>(obj) << " ";
+          os << "  entryPointFromJni: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
+          os << "  entryPointFromInterpreter: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromInterpreterPtrSize<kVerifyNone>(pointer_size))
+             << ", ";
+          os << "  entryPointFromQuickCompiledCode: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
+             << ", ";
+          os << "  isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
+          os << "  class_status (local): " << declaring_class->GetStatus();
+          os << "  class_status (remote): " << remote_declaring_class->GetStatus();
+          os << "\n";
+        }
+      }
+      if (strcmp(descriptor.c_str(), "Ljava/lang/Class;") == 0) {
+        os << "       sample object addresses: ";
+        for (size_t i = 0; i < class_dirty_objects.size() && i < kMaxAddressPrint; ++i) {
+          auto class_ptr = class_dirty_objects[i];
+
+          os << reinterpret_cast<void*>(class_ptr) << ", ";
+        }
+        os << "\n";
+
+        os << "       dirty byte +offset:count list = ";
+        auto class_field_dirty_count_sorted = SortByValueDesc(class_field_dirty_count);
+        for (auto pair : class_field_dirty_count_sorted) {
+          off_t offset = pair.second;
+          int count = pair.first;
+
+          os << "+" << offset << ":" << count << ", ";
+        }
+        os << "\n";
+
+        os << "      field contents:\n";
+        const auto& dirty_objects_list = dirty_objects_by_class[klass];
+        for (mirror::Object* obj : dirty_objects_list) {
+          // remote class object
+          auto remote_klass = reinterpret_cast<mirror::Class*>(obj);
+
+          // local class object
+          auto local_klass = RemoteContentsPointerToLocal(remote_klass,
+                                                          remote_contents,
+                                                          boot_image_header);
+
+          os << "        " << reinterpret_cast<void*>(obj) << " ";
+          os << "  class_status (remote): " << remote_klass->GetStatus() << ", ";
+          os << "  class_status (local): " << local_klass->GetStatus();
+          os << "\n";
+        }
+      }
+    }
+
+    auto false_dirty_object_class_values = SortByValueDesc(false_dirty_object_count);
+
+    os << "\n" << "  False-dirty object count by class:\n";
+    for (const auto& vk_pair : false_dirty_object_class_values) {
+      int object_count = vk_pair.first;
+      mirror::Class* klass = vk_pair.second;
+      int object_sizes = false_dirty_byte_count[klass];
+      float avg_object_size = object_sizes * 1.0f / object_count;
+      const std::string& descriptor = class_to_descriptor_map[klass];
+      os << "    " << PrettyClass(klass) << " ("
+         << "objects: " << object_count << ", "
+         << "avg object size: " << avg_object_size << ", "
+         << "total bytes: " << object_sizes << ", "
+         << "class descriptor: '" << descriptor << "'"
+         << ")\n";
+
+      if (strcmp(descriptor.c_str(), "Ljava/lang/reflect/ArtMethod;") == 0) {
+        auto& art_method_false_dirty_objects = false_dirty_objects_map[klass];
+
+        os << "      field contents:\n";
+        for (mirror::Object* obj : art_method_false_dirty_objects) {
+          // local method
+          auto art_method = reinterpret_cast<mirror::ArtMethod*>(obj);
+
+          // local class
+          mirror::Class* declaring_class = art_method->GetDeclaringClass();
+
+          os << "        " << reinterpret_cast<void*>(obj) << " ";
+          os << "  entryPointFromJni: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromJniPtrSize(pointer_size)) << ", ";
+          os << "  entryPointFromInterpreter: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromInterpreterPtrSize<kVerifyNone>(pointer_size))
+             << ", ";
+          os << "  entryPointFromQuickCompiledCode: "
+             << reinterpret_cast<const void*>(
+                    art_method->GetEntryPointFromQuickCompiledCodePtrSize(pointer_size))
+             << ", ";
+          os << "  isNative? " << (art_method->IsNative() ? "yes" : "no") << ", ";
+          os << "  class_status (local): " << declaring_class->GetStatus();
+          os << "\n";
+        }
+      }
+    }
+
+    os << "\n" << "  Clean object count by class:\n";
+    for (const auto& vk_pair : clean_object_class_values) {
+      os << "    " << PrettyClass(vk_pair.second) << " (" << vk_pair.first << ")\n";
+    }
+
+    return true;
+  }
+
+  // Fixup a remote pointer that we read from a foreign boot.art to point to our own memory.
+  // Returned pointer will point to inside of remote_contents.
+  template <typename T>
+  static T* FixUpRemotePointer(T* remote_ptr,
+                               std::vector<uint8_t>& remote_contents,
+                               const backtrace_map_t& boot_map) {
+    if (remote_ptr == nullptr) {
+      return nullptr;
+    }
+
+    uintptr_t remote = reinterpret_cast<uintptr_t>(remote_ptr);
+
+    CHECK_LE(boot_map.start, remote);
+    CHECK_GT(boot_map.end, remote);
+
+    off_t boot_offset = remote - boot_map.start;
+
+    return reinterpret_cast<T*>(&remote_contents[boot_offset]);
+  }
+
+  template <typename T>
+  static T* RemoteContentsPointerToLocal(T* remote_ptr,
+                                         std::vector<uint8_t>& remote_contents,
+                                         const ImageHeader& image_header) {
+    if (remote_ptr == nullptr) {
+      return nullptr;
+    }
+
+    uint8_t* remote = reinterpret_cast<uint8_t*>(remote_ptr);
+    ptrdiff_t boot_offset = remote - &remote_contents[0];
+
+    const uint8_t* local_ptr = reinterpret_cast<const uint8_t*>(&image_header) + boot_offset;
+
+    return reinterpret_cast<T*>(const_cast<uint8_t*>(local_ptr));
+  }
+
+  static std::string GetClassDescriptor(mirror::Class* klass)
+    SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
+    CHECK(klass != nullptr);
+
+    std::string descriptor;
+    const char* descriptor_str = klass->GetDescriptor(&descriptor);
+
+    return std::string(descriptor_str);
+  }
+
+  template <typename K, typename V>
+  static std::vector<std::pair<V, K>> SortByValueDesc(const std::map<K, V> map) {
+    // Store value->key so that we can use the default sort from pair which
+    // sorts by value first and then key
+    std::vector<std::pair<V, K>> value_key_vector;
+
+    for (const auto& kv_pair : map) {
+      value_key_vector.push_back(std::make_pair(kv_pair.second, kv_pair.first));
+    }
+
+    // Sort in reverse (descending order)
+    std::sort(value_key_vector.rbegin(), value_key_vector.rend());
+    return value_key_vector;
+  }
+
+  static bool GetPageFrameNumber(File* page_map_file,
+                                size_t virtual_page_index,
+                                uint64_t* page_frame_number,
+                                std::string* error_msg) {
+    CHECK(page_map_file != nullptr);
+    CHECK(page_frame_number != nullptr);
+    CHECK(error_msg != nullptr);
+
+    constexpr size_t kPageMapEntrySize = sizeof(uint64_t);
+    constexpr uint64_t kPageFrameNumberMask = (1ULL << 55) - 1;  // bits 0-54 [in /proc/$pid/pagemap]
+    constexpr uint64_t kPageSoftDirtyMask = (1ULL << 55);  // bit 55 [in /proc/$pid/pagemap]
+
+    uint64_t page_map_entry = 0;
+
+    // Read 64-bit entry from /proc/$pid/pagemap to get the physical page frame number
+    if (!page_map_file->PreadFully(&page_map_entry, kPageMapEntrySize,
+                                  virtual_page_index * kPageMapEntrySize)) {
+      *error_msg = StringPrintf("Failed to read the virtual page index entry from %s",
+                                page_map_file->GetPath().c_str());
+      return false;
+    }
+
+    // TODO: seems useless, remove this.
+    bool soft_dirty = (page_map_entry & kPageSoftDirtyMask) != 0;
+    if ((false)) {
+      LOG(VERBOSE) << soft_dirty;  // Suppress unused warning
+      UNREACHABLE();
+    }
+
+    *page_frame_number = page_map_entry & kPageFrameNumberMask;
+
+    return true;
+  }
+
+  static int IsPageDirty(File* page_map_file,
+                         File* clean_page_map_file,
+                         File* kpage_flags_file,
+                         File* kpage_count_file,
+                         size_t virtual_page_idx,
+                         size_t clean_virtual_page_idx,
+                         // Out parameters:
+                         uint64_t* page_count, std::string* error_msg) {
+    CHECK(page_map_file != nullptr);
+    CHECK(clean_page_map_file != nullptr);
+    CHECK_NE(page_map_file, clean_page_map_file);
+    CHECK(kpage_flags_file != nullptr);
+    CHECK(kpage_count_file != nullptr);
+    CHECK(page_count != nullptr);
+    CHECK(error_msg != nullptr);
+
+    // Constants are from https://www.kernel.org/doc/Documentation/vm/pagemap.txt
+
+    constexpr size_t kPageFlagsEntrySize = sizeof(uint64_t);
+    constexpr size_t kPageCountEntrySize = sizeof(uint64_t);
+    constexpr uint64_t kPageFlagsDirtyMask = (1ULL << 4);  // in /proc/kpageflags
+    constexpr uint64_t kPageFlagsNoPageMask = (1ULL << 20);  // in /proc/kpageflags
+    constexpr uint64_t kPageFlagsMmapMask = (1ULL << 11);  // in /proc/kpageflags
+
+    uint64_t page_frame_number = 0;
+    if (!GetPageFrameNumber(page_map_file, virtual_page_idx, &page_frame_number, error_msg)) {
+      return -1;
+    }
+
+    uint64_t page_frame_number_clean = 0;
+    if (!GetPageFrameNumber(clean_page_map_file, clean_virtual_page_idx, &page_frame_number_clean,
+                            error_msg)) {
+      return -1;
+    }
+
+    // Read 64-bit entry from /proc/kpageflags to get the dirty bit for a page
+    uint64_t kpage_flags_entry = 0;
+    if (!kpage_flags_file->PreadFully(&kpage_flags_entry,
+                                     kPageFlagsEntrySize,
+                                     page_frame_number * kPageFlagsEntrySize)) {
+      *error_msg = StringPrintf("Failed to read the page flags from %s",
+                                kpage_flags_file->GetPath().c_str());
+      return -1;
+    }
+
+    // Read 64-bit entyry from /proc/kpagecount to get mapping counts for a page
+    if (!kpage_count_file->PreadFully(page_count /*out*/,
+                                     kPageCountEntrySize,
+                                     page_frame_number * kPageCountEntrySize)) {
+      *error_msg = StringPrintf("Failed to read the page count from %s",
+                                kpage_count_file->GetPath().c_str());
+      return -1;
+    }
+
+    // There must be a page frame at the requested address.
+    CHECK_EQ(kpage_flags_entry & kPageFlagsNoPageMask, 0u);
+    // The page frame must be memory mapped
+    CHECK_NE(kpage_flags_entry & kPageFlagsMmapMask, 0u);
+
+    // Page is dirty, i.e. has diverged from file, if the 4th bit is set to 1
+    bool flags_dirty = (kpage_flags_entry & kPageFlagsDirtyMask) != 0;
+
+    // page_frame_number_clean must come from the *same* process
+    // but a *different* mmap than page_frame_number
+    if (flags_dirty) {
+      CHECK_NE(page_frame_number, page_frame_number_clean);
+    }
+
+    return page_frame_number != page_frame_number_clean;
+  }
+
+  static const ImageHeader& GetBootImageHeader() {
+    gc::Heap* heap = Runtime::Current()->GetHeap();
+    gc::space::ImageSpace* image_space = heap->GetImageSpace();
+    CHECK(image_space != nullptr);
+    const ImageHeader& image_header = image_space->GetImageHeader();
+    return image_header;
+  }
+
+ private:
+  // Return the image location, stripped of any directories, e.g. "boot.art" or "core.art"
+  std::string GetImageLocationBaseName() const {
+    return BaseName(std::string(image_location_));
+  }
+
+  std::ostream* os_;
+  const ImageHeader& image_header_;
+  const char* image_location_;
+  pid_t image_diff_pid_;  // Dump image diff against boot.art if pid is non-negative
+
+  DISALLOW_COPY_AND_ASSIGN(ImgDiagDumper);
+};
+
+static int DumpImage(Runtime* runtime, const char* image_location,
+                     std::ostream* os, pid_t image_diff_pid) {
+  ScopedObjectAccess soa(Thread::Current());
+  gc::Heap* heap = runtime->GetHeap();
+  gc::space::ImageSpace* image_space = heap->GetImageSpace();
+  CHECK(image_space != nullptr);
+  const ImageHeader& image_header = image_space->GetImageHeader();
+  if (!image_header.IsValid()) {
+    fprintf(stderr, "Invalid image header %s\n", image_location);
+    return EXIT_FAILURE;
+  }
+
+  ImgDiagDumper img_diag_dumper(os, image_header, image_location, image_diff_pid);
+
+  bool success = img_diag_dumper.Dump();
+  return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
+
+struct ImgDiagArgs : public CmdlineArgs {
+ protected:
+  using Base = CmdlineArgs;
+
+  virtual ParseStatus ParseCustom(const StringPiece& option,
+                                  std::string* error_msg) OVERRIDE {
+    {
+      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
+      if (base_parse != kParseUnknownArgument) {
+        return base_parse;
+      }
+    }
+
+    if (option.starts_with("--image-diff-pid=")) {
+      const char* image_diff_pid = option.substr(strlen("--image-diff-pid=")).data();
+
+      if (!ParseInt(image_diff_pid, &image_diff_pid_)) {
+        *error_msg = "Image diff pid out of range";
+        return kParseError;
+      }
+    } else {
+      return kParseUnknownArgument;
+    }
+
+    return kParseOk;
+  }
+
+  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
+    // Perform the parent checks.
+    ParseStatus parent_checks = Base::ParseChecks(error_msg);
+    if (parent_checks != kParseOk) {
+      return parent_checks;
+    }
+
+    // Perform our own checks.
+
+    if (kill(image_diff_pid_,
+             /*sig*/0) != 0) {  // No signal is sent, perform error-checking only.
+      // Check if the pid exists before proceeding.
+      if (errno == ESRCH) {
+        *error_msg = "Process specified does not exist";
+      } else {
+        *error_msg = StringPrintf("Failed to check process status: %s", strerror(errno));
+      }
+      return kParseError;
+    } else if (instruction_set_ != kRuntimeISA) {
+      // Don't allow different ISAs since the images are ISA-specific.
+      // Right now the code assumes both the runtime ISA and the remote ISA are identical.
+      *error_msg = "Must use the default runtime ISA; changing ISA is not supported.";
+      return kParseError;
+    }
+
+    return kParseOk;
+  }
+
+  virtual std::string GetUsage() const {
+    std::string usage;
+
+    usage +=
+        "Usage: imgdiag [options] ...\n"
+        "    Example: imgdiag --image-diff-pid=$(pidof dex2oat)\n"
+        "    Example: adb shell imgdiag --image-diff-pid=$(pid zygote)\n"
+        "\n";
+
+    usage += Base::GetUsage();
+
+    usage +=  // Optional.
+        "  --image-diff-pid=<pid>: provide the PID of a process whose boot.art you want to diff.\n"
+        "      Example: --image-diff-pid=$(pid zygote)\n"
+        "\n";
+
+    return usage;
+  }
+
+ public:
+  pid_t image_diff_pid_ = -1;
+};
+
+struct ImgDiagMain : public CmdlineMain<ImgDiagArgs> {
+  virtual bool ExecuteWithRuntime(Runtime* runtime) {
+    CHECK(args_ != nullptr);
+
+    return DumpImage(runtime,
+                     args_->boot_image_location_,
+                     args_->os_,
+                     args_->image_diff_pid_) == EXIT_SUCCESS;
+  }
+};
+
+}  // namespace art
+
+int main(int argc, char** argv) {
+  art::ImgDiagMain main;
+  return main.Main(argc, argv);
+}
diff --git a/imgdiag/imgdiag_test.cc b/imgdiag/imgdiag_test.cc
new file mode 100644
index 0000000..1ac7930
--- /dev/null
+++ b/imgdiag/imgdiag_test.cc
@@ -0,0 +1,138 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <string>
+#include <vector>
+#include <sstream>
+
+#include "common_runtime_test.h"
+
+#include "runtime/os.h"
+#include "runtime/arch/instruction_set.h"
+#include "runtime/utils.h"
+#include "runtime/gc/space/image_space.h"
+#include "runtime/gc/heap.h"
+#include "base/stringprintf.h"
+
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace art {
+
+static const char* kImgDiagDiffPid = "--image-diff-pid";
+static const char* kImgDiagBootImage = "--boot-image";
+static const char* kImgDiagBinaryName = "imgdiag";
+
+class ImgDiagTest : public CommonRuntimeTest {
+ protected:
+  virtual void SetUp() {
+    CommonRuntimeTest::SetUp();
+
+    // We loaded the runtime with an explicit image. Therefore the image space must exist.
+    gc::space::ImageSpace* image_space = Runtime::Current()->GetHeap()->GetImageSpace();
+    ASSERT_TRUE(image_space != nullptr);
+    boot_image_location_ = image_space->GetImageLocation();
+  }
+
+  virtual void SetUpRuntimeOptions(RuntimeOptions* options) OVERRIDE {
+    // Needs to live until CommonRuntimeTest::SetUp finishes, since we pass it a cstring.
+    runtime_args_image_ = StringPrintf("-Ximage:%s", GetCoreArtLocation().c_str());
+    options->push_back(std::make_pair(runtime_args_image_, nullptr));
+  }
+
+  // Path to the imgdiag(d?)[32|64] binary.
+  std::string GetImgDiagFilePath() {
+    std::string root = GetTestAndroidRoot();
+
+    root += "/bin/";
+    root += kImgDiagBinaryName;
+
+    if (kIsDebugBuild) {
+      root += "d";
+    }
+
+    std::string root32 = root + "32";
+    // If we have both a 32-bit and a 64-bit build, the 32-bit file will have a 32 suffix.
+    if (OS::FileExists(root32.c_str()) && !Is64BitInstructionSet(kRuntimeISA)) {
+      return root32;
+    // Only a single build exists, so the filename never has an extra suffix.
+    } else {
+      return root;
+    }
+  }
+
+  // Run imgdiag with a custom boot image location.
+  bool Exec(pid_t image_diff_pid, const std::string& boot_image, std::string* error_msg) {
+    // Invoke 'img_diag' against the current process.
+    // This should succeed because we have a runtime and so it should
+    // be able to map in the boot.art and do a diff for it.
+    std::string file_path = GetImgDiagFilePath();
+    EXPECT_TRUE(OS::FileExists(file_path.c_str())) << file_path << " should be a valid file path";
+
+    // Run imgdiag --image-diff-pid=$image_diff_pid and wait until it's done with a 0 exit code.
+    std::string diff_pid_args;
+    {
+      std::stringstream diff_pid_args_ss;
+      diff_pid_args_ss << kImgDiagDiffPid << "=" << image_diff_pid;
+      diff_pid_args = diff_pid_args_ss.str();
+    }
+    std::string boot_image_args;
+    {
+      boot_image_args = boot_image_args + kImgDiagBootImage + "=" + boot_image;
+    }
+
+    std::vector<std::string> exec_argv = { file_path, diff_pid_args, boot_image_args };
+
+    return ::art::Exec(exec_argv, error_msg);
+  }
+
+  // Run imgdiag with the default boot image location.
+  bool ExecDefaultBootImage(pid_t image_diff_pid, std::string* error_msg) {
+    return Exec(image_diff_pid, boot_image_location_, error_msg);
+  }
+
+ private:
+  std::string runtime_args_image_;
+  std::string boot_image_location_;
+};
+
+#if defined (ART_TARGET)
+TEST_F(ImgDiagTest, ImageDiffPidSelf) {
+#else
+// Can't run this test on the host, it will fail when trying to open /proc/kpagestats
+// because it's root read-only.
+TEST_F(ImgDiagTest, DISABLED_ImageDiffPidSelf) {
+#endif
+  // Invoke 'img_diag' against the current process.
+  // This should succeed because we have a runtime and so it should
+  // be able to map in the boot.art and do a diff for it.
+
+  // Run imgdiag --image-diff-pid=$(self pid) and wait until it's done with a 0 exit code.
+  std::string error_msg;
+  ASSERT_TRUE(ExecDefaultBootImage(getpid(), &error_msg)) << "Failed to execute -- because: "
+                                                          << error_msg;
+}
+
+TEST_F(ImgDiagTest, ImageDiffBadPid) {
+  // Invoke 'img_diag' against a non-existing process. This should fail.
+
+  // Run imgdiag --image-diff-pid=some_bad_pid and wait until it's done with a 0 exit code.
+  std::string error_msg;
+  ASSERT_FALSE(ExecDefaultBootImage(-12345, &error_msg)) << "Incorrectly executed";
+  UNUSED(error_msg);
+}
+
+}  // namespace art
diff --git a/oatdump/Android.mk b/oatdump/Android.mk
index b8a3b49..f01afc5 100644
--- a/oatdump/Android.mk
+++ b/oatdump/Android.mk
@@ -21,19 +21,8 @@
 OATDUMP_SRC_FILES := \
 	oatdump.cc
 
-ifeq ($(ART_BUILD_TARGET_NDEBUG),true)
-  $(eval $(call build-art-executable,oatdump,$(OATDUMP_SRC_FILES),libcutils libart-disassembler libart-compiler,art/disassembler art/compiler,target,ndebug))
-endif
-ifeq ($(ART_BUILD_TARGET_DEBUG),true)
-  $(eval $(call build-art-executable,oatdump,$(OATDUMP_SRC_FILES),libcutils libartd-disassembler libartd-compiler,art/disassembler art/compiler,target,debug))
-endif
-
-ifeq ($(ART_BUILD_HOST_NDEBUG),true)
-  $(eval $(call build-art-executable,oatdump,$(OATDUMP_SRC_FILES),libart-disassembler libart-compiler,art/disassembler art/compiler,host,ndebug))
-endif
-ifeq ($(ART_BUILD_HOST_DEBUG),true)
-  $(eval $(call build-art-executable,oatdump,$(OATDUMP_SRC_FILES),libartd-disassembler libartd-compiler,art/disassembler art/compiler,host,debug))
-endif
+# Build variants {target,host} x {debug,ndebug}
+$(eval $(call build-art-multi-executable,oatdump,$(OATDUMP_SRC_FILES),libart-compiler libart-disassembler,libcutils,,art/compiler art/disassembler))
 
 ########################################################################
 # oatdump targets
diff --git a/oatdump/oatdump.cc b/oatdump/oatdump.cc
index 5c5e2b4..b989c7f 100644
--- a/oatdump/oatdump.cc
+++ b/oatdump/oatdump.cc
@@ -19,12 +19,13 @@
 
 #include <fstream>
 #include <iostream>
+#include <map>
+#include <set>
 #include <string>
 #include <unordered_map>
 #include <vector>
 
 #include "arch/instruction_set_features.h"
-#include "base/stringpiece.h"
 #include "base/unix_file/fd_file.h"
 #include "class_linker.h"
 #include "class_linker-inl.h"
@@ -45,12 +46,10 @@
 #include "mirror/class-inl.h"
 #include "mirror/object-inl.h"
 #include "mirror/object_array-inl.h"
-#include "noop_compiler_callbacks.h"
 #include "oat.h"
 #include "oat_file-inl.h"
 #include "os.h"
 #include "output_stream.h"
-#include "runtime.h"
 #include "safe_map.h"
 #include "scoped_thread_state_change.h"
 #include "ScopedLocalRef.h"
@@ -60,58 +59,10 @@
 #include "vmap_table.h"
 #include "well_known_classes.h"
 
-namespace art {
+#include <sys/stat.h>
+#include "cmdline.h"
 
-static void usage() {
-  fprintf(stderr,
-          "Usage: oatdump [options] ...\n"
-          "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
-          "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
-          "\n");
-  fprintf(stderr,
-          "  --oat-file=<file.oat>: specifies an input oat filename.\n"
-          "      Example: --oat-file=/system/framework/boot.oat\n"
-          "\n");
-  fprintf(stderr,
-          "  --image=<file.art>: specifies an input image filename.\n"
-          "      Example: --image=/system/framework/boot.art\n"
-          "\n");
-  fprintf(stderr,
-          "  --boot-image=<file.art>: provide the image file for the boot class path.\n"
-          "      Example: --boot-image=/system/framework/boot.art\n"
-          "\n");
-  fprintf(stderr,
-          "  --instruction-set=(arm|arm64|mips|x86|x86_64): for locating the image\n"
-          "      file based on the image location set.\n"
-          "      Example: --instruction-set=x86\n"
-          "      Default: %s\n"
-          "\n",
-          GetInstructionSetString(kRuntimeISA));
-  fprintf(stderr,
-          "  --output=<file> may be used to send the output to a file.\n"
-          "      Example: --output=/tmp/oatdump.txt\n"
-          "\n");
-  fprintf(stderr,
-          "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
-          "      Example: --dump:raw_mapping_table\n"
-          "\n");
-  fprintf(stderr,
-          "  --dump:raw_mapping_table enables dumping of the GC map.\n"
-          "      Example: --dump:raw_gc_map\n"
-          "\n");
-  fprintf(stderr,
-          "  --no-dump:vmap may be used to disable vmap dumping.\n"
-          "      Example: --no-dump:vmap\n"
-          "\n");
-  fprintf(stderr,
-          "  --no-disassemble may be used to disable disassembly.\n"
-          "      Example: --no-disassemble\n"
-          "\n");
-  fprintf(stderr,
-          "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
-          "      Example: --method-filter=foo\n"
-          "\n");
-}
+namespace art {
 
 const char* image_roots_descriptions_[] = {
   "kResolutionMethod",
@@ -360,15 +311,14 @@
                    bool dump_vmap,
                    bool disassemble_code,
                    bool absolute_addresses,
-                   const char* method_filter,
-                   Handle<mirror::ClassLoader>* class_loader)
+                   const char* method_filter)
     : dump_raw_mapping_table_(dump_raw_mapping_table),
       dump_raw_gc_map_(dump_raw_gc_map),
       dump_vmap_(dump_vmap),
       disassemble_code_(disassemble_code),
       absolute_addresses_(absolute_addresses),
       method_filter_(method_filter),
-      class_loader_(class_loader) {}
+      class_loader_(nullptr) {}
 
   const bool dump_raw_mapping_table_;
   const bool dump_raw_gc_map_;
@@ -1983,45 +1933,6 @@
   DISALLOW_COPY_AND_ASSIGN(ImageDumper);
 };
 
-static NoopCompilerCallbacks callbacks;
-
-static Runtime* StartRuntime(const char* boot_image_location, const char* image_location,
-                             InstructionSet instruction_set) {
-  RuntimeOptions options;
-  std::string image_option;
-  std::string oat_option;
-  std::string boot_image_option;
-  std::string boot_oat_option;
-
-  // We are more like a compiler than a run-time. We don't want to execute code.
-  options.push_back(std::make_pair("compilercallbacks", &callbacks));
-
-  if (boot_image_location != nullptr) {
-    boot_image_option += "-Ximage:";
-    boot_image_option += boot_image_location;
-    options.push_back(std::make_pair(boot_image_option.c_str(), nullptr));
-  }
-  if (image_location != nullptr) {
-    image_option += "-Ximage:";
-    image_option += image_location;
-    options.push_back(std::make_pair(image_option.c_str(), nullptr));
-  }
-  options.push_back(
-      std::make_pair("imageinstructionset",
-                     reinterpret_cast<const void*>(GetInstructionSetString(instruction_set))));
-
-  if (!Runtime::Create(options, false)) {
-    fprintf(stderr, "Failed to create runtime\n");
-    return nullptr;
-  }
-
-  // Runtime::Create acquired the mutator_lock_ that is normally given away when we Runtime::Start,
-  // give it away now and then switch to a more manageable ScopedObjectAccess.
-  Thread::Current()->TransitionFromRunnableToSuspended(kNative);
-
-  return Runtime::Current();
-}
-
 static int DumpImage(Runtime* runtime, const char* image_location, OatDumperOptions* options,
                      std::ostream* os) {
   // Dumping the image, no explicit class loader.
@@ -2037,7 +1948,9 @@
     fprintf(stderr, "Invalid image header %s\n", image_location);
     return EXIT_FAILURE;
   }
+
   ImageDumper image_dumper(os, *image_space, image_header, options);
+
   bool success = image_dumper.Dump();
   return (success) ? EXIT_SUCCESS : EXIT_FAILURE;
 }
@@ -2096,7 +2009,8 @@
 static int DumpOat(Runtime* runtime, const char* oat_filename, OatDumperOptions* options,
                    std::ostream* os) {
   std::string error_msg;
-  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
+  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
+                                    &error_msg);
   if (oat_file == nullptr) {
     fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
     return EXIT_FAILURE;
@@ -2111,7 +2025,8 @@
 
 static int SymbolizeOat(const char* oat_filename, std::string& output_name) {
   std::string error_msg;
-  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false, &error_msg);
+  OatFile* oat_file = OatFile::Open(oat_filename, oat_filename, nullptr, nullptr, false,
+                                    &error_msg);
   if (oat_file == nullptr) {
     fprintf(stderr, "Failed to open oat file from '%s': %s\n", oat_filename, error_msg.c_str());
     return EXIT_FAILURE;
@@ -2130,86 +2045,110 @@
   return EXIT_SUCCESS;
 }
 
-struct OatdumpArgs {
-  bool Parse(int argc, char** argv) {
-    // Skip over argv[0].
-    argv++;
-    argc--;
+struct OatdumpArgs : public CmdlineArgs {
+ protected:
+  using Base = CmdlineArgs;
 
-    if (argc == 0) {
-      fprintf(stderr, "No arguments specified\n");
-      usage();
-      return false;
-    }
-
-    for (int i = 0; i < argc; i++) {
-      const StringPiece option(argv[i]);
-      if (option.starts_with("--oat-file=")) {
-        oat_filename_ = option.substr(strlen("--oat-file=")).data();
-      } else if (option.starts_with("--image=")) {
-        image_location_ = option.substr(strlen("--image=")).data();
-      } else if (option.starts_with("--boot-image=")) {
-        boot_image_location_ = option.substr(strlen("--boot-image=")).data();
-      } else if (option.starts_with("--instruction-set=")) {
-        StringPiece instruction_set_str = option.substr(strlen("--instruction-set=")).data();
-        instruction_set_ = GetInstructionSetFromString(instruction_set_str.data());
-        if (instruction_set_ == kNone) {
-          fprintf(stderr, "Unsupported instruction set %s\n", instruction_set_str.data());
-          usage();
-          return false;
-        }
-      } else if (option =="--dump:raw_mapping_table") {
-        dump_raw_mapping_table_ = true;
-      } else if (option == "--dump:raw_gc_map") {
-        dump_raw_gc_map_ = true;
-      } else if (option == "--no-dump:vmap") {
-        dump_vmap_ = false;
-      } else if (option == "--no-disassemble") {
-        disassemble_code_ = false;
-      } else if (option.starts_with("--output=")) {
-        output_name_ = option.substr(strlen("--output=")).ToString();
-        const char* filename = output_name_.c_str();
-        out_.reset(new std::ofstream(filename));
-        if (!out_->good()) {
-          fprintf(stderr, "Failed to open output filename %s\n", filename);
-          usage();
-          return false;
-        }
-        os_ = out_.get();
-      } else if (option.starts_with("--symbolize=")) {
-        oat_filename_ = option.substr(strlen("--symbolize=")).data();
-        symbolize_ = true;
-      } else if (option.starts_with("--method-filter=")) {
-        method_filter_ = option.substr(strlen("--method-filter=")).data();
-      } else {
-        fprintf(stderr, "Unknown argument %s\n", option.data());
-        usage();
-        return false;
+  virtual ParseStatus ParseCustom(const StringPiece& option,
+                                  std::string* error_msg) OVERRIDE {
+    {
+      ParseStatus base_parse = Base::ParseCustom(option, error_msg);
+      if (base_parse != kParseUnknownArgument) {
+        return base_parse;
       }
     }
 
-    if (image_location_ == nullptr && oat_filename_ == nullptr) {
-      fprintf(stderr, "Either --image or --oat must be specified\n");
-      return false;
+    if (option.starts_with("--oat-file=")) {
+      oat_filename_ = option.substr(strlen("--oat-file=")).data();
+    } else if (option.starts_with("--image=")) {
+      image_location_ = option.substr(strlen("--image=")).data();
+    } else if (option =="--dump:raw_mapping_table") {
+      dump_raw_mapping_table_ = true;
+    } else if (option == "--dump:raw_gc_map") {
+      dump_raw_gc_map_ = true;
+    } else if (option == "--no-dump:vmap") {
+      dump_vmap_ = false;
+    } else if (option == "--no-disassemble") {
+      disassemble_code_ = false;
+    } else if (option.starts_with("--symbolize=")) {
+      oat_filename_ = option.substr(strlen("--symbolize=")).data();
+      symbolize_ = true;
+    } else if (option.starts_with("--method-filter=")) {
+      method_filter_ = option.substr(strlen("--method-filter=")).data();
+    } else {
+      return kParseUnknownArgument;
     }
 
-    if (image_location_ != nullptr && oat_filename_ != nullptr) {
-      fprintf(stderr, "Either --image or --oat must be specified but not both\n");
-      return false;
-    }
-
-    return true;
+    return kParseOk;
   }
 
+  virtual ParseStatus ParseChecks(std::string* error_msg) OVERRIDE {
+    // Infer boot image location from the image location if possible.
+    if (boot_image_location_ == nullptr) {
+      boot_image_location_ = image_location_;
+    }
+
+    // Perform the parent checks.
+    ParseStatus parent_checks = Base::ParseChecks(error_msg);
+    if (parent_checks != kParseOk) {
+      return parent_checks;
+    }
+
+    // Perform our own checks.
+    if (image_location_ == nullptr && oat_filename_ == nullptr) {
+      *error_msg = "Either --image or --oat-file must be specified";
+      return kParseError;
+    } else if (image_location_ != nullptr && oat_filename_ != nullptr) {
+      *error_msg = "Either --image or --oat-file must be specified but not both";
+      return kParseError;
+    }
+
+    return kParseOk;
+  }
+
+  virtual std::string GetUsage() const {
+    std::string usage;
+
+    usage +=
+        "Usage: oatdump [options] ...\n"
+        "    Example: oatdump --image=$ANDROID_PRODUCT_OUT/system/framework/boot.art\n"
+        "    Example: adb shell oatdump --image=/system/framework/boot.art\n"
+        "\n"
+        // Either oat-file or image is required.
+        "  --oat-file=<file.oat>: specifies an input oat filename.\n"
+        "      Example: --oat-file=/system/framework/boot.oat\n"
+        "\n"
+        "  --image=<file.art>: specifies an input image location.\n"
+        "      Example: --image=/system/framework/boot.art\n"
+        "\n";
+
+    usage += Base::GetUsage();
+
+    usage +=  // Optional.
+        "  --dump:raw_mapping_table enables dumping of the mapping table.\n"
+        "      Example: --dump:raw_mapping_table\n"
+        "\n"
+        "  --dump:raw_mapping_table enables dumping of the GC map.\n"
+        "      Example: --dump:raw_gc_map\n"
+        "\n"
+        "  --no-dump:vmap may be used to disable vmap dumping.\n"
+        "      Example: --no-dump:vmap\n"
+        "\n"
+        "  --no-disassemble may be used to disable disassembly.\n"
+        "      Example: --no-disassemble\n"
+        "\n"
+        "  --method-filter=<method name>: only dumps methods that contain the filter.\n"
+        "      Example: --method-filter=foo\n"
+        "\n";
+
+    return usage;
+  }
+
+ public:
   const char* oat_filename_ = nullptr;
   const char* method_filter_ = "";
   const char* image_location_ = nullptr;
-  const char* boot_image_location_ = nullptr;
-  InstructionSet instruction_set_ = kRuntimeISA;
   std::string elf_filename_prefix_;
-  std::ostream* os_ = &std::cout;
-  std::unique_ptr<std::ofstream> out_;
-  std::string output_name_;
   bool dump_raw_mapping_table_ = false;
   bool dump_raw_gc_map_ = false;
   bool dump_vmap_ = true;
@@ -2217,55 +2156,54 @@
   bool symbolize_ = false;
 };
 
-static int oatdump(int argc, char** argv) {
-  InitLogging(argv);
+struct OatdumpMain : public CmdlineMain<OatdumpArgs> {
+  virtual bool NeedsRuntime() OVERRIDE {
+    CHECK(args_ != nullptr);
 
-  OatdumpArgs args;
-  if (!args.Parse(argc, argv)) {
-    return EXIT_FAILURE;
+    // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
+    bool absolute_addresses = (args_->oat_filename_ == nullptr);
+
+    oat_dumper_options_ = std::unique_ptr<OatDumperOptions>(new OatDumperOptions(
+        args_->dump_raw_mapping_table_,
+        args_->dump_raw_gc_map_,
+        args_->dump_vmap_,
+        args_->disassemble_code_,
+        absolute_addresses,
+        args_->method_filter_));
+
+    return (args_->boot_image_location_ != nullptr || args_->image_location_ != nullptr) &&
+          !args_->symbolize_;
   }
 
-  // If we are only doing the oat file, disable absolute_addresses. Keep them for image dumping.
-  bool absolute_addresses = (args.oat_filename_ == nullptr);
+  virtual bool ExecuteWithoutRuntime() OVERRIDE {
+    CHECK(args_ != nullptr);
+    CHECK(args_->symbolize_);
 
-  std::unique_ptr<OatDumperOptions> oat_dumper_options(new OatDumperOptions(
-      args.dump_raw_mapping_table_,
-      args.dump_raw_gc_map_,
-      args.dump_vmap_,
-      args.disassemble_code_,
-      absolute_addresses,
-      args.method_filter_,
-      nullptr));
-
-  std::unique_ptr<Runtime> runtime;
-  if ((args.boot_image_location_ != nullptr || args.image_location_ != nullptr) &&
-      !args.symbolize_) {
-    // If we have a boot image option, try to start the runtime; except when just symbolizing.
-    runtime.reset(StartRuntime(args.boot_image_location_,
-                               args.image_location_,
-                               args.instruction_set_));
-  } else {
     MemMap::Init();
+
+    return SymbolizeOat(args_->oat_filename_, args_->output_name_) == EXIT_SUCCESS;
   }
 
-  if (args.oat_filename_ != nullptr) {
-    if (args.symbolize_) {
-      return SymbolizeOat(args.oat_filename_, args.output_name_);
-    } else {
-      return DumpOat(runtime.get(), args.oat_filename_, oat_dumper_options.release(), args.os_);
+  virtual bool ExecuteWithRuntime(Runtime* runtime) {
+    CHECK(args_ != nullptr);
+
+    if (args_->oat_filename_ != nullptr) {
+      return DumpOat(runtime,
+                     args_->oat_filename_,
+                     oat_dumper_options_.release(),
+                     args_->os_) == EXIT_SUCCESS;
     }
+
+    return DumpImage(runtime, args_->image_location_, oat_dumper_options_.release(), args_->os_)
+      == EXIT_SUCCESS;
   }
 
-  if (runtime.get() == nullptr) {
-    // We need the runtime when printing an image.
-    return EXIT_FAILURE;
-  }
-
-  return DumpImage(runtime.get(), args.image_location_, oat_dumper_options.release(), args.os_);
-}
+  std::unique_ptr<OatDumperOptions> oat_dumper_options_;
+};
 
 }  // namespace art
 
 int main(int argc, char** argv) {
-  return art::oatdump(argc, argv);
+  art::OatdumpMain main;
+  return main.Main(argc, argv);
 }
diff --git a/runtime/arch/mips/asm_support_mips.S b/runtime/arch/mips/asm_support_mips.S
index 0d18f1a..eea6537 100644
--- a/runtime/arch/mips/asm_support_mips.S
+++ b/runtime/arch/mips/asm_support_mips.S
@@ -66,5 +66,54 @@
     END \name
 .endm
 
+#if defined(__mips_isa_rev) && __mips_isa_rev > 2
+  /* mips32r5 & mips32r6 have mthc1 op, and have 64-bit fp regs,
+     and in FPXX abi we avoid referring to odd-numbered fp regs */
+
+/* LDu: Load 64-bit floating-point value to float reg feven,
+   from unaligned (mod-4-aligned) mem location disp(base) */
+.macro LDu feven,fodd,disp,base,temp
+  l.s   \feven, \disp(\base)
+  lw    \temp, \disp+4(\base)
+  mthc1 \temp, \feven
+.endm
+
+/* SDu: Store 64-bit floating-point value from float reg feven,
+   to unaligned (mod-4-aligned) mem location disp(base) */
+.macro SDu feven,fodd,disp,base,temp
+  mfhc1 \temp, \feven
+  s.s   \feven, \disp(\base)
+  sw    \temp, \disp+4(\base)
+.endm
+
+/* MTD: Move double, from general regpair (reven,rodd)
+        to float regpair (feven,fodd) */
+.macro MTD reven,rodd,feven,fodd
+  mtc1  \reven, \feven
+  mthc1 \rodd, \feven
+.endm
+
+#else
+  /* mips32r1 has no mthc1 op;
+     mips32r1 and mips32r2 use 32-bit floating point register mode (FR=0),
+     and always hold doubles as (feven, fodd) fp reg pair */
+
+.macro LDu feven,fodd,disp,base,temp
+  l.s   \feven, \disp(\base)
+  l.s   \fodd,  \disp+4(\base)
+.endm
+
+.macro SDu feven,fodd,disp,base,temp
+  s.s   \feven, \disp(\base)
+  s.s   \fodd,  \disp+4(\base)
+.endm
+
+.macro MTD reven,rodd,feven,fodd
+  mtc1  \reven, \feven
+  mtc1  \rodd, \fodd
+.endm
+
+#endif  /* mips_isa_rev */
+
 
 #endif  // ART_RUNTIME_ARCH_MIPS_ASM_SUPPORT_MIPS_S_
diff --git a/runtime/arch/mips/quick_entrypoints_mips.S b/runtime/arch/mips/quick_entrypoints_mips.S
index 44feee6..a0fc6d2 100644
--- a/runtime/arch/mips/quick_entrypoints_mips.S
+++ b/runtime/arch/mips/quick_entrypoints_mips.S
@@ -314,38 +314,23 @@
      * FIXME: just guessing about the shape of the jmpbuf.  Where will pc be?
      */
 ENTRY art_quick_do_long_jump
-    l.s     $f0, 0($a1)
-    l.s     $f1, 4($a1)
-    l.s     $f2, 8($a1)
-    l.s     $f3, 12($a1)
-    l.s     $f4, 16($a1)
-    l.s     $f5, 20($a1)
-    l.s     $f6, 24($a1)
-    l.s     $f7, 28($a1)
-    l.s     $f8, 32($a1)
-    l.s     $f9, 36($a1)
-    l.s     $f10, 40($a1)
-    l.s     $f11, 44($a1)
-    l.s     $f12, 48($a1)
-    l.s     $f13, 52($a1)
-    l.s     $f14, 56($a1)
-    l.s     $f15, 60($a1)
-    l.s     $f16, 64($a1)
-    l.s     $f17, 68($a1)
-    l.s     $f18, 72($a1)
-    l.s     $f19, 76($a1)
-    l.s     $f20, 80($a1)
-    l.s     $f21, 84($a1)
-    l.s     $f22, 88($a1)
-    l.s     $f23, 92($a1)
-    l.s     $f24, 96($a1)
-    l.s     $f25, 100($a1)
-    l.s     $f26, 104($a1)
-    l.s     $f27, 108($a1)
-    l.s     $f28, 112($a1)
-    l.s     $f29, 116($a1)
-    l.s     $f30, 120($a1)
-    l.s     $f31, 124($a1)
+    LDu  $f0,  $f1,   0*8, $a1, $t1
+    LDu  $f2,  $f3,   1*8, $a1, $t1
+    LDu  $f4,  $f5,   2*8, $a1, $t1
+    LDu  $f6,  $f7,   3*8, $a1, $t1
+    LDu  $f8,  $f9,   4*8, $a1, $t1
+    LDu  $f10, $f11,  5*8, $a1, $t1
+    LDu  $f12, $f13,  6*8, $a1, $t1
+    LDu  $f14, $f15,  7*8, $a1, $t1
+    LDu  $f16, $f17,  8*8, $a1, $t1
+    LDu  $f18, $f19,  9*8, $a1, $t1
+    LDu  $f20, $f21, 10*8, $a1, $t1
+    LDu  $f22, $f23, 11*8, $a1, $t1
+    LDu  $f24, $f25, 12*8, $a1, $t1
+    LDu  $f26, $f27, 13*8, $a1, $t1
+    LDu  $f28, $f29, 14*8, $a1, $t1
+    LDu  $f30, $f31, 15*8, $a1, $t1
+
     .set push
     .set nomacro
     .set noat
@@ -558,9 +543,9 @@
     jr    $ra
     sw    $v1, 4($t0)           # store the other half of the result
 1:
-    s.s   $f0, 0($t0)           # store floating point result
+    SDu   $f0, $f1, 0, $t0, $t1 # store floating point result
     jr    $ra
-    s.s   $f1, 4($t0)           # store other half of floating point result
+    nop
 END art_quick_invoke_stub
 
     /*
@@ -699,7 +684,7 @@
     lw     $a2, 8($sp)
     lw     $a1, 4($sp)
     lw     $a0, 0($sp)
-    add    $sp, 32
+    addiu  $sp, 32
     .cfi_adjust_cfa_offset -32
     bnez   $v0, .Ldo_aput
     nop
@@ -1080,7 +1065,7 @@
 ENTRY art_quick_test_suspend
     lh     $a0, THREAD_FLAGS_OFFSET(rSELF)
     bnez   $a0, 1f
-    addi  rSUSPEND, $zero, SUSPEND_CHECK_INTERVAL   # reset rSUSPEND to SUSPEND_CHECK_INTERVAL
+    addiu  rSUSPEND, $zero, SUSPEND_CHECK_INTERVAL   # reset rSUSPEND to SUSPEND_CHECK_INTERVAL
     jr     $ra
     nop
 1:
@@ -1103,9 +1088,10 @@
     lw      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
     RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
     bnez    $t0, 1f
-    mtc1    $v0, $f0               # place return value to FP return value
+    # don't care if $v0 and/or $v1 are modified, when exception branch taken
+    MTD     $v0, $v1, $f0, $f1          # move float value to return value
     jr      $ra
-    mtc1    $v1, $f1               # place return value to FP return value
+    nop
 1:
     DELIVER_PENDING_EXCEPTION
 END art_quick_proxy_invoke_handler
@@ -1191,9 +1177,9 @@
     # tear dpown the callee-save frame
     RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
 
-    mtc1    $v0, $f0               # place return value to FP return value
+    MTD     $v0, $v1, $f0, $f1     # move float value to return value
     jr      $ra
-    mtc1    $v1, $f1               # place return value to FP return value
+    nop
 
 1:
     move    $sp, $s8               # tear down the alloca
@@ -1211,9 +1197,10 @@
     lw      $t0, THREAD_EXCEPTION_OFFSET(rSELF) # load Thread::Current()->exception_
     RESTORE_REFS_AND_ARGS_CALLEE_SAVE_FRAME
     bnez    $t0, 1f
-    mtc1    $v0, $f0               # place return value to FP return value
+    # don't care if $v0 and/or $v1 are modified, when exception branch taken
+    MTD     $v0, $v1, $f0, $f1                  # move float value to return value
     jr      $ra
-    mtc1    $v1, $f1               # place return value to FP return value
+    nop
 1:
     DELIVER_PENDING_EXCEPTION
 END art_quick_to_interpreter_bridge
@@ -1248,12 +1235,10 @@
     sw       $v0, 12($sp)
     .cfi_rel_offset 2, 32
     sw       $v1, 8($sp)
-    .cfi_rel_offset 3, 36 
-    s.s      $f0, 4($sp)
-    s.s      $f1, 0($sp)
+    .cfi_rel_offset 3, 36
+    s.d      $f0, 0($sp)
     SETUP_REFS_ONLY_CALLEE_SAVE_FRAME
-    s.s      $f0, 16($sp)   # pass fpr result
-    s.s      $f1, 20($sp)
+    s.d      $f0, 16($sp)   # pass fpr result
     move     $a2, $v0       # pass gpr result
     move     $a3, $v1
     addiu    $a1, $sp, ARG_SLOT_SIZE   # pass $sp (remove arg slots)
@@ -1264,8 +1249,7 @@
     addiu    $sp, $sp, ARG_SLOT_SIZE+FRAME_SIZE_REFS_ONLY_CALLEE_SAVE  # args slot + refs_only callee save frame
     lw       $v0, 12($sp)   # restore return values
     lw       $v1, 8($sp)
-    l.s      $f0, 4($sp)
-    l.s      $f1, 0($sp)
+    l.d      $f0, 0($sp)
     jr       $t0            # return
     addiu    $sp, $sp, 16   # remove temp storage from stack
     .cfi_adjust_cfa_offset -16
@@ -1300,11 +1284,15 @@
     srl     $a0, 1
     srl     $a0, $v1                         #  alo<- alo >> (32-(shift&31))
     sll     $v1, $a1, $a2                    #  rhi<- ahi << (shift&31)
-    or      $v1, $a0                         #  rhi<- rhi | alo
     andi    $a2, 0x20                        #  shift< shift & 0x20
-    movn    $v1, $v0, $a2                    #  rhi<- rlo (if shift&0x20)
-    jr      $ra
-    movn    $v0, $zero, $a2                  #  rlo<- 0  (if shift&0x20)
+    beqz    $a2, 1f
+    or      $v1, $a0                         #  rhi<- rhi | alo
+
+    move    $v1, $v0                         #  rhi<- rlo (if shift&0x20)
+    move    $v0, $zero                       #  rlo<- 0 (if shift&0x20)
+
+1:  jr      $ra
+    nop
 END art_quick_shl_long
 
     /*
@@ -1324,11 +1312,15 @@
     not     $a0, $a2                         #  alo<- 31-shift (shift is 5b)
     sll     $a1, 1
     sll     $a1, $a0                         #  ahi<- ahi << (32-(shift&31))
-    or      $v0, $a1                         #  rlo<- rlo | ahi
     andi    $a2, 0x20                        #  shift & 0x20
-    movn    $v0, $v1, $a2                    #  rlo<- rhi (if shift&0x20)
-    jr      $ra
-    movn    $v1, $a3, $a2                    #  rhi<- sign(ahi) (if shift&0x20)
+    beqz    $a2, 1f
+    or      $v0, $a1                         #  rlo<- rlo | ahi
+
+    move    $v0, $v1                         #  rlo<- rhi (if shift&0x20)
+    move    $v1, $a3                         #  rhi<- sign(ahi) (if shift&0x20)
+
+1:  jr      $ra
+    nop
 END art_quick_shr_long
 
     /*
@@ -1348,11 +1340,15 @@
     not     $a0, $a2                         #  alo<- 31-shift (shift is 5b)
     sll     $a1, 1
     sll     $a1, $a0                         #  ahi<- ahi << (32-(shift&31))
-    or      $v0, $a1                         #  rlo<- rlo | ahi
     andi    $a2, 0x20                        #  shift & 0x20
-    movn    $v0, $v1, $a2                    #  rlo<- rhi (if shift&0x20)
-    jr      $ra
-    movn    $v1, $zero, $a2                  #  rhi<- 0 (if shift&0x20)
+    beqz    $a2, 1f
+    or      $v0, $a1                         #  rlo<- rlo | ahi
+
+    move    $v0, $v1                         #  rlo<- rhi (if shift&0x20)
+    move    $v1, $zero                       #  rhi<- 0 (if shift&0x20)
+
+1:  jr      $ra
+    nop
 END art_quick_ushr_long
 
 UNIMPLEMENTED art_quick_indexof
diff --git a/runtime/base/histogram-inl.h b/runtime/base/histogram-inl.h
index b329a31..812ed86 100644
--- a/runtime/base/histogram-inl.h
+++ b/runtime/base/histogram-inl.h
@@ -35,10 +35,13 @@
     DCHECK_GT(new_max, max_);
     GrowBuckets(new_max);
   }
-
   BucketiseValue(value);
 }
 
+template <class Value> inline void Histogram<Value>::AdjustAndAddValue(Value value) {
+  AddValue(value / kAdjust);
+}
+
 template <class Value> inline Histogram<Value>::Histogram(const char* name)
     : kAdjust(0),
       kInitialBucketCount(0),
diff --git a/runtime/base/histogram.h b/runtime/base/histogram.h
index 1e12be8..78f6e1c 100644
--- a/runtime/base/histogram.h
+++ b/runtime/base/histogram.h
@@ -46,6 +46,7 @@
   // This is the expected constructor when creating new Histograms.
   Histogram(const char* name, Value initial_bucket_width, size_t max_buckets = 100);
   void AddValue(Value);
+  void AdjustAndAddValue(Value);  // Add a value after dividing it by kAdjust.
   // Builds the cumulative distribution function from the frequency data.
   // Accumulative summation of frequencies.
   // cumulative_freq[i] = sum(frequency[j] : 0 < j < i )
diff --git a/runtime/base/unix_file/fd_file.cc b/runtime/base/unix_file/fd_file.cc
index 6e5e7a1..780e37a 100644
--- a/runtime/base/unix_file/fd_file.cc
+++ b/runtime/base/unix_file/fd_file.cc
@@ -178,10 +178,16 @@
   return fd_ >= 0;
 }
 
-bool FdFile::ReadFully(void* buffer, size_t byte_count) {
+static ssize_t ReadIgnoreOffset(int fd, void *buf, size_t count, off_t offset) {
+  DCHECK_EQ(offset, 0);
+  return read(fd, buf, count);
+}
+
+template <ssize_t (*read_func)(int, void*, size_t, off_t)>
+static bool ReadFullyGeneric(int fd, void* buffer, size_t byte_count, size_t offset) {
   char* ptr = static_cast<char*>(buffer);
   while (byte_count > 0) {
-    ssize_t bytes_read = TEMP_FAILURE_RETRY(read(fd_, ptr, byte_count));
+    ssize_t bytes_read = TEMP_FAILURE_RETRY(read_func(fd, ptr, byte_count, offset));
     if (bytes_read <= 0) {
       // 0: end of file
       // -1: error
@@ -189,10 +195,19 @@
     }
     byte_count -= bytes_read;  // Reduce the number of remaining bytes.
     ptr += bytes_read;  // Move the buffer forward.
+    offset += static_cast<size_t>(bytes_read);  // Move the offset forward.
   }
   return true;
 }
 
+bool FdFile::ReadFully(void* buffer, size_t byte_count) {
+  return ReadFullyGeneric<ReadIgnoreOffset>(fd_, buffer, byte_count, 0);
+}
+
+bool FdFile::PreadFully(void* buffer, size_t byte_count, size_t offset) {
+  return ReadFullyGeneric<pread>(fd_, buffer, byte_count, offset);
+}
+
 bool FdFile::WriteFully(const void* buffer, size_t byte_count) {
   const char* ptr = static_cast<const char*>(buffer);
   moveTo(GuardState::kBase, GuardState::kClosed, "Writing into closed file.");
diff --git a/runtime/base/unix_file/fd_file.h b/runtime/base/unix_file/fd_file.h
index 8db2ee4..1b1fb4e 100644
--- a/runtime/base/unix_file/fd_file.h
+++ b/runtime/base/unix_file/fd_file.h
@@ -74,6 +74,7 @@
   }
   void DisableAutoClose();
   bool ReadFully(void* buffer, size_t byte_count) WARN_UNUSED;
+  bool PreadFully(void* buffer, size_t byte_count, size_t offset) WARN_UNUSED;
   bool WriteFully(const void* buffer, size_t byte_count) WARN_UNUSED;
 
   // This enum is public so that we can define the << operator over it.
diff --git a/runtime/base/unix_file/fd_file_test.cc b/runtime/base/unix_file/fd_file_test.cc
index a7e5b96..388f717 100644
--- a/runtime/base/unix_file/fd_file_test.cc
+++ b/runtime/base/unix_file/fd_file_test.cc
@@ -76,4 +76,38 @@
   EXPECT_FALSE(file.ReadFully(&buffer, 4));
 }
 
+template <size_t Size>
+static void NullTerminateCharArray(char (&array)[Size]) {
+  array[Size - 1] = '\0';
+}
+
+TEST_F(FdFileTest, ReadFullyWithOffset) {
+  // New scratch file, zero-length.
+  art::ScratchFile tmp;
+  FdFile file;
+  ASSERT_TRUE(file.Open(tmp.GetFilename(), O_RDWR));
+  EXPECT_GE(file.Fd(), 0);
+  EXPECT_TRUE(file.IsOpened());
+
+  char ignore_prefix[20] = {'a', };
+  NullTerminateCharArray(ignore_prefix);
+  char read_suffix[10] = {'b', };
+  NullTerminateCharArray(read_suffix);
+
+  off_t offset = 0;
+  // Write scratch data to file that we can read back into.
+  EXPECT_TRUE(file.Write(ignore_prefix, sizeof(ignore_prefix), offset));
+  offset += sizeof(ignore_prefix);
+  EXPECT_TRUE(file.Write(read_suffix, sizeof(read_suffix), offset));
+
+  ASSERT_EQ(file.Flush(), 0);
+
+  // Reading at an offset should only produce 'bbbb...', since we ignore the 'aaa...' prefix.
+  char buffer[sizeof(read_suffix)];
+  EXPECT_TRUE(file.PreadFully(buffer, sizeof(read_suffix), offset));
+  EXPECT_STREQ(&read_suffix[0], &buffer[0]);
+
+  ASSERT_EQ(file.Close(), 0);
+}
+
 }  // namespace unix_file
diff --git a/runtime/common_runtime_test.cc b/runtime/common_runtime_test.cc
index 03b33e9..3d7bf53 100644
--- a/runtime/common_runtime_test.cc
+++ b/runtime/common_runtime_test.cc
@@ -169,6 +169,13 @@
   }
 }
 
+std::string CommonRuntimeTest::GetCoreArtLocation() {
+  return GetCoreFileLocation("art");
+}
+
+std::string CommonRuntimeTest::GetCoreOatLocation() {
+  return GetCoreFileLocation("oat");
+}
 
 const DexFile* CommonRuntimeTest::LoadExpectSingleDexFile(const char* location) {
   std::vector<const DexFile*> dex_files;
@@ -358,6 +365,21 @@
   return class_loader;
 }
 
+std::string CommonRuntimeTest::GetCoreFileLocation(const char* suffix) {
+  CHECK(suffix != nullptr);
+
+  std::string location;
+  if (IsHost()) {
+    const char* host_dir = getenv("ANDROID_HOST_OUT");
+    CHECK(host_dir != NULL);
+    location = StringPrintf("%s/framework/core.%s", host_dir, suffix);
+  } else {
+    location = StringPrintf("/data/art-test/core.%s", suffix);
+  }
+
+  return location;
+}
+
 CheckJniAbortCatcher::CheckJniAbortCatcher() : vm_(Runtime::Current()->GetJavaVM()) {
   vm_->SetCheckJniAbortHook(Hook, &actual_);
 }
diff --git a/runtime/common_runtime_test.h b/runtime/common_runtime_test.h
index 1b56835..edc3e1e 100644
--- a/runtime/common_runtime_test.h
+++ b/runtime/common_runtime_test.h
@@ -80,6 +80,12 @@
     return !kIsTargetBuild;
   }
 
+  // File location to core.art, e.g. $ANDROID_HOST_OUT/system/framework/core.art
+  static std::string GetCoreArtLocation();
+
+  // File location to core.oat, e.g. $ANDROID_HOST_OUT/system/framework/core.oat
+  static std::string GetCoreOatLocation();
+
   const DexFile* LoadExpectSingleDexFile(const char* location);
 
   virtual void SetUp();
@@ -115,6 +121,8 @@
   ClassLinker* class_linker_;
 
  private:
+  static std::string GetCoreFileLocation(const char* suffix);
+
   std::unique_ptr<CompilerCallbacks> callbacks_;
   std::vector<const DexFile*> opened_dex_files_;
 };
diff --git a/runtime/debugger.cc b/runtime/debugger.cc
index a3a043f..556f2f8 100644
--- a/runtime/debugger.cc
+++ b/runtime/debugger.cc
@@ -3233,8 +3233,12 @@
   }
 }
 
+// Returns the deoptimization kind required to set a breakpoint in a method.
+// If a breakpoint has already been set, we also return the first breakpoint
+// through the given 'existing_brkpt' pointer.
 static DeoptimizationRequest::Kind GetRequiredDeoptimizationKind(Thread* self,
-                                                                 mirror::ArtMethod* m)
+                                                                 mirror::ArtMethod* m,
+                                                                 const Breakpoint** existing_brkpt)
     SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) {
   if (!Dbg::RequiresDeoptimization()) {
     // We already run in interpreter-only mode so we don't need to deoptimize anything.
@@ -3242,12 +3246,14 @@
                << PrettyMethod(m);
     return DeoptimizationRequest::kNothing;
   }
-  const Breakpoint* existing_breakpoint;
+  const Breakpoint* first_breakpoint;
   {
     ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
-    existing_breakpoint = FindFirstBreakpointForMethod(m);
+    first_breakpoint = FindFirstBreakpointForMethod(m);
+    *existing_brkpt = first_breakpoint;
   }
-  if (existing_breakpoint == nullptr) {
+
+  if (first_breakpoint == nullptr) {
     // There is no breakpoint on this method yet: we need to deoptimize. If this method may be
     // inlined, we deoptimize everything; otherwise we deoptimize only this method.
     // Note: IsMethodPossiblyInlined goes into the method verifier and may cause thread suspension.
@@ -3282,7 +3288,7 @@
     // There is at least one breakpoint for this method: we don't need to deoptimize.
     // Let's check that all breakpoints are configured the same way for deoptimization.
     VLOG(jdwp) << "Breakpoint already set: no deoptimization is required";
-    DeoptimizationRequest::Kind deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
+    DeoptimizationRequest::Kind deoptimization_kind = first_breakpoint->GetDeoptimizationKind();
     if (kIsDebugBuild) {
       ReaderMutexLock mu(self, *Locks::breakpoint_lock_);
       SanityCheckExistingBreakpoints(m, deoptimization_kind);
@@ -3298,7 +3304,9 @@
   mirror::ArtMethod* m = FromMethodId(location->method_id);
   DCHECK(m != nullptr) << "No method for method id " << location->method_id;
 
-  const DeoptimizationRequest::Kind deoptimization_kind = GetRequiredDeoptimizationKind(self, m);
+  const Breakpoint* existing_breakpoint = nullptr;
+  const DeoptimizationRequest::Kind deoptimization_kind =
+      GetRequiredDeoptimizationKind(self, m, &existing_breakpoint);
   req->SetKind(deoptimization_kind);
   if (deoptimization_kind == DeoptimizationRequest::kSelectiveDeoptimization) {
     req->SetMethod(m);
@@ -3310,7 +3318,15 @@
 
   {
     WriterMutexLock mu(self, *Locks::breakpoint_lock_);
-    gBreakpoints.push_back(Breakpoint(m, location->dex_pc, deoptimization_kind));
+    // If there is at least one existing breakpoint on the same method, the new breakpoint
+    // must have the same deoptimization kind than the existing breakpoint(s).
+    DeoptimizationRequest::Kind breakpoint_deoptimization_kind;
+    if (existing_breakpoint != nullptr) {
+      breakpoint_deoptimization_kind = existing_breakpoint->GetDeoptimizationKind();
+    } else {
+      breakpoint_deoptimization_kind = deoptimization_kind;
+    }
+    gBreakpoints.push_back(Breakpoint(m, location->dex_pc, breakpoint_deoptimization_kind));
     VLOG(jdwp) << "Set breakpoint #" << (gBreakpoints.size() - 1) << ": "
                << gBreakpoints[gBreakpoints.size() - 1];
   }
diff --git a/runtime/gc/collector/garbage_collector.cc b/runtime/gc/collector/garbage_collector.cc
index 9e6a800..8be18be 100644
--- a/runtime/gc/collector/garbage_collector.cc
+++ b/runtime/gc/collector/garbage_collector.cc
@@ -102,7 +102,7 @@
   total_time_ns_ += current_iteration->GetDurationNs();
   for (uint64_t pause_time : current_iteration->GetPauseTimes()) {
     MutexLock mu(self, pause_histogram_lock_);
-    pause_histogram_.AddValue(pause_time / 1000);
+    pause_histogram_.AdjustAndAddValue(pause_time);
   }
   ATRACE_END();
 }
diff --git a/runtime/gc/heap.cc b/runtime/gc/heap.cc
index d420500..8f09e07 100644
--- a/runtime/gc/heap.cc
+++ b/runtime/gc/heap.cc
@@ -143,6 +143,7 @@
       zygote_space_(nullptr),
       large_object_threshold_(large_object_threshold),
       gc_request_pending_(false),
+      conc_gc_running_(false),
       collector_type_running_(kCollectorTypeNone),
       last_gc_type_(collector::kGcTypeNone),
       next_gc_type_(collector::kGcTypePartial),
@@ -167,8 +168,6 @@
       verify_pre_gc_rosalloc_(verify_pre_gc_rosalloc),
       verify_pre_sweeping_rosalloc_(verify_pre_sweeping_rosalloc),
       verify_post_gc_rosalloc_(verify_post_gc_rosalloc),
-      last_gc_time_ns_(NanoTime()),
-      allocation_rate_(0),
       /* For GC a lot mode, we limit the allocations stacks to be kGcAlotInterval allocations. This
        * causes a lot of GC since we do a GC for alloc whenever the stack is full. When heap
        * verification is enabled, we limit the size of allocation stacks to speed up their
@@ -413,7 +412,6 @@
   gc_request_lock_ = new Mutex("GC request lock");
   gc_request_cond_.reset(new ConditionVariable("GC request condition variable", *gc_request_lock_));
   heap_trim_request_lock_ = new Mutex("Heap trim request lock");
-  last_gc_size_ = GetBytesAllocated();
   if (ignore_max_footprint_) {
     SetIdealFootprint(std::numeric_limits<size_t>::max());
     concurrent_start_bytes_ = std::numeric_limits<size_t>::max();
@@ -859,7 +857,7 @@
     os << "Zygote space size " << PrettySize(zygote_space_->Size()) << "\n";
   }
   os << "Total mutator paused time: " << PrettyDuration(total_paused_time) << "\n";
-  os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_) << "\n";
+  os << "Total time waiting for GC to complete: " << PrettyDuration(total_wait_time_);
   BaseMutex::DumpAll(os);
 }
 
@@ -874,6 +872,7 @@
   STLDeleteElements(&continuous_spaces_);
   STLDeleteElements(&discontinuous_spaces_);
   delete gc_complete_lock_;
+  delete gc_request_lock_;
   delete heap_trim_request_lock_;
   VLOG(heap) << "Finished ~Heap()";
 }
@@ -2153,16 +2152,9 @@
     ++runtime->GetStats()->gc_for_alloc_count;
     ++self->GetStats()->gc_for_alloc_count;
   }
-  uint64_t gc_start_time_ns = NanoTime();
-  uint64_t gc_start_size = GetBytesAllocated();
-  // Approximate allocation rate in bytes / second.
-  uint64_t ms_delta = NsToMs(gc_start_time_ns - last_gc_time_ns_);
-  // Back to back GCs can cause 0 ms of wait time in between GC invocations.
-  if (LIKELY(ms_delta != 0)) {
-    allocation_rate_ = ((gc_start_size - last_gc_size_) * 1000) / ms_delta;
-    ATRACE_INT("Allocation rate KB/s", allocation_rate_ / KB);
-    VLOG(heap) << "Allocation rate: " << PrettySize(allocation_rate_) << "/s";
-  }
+  const uint64_t bytes_allocated_before_gc = GetBytesAllocated();
+  // Approximate heap size.
+  ATRACE_INT("Heap size (KB)", bytes_allocated_before_gc / KB);
 
   DCHECK_LT(gc_type, collector::kGcTypeMax);
   DCHECK_NE(gc_type, collector::kGcTypeNone);
@@ -2219,7 +2211,7 @@
   // Enqueue cleared references.
   reference_processor_.EnqueueClearedReferences(self);
   // Grow the heap so that we know when to perform the next GC.
-  GrowForUtilization(collector);
+  GrowForUtilization(collector, bytes_allocated_before_gc);
   const size_t duration = GetCurrentGcIteration()->GetDurationNs();
   const std::vector<uint64_t>& pause_times = GetCurrentGcIteration()->GetPauseTimes();
   // Print the GC if it is an explicit GC (e.g. Runtime.gc()) or a slow GC
@@ -2929,25 +2921,24 @@
   return foreground_heap_growth_multiplier_;
 }
 
-void Heap::GrowForUtilization(collector::GarbageCollector* collector_ran) {
+void Heap::GrowForUtilization(collector::GarbageCollector* collector_ran,
+                              uint64_t bytes_allocated_before_gc) {
   // We know what our utilization is at this moment.
   // This doesn't actually resize any memory. It just lets the heap grow more when necessary.
   const uint64_t bytes_allocated = GetBytesAllocated();
-  last_gc_size_ = bytes_allocated;
-  last_gc_time_ns_ = NanoTime();
   uint64_t target_size;
   collector::GcType gc_type = collector_ran->GetGcType();
+  const double multiplier = HeapGrowthMultiplier();  // Use the multiplier to grow more for
+  // foreground.
+  const uint64_t adjusted_min_free = static_cast<uint64_t>(min_free_ * multiplier);
+  const uint64_t adjusted_max_free = static_cast<uint64_t>(max_free_ * multiplier);
   if (gc_type != collector::kGcTypeSticky) {
     // Grow the heap for non sticky GC.
-    const float multiplier = HeapGrowthMultiplier();  // Use the multiplier to grow more for
-    // foreground.
-    intptr_t delta = bytes_allocated / GetTargetHeapUtilization() - bytes_allocated;
+    ssize_t delta = bytes_allocated / GetTargetHeapUtilization() - bytes_allocated;
     CHECK_GE(delta, 0);
     target_size = bytes_allocated + delta * multiplier;
-    target_size = std::min(target_size,
-                           bytes_allocated + static_cast<uint64_t>(max_free_ * multiplier));
-    target_size = std::max(target_size,
-                           bytes_allocated + static_cast<uint64_t>(min_free_ * multiplier));
+    target_size = std::min(target_size, bytes_allocated + adjusted_max_free);
+    target_size = std::max(target_size, bytes_allocated + adjusted_min_free);
     native_need_to_run_finalization_ = true;
     next_gc_type_ = collector::kGcTypeSticky;
   } else {
@@ -2969,8 +2960,8 @@
       next_gc_type_ = non_sticky_gc_type;
     }
     // If we have freed enough memory, shrink the heap back down.
-    if (bytes_allocated + max_free_ < max_allowed_footprint_) {
-      target_size = bytes_allocated + max_free_;
+    if (bytes_allocated + adjusted_max_free < max_allowed_footprint_) {
+      target_size = bytes_allocated + adjusted_max_free;
     } else {
       target_size = std::max(bytes_allocated, static_cast<uint64_t>(max_allowed_footprint_));
     }
@@ -2978,11 +2969,18 @@
   if (!ignore_max_footprint_) {
     SetIdealFootprint(target_size);
     if (IsGcConcurrent()) {
+      const uint64_t freed_bytes = current_gc_iteration_.GetFreedBytes() +
+          current_gc_iteration_.GetFreedLargeObjectBytes();
+      // Bytes allocated will shrink by freed_bytes after the GC runs, so if we want to figure out
+      // how many bytes were allocated during the GC we need to add freed_bytes back on.
+      CHECK_GE(bytes_allocated + freed_bytes, bytes_allocated_before_gc);
+      const uint64_t bytes_allocated_during_gc = bytes_allocated + freed_bytes -
+          bytes_allocated_before_gc;
       // Calculate when to perform the next ConcurrentGC.
       // Calculate the estimated GC duration.
       const double gc_duration_seconds = NsToMs(current_gc_iteration_.GetDurationNs()) / 1000.0;
       // Estimate how many remaining bytes we will have when we need to start the next GC.
-      size_t remaining_bytes = allocation_rate_ * gc_duration_seconds;
+      size_t remaining_bytes = bytes_allocated_during_gc * gc_duration_seconds;
       remaining_bytes = std::min(remaining_bytes, kMaxConcurrentRemainingBytes);
       remaining_bytes = std::max(remaining_bytes, kMinConcurrentRemainingBytes);
       if (UNLIKELY(remaining_bytes > max_allowed_footprint_)) {
@@ -3277,17 +3275,21 @@
 void Heap::WaitForConcurrentGCRequest(Thread* self) {
   ScopedThreadStateChange tsc(self, kBlocked);
   MutexLock mu(self, *gc_request_lock_);
+  conc_gc_running_ = false;
   while (!gc_request_pending_) {
     gc_request_cond_->Wait(self);
   }
   gc_request_pending_ = false;
+  conc_gc_running_ = true;
 }
 
 void Heap::NotifyConcurrentGCRequest(Thread* self) {
   ScopedThreadStateChange tsc(self, kBlocked);
   MutexLock mu(self, *gc_request_lock_);
-  gc_request_pending_ = true;
-  gc_request_cond_->Signal(self);
+  if (!conc_gc_running_) {
+    gc_request_pending_ = true;
+    gc_request_cond_->Signal(self);
+  }
 }
 
 }  // namespace gc
diff --git a/runtime/gc/heap.h b/runtime/gc/heap.h
index 529af95..cf94eb6 100644
--- a/runtime/gc/heap.h
+++ b/runtime/gc/heap.h
@@ -755,8 +755,10 @@
 
   // Given the current contents of the alloc space, increase the allowed heap footprint to match
   // the target utilization ratio.  This should only be called immediately after a full garbage
-  // collection.
-  void GrowForUtilization(collector::GarbageCollector* collector_ran);
+  // collection. bytes_allocated_before_gc is used to measure bytes / second for the period which
+  // the GC was run.
+  void GrowForUtilization(collector::GarbageCollector* collector_ran,
+                          uint64_t bytes_allocated_before_gc = 0);
 
   size_t GetPercentFree();
 
@@ -881,6 +883,7 @@
   Mutex* gc_request_lock_ DEFAULT_MUTEX_ACQUIRED_AFTER;
   std::unique_ptr<ConditionVariable> gc_request_cond_ GUARDED_BY(gc_request_lock_);
   bool gc_request_pending_ GUARDED_BY(gc_request_lock_);
+  bool conc_gc_running_ GUARDED_BY(gc_request_lock_);
 
   // Reference processor;
   ReferenceProcessor reference_processor_;
@@ -971,12 +974,6 @@
   // Parallel GC data structures.
   std::unique_ptr<ThreadPool> thread_pool_;
 
-  // The nanosecond time at which the last GC ended.
-  uint64_t last_gc_time_ns_;
-
-  // How many bytes were allocated at the end of the last GC.
-  uint64_t last_gc_size_;
-
   // Estimated allocation rate (bytes / second). Computed between the time of the last GC cycle
   // and the start of the current one.
   uint64_t allocation_rate_;
diff --git a/runtime/monitor.cc b/runtime/monitor.cc
index 233267b..1ef5221 100644
--- a/runtime/monitor.cc
+++ b/runtime/monitor.cc
@@ -16,6 +16,9 @@
 
 #include "monitor.h"
 
+#define ATRACE_TAG ATRACE_TAG_DALVIK
+
+#include <cutils/trace.h>
 #include <vector>
 
 #include "base/mutex.h"
@@ -251,7 +254,12 @@
     {
       ScopedThreadStateChange tsc(self, kBlocked);  // Change to blocked and give up mutator_lock_.
       MutexLock mu2(self, monitor_lock_);  // Reacquire monitor_lock_ without mutator_lock_ for Wait.
-      if (owner_ != NULL) {  // Did the owner_ give the lock up?
+      if (owner_ != nullptr) {  // Did the owner_ give the lock up?
+        if (ATRACE_ENABLED()) {
+          std::string name;
+          owner_->GetThreadName(name);
+          ATRACE_BEGIN(("Contended on monitor with owner " + name).c_str());
+        }
         monitor_contenders_.Wait(self);  // Still contended so wait.
         // Woken from contention.
         if (log_contention) {
@@ -275,6 +283,7 @@
             LogContentionEvent(self, wait_ms, sample_percent, owners_filename, owners_line_number);
           }
         }
+        ATRACE_END();
       }
     }
     self->SetMonitorEnterObject(nullptr);
diff --git a/runtime/thread.cc b/runtime/thread.cc
index 2308cc9..4a7103b 100644
--- a/runtime/thread.cc
+++ b/runtime/thread.cc
@@ -1210,7 +1210,10 @@
     tlsPtr_.opeer = nullptr;
   }
 
-  Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this);
+  {
+    ScopedObjectAccess soa(self);
+    Runtime::Current()->GetHeap()->RevokeThreadLocalBuffers(this);
+  }
 }
 
 Thread::~Thread() {
diff --git a/runtime/thread_list.cc b/runtime/thread_list.cc
index 6a9111f..968e89d 100644
--- a/runtime/thread_list.cc
+++ b/runtime/thread_list.cc
@@ -27,6 +27,7 @@
 
 #include <sstream>
 
+#include "base/histogram-inl.h"
 #include "base/mutex.h"
 #include "base/mutex-inl.h"
 #include "base/timing_logger.h"
@@ -46,7 +47,8 @@
 
 ThreadList::ThreadList()
     : suspend_all_count_(0), debug_suspend_all_count_(0),
-      thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_) {
+      thread_exit_cond_("thread exit condition variable", *Locks::thread_list_lock_),
+      suspend_all_historam_("suspend all histogram", 16, 64) {
   CHECK(Monitor::IsValidLockWord(LockWord::FromThinLockId(kMaxThreadId, 1)));
 }
 
@@ -97,6 +99,12 @@
 }
 
 void ThreadList::DumpForSigQuit(std::ostream& os) {
+  {
+    ScopedObjectAccess soa(Thread::Current());
+    Histogram<uint64_t>::CumulativeData data;
+    suspend_all_historam_.CreateHistogram(&data);
+    suspend_all_historam_.PrintConfidenceIntervals(os, 0.99, data);  // Dump time to suspend.
+  }
   Dump(os);
   DumpUnattachedThreads(os);
 }
@@ -351,7 +359,7 @@
     VLOG(threads) << "Thread[null] SuspendAll starting...";
   }
   ATRACE_BEGIN("Suspending mutator threads");
-  uint64_t start_time = NanoTime();
+  const uint64_t start_time = NanoTime();
 
   Locks::mutator_lock_->AssertNotHeld(self);
   Locks::thread_list_lock_->AssertNotHeld(self);
@@ -384,9 +392,11 @@
   Locks::mutator_lock_->ExclusiveLock(self);
 #endif
 
-  uint64_t end_time = NanoTime();
-  if (end_time - start_time > kLongThreadSuspendThreshold) {
-    LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(end_time - start_time);
+  const uint64_t end_time = NanoTime();
+  const uint64_t suspend_time = end_time - start_time;
+  suspend_all_historam_.AdjustAndAddValue(suspend_time);
+  if (suspend_time > kLongThreadSuspendThreshold) {
+    LOG(WARNING) << "Suspending all threads took: " << PrettyDuration(suspend_time);
   }
 
   if (kDebugLocking) {
@@ -454,6 +464,9 @@
 }
 
 void ThreadList::Resume(Thread* thread, bool for_debugger) {
+  // This assumes there was an ATRACE_BEGIN when we suspended the thread.
+  ATRACE_END();
+
   Thread* self = Thread::Current();
   DCHECK_NE(thread, self);
   VLOG(threads) << "Resume(" << reinterpret_cast<void*>(thread) << ") starting..."
@@ -564,6 +577,12 @@
         // done.
         if (thread->IsSuspended()) {
           VLOG(threads) << "SuspendThreadByPeer thread suspended: " << *thread;
+          if (ATRACE_ENABLED()) {
+            std::string name;
+            thread->GetThreadName(name);
+            ATRACE_BEGIN(StringPrintf("SuspendThreadByPeer suspended %s for peer=%p", name.c_str(),
+                                      peer).c_str());
+          }
           return thread;
         }
         if (total_delay_us >= kTimeoutUs) {
@@ -648,6 +667,12 @@
         // count, or else we've waited and it has self suspended) or is the current thread, we're
         // done.
         if (thread->IsSuspended()) {
+          if (ATRACE_ENABLED()) {
+            std::string name;
+            thread->GetThreadName(name);
+            ATRACE_BEGIN(StringPrintf("SuspendThreadByThreadId suspended %s id=%d",
+                                      name.c_str(), thread_id).c_str());
+          }
           VLOG(threads) << "SuspendThreadByThreadId thread suspended: " << *thread;
           return thread;
         }
diff --git a/runtime/thread_list.h b/runtime/thread_list.h
index 13684c7..43c065a 100644
--- a/runtime/thread_list.h
+++ b/runtime/thread_list.h
@@ -17,6 +17,7 @@
 #ifndef ART_RUNTIME_THREAD_LIST_H_
 #define ART_RUNTIME_THREAD_LIST_H_
 
+#include "base/histogram.h"
 #include "base/mutex.h"
 #include "jni.h"
 #include "object_callbacks.h"
@@ -39,11 +40,10 @@
   ~ThreadList();
 
   void DumpForSigQuit(std::ostream& os)
-      LOCKS_EXCLUDED(Locks::thread_list_lock_);
+      LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::mutator_lock_);
   // For thread suspend timeout dumps.
   void Dump(std::ostream& os)
-      LOCKS_EXCLUDED(Locks::thread_list_lock_,
-                     Locks::thread_suspend_count_lock_);
+      LOCKS_EXCLUDED(Locks::thread_list_lock_, Locks::thread_suspend_count_lock_);
   pid_t GetLockOwner();  // For SignalCatcher.
 
   // Thread suspension support.
@@ -169,6 +169,10 @@
   // Signaled when threads terminate. Used to determine when all non-daemons have terminated.
   ConditionVariable thread_exit_cond_ GUARDED_BY(Locks::thread_list_lock_);
 
+  // Thread suspend time histogram. Only modified when all the threads are suspended, so guarding
+  // by mutator lock ensures no thread can read when another thread is modifying it.
+  Histogram<uint64_t> suspend_all_historam_ GUARDED_BY(Locks::mutator_lock_);
+
   friend class Thread;
 
   DISALLOW_COPY_AND_ASSIGN(ThreadList);
diff --git a/tools/checker.py b/tools/checker.py
new file mode 100755
index 0000000..82a1e6b
--- /dev/null
+++ b/tools/checker.py
@@ -0,0 +1,570 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+
+# Checker is a testing tool which compiles a given test file and compares the
+# state of the control-flow graph before and after each optimization pass
+# against a set of assertions specified alongside the tests.
+#
+# Tests are written in Java, turned into DEX and compiled with the Optimizing
+# compiler. "Check lines" are comments in the Java file which begin with prefix
+# 'CHECK' followed by a pattern that the engine attempts to match in the
+# compiler-generated output.
+#
+# Assertions are tested in groups which correspond to the individual compiler
+# passes. Each group of check lines therefore must start with a 'CHECK-START'
+# header which specifies the output group it should be tested against. The group
+# name must exactly match one of the groups recognized in the output (they can
+# be listed with the '--list-groups' command-line flag).
+#
+# Check line patterns are treated as plain text rather than regular expressions
+# but are whitespace agnostic.
+#
+# Actual regex patterns can be inserted enclosed in '{{' and '}}' brackets. If
+# curly brackets need to be used inside the body of the regex, they need to be
+# enclosed in round brackets. For example, the pattern '{{foo{2}}}' will parse
+# the invalid regex 'foo{2', but '{{(fo{2})}}' will match 'foo'.
+#
+# Regex patterns can be named and referenced later. A new variable is defined
+# with '[[name:regex]]' and can be referenced with '[[name]]'. Variables are
+# only valid within the scope of the defining group. Within a group they cannot
+# be redefined or used undefined.
+#
+# Example:
+#   The following assertions can be placed in a Java source file:
+#
+#   // CHECK-START: int MyClass.MyMethod() constant_folding (after)
+#   // CHECK:         [[ID:i[0-9]+]] IntConstant {{11|22}}
+#   // CHECK:                        Return [ [[ID]] ]
+#
+#   The engine will attempt to match the check lines against the output of the
+#   group named on the first line. Together they verify that the CFG after
+#   constant folding returns an integer constant with value either 11 or 22.
+#
+
+import argparse
+import os
+import re
+import shutil
+import sys
+import tempfile
+from subprocess import check_call
+
+class CommonEqualityMixin:
+  """Mixin for class equality as equality of the fields."""
+  def __eq__(self, other):
+    return (isinstance(other, self.__class__)
+           and self.__dict__ == other.__dict__)
+
+  def __ne__(self, other):
+    return not self.__eq__(other)
+
+  def __repr__(self):
+    return "<%s: %s>" % (type(self).__name__, str(self.__dict__))
+
+
+class CheckElement(CommonEqualityMixin):
+  """Single element of the check line."""
+
+  class Variant(object):
+    """Supported language constructs."""
+    Text, Pattern, VarRef, VarDef = range(4)
+
+  def __init__(self, variant, name, pattern):
+    self.variant = variant
+    self.name = name
+    self.pattern = pattern
+
+  @staticmethod
+  def parseText(text):
+    return CheckElement(CheckElement.Variant.Text, None, re.escape(text))
+
+  @staticmethod
+  def parsePattern(patternElem):
+    return CheckElement(CheckElement.Variant.Pattern, None, patternElem[2:len(patternElem)-2])
+
+  @staticmethod
+  def parseVariable(varElem):
+    colonPos = varElem.find(":")
+    if colonPos == -1:
+      # Variable reference
+      name = varElem[2:len(varElem)-2]
+      return CheckElement(CheckElement.Variant.VarRef, name, None)
+    else:
+      # Variable definition
+      name = varElem[2:colonPos]
+      body = varElem[colonPos+1:len(varElem)-2]
+      return CheckElement(CheckElement.Variant.VarDef, name, body)
+
+
+class CheckLine(CommonEqualityMixin):
+  """Representation of a single assertion in the check file formed of one or
+     more regex elements. Matching against an output line is successful only
+     if all regex elements can be matched in the given order."""
+
+  def __init__(self, lineContent, lineNo=-1):
+    lineContent = lineContent.strip()
+
+    self.lineNo = lineNo
+    self.content = lineContent
+
+    self.lineParts = self.__parse(lineContent)
+    if not self.lineParts:
+      raise Exception("Empty check line")
+
+  # Returns True if the given Match object was at the beginning of the line.
+  def __isMatchAtStart(self, match):
+    return (match is not None) and (match.start() == 0)
+
+  # Takes in a list of Match objects and returns the minimal start point among
+  # them. If there aren't any successful matches it returns the length of
+  # the searched string.
+  def __firstMatch(self, matches, string):
+    starts = map(lambda m: len(string) if m is None else m.start(), matches)
+    return min(starts)
+
+  # Returns the regex for finding a regex pattern in the check line.
+  def __getPatternRegex(self):
+    rStartSym = "\{\{"
+    rEndSym = "\}\}"
+    rBody = ".+?"
+    return rStartSym + rBody + rEndSym
+
+  # Returns the regex for finding a variable use in the check line.
+  def __getVariableRegex(self):
+    rStartSym = "\[\["
+    rEndSym = "\]\]"
+    rStartOptional = "("
+    rEndOptional = ")?"
+    rName = "[a-zA-Z][a-zA-Z0-9]*"
+    rSeparator = ":"
+    rBody = ".+?"
+    return rStartSym + rName + rStartOptional + rSeparator + rBody + rEndOptional + rEndSym
+
+  # This method parses the content of a check line stripped of the initial
+  # comment symbol and the CHECK keyword.
+  def __parse(self, line):
+    lineParts = []
+    # Loop as long as there is something to parse.
+    while line:
+      # Search for the nearest occurrence of the special markers.
+      matchWhitespace = re.search("\s+", line)
+      matchPattern = re.search(self.__getPatternRegex(), line)
+      matchVariable = re.search(self.__getVariableRegex(), line)
+
+      # If one of the above was identified at the current position, extract them
+      # from the line, parse them and add to the list of line parts.
+      if self.__isMatchAtStart(matchWhitespace):
+        # We want to be whitespace-agnostic so whenever a check line contains
+        # a whitespace, we add a regex pattern for an arbitrary non-zero number
+        # of whitespaces.
+        line = line[matchWhitespace.end():]
+        lineParts.append(CheckElement.parsePattern("{{\s+}}"))
+      elif self.__isMatchAtStart(matchPattern):
+        pattern = line[0:matchPattern.end()]
+        line = line[matchPattern.end():]
+        lineParts.append(CheckElement.parsePattern(pattern))
+      elif self.__isMatchAtStart(matchVariable):
+        var = line[0:matchVariable.end()]
+        line = line[matchVariable.end():]
+        lineParts.append(CheckElement.parseVariable(var))
+      else:
+        # If we're not currently looking at a special marker, this is a plain
+        # text match all the way until the first special marker (or the end
+        # of the line).
+        firstMatch = self.__firstMatch([ matchWhitespace, matchPattern, matchVariable ], line)
+        text = line[0:firstMatch]
+        line = line[firstMatch:]
+        lineParts.append(CheckElement.parseText(text))
+    return lineParts
+
+  # Returns the regex pattern to be matched in the output line. Variable
+  # references are substituted with their current values provided in the
+  # 'varState' argument.
+  # An exception is raised if a referenced variable is undefined.
+  def __generatePattern(self, linePart, varState):
+    if linePart.variant == CheckElement.Variant.VarRef:
+      try:
+        return re.escape(varState[linePart.name])
+      except KeyError:
+        raise Exception("Use of undefined variable '" + linePart.name + "' " +
+                        "(line " + str(self.lineNo))
+    else:
+      return linePart.pattern
+
+  # Attempts to match the check line against a line from the output file with
+  # the given initial variable values. It returns the new variable state if
+  # successful and None otherwise.
+  def match(self, outputLine, initialVarState):
+    initialSearchFrom = 0
+    initialPattern = self.__generatePattern(self.lineParts[0], initialVarState)
+    while True:
+      # Search for the first element on the regex parts list. This will mark
+      # the point on the line from which we will attempt to match the rest of
+      # the check pattern. If this iteration produces only a partial match,
+      # the next iteration will start searching further in the output.
+      firstMatch = re.search(initialPattern, outputLine[initialSearchFrom:])
+      if firstMatch is None:
+        return None
+      matchStart = initialSearchFrom + firstMatch.start()
+      initialSearchFrom += firstMatch.start() + 1
+
+      # Do the full matching on a shadow copy of the variable state. If the
+      # matching fails half-way, we will not need to revert the state.
+      varState = dict(initialVarState)
+
+      # Now try to parse all of the parts of the check line in the right order.
+      # Variable values are updated on-the-fly, meaning that a variable can
+      # be referenced immediately after its definition.
+      fullyMatched = True
+      for part in self.lineParts:
+        pattern = self.__generatePattern(part, varState)
+        match = re.match(pattern, outputLine[matchStart:])
+        if match is None:
+          fullyMatched = False
+          break
+        matchEnd = matchStart + match.end()
+        if part.variant == CheckElement.Variant.VarDef:
+          if part.name in varState:
+            raise Exception("Redefinition of variable '" + part.name + "'" +
+                            " (line " + str(self.lineNo) + ")")
+          varState[part.name] = outputLine[matchStart:matchEnd]
+        matchStart = matchEnd
+
+      # Return the new variable state if all parts were successfully matched.
+      # Otherwise loop and try to find another start point on the same line.
+      if fullyMatched:
+        return varState
+
+
+class CheckGroup(CommonEqualityMixin):
+  """Represents a named collection of check lines which are to be matched
+     against an output group of the same name."""
+
+  def __init__(self, name, lines):
+    if name:
+      self.name = name
+    else:
+      raise Exception("Check group does not have a name")
+    if lines:
+      self.lines = lines
+    else:
+      raise Exception("Check group " + self.name + " does not have a body")
+
+  def __headAndTail(self, list):
+    return list[0], list[1:]
+
+  # The driver of matching inside a group. It simultaneously reads lines from
+  # the output and check groups and attempts to match them against each other
+  # in the correct order.
+  def match(self, outputGroup):
+    readOutputLines = 0
+    lastMatch = 0
+
+    # Check and output lines which remain to be matched.
+    checkLines = self.lines
+    outputLines = outputGroup.body
+    varState = {}
+
+    # Retrieve the next check line.
+    while checkLines:
+      checkLine, checkLines = self.__headAndTail(checkLines)
+      foundMatch = False
+
+      # Retrieve the next output line.
+      while outputLines:
+        outputLine, outputLines = self.__headAndTail(outputLines)
+        readOutputLines += 1
+
+        # Try to match the current lines against each other. If successful,
+        # save the new state of variables and continue to the next check line.
+        newVarState = checkLine.match(outputLine, varState)
+        if newVarState is not None:
+          varState = newVarState
+          lastMatch = readOutputLines
+          foundMatch = True
+          break
+      if not foundMatch:
+        raise Exception("Could not match check line \"" + checkLine.content + "\" from line " +
+                        str(lastMatch+1) + " of the output. [vars=" + str(varState) + "]")
+
+  @staticmethod
+  def parse(name, lines):
+    return CheckGroup(name, list(map(lambda line: CheckLine(line), lines)))
+
+
+class OutputGroup(CommonEqualityMixin):
+  """Represents a named part of the test output against which a check group of
+     the same name is to be matched."""
+
+  def __init__(self, name, body):
+    if name:
+      self.name = name
+    else:
+      raise Exception("Output group does not have a name")
+    if body:
+      self.body = body
+    else:
+      raise Exception("Output group " + self.name + " does not have a body")
+
+
+class FileSplitMixin(object):
+  """Mixin for representing text files which need to be split into smaller
+     chunks before being parsed."""
+
+  def _parseStream(self, stream):
+    lineNo = 0
+    allGroups = []
+    currentGroup = None
+
+    for line in stream:
+      lineNo += 1
+      line = line.strip()
+      if not line:
+        continue
+
+      # Let the child class process the line and return information about it.
+      # The _processLine method can modify the content of the line (or delete it
+      # entirely) and specify whether it starts a new group.
+      processedLine, newGroupName = self._processLine(line, lineNo)
+      if newGroupName is not None:
+        currentGroup = (newGroupName, [])
+        allGroups.append(currentGroup)
+      if processedLine is not None:
+        currentGroup[1].append(processedLine)
+
+    # Finally, take the generated line groups and let the child class process
+    # each one before storing the final outcome.
+    return list(map(lambda group: self._processGroup(group[0], group[1]), allGroups))
+
+
+class CheckFile(FileSplitMixin):
+  """Collection of check groups extracted from the input test file."""
+
+  def __init__(self, prefix, checkStream):
+    self.prefix = prefix
+    self.groups = self._parseStream(checkStream)
+
+  # Attempts to parse a check line. The regex searches for a comment symbol
+  # followed by the CHECK keyword, given attribute and a colon at the very
+  # beginning of the line. Whitespaces are ignored.
+  def _extractLine(self, prefix, line):
+    ignoreWhitespace = "\s*"
+    commentSymbols = ["//", "#"]
+    prefixRegex = ignoreWhitespace + \
+                  "(" + "|".join(commentSymbols) + ")" + \
+                  ignoreWhitespace + \
+                  prefix + ":"
+
+    # The 'match' function succeeds only if the pattern is matched at the
+    # beginning of the line.
+    match = re.match(prefixRegex, line)
+    if match is not None:
+      return line[match.end():].strip()
+    else:
+      return None
+
+  def _processLine(self, line, lineNo):
+    startLine = self._extractLine(self.prefix + "-START", line)
+    if startLine is not None:
+      # Line starts with the CHECK-START keyword, start a new group
+      return (None, startLine)
+    else:
+      # Otherwise try to parse it as a standard CHECK line. If unsuccessful,
+      # _extractLine will return None and the line will be ignored.
+      return (self._extractLine(self.prefix, line), None)
+
+  def _exceptionLineOutsideGroup(self, line, lineNo):
+    raise Exception("Check file line lies outside a group (line " + str(lineNo) + ")")
+
+  def _processGroup(self, name, lines):
+    return CheckGroup.parse(name, lines)
+
+  def match(self, outputFile, printInfo=False):
+    for checkGroup in self.groups:
+      # TODO: Currently does not handle multiple occurrences of the same group
+      # name, e.g. when a pass is run multiple times. It will always try to
+      # match a check group against the first output group of the same name.
+      outputGroup = outputFile.findGroup(checkGroup.name)
+      if outputGroup is None:
+        raise Exception("Group " + checkGroup.name + " not found in the output")
+      if printInfo:
+        print("TEST " + checkGroup.name + "... ", end="", flush=True)
+      try:
+        checkGroup.match(outputGroup)
+        if printInfo:
+          print("PASSED")
+      except Exception as e:
+        if printInfo:
+          print("FAILED!")
+        raise e
+
+
+class OutputFile(FileSplitMixin):
+  """Representation of the output generated by the test and split into groups
+     within which the checks are performed.
+
+     C1visualizer format is parsed with a state machine which differentiates
+     between the 'compilation' and 'cfg' blocks. The former marks the beginning
+     of a method. It is parsed for the method's name but otherwise ignored. Each
+     subsequent CFG block represents one stage of the compilation pipeline and
+     is parsed into an output group named "<method name> <pass name>".
+     """
+
+  class ParsingState:
+    OutsideBlock, InsideCompilationBlock, StartingCfgBlock, InsideCfgBlock = range(4)
+
+  def __init__(self, outputStream):
+    # Initialize the state machine
+    self.lastMethodName = None
+    self.state = OutputFile.ParsingState.OutsideBlock
+    self.groups = self._parseStream(outputStream)
+
+  def _processLine(self, line, lineNo):
+    if self.state == OutputFile.ParsingState.StartingCfgBlock:
+      # Previous line started a new 'cfg' block which means that this one must
+      # contain the name of the pass (this is enforced by C1visualizer).
+      if re.match("name\s+\"[^\"]+\"", line):
+        # Extract the pass name, prepend it with the name of the method and
+        # return as the beginning of a new group.
+        self.state = OutputFile.ParsingState.InsideCfgBlock
+        return (None, self.lastMethodName + " " + line.split("\"")[1])
+      else:
+        raise Exception("Expected group name in output file (line " + str(lineNo) + ")")
+
+    elif self.state == OutputFile.ParsingState.InsideCfgBlock:
+      if line == "end_cfg":
+        self.state = OutputFile.ParsingState.OutsideBlock
+        return (None, None)
+      else:
+        return (line, None)
+
+    elif self.state == OutputFile.ParsingState.InsideCompilationBlock:
+      # Search for the method's name. Format: method "<name>"
+      if re.match("method\s+\"[^\"]+\"", line):
+        self.lastMethodName = line.split("\"")[1]
+      elif line == "end_compilation":
+        self.state = OutputFile.ParsingState.OutsideBlock
+      return (None, None)
+
+    else:  # self.state == OutputFile.ParsingState.OutsideBlock:
+      if line == "begin_cfg":
+        # The line starts a new group but we'll wait until the next line from
+        # which we can extract the name of the pass.
+        if self.lastMethodName is None:
+          raise Exception("Output contains a pass without a method header" +
+                          " (line " + str(lineNo) + ")")
+        self.state = OutputFile.ParsingState.StartingCfgBlock
+        return (None, None)
+      elif line == "begin_compilation":
+        self.state = OutputFile.ParsingState.InsideCompilationBlock
+        return (None, None)
+      else:
+        raise Exception("Output line lies outside a group (line " + str(lineNo) + ")")
+
+  def _processGroup(self, name, lines):
+    return OutputGroup(name, lines)
+
+  def findGroup(self, name):
+    for group in self.groups:
+      if group.name == name:
+        return group
+    return None
+
+
+def ParseArguments():
+  parser = argparse.ArgumentParser()
+  parser.add_argument("test_file", help="the source of the test with checking annotations")
+  parser.add_argument("--check-prefix", dest="check_prefix", default="CHECK", metavar="PREFIX",
+                      help="prefix of checks in the test file (default: CHECK)")
+  parser.add_argument("--list-groups", dest="list_groups", action="store_true",
+                      help="print a list of all groups found in the test output")
+  parser.add_argument("--dump-group", dest="dump_group", metavar="GROUP",
+                      help="print the contents of an output group")
+  return parser.parse_args()
+
+
+class cd:
+  """Helper class which temporarily changes the working directory."""
+
+  def __init__(self, newPath):
+    self.newPath = newPath
+
+  def __enter__(self):
+    self.savedPath = os.getcwd()
+    os.chdir(self.newPath)
+
+  def __exit__(self, etype, value, traceback):
+    os.chdir(self.savedPath)
+
+
+def CompileTest(inputFile, tempFolder):
+  classFolder = tempFolder + "/classes"
+  dexFile = tempFolder + "/test.dex"
+  oatFile = tempFolder + "/test.oat"
+  outputFile = tempFolder + "/art.cfg"
+  os.makedirs(classFolder)
+
+  # Build a DEX from the source file. We pass "--no-optimize" to dx to avoid
+  # interference with its optimizations.
+  check_call(["javac", "-d", classFolder, inputFile])
+  check_call(["dx", "--dex", "--no-optimize", "--output=" + dexFile, classFolder])
+
+  # Run dex2oat and export the HGraph. The output is stored into ${PWD}/art.cfg.
+  with cd(tempFolder):
+    check_call(["dex2oat", "-j1", "--dump-passes", "--compiler-backend=Optimizing",
+                "--android-root=" + os.environ["ANDROID_HOST_OUT"],
+                "--boot-image=" + os.environ["ANDROID_HOST_OUT"] + "/framework/core-optimizing.art",
+                "--runtime-arg", "-Xnorelocate", "--dex-file=" + dexFile, "--oat-file=" + oatFile])
+
+  return outputFile
+
+
+def ListGroups(outputFilename):
+  outputFile = OutputFile(open(outputFilename, "r"))
+  for group in outputFile.groups:
+    print(group.name)
+
+
+def DumpGroup(outputFilename, groupName):
+  outputFile = OutputFile(open(outputFilename, "r"))
+  group = outputFile.findGroup(groupName)
+  if group:
+    print("\n".join(group.body))
+  else:
+    raise Exception("Check group " + groupName + " not found in the output")
+
+
+def RunChecks(checkPrefix, checkFilename, outputFilename):
+  checkFile = CheckFile(checkPrefix, open(checkFilename, "r"))
+  outputFile = OutputFile(open(outputFilename, "r"))
+  checkFile.match(outputFile, True)
+
+
+if __name__ == "__main__":
+  args = ParseArguments()
+  tempFolder = tempfile.mkdtemp()
+
+  try:
+    outputFile = CompileTest(args.test_file, tempFolder)
+    if args.list_groups:
+      ListGroups(outputFile)
+    elif args.dump_group:
+      DumpGroup(outputFile, args.dump_group)
+    else:
+      RunChecks(args.check_prefix, args.test_file, outputFile)
+  finally:
+    shutil.rmtree(tempFolder)
diff --git a/tools/checker_test.py b/tools/checker_test.py
new file mode 100755
index 0000000..f69f9e3
--- /dev/null
+++ b/tools/checker_test.py
@@ -0,0 +1,371 @@
+#!/usr/bin/env python3
+#
+# Copyright (C) 2014 The Android Open Source Project
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# This is a test file which exercises all feautres supported by the domain-
+# specific markup language implemented by Checker.
+
+import checker
+import io
+import unittest
+
+
+class TestCheckFile_PrefixExtraction(unittest.TestCase):
+  def __tryParse(self, string):
+    checkFile = checker.CheckFile(None, [])
+    return checkFile._extractLine("CHECK", string)
+
+  def test_InvalidFormat(self):
+    self.assertIsNone(self.__tryParse("CHECK"))
+    self.assertIsNone(self.__tryParse(":CHECK"))
+    self.assertIsNone(self.__tryParse("CHECK:"))
+    self.assertIsNone(self.__tryParse("//CHECK"))
+    self.assertIsNone(self.__tryParse("#CHECK"))
+
+    self.assertIsNotNone(self.__tryParse("//CHECK:foo"))
+    self.assertIsNotNone(self.__tryParse("#CHECK:bar"))
+
+  def test_InvalidLabel(self):
+    self.assertIsNone(self.__tryParse("//ACHECK:foo"))
+    self.assertIsNone(self.__tryParse("#ACHECK:foo"))
+
+  def test_NotFirstOnTheLine(self):
+    self.assertIsNone(self.__tryParse("A// CHECK: foo"))
+    self.assertIsNone(self.__tryParse("A # CHECK: foo"))
+    self.assertIsNone(self.__tryParse("// // CHECK: foo"))
+    self.assertIsNone(self.__tryParse("# # CHECK: foo"))
+
+  def test_WhitespaceAgnostic(self):
+    self.assertIsNotNone(self.__tryParse("  //CHECK: foo"))
+    self.assertIsNotNone(self.__tryParse("//  CHECK: foo"))
+    self.assertIsNotNone(self.__tryParse("    //CHECK: foo"))
+    self.assertIsNotNone(self.__tryParse("//    CHECK: foo"))
+
+
+class TestCheckLine_Parse(unittest.TestCase):
+  def __getRegex(self, checkLine):
+    return "".join(map(lambda x: "(" + x.pattern + ")", checkLine.lineParts))
+
+  def __tryParse(self, string):
+    return checker.CheckLine(string)
+
+  def __parsesTo(self, string, expected):
+    self.assertEqual(expected, self.__getRegex(self.__tryParse(string)))
+
+  def __parsesPattern(self, string, pattern):
+    line = self.__tryParse(string)
+    self.assertEqual(1, len(line.lineParts))
+    self.assertEqual(checker.CheckElement.Variant.Pattern, line.lineParts[0].variant)
+    self.assertEqual(pattern, line.lineParts[0].pattern)
+
+  def __parsesVarRef(self, string, name):
+    line = self.__tryParse(string)
+    self.assertEqual(1, len(line.lineParts))
+    self.assertEqual(checker.CheckElement.Variant.VarRef, line.lineParts[0].variant)
+    self.assertEqual(name, line.lineParts[0].name)
+
+  def __parsesVarDef(self, string, name, body):
+    line = self.__tryParse(string)
+    self.assertEqual(1, len(line.lineParts))
+    self.assertEqual(checker.CheckElement.Variant.VarDef, line.lineParts[0].variant)
+    self.assertEqual(name, line.lineParts[0].name)
+    self.assertEqual(body, line.lineParts[0].pattern)
+
+  def __doesNotParse(self, string, partType):
+    line = self.__tryParse(string)
+    self.assertEqual(1, len(line.lineParts))
+    self.assertNotEqual(partType, line.lineParts[0].variant)
+
+  # Test that individual parts of the line are recognized
+
+  def test_TextOnly(self):
+    self.__parsesTo("foo", "(foo)")
+    self.__parsesTo("  foo  ", "(foo)")
+    self.__parsesTo("f$o^o", "(f\$o\^o)")
+
+  def test_TextWithWhitespace(self):
+    self.__parsesTo("foo bar", "(foo)(\s+)(bar)")
+    self.__parsesTo("foo   bar", "(foo)(\s+)(bar)")
+
+  def test_RegexOnly(self):
+    self.__parsesPattern("{{a?b.c}}", "a?b.c")
+
+  def test_VarRefOnly(self):
+    self.__parsesVarRef("[[ABC]]", "ABC")
+
+  def test_VarDefOnly(self):
+    self.__parsesVarDef("[[ABC:a?b.c]]", "ABC", "a?b.c")
+
+  def test_TextWithRegex(self):
+    self.__parsesTo("foo{{abc}}bar", "(foo)(abc)(bar)")
+
+  def test_TextWithVar(self):
+    self.__parsesTo("foo[[ABC:abc]]bar", "(foo)(abc)(bar)")
+
+  def test_PlainWithRegexAndWhitespaces(self):
+    self.__parsesTo("foo {{abc}}bar", "(foo)(\s+)(abc)(bar)")
+    self.__parsesTo("foo{{abc}} bar", "(foo)(abc)(\s+)(bar)")
+    self.__parsesTo("foo {{abc}} bar", "(foo)(\s+)(abc)(\s+)(bar)")
+
+  def test_PlainWithVarAndWhitespaces(self):
+    self.__parsesTo("foo [[ABC:abc]]bar", "(foo)(\s+)(abc)(bar)")
+    self.__parsesTo("foo[[ABC:abc]] bar", "(foo)(abc)(\s+)(bar)")
+    self.__parsesTo("foo [[ABC:abc]] bar", "(foo)(\s+)(abc)(\s+)(bar)")
+
+  def test_AllKinds(self):
+    self.__parsesTo("foo [[ABC:abc]]{{def}}bar", "(foo)(\s+)(abc)(def)(bar)")
+    self.__parsesTo("foo[[ABC:abc]] {{def}}bar", "(foo)(abc)(\s+)(def)(bar)")
+    self.__parsesTo("foo [[ABC:abc]] {{def}} bar", "(foo)(\s+)(abc)(\s+)(def)(\s+)(bar)")
+
+  # Test that variables and patterns are parsed correctly
+
+  def test_ValidPattern(self):
+    self.__parsesPattern("{{abc}}", "abc")
+    self.__parsesPattern("{{a[b]c}}", "a[b]c")
+    self.__parsesPattern("{{(a{bc})}}", "(a{bc})")
+
+  def test_ValidRef(self):
+    self.__parsesVarRef("[[ABC]]", "ABC")
+    self.__parsesVarRef("[[A1BC2]]", "A1BC2")
+
+  def test_ValidDef(self):
+    self.__parsesVarDef("[[ABC:abc]]", "ABC", "abc")
+    self.__parsesVarDef("[[ABC:ab:c]]", "ABC", "ab:c")
+    self.__parsesVarDef("[[ABC:a[b]c]]", "ABC", "a[b]c")
+    self.__parsesVarDef("[[ABC:(a[bc])]]", "ABC", "(a[bc])")
+
+  def test_Empty(self):
+    self.__doesNotParse("{{}}", checker.CheckElement.Variant.Pattern)
+    self.__doesNotParse("[[]]", checker.CheckElement.Variant.VarRef)
+    self.__doesNotParse("[[:]]", checker.CheckElement.Variant.VarDef)
+
+  def test_InvalidVarName(self):
+    self.__doesNotParse("[[0ABC]]", checker.CheckElement.Variant.VarRef)
+    self.__doesNotParse("[[AB=C]]", checker.CheckElement.Variant.VarRef)
+    self.__doesNotParse("[[ABC=]]", checker.CheckElement.Variant.VarRef)
+    self.__doesNotParse("[[0ABC:abc]]", checker.CheckElement.Variant.VarDef)
+    self.__doesNotParse("[[AB=C:abc]]", checker.CheckElement.Variant.VarDef)
+    self.__doesNotParse("[[ABC=:abc]]", checker.CheckElement.Variant.VarDef)
+
+  def test_BodyMatchNotGreedy(self):
+    self.__parsesTo("{{abc}}{{def}}", "(abc)(def)")
+    self.__parsesTo("[[ABC:abc]][[DEF:def]]", "(abc)(def)")
+
+
+class TestCheckLine_Match(unittest.TestCase):
+  def __matchSingle(self, checkString, outputString, varState={}):
+    checkLine = checker.CheckLine(checkString)
+    newVarState = checkLine.match(outputString, varState)
+    self.assertIsNotNone(newVarState)
+    return newVarState
+
+  def __notMatchSingle(self, checkString, outputString, varState={}):
+    checkLine = checker.CheckLine(checkString)
+    self.assertIsNone(checkLine.match(outputString, varState))
+
+  def test_TextAndWhitespace(self):
+    self.__matchSingle("foo", "foo")
+    self.__matchSingle("foo", "XfooX")
+    self.__matchSingle("foo", "foo bar")
+    self.__notMatchSingle("foo", "zoo")
+
+    self.__matchSingle("foo bar", "foo   bar")
+    self.__matchSingle("foo bar", "abc foo bar def")
+    self.__matchSingle("foo bar", "foo foo bar bar")
+    self.__notMatchSingle("foo bar", "foo abc bar")
+
+  def test_Pattern(self):
+    self.__matchSingle("foo{{A|B}}bar", "fooAbar")
+    self.__matchSingle("foo{{A|B}}bar", "fooBbar")
+    self.__notMatchSingle("foo{{A|B}}bar", "fooCbar")
+
+  def test_VariableReference(self):
+    self.__matchSingle("foo[[X]]bar", "foobar", {"X": ""})
+    self.__matchSingle("foo[[X]]bar", "fooAbar", {"X": "A"})
+    self.__matchSingle("foo[[X]]bar", "fooBbar", {"X": "B"})
+    self.__notMatchSingle("foo[[X]]bar", "foobar", {"X": "A"})
+    self.__notMatchSingle("foo[[X]]bar", "foo bar", {"X": "A"})
+    with self.assertRaises(Exception):
+      self.__matchSingle("foo[[X]]bar", "foobar", {})
+
+  def test_VariableDefinition(self):
+    self.__matchSingle("foo[[X:A|B]]bar", "fooAbar")
+    self.__matchSingle("foo[[X:A|B]]bar", "fooBbar")
+    self.__notMatchSingle("foo[[X:A|B]]bar", "fooCbar")
+
+    env = self.__matchSingle("foo[[X:A.*B]]bar", "fooABbar", {})
+    self.assertEqual(env, {"X": "AB"})
+    env = self.__matchSingle("foo[[X:A.*B]]bar", "fooAxxBbar", {})
+    self.assertEqual(env, {"X": "AxxB"})
+
+    self.__matchSingle("foo[[X:A|B]]bar[[X]]baz", "fooAbarAbaz")
+    self.__matchSingle("foo[[X:A|B]]bar[[X]]baz", "fooBbarBbaz")
+    self.__notMatchSingle("foo[[X:A|B]]bar[[X]]baz", "fooAbarBbaz")
+
+  def test_NoVariableRedefinition(self):
+    with self.assertRaises(Exception):
+      self.__matchSingle("[[X:...]][[X]][[X:...]][[X]]", "foofoobarbar")
+
+  def test_EnvNotChangedOnPartialMatch(self):
+    env = {"Y": "foo"}
+    self.__notMatchSingle("[[X:A]]bar", "Abaz", env)
+    self.assertFalse("X" in env.keys())
+
+  def test_VariableContentEscaped(self):
+    self.__matchSingle("[[X:..]]foo[[X]]", ".*foo.*")
+    self.__notMatchSingle("[[X:..]]foo[[X]]", ".*fooAAAA")
+
+
+class TestCheckGroup_Match(unittest.TestCase):
+  def __matchMulti(self, checkString, outputString):
+    checkGroup = checker.CheckGroup.parse("MyGroup", checkString.splitlines())
+    outputGroup = checker.OutputGroup("MyGroup", outputString.splitlines())
+    return checkGroup.match(outputGroup)
+
+  def __notMatchMulti(self, checkString, outputString):
+    with self.assertRaises(Exception):
+      self.__matchMulti(checkString, outputString)
+
+  def test_TextAndPattern(self):
+    self.__matchMulti("""foo bar
+                         abc {{def}}""",
+                      """foo bar
+                         abc def""");
+    self.__matchMulti("""foo bar
+                         abc {{de.}}""",
+                      """=======
+                         foo bar
+                         =======
+                         abc de#
+                         =======""");
+    self.__notMatchMulti("""//XYZ: foo bar
+                            //XYZ: abc {{def}}""",
+                         """=======
+                            foo bar
+                            =======
+                            abc de#
+                            =======""");
+
+  def test_Variables(self):
+    self.__matchMulti("""foo[[X:.]]bar
+                         abc[[X]]def""",
+                      """foo bar
+                         abc def""");
+    self.__matchMulti("""foo[[X:([0-9]+)]]bar
+                         abc[[X]]def
+                         ### [[X]] ###""",
+                      """foo1234bar
+                         abc1234def
+                         ### 1234 ###""");
+
+  def test_Ordering(self):
+    self.__matchMulti("""foo
+                         bar""",
+                      """foo
+                         bar""")
+    self.__notMatchMulti("""foo
+                            bar""",
+                         """bar
+                            foo""")
+
+class TestOutputFile_Parse(unittest.TestCase):
+  def __parsesTo(self, string, expected):
+    outputStream = io.StringIO(string)
+    return self.assertEqual(checker.OutputFile(outputStream).groups, expected)
+
+  def test_NoInput(self):
+    self.__parsesTo(None, [])
+    self.__parsesTo("", [])
+
+  def test_SingleGroup(self):
+    self.__parsesTo("""begin_compilation
+                         method "MyMethod"
+                       end_compilation
+                       begin_cfg
+                         name "pass1"
+                         foo
+                         bar
+                       end_cfg""",
+                    [ checker.OutputGroup("MyMethod pass1", [ "foo", "bar" ]) ])
+
+  def test_MultipleGroups(self):
+    self.__parsesTo("""begin_compilation
+                         name "xyz1"
+                         method "MyMethod1"
+                         date 1234
+                       end_compilation
+                       begin_cfg
+                         name "pass1"
+                         foo
+                         bar
+                       end_cfg
+                       begin_cfg
+                         name "pass2"
+                         abc
+                         def
+                       end_cfg""",
+                    [ checker.OutputGroup("MyMethod1 pass1", [ "foo", "bar" ]),
+                      checker.OutputGroup("MyMethod1 pass2", [ "abc", "def" ]) ])
+
+    self.__parsesTo("""begin_compilation
+                         name "xyz1"
+                         method "MyMethod1"
+                         date 1234
+                       end_compilation
+                       begin_cfg
+                         name "pass1"
+                         foo
+                         bar
+                       end_cfg
+                       begin_compilation
+                         name "xyz2"
+                         method "MyMethod2"
+                         date 5678
+                       end_compilation
+                       begin_cfg
+                         name "pass2"
+                         abc
+                         def
+                       end_cfg""",
+                    [ checker.OutputGroup("MyMethod1 pass1", [ "foo", "bar" ]),
+                      checker.OutputGroup("MyMethod2 pass2", [ "abc", "def" ]) ])
+
+class TestCheckFile_Parse(unittest.TestCase):
+  def __parsesTo(self, string, expected):
+    checkStream = io.StringIO(string)
+    return self.assertEqual(checker.CheckFile("CHECK", checkStream).groups, expected)
+
+  def test_NoInput(self):
+    self.__parsesTo(None, [])
+    self.__parsesTo("", [])
+
+  def test_SingleGroup(self):
+    self.__parsesTo("""// CHECK-START: Example Group
+                       // CHECK:  foo
+                       // CHECK:    bar""",
+                    [ checker.CheckGroup.parse("Example Group", [ "foo", "bar" ]) ])
+
+  def test_MultipleGroups(self):
+    self.__parsesTo("""// CHECK-START: Example Group1
+                       // CHECK: foo
+                       // CHECK: bar
+                       // CHECK-START: Example Group2
+                       // CHECK: abc
+                       // CHECK: def""",
+                    [ checker.CheckGroup.parse("Example Group1", [ "foo", "bar" ]),
+                      checker.CheckGroup.parse("Example Group2", [ "abc", "def" ]) ])
+
+if __name__ == '__main__':
+  unittest.main()
diff --git a/tools/libcore_failures.txt b/tools/libcore_failures.txt
index 6f9911d..fd347ca 100644
--- a/tools/libcore_failures.txt
+++ b/tools/libcore_failures.txt
@@ -38,5 +38,11 @@
           "org.apache.harmony.tests.java.util.ScannerTest#test_Constructor_LReadableByteChannel",
           "org.apache.harmony.tests.java.util.TimeZoneTest#test_hasSameRules_Ljava_util_TimeZone",
           "libcore.java.util.TimeZoneTest#testAllDisplayNames"]
+},
+{
+  description: "Test timeouts",
+  result: EXEC_TIMEOUT,
+  modes: [device],
+  names: ["org.apache.harmony.tests.java.util.ScannerTest#testPerformance"]
 }
 ]