Support using best-effort ART_TI_VERSION_1_2 env

This is needed since on user-debug and eng builds we can try to debug
non-debuggable processes. To support his we will try to fall-back to
the best-effort ART_TI_VERSION_1_2 envs.

Bug: 62821960
Test: Manual

Change-Id: I3012279f89f46522cfefe561333e8df333260abe
diff --git a/src/share/back/debugInit.c b/src/share/back/debugInit.c
index 043e639..bd45fdc 100644
--- a/src/share/back/debugInit.c
+++ b/src/share/back/debugInit.c
@@ -58,6 +58,10 @@
     #define DEFAULT_LOGFILE             NULL
 #endif
 
+// ANDROID-CHANGED: Special Art Version to get an ArtTiEnv. This has the same basic api as a
+// jvmtiEnv but generally has a caveat that everything is best effort.
+#define ART_TI_VERSION_1_2 (JVMTI_VERSION_1_2 | 0x40000000)
+
 static jboolean vmInitialized;
 static jrawMonitorID initMonitor;
 static jboolean initComplete;
@@ -278,11 +282,23 @@
     /* Get the JVMTI Env, IMPORTANT: Do this first! For jvmtiAllocate(). */
     error = JVM_FUNC_PTR(vm,GetEnv)
                 (vm, (void **)&(gdata->jvmti), JVMTI_VERSION_1);
+    // ANDROID-CHANGED: Check for ART_TI_VERSION_1_2 if we cannot get real JVMTI. This is done only
+    // to support the userdebug debug-anything behavior.
     if (error != JNI_OK) {
         ERROR_MESSAGE(("JDWP unable to access JVMTI Version 1 (0x%x),"
-                         " is your J2SE a 1.5 or newer version?"
+                         " retrying using ART_TI instead since this might be a userdebug device."
                          " JNIEnv's GetEnv() returned %d",
                          JVMTI_VERSION_1, error));
+        // Try to get an ArtTiEnv instead
+        error = JVM_FUNC_PTR(vm,GetEnv)
+                    (vm, (void **)&(gdata->jvmti), ART_TI_VERSION_1_2);
+    }
+    if (error != JNI_OK) {
+        ERROR_MESSAGE(("JDWP unable to access either JVMTI Version 1 (0x%x)"
+                         " or ART_TI_VERSION_1_2 (0x%x),"
+                         " is your J2SE a 1.5 or newer version?"
+                         " JNIEnv's GetEnv() returned %d",
+                         JVMTI_VERSION_1, ART_TI_VERSION_1_2, error));
         forceExit(1); /* Kill entire process, no core dump */
     }
 
diff --git a/src/share/back/util.c b/src/share/back/util.c
index eece730..ec31bfb 100644
--- a/src/share/back/util.c
+++ b/src/share/back/util.c
@@ -1779,8 +1779,10 @@
     /* Get one time use JVMTI Env */
     jvmtiCapabilities caps;
 
+    // ANDROID-CHANGED: Always get a new jvmti-env using the same version as the main env. This
+    // means that everything will still work even when using a best-effort ArtTiEnv.
     rc = JVM_FUNC_PTR(gdata->jvm,GetEnv)
-                     (gdata->jvm, (void **)&jvmti, JVMTI_VERSION_1);
+                     (gdata->jvm, (void **)&jvmti, jvmtiVersion());
     if (rc != JNI_OK) {
         return NULL;
     }