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));
diff --git a/vm/compiler/Compiler.h b/vm/compiler/Compiler.h
index 5b66735..ba23d7d 100644
--- a/vm/compiler/Compiler.h
+++ b/vm/compiler/Compiler.h
@@ -79,17 +79,20 @@
     PredictedChainingCell cellContent;  /* content of the new cell */
 } ICPatchWorkOrder;
 
+/* States of the dbg interpreter when serving a JIT-related request */
 typedef enum JitState {
-    kJitOff = 0,
-    kJitNormal = 1,            // Profiling in mterp or running native
-    kJitTSelectRequest = 2,    // Transition state - start trace selection
-    kJitTSelectRequestHot = 3, // Transition state - start hot trace selection
-    kJitTSelect = 4,           // Actively selecting trace in dbg interp
-    kJitTSelectAbort = 5,      // Something threw during selection - abort
-    kJitTSelectEnd = 6,        // Done with the trace - wrap it up
-    kJitSingleStep = 7,        // Single step interpretation
-    kJitSingleStepEnd = 8,     // Done with single step, return to mterp
-    kJitSelfVerification = 9,  // Self Verification Mode
+    /* Entering states in the debug interpreter */
+    kJitNot = 0,               // Non-JIT related reasons */
+    kJitTSelectRequest = 1,    // Request a trace (subject to filtering)
+    kJitTSelectRequestHot = 2, // Request a hot trace (bypass the filter)
+    kJitSelfVerification = 3,  // Self Verification Mode
+
+    /* Operational states in the debug interpreter */
+    kJitTSelect = 4,           // Actively selecting a trace
+    kJitTSelectEnd = 5,        // Done with the trace - wrap it up
+    kJitSingleStep = 6,        // Single step interpretation
+    kJitSingleStepEnd = 7,     // Done with single step, ready return to mterp
+    kJitDone = 8,              // Ready to leave the debug interpreter
 } JitState;
 
 #if defined(WITH_SELF_VERIFICATION)
diff --git a/vm/compiler/Utility.c b/vm/compiler/Utility.c
index 83caab7..b2654c4 100644
--- a/vm/compiler/Utility.c
+++ b/vm/compiler/Utility.c
@@ -76,7 +76,7 @@
         currentArena = newArena;
         numArenaBlocks++;
         if (numArenaBlocks > 10)
-            LOGD("Total arena pages for JIT: %d", numArenaBlocks);
+            LOGI("Total arena pages for JIT: %d", numArenaBlocks);
         goto retry;
     }
     return NULL;
diff --git a/vm/compiler/codegen/arm/ArchUtility.c b/vm/compiler/codegen/arm/ArchUtility.c
index 20b3f85..b0478f4 100644
--- a/vm/compiler/codegen/arm/ArchUtility.c
+++ b/vm/compiler/codegen/arm/ArchUtility.c
@@ -257,41 +257,41 @@
             break;
         case kArmPseudoTargetLabel:
             break;
-        case ARM_PSEUDO_kChainingCellBackwardBranch:
+        case kArmPseudoChainingCellBackwardBranch:
             LOGD("-------- chaining cell (backward branch): 0x%04x\n", dest);
             break;
-        case ARM_PSEUDO_kChainingCellNormal:
+        case kArmPseudoChainingCellNormal:
             LOGD("-------- chaining cell (normal): 0x%04x\n", dest);
             break;
-        case ARM_PSEUDO_kChainingCellHot:
+        case kArmPseudoChainingCellHot:
             LOGD("-------- chaining cell (hot): 0x%04x\n", dest);
             break;
-        case ARM_PSEUDO_kChainingCellInvokePredicted:
+        case kArmPseudoChainingCellInvokePredicted:
             LOGD("-------- chaining cell (predicted)\n");
             break;
-        case ARM_PSEUDO_kChainingCellInvokeSingleton:
+        case kArmPseudoChainingCellInvokeSingleton:
             LOGD("-------- chaining cell (invoke singleton): %s/%p\n",
                  ((Method *)dest)->name,
                  ((Method *)dest)->insns);
             break;
-        case ARM_PSEUDO_kEntryBlock:
+        case kArmPseudoEntryBlock:
             LOGD("-------- entry offset: 0x%04x\n", dest);
             break;
-        case ARM_PSEUDO_kDalvikByteCode_BOUNDARY:
+        case kArmPseudoDalvikByteCodeBoundary:
             LOGD("-------- dalvik offset: 0x%04x @ %s\n", dest,
                  (char *) lir->operands[1]);
             break;
