Add support for JVMTI monitor events.

Adds support for the JVMTI can_generate_monitor_events capability and
all associated events. This adds support for the
JVMTI_EVENT_MONITOR_WAIT, JVMTI_EVENT_MONITOR_WAITED,
JVMTI_EVENT_MONITOR_CONTENDED_ENTER, and
JVMTI_EVENT_MONITOR_CONTENDED_ENTERED events.

Bug: 65558434
Bug: 62821960
Bug: 34415266

Test: ./test.py --host -j50

Change-Id: I0fe8038e6c4249e77d37a67e5056b5d2a94b6f48
diff --git a/runtime/entrypoints/quick/quick_lock_entrypoints.cc b/runtime/entrypoints/quick/quick_lock_entrypoints.cc
index b4f945a..4bcce21 100644
--- a/runtime/entrypoints/quick/quick_lock_entrypoints.cc
+++ b/runtime/entrypoints/quick/quick_lock_entrypoints.cc
@@ -29,15 +29,22 @@
     ThrowNullPointerException("Null reference used for synchronization (monitor-enter)");
     return -1;  // Failure.
   } else {
-    if (kIsDebugBuild) {
-      obj = obj->MonitorEnter(self);  // May block
-      CHECK(self->HoldsLock(obj));
-      CHECK(!self->IsExceptionPending());
+    obj = obj->MonitorEnter(self);  // May block
+    DCHECK(self->HoldsLock(obj));
+    // Exceptions can be thrown by monitor event listeners. This is expected to be rare however.
+    if (UNLIKELY(self->IsExceptionPending())) {
+      // TODO Remove this DCHECK if we expand the use of monitor callbacks.
+      DCHECK(Runtime::Current()->HasLoadedPlugins())
+          << "Exceptions are only expected to be thrown by plugin code which doesn't seem to be "
+          << "loaded.";
+      // We need to get rid of the lock
+      bool unlocked = obj->MonitorExit(self);
+      DCHECK(unlocked);
+      return -1;  // Failure.
     } else {
-      obj->MonitorEnter(self);  // May block
+      DCHECK(self->HoldsLock(obj));
+      return 0;  // Success.
     }
-    return 0;  // Success.
-    // Only possible exception is NPE and is handled before entry
   }
 }