Fix a race condition in JIT state refresh under debugging / misc code cleanup.

Bug: 2561283
Change-Id: I9fd94928f3e661de97098808340ea92b28cafa07
diff --git a/vm/compiler/Compiler.c b/vm/compiler/Compiler.c
index efbe0a5..6cb1c77 100644
--- a/vm/compiler/Compiler.c
+++ b/vm/compiler/Compiler.c
@@ -385,7 +385,11 @@
     gDvmJit.jitTableEntriesUsed = 0;
     gDvmJit.compilerHighWater =
         COMPILER_WORK_QUEUE_SIZE - (COMPILER_WORK_QUEUE_SIZE/4);
-    gDvmJit.pProfTable = pJitProfTable;
+    /*
+     * If the VM is launched with wait-on-the-debugger, we will need to hide
+     * the profile table here
+     */
+    gDvmJit.pProfTable = dvmDebuggerOrProfilerActive() ? NULL : pJitProfTable;
     gDvmJit.pProfTableCopy = pJitProfTable;
     dvmUnlockMutex(&gDvmJit.tableLock);
 
@@ -708,6 +712,16 @@
     bool jitActivate;
     bool needUnchain = false;
 
+    /*
+     * The tableLock might not be initialized yet by the compiler thread if
+     * debugger is attached from the very beginning of the VM launch. If
+     * pProfTableCopy is NULL, the lock is not initialized yet and we don't
+     * need to refresh anything either.
+     */
+    if (gDvmJit.pProfTableCopy == NULL) {
+        return;
+    }
+
     dvmLockMutex(&gDvmJit.tableLock);
     jitActive = gDvmJit.pProfTable != NULL;
     jitActivate = !(gDvm.debuggerActive || (gDvm.activeProfilers > 0));