-        case ARM_PSEUDO_kExitBlock:
+        case kArmPseudoExitBlock:
             LOGD("-------- exit offset: 0x%04x\n", dest);
             break;
         case kArmPseudoPseudoAlign4:
             LOGD("%p (%04x): .align4\n", baseAddr + offset, offset);
             break;
-        case ARM_PSEUDO_kPCReconstruction_CELL:
+        case kArmPseudoPCReconstructionCell:
             LOGD("-------- reconstruct dalvik PC : 0x%04x @ +0x%04x\n", dest,
                  lir->operands[1]);
             break;
-        case ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL:
+        case kArmPseudoPCReconstructionBlockLabel:
             /* Do nothing */
             break;
         case kArmPseudoEHBlockLabel:
diff --git a/vm/compiler/codegen/arm/ArmLIR.h b/vm/compiler/codegen/arm/ArmLIR.h
index e1073b6..f7704ad 100644
--- a/vm/compiler/codegen/arm/ArmLIR.h
+++ b/vm/compiler/codegen/arm/ArmLIR.h
@@ -318,18 +318,18 @@
     kArmPseudoBarrier = -17,
     kArmPseudoExtended = -16,
     kArmPseudoSSARep = -15,
-    ARM_PSEUDO_kEntryBlock = -14,
-    ARM_PSEUDO_kExitBlock = -13,
+    kArmPseudoEntryBlock = -14,
+    kArmPseudoExitBlock = -13,
     kArmPseudoTargetLabel = -12,
-    ARM_PSEUDO_kChainingCellBackwardBranch = -11,
-    ARM_PSEUDO_kChainingCellHot = -10,
-    ARM_PSEUDO_kChainingCellInvokePredicted = -9,
-    ARM_PSEUDO_kChainingCellInvokeSingleton = -8,
-    ARM_PSEUDO_kChainingCellNormal = -7,
-    ARM_PSEUDO_kDalvikByteCode_BOUNDARY = -6,
+    kArmPseudoChainingCellBackwardBranch = -11,
+    kArmPseudoChainingCellHot = -10,
+    kArmPseudoChainingCellInvokePredicted = -9,
+    kArmPseudoChainingCellInvokeSingleton = -8,
+    kArmPseudoChainingCellNormal = -7,
+    kArmPseudoDalvikByteCodeBoundary = -6,
     kArmPseudoPseudoAlign4 = -5,
-    ARM_PSEUDO_kPCReconstruction_CELL = -4,
-    ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL = -3,
+    kArmPseudoPCReconstructionCell = -4,
+    kArmPseudoPCReconstructionBlockLabel = -3,
     kArmPseudoEHBlockLabel = -2,
     kArmPseudoNormalBlockLabel = -1,
     /************************************************************************/
