Use static thread safety analysis when available, and fix the bugs GCC finds.

It's impossible to express the Heap locking and the ThreadList locking with
GCC, but Clang is supposed to be able to do it. This patch does what's possible
for now.

Change-Id: Ib64a890c9d27c6ce255d5003cb755c2ef1beba95
diff --git a/src/jdwp/jdwp.h b/src/jdwp/jdwp.h
index 7d8bf22..4e9b0a0 100644
--- a/src/jdwp/jdwp.h
+++ b/src/jdwp/jdwp.h
@@ -118,7 +118,7 @@
    *
    * Returns a newly-allocated JdwpState struct on success, or NULL on failure.
    */
-  static JdwpState* Create(const JdwpOptions* options);
+  static JdwpState* Create(const JdwpOptions* options) NO_THREAD_SAFETY_ANALYSIS; // TODO: make GCC understand.
 
   ~JdwpState();
 
@@ -264,11 +264,15 @@
   explicit JdwpState(const JdwpOptions* options);
   bool InvokeInProgress();
   bool IsConnected();
-  void SuspendByPolicy(JdwpSuspendPolicy suspendPolicy);
-  void CleanupMatchList(JdwpEvent** matchList, int matchCount);
+  void SuspendByPolicy(JdwpSuspendPolicy suspend_policy);
+  void CleanupMatchList(JdwpEvent** match_list,
+                        int match_count) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
   void EventFinish(ExpandBuf* pReq);
-  void FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount);
-  void UnregisterEvent(JdwpEvent* pEvent);
+  void FindMatchingEvents(JdwpEventKind eventKind,
+                          ModBasket* basket,
+                          JdwpEvent** match_list,
+                          int* pMatchCount) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
+  void UnregisterEvent(JdwpEvent* pEvent) EXCLUSIVE_LOCKS_REQUIRED(event_list_lock_);
 
 public: // TODO: fix privacy
   const JdwpOptions* options_;
@@ -299,17 +303,17 @@
   int64_t lastActivityWhen;
 
   /* global counters and a mutex to protect them */
-  uint32_t requestSerial;
-  uint32_t eventSerial;
   Mutex serial_lock_;
+  uint32_t request_serial_ GUARDED_BY(serial_lock_);
+  uint32_t event_serial_ GUARDED_BY(serial_lock_);
 
   /*
    * Events requested by the debugger (breakpoints, class prep, etc).
    */
 public: // TODO: fix privacy
-  int numEvents;      /* #of elements in eventList */
-  JdwpEvent* eventList;      /* linked list of events */
-  Mutex event_lock_;      /* guards numEvents/eventList */
+  Mutex event_list_lock_;
+  JdwpEvent* event_list_ GUARDED_BY(event_list_lock_); // Linked list of events.
+  int event_list_size_ GUARDED_BY(event_list_lock_); // Number of elements in event_list_.
 private:
 
   /*
diff --git a/src/jdwp/jdwp_event.cc b/src/jdwp/jdwp_event.cc
index 6476c3c..a92590d 100644
--- a/src/jdwp/jdwp_event.cc
+++ b/src/jdwp/jdwp_event.cc
@@ -124,7 +124,7 @@
  */
 static void dumpEvent(const JdwpEvent* pEvent) {
   LOG(INFO) << StringPrintf("Event id=0x%4x %p (prev=%p next=%p):", pEvent->requestId, pEvent, pEvent->prev, pEvent->next);
-  LOG(INFO) << "  kind=" << pEvent->eventKind << " susp=" << pEvent->suspendPolicy << " modCount=" << pEvent->modCount;
+  LOG(INFO) << "  kind=" << pEvent->eventKind << " susp=" << pEvent->suspend_policy << " modCount=" << pEvent->modCount;
 
   for (int i = 0; i < pEvent->modCount; i++) {
     const JdwpEventMod* pMod = &pEvent->mods[i];
@@ -141,7 +141,7 @@
  * not be added to the list, and an appropriate error will be returned.
  */
 JdwpError JdwpState::RegisterEvent(JdwpEvent* pEvent) {
-  MutexLock mu(event_lock_);
+  MutexLock mu(event_list_lock_);
 
   CHECK(pEvent != NULL);
   CHECK(pEvent->prev == NULL);
@@ -173,12 +173,12 @@
   /*
    * Add to list.
    */
-  if (eventList != NULL) {
-    pEvent->next = eventList;
-    eventList->prev = pEvent;
+  if (event_list_ != NULL) {
+    pEvent->next = event_list_;
+    event_list_->prev = pEvent;
   }
-  eventList = pEvent;
-  numEvents++;
+  event_list_ = pEvent;
+  ++event_list_size_;
 
   return ERR_NONE;
 }
@@ -194,9 +194,9 @@
 void JdwpState::UnregisterEvent(JdwpEvent* pEvent) {
   if (pEvent->prev == NULL) {
     /* head of the list */
-    CHECK(eventList == pEvent);
+    CHECK(event_list_ == pEvent);
 
-    eventList = pEvent->next;
+    event_list_ = pEvent->next;
   } else {
     pEvent->prev->next = pEvent->next;
   }
@@ -222,8 +222,8 @@
     }
   }
 
-  numEvents--;
-  CHECK(numEvents != 0 || eventList == NULL);
+  --event_list_size_;
+  CHECK(event_list_size_ != 0 || event_list_ == NULL);
 }
 
 /*
@@ -234,9 +234,9 @@
  * explicitly remove one-off single-step events.)
  */
 void JdwpState::UnregisterEventById(uint32_t requestId) {
-  MutexLock mu(event_lock_);
+  MutexLock mu(event_list_lock_);
 
-  JdwpEvent* pEvent = eventList;
+  JdwpEvent* pEvent = event_list_;
   while (pEvent != NULL) {
     if (pEvent->requestId == requestId) {
       UnregisterEvent(pEvent);
@@ -254,9 +254,9 @@
  * Remove all entries from the event list.
  */
 void JdwpState::UnregisterAll() {
-  MutexLock mu(event_lock_);
+  MutexLock mu(event_list_lock_);
 
-  JdwpEvent* pEvent = eventList;
+  JdwpEvent* pEvent = event_list_;
   while (pEvent != NULL) {
     JdwpEvent* pNextEvent = pEvent->next;
 
@@ -265,7 +265,7 @@
     pEvent = pNextEvent;
   }
 
-  eventList = NULL;
+  event_list_ = NULL;
 }
 
 /*
@@ -293,7 +293,7 @@
   /* make sure it was removed from the list */
   CHECK(pEvent->prev == NULL);
   CHECK(pEvent->next == NULL);
-  /* want to check state->eventList != pEvent */
+  /* want to check state->event_list_ != pEvent */
 
   /*
    * Free any hairy bits in the mods.
@@ -326,8 +326,8 @@
  * Run through the list and remove any entries with an expired "count" mod
  * from the event list, then free the match list.
  */
-void JdwpState::CleanupMatchList(JdwpEvent** matchList, int match_count) {
-  JdwpEvent** ppEvent = matchList;
+void JdwpState::CleanupMatchList(JdwpEvent** match_list, int match_count) {
+  JdwpEvent** ppEvent = match_list;
 
   while (match_count--) {
     JdwpEvent* pEvent = *ppEvent;
@@ -344,7 +344,7 @@
     ppEvent++;
   }
 
-  delete[] matchList;
+  delete[] match_list;
 }
 
 /*
@@ -446,20 +446,20 @@
  * Find all events of type "eventKind" with mods that match up with the
  * rest of the arguments.
  *
- * Found events are appended to "matchList", and "*pMatchCount" is advanced,
+ * Found events are appended to "match_list", and "*pMatchCount" is advanced,
  * so this may be called multiple times for grouped events.
  *
  * DO NOT call this multiple times for the same eventKind, as Count mods are
  * decremented during the scan.
  */
-void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** matchList, int* pMatchCount) {
+void JdwpState::FindMatchingEvents(JdwpEventKind eventKind, ModBasket* basket, JdwpEvent** match_list, int* pMatchCount) {
   /* start after the existing entries */
-  matchList += *pMatchCount;
+  match_list += *pMatchCount;
 
-  JdwpEvent* pEvent = eventList;
+  JdwpEvent* pEvent = event_list_;
   while (pEvent != NULL) {
     if (pEvent->eventKind == eventKind && ModsMatch(pEvent, basket)) {
-      *matchList++ = pEvent;
+      *match_list++ = pEvent;
       (*pMatchCount)++;
     }
 
@@ -471,14 +471,14 @@
  * Scan through the list of matches and determine the most severe
  * suspension policy.
  */
-static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** matchList, int match_count) {
+static JdwpSuspendPolicy scanSuspendPolicy(JdwpEvent** match_list, int match_count) {
   JdwpSuspendPolicy policy = SP_NONE;
 
   while (match_count--) {
-    if ((*matchList)->suspendPolicy > policy) {
-      policy = (*matchList)->suspendPolicy;
+    if ((*match_list)->suspend_policy > policy) {
+      policy = (*match_list)->suspend_policy;
     }
-    matchList++;
+    match_list++;
   }
 
   return policy;
@@ -490,16 +490,16 @@
  *  SP_EVENT_THREAD - suspend ourselves
  *  SP_ALL - suspend everybody except JDWP support thread
  */
-void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspendPolicy) {
-  VLOG(jdwp) << "SuspendByPolicy(" << suspendPolicy << ")";
-  if (suspendPolicy == SP_NONE) {
+void JdwpState::SuspendByPolicy(JdwpSuspendPolicy suspend_policy) {
+  VLOG(jdwp) << "SuspendByPolicy(" << suspend_policy << ")";
+  if (suspend_policy == SP_NONE) {
     return;
   }
 
-  if (suspendPolicy == SP_ALL) {
+  if (suspend_policy == SP_ALL) {
     Dbg::SuspendVM();
   } else {
-    CHECK_EQ(suspendPolicy, SP_EVENT_THREAD);
+    CHECK_EQ(suspend_policy, SP_EVENT_THREAD);
   }
 
   /* this is rare but possible -- see CLASS_PREPARE handling */
@@ -645,23 +645,23 @@
  * must be for the main thread.
  */
 bool JdwpState::PostVMStart() {
-  JdwpSuspendPolicy suspendPolicy;
+  JdwpSuspendPolicy suspend_policy;
   ObjectId threadId = Dbg::GetThreadSelfId();
 
   if (options_->suspend) {
-    suspendPolicy = SP_ALL;
+    suspend_policy = SP_ALL;
   } else {
-    suspendPolicy = SP_NONE;
+    suspend_policy = SP_NONE;
   }
 
   ExpandBuf* pReq = eventPrep();
   {
-    MutexLock mu(event_lock_); // probably don't need this here
+    MutexLock mu(event_list_lock_); // probably don't need this here
 
     VLOG(jdwp) << "EVENT: " << EK_VM_START;
-    VLOG(jdwp) << "  suspendPolicy=" << suspendPolicy;
+    VLOG(jdwp) << "  suspend_policy=" << suspend_policy;
 
-    expandBufAdd1(pReq, suspendPolicy);
+    expandBufAdd1(pReq, suspend_policy);
     expandBufAdd4BE(pReq, 1);
 
     expandBufAdd1(pReq, EK_VM_START);
@@ -672,13 +672,13 @@
   /* send request and possibly suspend ourselves */
   if (pReq != NULL) {
     int old_state = Dbg::ThreadWaiting();
-    if (suspendPolicy != SP_NONE) {
+    if (suspend_policy != SP_NONE) {
       SetWaitForEventThread(threadId);
     }
 
     EventFinish(pReq);
 
-    SuspendByPolicy(suspendPolicy);
+    SuspendByPolicy(suspend_policy);
     Dbg::ThreadContinuing(old_state);
   }
 
@@ -741,61 +741,62 @@
     return false;
   }
 
-  JdwpEvent** matchList = AllocMatchList(numEvents);
+  JdwpEvent** match_list = NULL;
   int match_count = 0;
   ExpandBuf* pReq = NULL;
-  JdwpSuspendPolicy suspendPolicy = SP_NONE;
+  JdwpSuspendPolicy suspend_policy = SP_NONE;
 
   {
-    MutexLock mu(event_lock_);
+    MutexLock mu(event_list_lock_);
+    match_list = AllocMatchList(event_list_size_);
     if ((eventFlags & Dbg::kBreakpoint) != 0) {
-      FindMatchingEvents(EK_BREAKPOINT, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_BREAKPOINT, &basket, match_list, &match_count);
     }
     if ((eventFlags & Dbg::kSingleStep) != 0) {
-      FindMatchingEvents(EK_SINGLE_STEP, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_SINGLE_STEP, &basket, match_list, &match_count);
     }
     if ((eventFlags & Dbg::kMethodEntry) != 0) {
-      FindMatchingEvents(EK_METHOD_ENTRY, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_METHOD_ENTRY, &basket, match_list, &match_count);
     }
     if ((eventFlags & Dbg::kMethodExit) != 0) {
-      FindMatchingEvents(EK_METHOD_EXIT, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_METHOD_EXIT, &basket, match_list, &match_count);
 
       // TODO: match EK_METHOD_EXIT_WITH_RETURN_VALUE too; we need to include the 'value', though.
-      //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, matchList, &match_count);
+      //FindMatchingEvents(EK_METHOD_EXIT_WITH_RETURN_VALUE, &basket, match_list, &match_count);
     }
     if (match_count != 0) {
-      VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << match_count << " total) "
+      VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
                  << basket.className << "." << Dbg::GetMethodName(pLoc->classId, pLoc->methodId)
                  << StringPrintf(" thread=%#llx dex_pc=%#llx)", basket.threadId, pLoc->dex_pc);
 
-      suspendPolicy = scanSuspendPolicy(matchList, match_count);
-      VLOG(jdwp) << "  suspendPolicy=" << suspendPolicy;
+      suspend_policy = scanSuspendPolicy(match_list, match_count);
+      VLOG(jdwp) << "  suspend_policy=" << suspend_policy;
 
       pReq = eventPrep();
-      expandBufAdd1(pReq, suspendPolicy);
+      expandBufAdd1(pReq, suspend_policy);
       expandBufAdd4BE(pReq, match_count);
 
       for (int i = 0; i < match_count; i++) {
-        expandBufAdd1(pReq, matchList[i]->eventKind);
-        expandBufAdd4BE(pReq, matchList[i]->requestId);
+        expandBufAdd1(pReq, match_list[i]->eventKind);
+        expandBufAdd4BE(pReq, match_list[i]->requestId);
         expandBufAdd8BE(pReq, basket.threadId);
         AddLocation(pReq, pLoc);
       }
     }
 
-    CleanupMatchList(matchList, match_count);
+    CleanupMatchList(match_list, match_count);
   }
 
   /* send request and possibly suspend ourselves */
   if (pReq != NULL) {
     int old_state = Dbg::ThreadWaiting();
-    if (suspendPolicy != SP_NONE) {
+    if (suspend_policy != SP_NONE) {
       SetWaitForEventThread(basket.threadId);
     }
 
     EventFinish(pReq);
 
-    SuspendByPolicy(suspendPolicy);
+    SuspendByPolicy(suspend_policy);
     Dbg::ThreadContinuing(old_state);
   }
 
@@ -824,49 +825,49 @@
   basket.threadId = threadId;
 
   ExpandBuf* pReq = NULL;
-  JdwpSuspendPolicy suspendPolicy = SP_NONE;
+  JdwpSuspendPolicy suspend_policy = SP_NONE;
   int match_count = 0;
   {
     // Don't allow the list to be updated while we scan it.
-    MutexLock mu(event_lock_);
-    JdwpEvent** matchList = AllocMatchList(numEvents);
+    MutexLock mu(event_list_lock_);
+    JdwpEvent** match_list = AllocMatchList(event_list_size_);
 
     if (start) {
-      FindMatchingEvents(EK_THREAD_START, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_THREAD_START, &basket, match_list, &match_count);
     } else {
-      FindMatchingEvents(EK_THREAD_DEATH, &basket, matchList, &match_count);
+      FindMatchingEvents(EK_THREAD_DEATH, &basket, match_list, &match_count);
     }
 
     if (match_count != 0) {
-      VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << match_count << " total) "
+      VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
                  << StringPrintf("thread=%#llx", basket.threadId) << ")";
 
-      suspendPolicy = scanSuspendPolicy(matchList, match_count);
-      VLOG(jdwp) << "  suspendPolicy=" << suspendPolicy;
+      suspend_policy = scanSuspendPolicy(match_list, match_count);
+      VLOG(jdwp) << "  suspend_policy=" << suspend_policy;
 
       pReq = eventPrep();
-      expandBufAdd1(pReq, suspendPolicy);
+      expandBufAdd1(pReq, suspend_policy);
       expandBufAdd4BE(pReq, match_count);
 
       for (int i = 0; i < match_count; i++) {
-        expandBufAdd1(pReq, matchList[i]->eventKind);
-        expandBufAdd4BE(pReq, matchList[i]->requestId);
+        expandBufAdd1(pReq, match_list[i]->eventKind);
+        expandBufAdd4BE(pReq, match_list[i]->requestId);
         expandBufAdd8BE(pReq, basket.threadId);
       }
     }
 
-    CleanupMatchList(matchList, match_count);
+    CleanupMatchList(match_list, match_count);
   }
 
   /* send request and possibly suspend ourselves */
   if (pReq != NULL) {
     int old_state = Dbg::ThreadWaiting();
-    if (suspendPolicy != SP_NONE) {
+    if (suspend_policy != SP_NONE) {
       SetWaitForEventThread(basket.threadId);
     }
     EventFinish(pReq);
 
-    SuspendByPolicy(suspendPolicy);
+    SuspendByPolicy(suspend_policy);
     Dbg::ThreadContinuing(old_state);
   }
 
@@ -923,15 +924,16 @@
     return false;
   }
 
-  JdwpEvent** matchList = AllocMatchList(numEvents);
+  JdwpEvent** match_list = NULL;
   int match_count = 0;
   ExpandBuf* pReq = NULL;
-  JdwpSuspendPolicy suspendPolicy = SP_NONE;
+  JdwpSuspendPolicy suspend_policy = SP_NONE;
   {
-    MutexLock mu(event_lock_);
-    FindMatchingEvents(EK_EXCEPTION, &basket, matchList, &match_count);
+    MutexLock mu(event_list_lock_);
+    match_list = AllocMatchList(event_list_size_);
+    FindMatchingEvents(EK_EXCEPTION, &basket, match_list, &match_count);
     if (match_count != 0) {
-      VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << match_count << " total)"
+      VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total)"
                  << StringPrintf(" thread=%#llx", basket.threadId)
                  << StringPrintf(" exceptId=%#llx", exceptionId)
                  << " caught=" << basket.caught << ")"
@@ -942,16 +944,16 @@
         VLOG(jdwp) << "  catch: " << *pCatchLoc;
       }
 
-      suspendPolicy = scanSuspendPolicy(matchList, match_count);
-      VLOG(jdwp) << "  suspendPolicy=" << suspendPolicy;
+      suspend_policy = scanSuspendPolicy(match_list, match_count);
+      VLOG(jdwp) << "  suspend_policy=" << suspend_policy;
 
       pReq = eventPrep();
-      expandBufAdd1(pReq, suspendPolicy);
+      expandBufAdd1(pReq, suspend_policy);
       expandBufAdd4BE(pReq, match_count);
 
       for (int i = 0; i < match_count; i++) {
-        expandBufAdd1(pReq, matchList[i]->eventKind);
-        expandBufAdd4BE(pReq, matchList[i]->requestId);
+        expandBufAdd1(pReq, match_list[i]->eventKind);
+        expandBufAdd4BE(pReq, match_list[i]->requestId);
         expandBufAdd8BE(pReq, basket.threadId);
 
         AddLocation(pReq, pThrowLoc);
@@ -964,19 +966,19 @@
       Dbg::RegisterObjectId(exceptionId);
     }
 
-    CleanupMatchList(matchList, match_count);
+    CleanupMatchList(match_list, match_count);
   }
 
   /* send request and possibly suspend ourselves */
   if (pReq != NULL) {
     int old_state = Dbg::ThreadWaiting();
-    if (suspendPolicy != SP_NONE) {
+    if (suspend_policy != SP_NONE) {
       SetWaitForEventThread(basket.threadId);
     }
 
     EventFinish(pReq);
 
-    SuspendByPolicy(suspendPolicy);
+    SuspendByPolicy(suspend_policy);
     Dbg::ThreadContinuing(old_state);
   }
 
@@ -1003,19 +1005,19 @@
     return false;
   }
 
-  JdwpEvent** matchList = AllocMatchList(numEvents);
-  int match_count = 0;
   ExpandBuf* pReq = NULL;
-  JdwpSuspendPolicy suspendPolicy = SP_NONE;
+  JdwpSuspendPolicy suspend_policy = SP_NONE;
+  int match_count = 0;
   {
-    MutexLock mu(event_lock_);
-    FindMatchingEvents(EK_CLASS_PREPARE, &basket, matchList, &match_count);
+    MutexLock mu(event_list_lock_);
+    JdwpEvent** match_list = AllocMatchList(event_list_size_);
+    FindMatchingEvents(EK_CLASS_PREPARE, &basket, match_list, &match_count);
     if (match_count != 0) {
-      VLOG(jdwp) << "EVENT: " << matchList[0]->eventKind << "(" << match_count << " total) "
+      VLOG(jdwp) << "EVENT: " << match_list[0]->eventKind << "(" << match_count << " total) "
                  << StringPrintf("thread=%#llx", basket.threadId) << ") " << signature;
 
-      suspendPolicy = scanSuspendPolicy(matchList, match_count);
-      VLOG(jdwp) << "  suspendPolicy=" << suspendPolicy;
+      suspend_policy = scanSuspendPolicy(match_list, match_count);
+      VLOG(jdwp) << "  suspend_policy=" << suspend_policy;
 
       if (basket.threadId == debugThreadId) {
         /*
@@ -1025,18 +1027,18 @@
          */
         VLOG(jdwp) << "  NOTE: class prepare in debugger thread!";
         basket.threadId = 0;
-        if (suspendPolicy == SP_EVENT_THREAD) {
-          suspendPolicy = SP_ALL;
+        if (suspend_policy == SP_EVENT_THREAD) {
+          suspend_policy = SP_ALL;
         }
       }
 
       pReq = eventPrep();
-      expandBufAdd1(pReq, suspendPolicy);
+      expandBufAdd1(pReq, suspend_policy);
       expandBufAdd4BE(pReq, match_count);
 
       for (int i = 0; i < match_count; i++) {
-        expandBufAdd1(pReq, matchList[i]->eventKind);
-        expandBufAdd4BE(pReq, matchList[i]->requestId);
+        expandBufAdd1(pReq, match_list[i]->eventKind);
+        expandBufAdd4BE(pReq, match_list[i]->requestId);
         expandBufAdd8BE(pReq, basket.threadId);
 
         expandBufAdd1(pReq, tag);
@@ -1045,18 +1047,18 @@
         expandBufAdd4BE(pReq, status);
       }
     }
-    CleanupMatchList(matchList, match_count);
+    CleanupMatchList(match_list, match_count);
   }
 
   /* send request and possibly suspend ourselves */
   if (pReq != NULL) {
     int old_state = Dbg::ThreadWaiting();
-    if (suspendPolicy != SP_NONE) {
+    if (suspend_policy != SP_NONE) {
       SetWaitForEventThread(basket.threadId);
     }
     EventFinish(pReq);
 
-    SuspendByPolicy(suspendPolicy);
+    SuspendByPolicy(suspend_policy);
     Dbg::ThreadContinuing(old_state);
   }
 
diff --git a/src/jdwp/jdwp_event.h b/src/jdwp/jdwp_event.h
index e8f633f..b28aac9 100644
--- a/src/jdwp/jdwp_event.h
+++ b/src/jdwp/jdwp_event.h
@@ -92,7 +92,7 @@
   JdwpEvent* next;
 
   JdwpEventKind eventKind;      /* what kind of event is this? */
-  JdwpSuspendPolicy suspendPolicy;  /* suspend all, none, or self? */
+  JdwpSuspendPolicy suspend_policy;  /* suspend all, none, or self? */
   int modCount;       /* #of entries in mods[] */
   uint32_t requestId;      /* serial#, reported to debugger */
 
diff --git a/src/jdwp/jdwp_handler.cc b/src/jdwp/jdwp_handler.cc
index cf1e188..c49e9da 100644
--- a/src/jdwp/jdwp_handler.cc
+++ b/src/jdwp/jdwp_handler.cc
@@ -1157,18 +1157,18 @@
   const uint8_t* origBuf = buf;
 
   uint8_t eventKind = Read1(&buf);
-  uint8_t suspendPolicy = Read1(&buf);
+  uint8_t suspend_policy = Read1(&buf);
   uint32_t modifierCount = Read4BE(&buf);
 
   VLOG(jdwp) << "  Set(kind=" << JdwpEventKind(eventKind)
-               << " suspend=" << JdwpSuspendPolicy(suspendPolicy)
+               << " suspend=" << JdwpSuspendPolicy(suspend_policy)
                << " mods=" << modifierCount << ")";
 
   CHECK_LT(modifierCount, 256U);    /* reasonableness check */
 
   JdwpEvent* pEvent = EventAlloc(modifierCount);
   pEvent->eventKind = static_cast<JdwpEventKind>(eventKind);
-  pEvent->suspendPolicy = static_cast<JdwpSuspendPolicy>(suspendPolicy);
+  pEvent->suspend_policy = static_cast<JdwpSuspendPolicy>(suspend_policy);
   pEvent->modCount = modifierCount;
 
   /*
diff --git a/src/jdwp/jdwp_main.cc b/src/jdwp/jdwp_main.cc
index df24b8c..fb24e29 100644
--- a/src/jdwp/jdwp_main.cc
+++ b/src/jdwp/jdwp_main.cc
@@ -72,7 +72,7 @@
  */
 uint32_t JdwpState::NextRequestSerial() {
   MutexLock mu(serial_lock_);
-  return requestSerial++;
+  return request_serial_++;
 }
 
 /*
@@ -81,7 +81,7 @@
  */
 uint32_t JdwpState::NextEventSerial() {
   MutexLock mu(serial_lock_);
-  return eventSerial++;
+  return event_serial_++;
 }
 
 JdwpState::JdwpState(const JdwpOptions* options)
@@ -96,12 +96,12 @@
       attach_lock_("JDWP attach lock"),
       attach_cond_("JDWP attach condition variable"),
       lastActivityWhen(0),
-      requestSerial(0x10000000),
-      eventSerial(0x20000000),
       serial_lock_("JDWP serial lock"),
-      numEvents(0),
-      eventList(NULL),
-      event_lock_("JDWP event lock"),
+      request_serial_(0x10000000),
+      event_serial_(0x20000000),
+      event_list_lock_("JDWP event list lock"),
+      event_list_(NULL),
+      event_list_size_(0),
       event_thread_lock_("JDWP event thread lock"),
       event_thread_cond_("JDWP event thread condition variable"),
       eventThreadId(0),
@@ -140,7 +140,8 @@
    * won't signal the cond var before we're waiting.
    */
   state->thread_start_lock_.Lock();
-  if (options->suspend) {
+  const bool should_suspend = options->suspend;
+  if (should_suspend) {
     state->attach_lock_.Lock();
   }
 
@@ -165,7 +166,7 @@
    * times out (for timeout=xxx), so we have to check to see what happened
    * when we wake up.
    */
-  if (options->suspend) {
+  if (should_suspend) {
     {
       ScopedThreadStateChange tsc(Thread::Current(), kVmWait);
 
@@ -201,7 +202,10 @@
   /* could reset the serial numbers, but no need to */
 
   UnregisterAll();
-  CHECK(eventList == NULL);
+  {
+    MutexLock mu(event_list_lock_);
+    CHECK(event_list_ == NULL);
+  }
 
   /*
    * Should not have one of these in progress.  If the debugger went away
diff --git a/src/jdwp/jdwp_priv.h b/src/jdwp/jdwp_priv.h
index f1e97b7..7a6a528 100644
--- a/src/jdwp/jdwp_priv.h
+++ b/src/jdwp/jdwp_priv.h
@@ -70,14 +70,15 @@
  * Base class for JdwpNetState
  */
 class JdwpNetStateBase {
-public:
+ public:
   int clientSock;     /* active connection to debugger */
 
   JdwpNetStateBase();
   ssize_t writePacket(ExpandBuf* pReply);
   ssize_t writeBufferedPacket(const iovec* iov, int iov_count);
 
-private:
+ private:
+  // Used to serialize writes to the socket.
   Mutex socket_lock_;
 };