diff --git a/vm/compiler/codegen/arm/CodegenCommon.c b/vm/compiler/codegen/arm/CodegenCommon.c
index 8f5c11d..0cec99d 100644
--- a/vm/compiler/codegen/arm/CodegenCommon.c
+++ b/vm/compiler/codegen/arm/CodegenCommon.c
@@ -370,7 +370,7 @@
     if (pcrLabel == NULL) {
         int dPC = (int) (cUnit->method->insns + dOffset);
         pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
-        pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
+        pcrLabel->opCode = kArmPseudoPCReconstructionCell;
         pcrLabel->operands[0] = dPC;
         pcrLabel->operands[1] = dOffset;
         /* Insert the place holder to the growable list */
diff --git a/vm/compiler/codegen/arm/CodegenDriver.c b/vm/compiler/codegen/arm/CodegenDriver.c
index e3ac5bb..695f18c 100644
--- a/vm/compiler/codegen/arm/CodegenDriver.c
+++ b/vm/compiler/codegen/arm/CodegenDriver.c
@@ -911,7 +911,7 @@
     ArmLIR *branch = genUnconditionalBranch(cUnit, NULL);
     /* Set up the place holder to reconstruct this Dalvik PC */
     ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
-    pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
+    pcrLabel->opCode = kArmPseudoPCReconstructionCell;
     pcrLabel->operands[0] = dPC;
     pcrLabel->operands[1] = mir->offset;
     /* Insert the place holder to the growable list */
@@ -1148,7 +1148,7 @@
     if (pcrLabel == NULL) {
         int dPC = (int) (cUnit->method->insns + mir->offset);
         pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
-        pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
+        pcrLabel->opCode = kArmPseudoPCReconstructionCell;
         pcrLabel->operands[0] = dPC;
         pcrLabel->operands[1] = mir->offset;
         /* Insert the place holder to the growable list */
@@ -2811,7 +2811,7 @@
             if (pcrLabel == NULL) {
                 int dPC = (int) (cUnit->method->insns + mir->offset);
                 pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
-                pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
+                pcrLabel->opCode = kArmPseudoPCReconstructionCell;
                 pcrLabel->operands[0] = dPC;
                 pcrLabel->operands[1] = mir->offset;
                 /* Insert the place holder to the growable list */
@@ -3487,7 +3487,7 @@
 {
     /* Set up the place holder to reconstruct this Dalvik PC */
     ArmLIR *pcrLabel = dvmCompilerNew(sizeof(ArmLIR), true);
-    pcrLabel->opCode = ARM_PSEUDO_kPCReconstruction_CELL;
+    pcrLabel->opCode = kArmPseudoPCReconstructionCell;
     pcrLabel->operands[0] =
         (int) (cUnit->method->insns + entry->startOffset);
     pcrLabel->operands[1] = entry->startOffset;
@@ -3598,7 +3598,7 @@
         }
 
         if (blockList[i]->blockType == kEntryBlock) {
-            labelList[i].opCode = ARM_PSEUDO_kEntryBlock;
+            labelList[i].opCode = kArmPseudoEntryBlock;
             if (blockList[i]->firstMIRInsn == NULL) {
                 continue;
             } else {
@@ -3606,7 +3606,7 @@
                                   &labelList[blockList[i]->fallThrough->id]);
             }
         } else if (blockList[i]->blockType == kExitBlock) {
-            labelList[i].opCode = ARM_PSEUDO_kExitBlock;
+            labelList[i].opCode = kArmPseudoExitBlock;
             goto gen_fallthrough;
         } else if (blockList[i]->blockType == kDalvikByteCode) {
             labelList[i].opCode = kArmPseudoNormalBlockLabel;
@@ -3617,14 +3617,14 @@
         } else {
             switch (blockList[i]->blockType) {
                 case kChainingCellNormal:
-                    labelList[i].opCode = ARM_PSEUDO_kChainingCellNormal;
+                    labelList[i].opCode = kArmPseudoChainingCellNormal;
                     /* handle the codegen later */
                     dvmInsertGrowableList(
                         &chainingListByType[kChainingCellNormal], (void *) i);
                     break;
                 case kChainingCellInvokeSingleton:
                     labelList[i].opCode =
-                        ARM_PSEUDO_kChainingCellInvokeSingleton;
+                        kArmPseudoChainingCellInvokeSingleton;
                     labelList[i].operands[0] =
                         (int) blockList[i]->containingMethod;
                     /* handle the codegen later */
@@ -3634,7 +3634,7 @@
                     break;
                 case kChainingCellInvokePredicted:
                     labelList[i].opCode =
-                        ARM_PSEUDO_kChainingCellInvokePredicted;
+                        kArmPseudoChainingCellInvokePredicted;
                     /* handle the codegen later */
                     dvmInsertGrowableList(
                         &chainingListByType[kChainingCellInvokePredicted],
@@ -3642,7 +3642,7 @@
                     break;
                 case kChainingCellHot:
                     labelList[i].opCode =
-                        ARM_PSEUDO_kChainingCellHot;
+                        kArmPseudoChainingCellHot;
                     /* handle the codegen later */
                     dvmInsertGrowableList(
                         &chainingListByType[kChainingCellHot],
@@ -3651,7 +3651,7 @@
                 case kPCReconstruction:
                     /* Make sure exception handling block is next */
                     labelList[i].opCode =
-                        ARM_PSEUDO_kPCReconstruction_BLOCK_LABEL;
+                        kArmPseudoPCReconstructionBlockLabel;
                     assert (i == cUnit->numBlocks - 2);
                     handlePCReconstruction(cUnit, &labelList[i+1]);
                     break;
@@ -3667,7 +3667,7 @@
 #if defined(WITH_SELF_VERIFICATION) || defined(WITH_JIT_TUNING)
                 case kChainingCellBackwardBranch:
                     labelList[i].opCode =
-                        ARM_PSEUDO_kChainingCellBackwardBranch;
+                        kArmPseudoChainingCellBackwardBranch;
                     /* handle the codegen later */
                     dvmInsertGrowableList(
                         &chainingListByType[kChainingCellBackwardBranch],
@@ -3703,7 +3703,7 @@
             InstructionFormat dalvikFormat =
                 dexGetInstrFormat(gDvm.instrFormat, dalvikOpCode);
             ArmLIR *boundaryLIR =
-                newLIR2(cUnit, ARM_PSEUDO_kDalvikByteCode_BOUNDARY,
+                newLIR2(cUnit, kArmPseudoDalvikByteCodeBoundary,
                         mir->offset,
                         (int) dvmCompilerGetDalvikDisassembly(&mir->dalvikInsn)
                        );