Clean up more of the JDWP request handling.

There are no longer any naked uint8_t*s; it's JDWP::Requests all the way.

Also remove a bunch of the adb/socket transport duplication.

Change-Id: I0a33e393b54924d4ab9cc9f76fb346661931284f
diff --git a/src/jdwp/jdwp.h b/src/jdwp/jdwp.h
index 0cea23c..d3b6c1b 100644
--- a/src/jdwp/jdwp.h
+++ b/src/jdwp/jdwp.h
@@ -95,9 +95,9 @@
 
 struct JdwpEvent;
 struct JdwpNetState;
-struct JdwpReqHeader;
 struct JdwpTransport;
 struct ModBasket;
+struct Request;
 
 /*
  * State for JDWP functions.
@@ -225,13 +225,7 @@
   void DdmSendChunkV(uint32_t type, const iovec* iov, int iov_count)
       SHARED_LOCKS_REQUIRED(Locks::mutator_lock_);
 
-  /*
-   * Process a request from the debugger.
-   *
-   * "buf" points past the header, to the content of the message.  "dataLen"
-   * can therefore be zero.
-   */
-  void ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply);
+  bool HandlePacket();
 
   /*
    * Send an event, formatted into "pReq", to the debugger.
@@ -278,6 +272,7 @@
 
  private:
   explicit JdwpState(const JdwpOptions* options);
+  void ProcessRequest(Request& request, ExpandBuf* pReply);
   bool InvokeInProgress();
   bool IsConnected();
   void SuspendByPolicy(JdwpSuspendPolicy suspend_policy, JDWP::ObjectId thread_self_id)
@@ -357,8 +352,7 @@
 
 class Request {
  public:
-  Request(const uint8_t* bytes, size_t byte_count);
-
+  Request(const uint8_t* bytes, uint32_t available);
   ~Request();
 
   std::string ReadUtf8String();
@@ -389,7 +383,7 @@
   FrameId ReadFrameId();
 
   template <typename T> T ReadEnum1(const char* specific_kind) {
-    T value = static_cast<T>(Read1(&p_));
+    T value = static_cast<T>(Read1());
     VLOG(jdwp) << "    " << specific_kind << " " << value;
     return value;
   }
@@ -402,7 +396,18 @@
 
   JdwpModKind ReadModKind();
 
+  //
+  // Return values from this JDWP packet's header.
+  //
+  size_t GetLength() { return byte_count_; }
+  uint32_t GetId() { return id_; }
+  uint8_t GetCommandSet() { return command_set_; }
+  uint8_t GetCommand() { return command_; }
+
+  // Returns the number of bytes remaining.
   size_t size() { return end_ - p_; }
+
+  // Returns a pointer to the next byte.
   const uint8_t* data() { return p_; }
 
   void Skip(size_t count) { p_ += count; }
@@ -410,9 +415,16 @@
   void CheckConsumed();
 
  private:
+  uint8_t Read1();
   uint16_t Read2BE();
+  uint32_t Read4BE();
   uint64_t Read8BE();
 
+  uint32_t byte_count_;
+  uint32_t id_;
+  uint8_t command_set_;
+  uint8_t command_;
+
   const uint8_t* p_;
   const uint8_t* end_;
 
diff --git a/src/jdwp/jdwp_adb.cc b/src/jdwp/jdwp_adb.cc
index 27f9aaa..8734077 100644
--- a/src/jdwp/jdwp_adb.cc
+++ b/src/jdwp/jdwp_adb.cc
@@ -22,7 +22,6 @@
 
 #include "base/logging.h"
 #include "base/stringprintf.h"
-#include "jdwp/jdwp_handler.h"
 #include "jdwp/jdwp_priv.h"
 
 #ifdef HAVE_ANDROID_OS
@@ -46,8 +45,6 @@
  *    JDWP-handshake, etc...
  */
 
-#define kInputBufferSize    8192
-
 #define kJdwpControlName    "\0jdwp-control"
 #define kJdwpControlNameLen (sizeof(kJdwpControlName)-1)
 
@@ -61,9 +58,6 @@
   bool                shuttingDown;
   int                 wakeFds[2];
 
-  size_t inputCount;
-  unsigned char       inputBuffer[kInputBufferSize];
-
   socklen_t           controlAddrLen;
   union {
     sockaddr_un  controlAddrUn;
@@ -77,8 +71,6 @@
     wakeFds[0] = -1;
     wakeFds[1] = -1;
 
-    inputCount = 0;
-
     controlAddr.controlAddrUn.sun_family = AF_UNIX;
     controlAddrLen = sizeof(controlAddr.controlAddrUn.sun_family) + kJdwpControlNameLen;
     memcpy(controlAddr.controlAddrUn.sun_path, kJdwpControlName, kJdwpControlNameLen);
@@ -386,77 +378,6 @@
 }
 
 /*
- * Consume bytes from the buffer.
- *
- * This would be more efficient with a circular buffer.  However, we're
- * usually only going to find one packet, which is trivial to handle.
- */
-static void consumeBytes(JdwpNetState* netState, size_t count) {
-  CHECK_GT(count, 0U);
-  CHECK_LE(count, netState->inputCount);
-
-  if (count == netState->inputCount) {
-    netState->inputCount = 0;
-    return;
-  }
-
-  memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
-  netState->inputCount -= count;
-}
-
-/*
- * Handle a packet.  Returns "false" if we encounter a connection-fatal error.
- */
-static bool handlePacket(JdwpState* state) {
-  JdwpNetState* netState = state->netState;
-  const unsigned char* buf = netState->inputBuffer;
-  JdwpReqHeader hdr;
-  uint32_t length, id;
-  uint8_t flags, cmdSet, cmd;
-  int dataLen;
-
-  cmd = cmdSet = 0;       // shut up gcc
-
-  length = Read4BE(&buf);
-  id = Read4BE(&buf);
-  flags = Read1(&buf);
-  if ((flags & kJDWPFlagReply) != 0) {
-    LOG(FATAL) << "reply?!";
-  } else {
-    cmdSet = Read1(&buf);
-    cmd = Read1(&buf);
-  }
-
-  CHECK_LE(length, netState->inputCount);
-  dataLen = length - (buf - netState->inputBuffer);
-
-  ExpandBuf* pReply = expandBufAlloc();
-
-  hdr.length = length;
-  hdr.id = id;
-  hdr.cmdSet = cmdSet;
-  hdr.cmd = cmd;
-  state->ProcessRequest(&hdr, buf, dataLen, pReply);
-  if (expandBufGetLength(pReply) > 0) {
-    ssize_t cc = netState->writePacket(pReply);
-
-    if (cc != (ssize_t) expandBufGetLength(pReply)) {
-      PLOG(ERROR) << "Failed sending reply to debugger";
-      expandBufFree(pReply);
-      return false;
-    }
-  } else {
-    LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
-  }
-  expandBufFree(pReply);
-
-  VLOG(jdwp) << "----------";
-
-  consumeBytes(netState, length);
-  return true;
-}
-
-/*
  * Process incoming data.  If no data is available, this will block until
  * some arrives.
  *
@@ -605,7 +526,7 @@
       goto fail;
     }
 
-    consumeBytes(netState, kMagicHandshakeLen);
+    netState->ConsumeBytes(kMagicHandshakeLen);
     netState->awaitingHandshake = false;
     VLOG(jdwp) << "+++ handshake complete";
     return true;
@@ -614,7 +535,7 @@
   /*
    * Handle this packet.
    */
-  return handlePacket(state);
+  return state->HandlePacket();
 
  fail:
   closeConnection(state);
@@ -640,7 +561,7 @@
 
   errno = 0;
 
-  ssize_t cc = netState->writePacket(pReq);
+  ssize_t cc = netState->WritePacket(pReq);
 
   if (cc != (ssize_t) expandBufGetLength(pReq)) {
     PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
@@ -672,7 +593,7 @@
     expected += iov[i].iov_len;
   }
 
-  ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
+  ssize_t actual = netState->WriteBufferedPacket(iov, iov_count);
   if ((size_t)actual != expected) {
     PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
     return false;
diff --git a/src/jdwp/jdwp_bits.h b/src/jdwp/jdwp_bits.h
index 270af5d..2a3c775 100644
--- a/src/jdwp/jdwp_bits.h
+++ b/src/jdwp/jdwp_bits.h
@@ -32,20 +32,6 @@
   return (pSrc[0] << 24) | (pSrc[1] << 16) | (pSrc[2] << 8) | pSrc[3];
 }
 
-static inline uint8_t Read1(unsigned const char** ppSrc) {
-  return *(*ppSrc)++;
-}
-
-static inline uint32_t Read4BE(uint8_t const** ppSrc) {
-  const unsigned char* pSrc = *ppSrc;
-  uint32_t result = pSrc[0] << 24;
-  result |= pSrc[1] << 16;
-  result |= pSrc[2] << 8;
-  result |= pSrc[3];
-  *ppSrc = pSrc + 4;
-  return result;
-}
-
 static inline void Append1BE(std::vector<uint8_t>& bytes, uint8_t value) {
   bytes.push_back(value);
 }
diff --git a/src/jdwp/jdwp_event.cc b/src/jdwp/jdwp_event.cc
index 4423058..eb385fc 100644
--- a/src/jdwp/jdwp_event.cc
+++ b/src/jdwp/jdwp_event.cc
@@ -26,7 +26,6 @@
 #include "debugger.h"
 #include "jdwp/jdwp_constants.h"
 #include "jdwp/jdwp_expand_buf.h"
-#include "jdwp/jdwp_handler.h"
 #include "jdwp/jdwp_priv.h"
 #include "thread-inl.h"
 
diff --git a/src/jdwp/jdwp_handler.cc b/src/jdwp/jdwp_handler.cc
index d73573f..8ef146c 100644
--- a/src/jdwp/jdwp_handler.cc
+++ b/src/jdwp/jdwp_handler.cc
@@ -14,8 +14,6 @@
  * limitations under the License.
  */
 
-#include "jdwp/jdwp_handler.h"
-
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
@@ -1503,7 +1501,7 @@
  * Command sets 0-63 are incoming requests, 64-127 are outbound requests,
  * and 128-256 are vendor-defined.
  */
-static const JdwpHandlerMap gHandlerMap[] = {
+static const JdwpHandlerMap gHandlers[] = {
   /* VirtualMachine command set (1) */
   { 1,    1,  VM_Version,               "VirtualMachine.Version" },
   { 1,    2,  VM_ClassesBySignature,    "VirtualMachine.ClassesBySignature" },
@@ -1631,20 +1629,20 @@
   { 199,  1,  DDM_Chunk,        "DDM.Chunk" },
 };
 
-static const char* GetCommandName(size_t cmdSet, size_t cmd) {
-  for (size_t i = 0; i < arraysize(gHandlerMap); ++i) {
-    if (gHandlerMap[i].cmdSet == cmdSet && gHandlerMap[i].cmd == cmd) {
-      return gHandlerMap[i].name;
+static const char* GetCommandName(Request& request) {
+  for (size_t i = 0; i < arraysize(gHandlers); ++i) {
+    if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand()) {
+      return gHandlers[i].name;
     }
   }
   return "?UNKNOWN?";
 }
 
-static std::string DescribeCommand(const JdwpReqHeader* pHeader, int dataLen) {
+static std::string DescribeCommand(Request& request) {
   std::string result;
   result += "REQUEST: ";
-  result += GetCommandName(pHeader->cmdSet, pHeader->cmd);
-  result += StringPrintf(" (length=%d id=0x%06x)", dataLen, pHeader->id);
+  result += GetCommandName(request);
+  result += StringPrintf(" (length=%d id=0x%06x)", request.GetLength(), request.GetId());
   return result;
 }
 
@@ -1653,10 +1651,10 @@
  *
  * On entry, the JDWP thread is in VMWAIT.
  */
-void JdwpState::ProcessRequest(const JdwpReqHeader* pHeader, const uint8_t* buf, int dataLen, ExpandBuf* pReply) {
+void JdwpState::ProcessRequest(Request& request, ExpandBuf* pReply) {
   JdwpError result = ERR_NONE;
 
-  if (pHeader->cmdSet != kJDWPDdmCmdSet) {
+  if (request.GetCommandSet() != kJDWPDdmCmdSet) {
     /*
      * Activity from a debugger, not merely ddms.  Mark us as having an
      * active debugger session, and zero out the last-activity timestamp
@@ -1694,20 +1692,19 @@
   expandBufAddSpace(pReply, kJDWPHeaderLen);
 
   size_t i;
-  for (i = 0; i < arraysize(gHandlerMap); ++i) {
-    if (gHandlerMap[i].cmdSet == pHeader->cmdSet && gHandlerMap[i].cmd == pHeader->cmd && gHandlerMap[i].func != NULL) {
-      VLOG(jdwp) << DescribeCommand(pHeader, dataLen);
-      Request request(buf, dataLen);
-      result = (*gHandlerMap[i].func)(this, request, pReply);
+  for (i = 0; i < arraysize(gHandlers); ++i) {
+    if (gHandlers[i].cmdSet == request.GetCommandSet() && gHandlers[i].cmd == request.GetCommand() && gHandlers[i].func != NULL) {
+      VLOG(jdwp) << DescribeCommand(request);
+      result = (*gHandlers[i].func)(this, request, pReply);
       if (result == ERR_NONE) {
         request.CheckConsumed();
       }
       break;
     }
   }
-  if (i == arraysize(gHandlerMap)) {
-    LOG(ERROR) << "Command not implemented: " << DescribeCommand(pHeader, dataLen);
-    LOG(ERROR) << HexDump(buf, dataLen);
+  if (i == arraysize(gHandlers)) {
+    LOG(ERROR) << "Command not implemented: " << DescribeCommand(request);
+    LOG(ERROR) << HexDump(request.data(), request.size());
     result = ERR_NOT_IMPLEMENTED;
   }
 
@@ -1717,7 +1714,7 @@
    * If we encountered an error, only send the header back.
    */
   uint8_t* replyBuf = expandBufGetBuffer(pReply);
-  Set4BE(replyBuf + 4, pHeader->id);
+  Set4BE(replyBuf + 4, request.GetId());
   Set1(replyBuf + 8, kJDWPFlagReply);
   Set2BE(replyBuf + 9, result);
   if (result == ERR_NONE) {
@@ -1726,17 +1723,21 @@
     Set4BE(replyBuf + 0, kJDWPHeaderLen);
   }
 
+  CHECK_GT(expandBufGetLength(pReply), 0U) << GetCommandName(request) << " " << request.GetId();
+
   size_t respLen = expandBufGetLength(pReply) - kJDWPHeaderLen;
-  VLOG(jdwp) << "REPLY: " << GetCommandName(pHeader->cmdSet, pHeader->cmd) << " " << result << " (length=" << respLen << ")";
+  VLOG(jdwp) << "REPLY: " << GetCommandName(request) << " " << result << " (length=" << respLen << ")";
   if (false) {
     VLOG(jdwp) << HexDump(expandBufGetBuffer(pReply) + kJDWPHeaderLen, respLen);
   }
 
+  VLOG(jdwp) << "----------";
+
   /*
    * Update last-activity timestamp.  We really only need this during
    * the initial setup.  Only update if this is a non-DDMS packet.
    */
-  if (pHeader->cmdSet != kJDWPDdmCmdSet) {
+  if (request.GetCommandSet() != kJDWPDdmCmdSet) {
     QuasiAtomic::Write64(&last_activity_time_ms_, MilliTime());
   }
 
diff --git a/src/jdwp/jdwp_handler.h b/src/jdwp/jdwp_handler.h
deleted file mode 100644
index c296e88..0000000
--- a/src/jdwp/jdwp_handler.h
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *      http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-/*
- * Handle requests.
- */
-#ifndef ART_JDWP_JDWPHANDLER_H_
-#define ART_JDWP_JDWPHANDLER_H_
-
-#include "jdwp_expand_buf.h"
-
-namespace art {
-
-namespace JDWP {
-
-/*
- * JDWP message header for a request.
- */
-struct JdwpReqHeader {
-  uint32_t length;
-  uint32_t id;
-  uint8_t cmdSet;
-  uint8_t cmd;
-};
-
-}  // namespace JDWP
-
-}  // namespace art
-
-#endif  // ART_JDWP_JDWPHANDLER_H_
diff --git a/src/jdwp/jdwp_main.cc b/src/jdwp/jdwp_main.cc
index cbac920..4a18d48 100644
--- a/src/jdwp/jdwp_main.cc
+++ b/src/jdwp/jdwp_main.cc
@@ -37,12 +37,26 @@
  */
 JdwpNetStateBase::JdwpNetStateBase() : socket_lock_("JdwpNetStateBase lock") {
   clientSock = -1;
+  inputCount = 0;
+}
+
+void JdwpNetStateBase::ConsumeBytes(size_t count) {
+  CHECK_GT(count, 0U);
+  CHECK_LE(count, inputCount);
+
+  if (count == inputCount) {
+    inputCount = 0;
+    return;
+  }
+
+  memmove(inputBuffer, inputBuffer + count, inputCount - count);
+  inputCount -= count;
 }
 
 /*
  * Write a packet. Grabs a mutex to assure atomicity.
  */
-ssize_t JdwpNetStateBase::writePacket(ExpandBuf* pReply) {
+ssize_t JdwpNetStateBase::WritePacket(ExpandBuf* pReply) {
   MutexLock mu(Thread::Current(), socket_lock_);
   return write(clientSock, expandBufGetBuffer(pReply), expandBufGetLength(pReply));
 }
@@ -50,7 +64,7 @@
 /*
  * Write a buffered packet. Grabs a mutex to assure atomicity.
  */
-ssize_t JdwpNetStateBase::writeBufferedPacket(const iovec* iov, int iov_count) {
+ssize_t JdwpNetStateBase::WriteBufferedPacket(const iovec* iov, int iov_count) {
   MutexLock mu(Thread::Current(), socket_lock_);
   return writev(clientSock, iov, iov_count);
 }
@@ -268,6 +282,24 @@
   return IsConnected();
 }
 
+// Returns "false" if we encounter a connection-fatal error.
+bool JdwpState::HandlePacket() {
+  JdwpNetStateBase* netStateBase = reinterpret_cast<JdwpNetStateBase*>(netState);
+  JDWP::Request request(netStateBase->inputBuffer, netStateBase->inputCount);
+
+  ExpandBuf* pReply = expandBufAlloc();
+  ProcessRequest(request, pReply);
+  ssize_t cc = netStateBase->WritePacket(pReply);
+  if (cc != (ssize_t) expandBufGetLength(pReply)) {
+    PLOG(ERROR) << "Failed sending reply to debugger";
+    expandBufFree(pReply);
+    return false;
+  }
+  expandBufFree(pReply);
+  netStateBase->ConsumeBytes(request.GetLength());
+  return true;
+}
+
 /*
  * Entry point for JDWP thread.  The thread was created through the VM
  * mechanisms, so there is a java/lang/Thread associated with us.
diff --git a/src/jdwp/jdwp_priv.h b/src/jdwp/jdwp_priv.h
index cead61a..c03ad2a 100644
--- a/src/jdwp/jdwp_priv.h
+++ b/src/jdwp/jdwp_priv.h
@@ -76,9 +76,15 @@
  public:
   int clientSock;     /* active connection to debugger */
 
+  enum { kInputBufferSize = 8192 };
+
+  unsigned char inputBuffer[kInputBufferSize];
+  size_t inputCount;
+
   JdwpNetStateBase();
-  ssize_t writePacket(ExpandBuf* pReply);
-  ssize_t writeBufferedPacket(const iovec* iov, int iov_count);
+  void ConsumeBytes(size_t byte_count);
+  ssize_t WritePacket(ExpandBuf* pReply);
+  ssize_t WriteBufferedPacket(const iovec* iov, int iov_count);
 
  private:
   // Used to serialize writes to the socket.
diff --git a/src/jdwp/jdwp_request.cc b/src/jdwp/jdwp_request.cc
index 52045b7..440b51b 100644
--- a/src/jdwp/jdwp_request.cc
+++ b/src/jdwp/jdwp_request.cc
@@ -17,12 +17,25 @@
 #include "jdwp/jdwp.h"
 
 #include "base/stringprintf.h"
+#include "jdwp/jdwp_priv.h"
 
 namespace art {
 
 namespace JDWP {
 
-Request::Request(const uint8_t* bytes, size_t byte_count) : p_(bytes), end_(bytes + byte_count) {
+Request::Request(const uint8_t* bytes, uint32_t available) : p_(bytes) {
+  byte_count_ = Read4BE();
+  end_ =  bytes + byte_count_;
+  CHECK_LE(byte_count_, available);
+
+  id_ = Read4BE();
+  int8_t flags = Read1();
+  if ((flags & kJDWPFlagReply) != 0) {
+    LOG(FATAL) << "reply?!";
+  }
+
+  command_set_ = Read1();
+  command_ = Read1();
 }
 
 Request::~Request() {
@@ -37,7 +50,7 @@
 }
 
 std::string Request::ReadUtf8String() {
-  uint32_t length = Read4BE(&p_);
+  uint32_t length = Read4BE();
   std::string s;
   s.resize(length);
   memcpy(&s[0], p_, length);
@@ -50,9 +63,9 @@
 uint64_t Request::ReadValue(size_t width) {
   uint64_t value = -1;
   switch (width) {
-    case 1: value = Read1(&p_); break;
+    case 1: value = Read1(); break;
     case 2: value = Read2BE(); break;
-    case 4: value = Read4BE(&p_); break;
+    case 4: value = Read4BE(); break;
     case 8: value = Read8BE(); break;
     default: LOG(FATAL) << width; break;
   }
@@ -60,25 +73,25 @@
 }
 
 int32_t Request::ReadSigned32(const char* what) {
-  int32_t value = static_cast<int32_t>(Read4BE(&p_));
+  int32_t value = static_cast<int32_t>(Read4BE());
   VLOG(jdwp) << "    " << what << " " << value;
   return value;
 }
 
 uint32_t Request::ReadUnsigned32(const char* what) {
-  uint32_t value = Read4BE(&p_);
+  uint32_t value = Read4BE();
   VLOG(jdwp) << "    " << what << " " << value;
   return value;
 }
 
 FieldId Request::ReadFieldId() {
-  FieldId id = Read4BE(&p_);
+  FieldId id = Read4BE();
   VLOG(jdwp) << "    field id " << DescribeField(id);
   return id;
 }
 
 MethodId Request::ReadMethodId() {
-  MethodId id = Read4BE(&p_);
+  MethodId id = Read4BE();
   VLOG(jdwp) << "    method id " << DescribeMethod(id);
   return id;
 }
@@ -140,15 +153,28 @@
   return ReadEnum1<JdwpModKind>("mod kind");
 }
 
+uint8_t Request::Read1() {
+  return *p_++;
+}
+
 uint16_t Request::Read2BE() {
   uint16_t result = p_[0] << 8 | p_[1];
   p_ += 2;
   return result;
 }
 
+uint32_t Request::Read4BE() {
+  uint32_t result = p_[0] << 24;
+  result |= p_[1] << 16;
+  result |= p_[2] << 8;
+  result |= p_[3];
+  p_ += 4;
+  return result;
+}
+
 uint64_t Request::Read8BE() {
-  uint64_t high = Read4BE(&p_);
-  uint64_t low = Read4BE(&p_);
+  uint64_t high = Read4BE();
+  uint64_t low = Read4BE();
   return (high << 32) | low;
 }
 
diff --git a/src/jdwp/jdwp_socket.cc b/src/jdwp/jdwp_socket.cc
index 6f6fbe5..43906ef 100644
--- a/src/jdwp/jdwp_socket.cc
+++ b/src/jdwp/jdwp_socket.cc
@@ -28,14 +28,11 @@
 
 #include "base/logging.h"
 #include "base/stringprintf.h"
-#include "jdwp/jdwp_handler.h"
 #include "jdwp/jdwp_priv.h"
 
 #define kBasePort           8000
 #define kMaxPort            8040
 
-#define kInputBufferSize    8192
-
 namespace art {
 
 namespace JDWP {
@@ -59,10 +56,6 @@
 
   bool    awaitingHandshake;  /* waiting for "JDWP-Handshake" */
 
-  /* pending data from the network; would be more efficient as circular buf */
-  unsigned char  inputBuffer[kInputBufferSize];
-  size_t inputCount;
-
   JdwpNetState() {
     listenPort  = 0;
     listenSock  = -1;
@@ -70,8 +63,6 @@
     wakePipe[1] = -1;
 
     awaitingHandshake = false;
-
-    inputCount = 0;
   }
 };
 
@@ -444,75 +435,6 @@
 }
 
 /*
- * Consume bytes from the buffer.
- *
- * This would be more efficient with a circular buffer.  However, we're
- * usually only going to find one packet, which is trivial to handle.
- */
-static void consumeBytes(JdwpNetState* netState, size_t count) {
-  CHECK_GT(count, 0U);
-  CHECK_LE(count, netState->inputCount);
-
-  if (count == netState->inputCount) {
-    netState->inputCount = 0;
-    return;
-  }
-
-  memmove(netState->inputBuffer, netState->inputBuffer + count, netState->inputCount - count);
-  netState->inputCount -= count;
-}
-
-/*
- * Handle a packet.  Returns "false" if we encounter a connection-fatal error.
- */
-static bool handlePacket(JdwpState* state) {
-  JdwpNetState* netState = state->netState;
-  const unsigned char* buf = netState->inputBuffer;
-  uint8_t cmdSet, cmd;
-
-  cmd = cmdSet = 0;       // shut up gcc
-
-  uint32_t length = Read4BE(&buf);
-  uint32_t id = Read4BE(&buf);
-  int8_t flags = Read1(&buf);
-  if ((flags & kJDWPFlagReply) != 0) {
-    LOG(FATAL) << "reply?!";
-  } else {
-    cmdSet = Read1(&buf);
-    cmd = Read1(&buf);
-  }
-
-  CHECK_LE(length, netState->inputCount);
-  int dataLen = length - (buf - netState->inputBuffer);
-
-  ExpandBuf* pReply = expandBufAlloc();
-
-  JdwpReqHeader hdr;
-  hdr.length = length;
-  hdr.id = id;
-  hdr.cmdSet = cmdSet;
-  hdr.cmd = cmd;
-  state->ProcessRequest(&hdr, buf, dataLen, pReply);
-  if (expandBufGetLength(pReply) > 0) {
-    ssize_t cc = netState->writePacket(pReply);
-
-    if (cc != (ssize_t) expandBufGetLength(pReply)) {
-      PLOG(ERROR) << "Failed sending reply to debugger";
-      expandBufFree(pReply);
-      return false;
-    }
-  } else {
-    LOG(WARNING) << "No reply created for set=" << cmdSet << " cmd=" << cmd;
-  }
-  expandBufFree(pReply);
-
-  VLOG(jdwp) << "----------";
-
-  consumeBytes(netState, length);
-  return true;
-}
-
-/*
  * Process incoming data.  If no data is available, this will block until
  * some arrives.
  *
@@ -665,7 +587,7 @@
       goto fail;
     }
 
-    consumeBytes(netState, kMagicHandshakeLen);
+    netState->ConsumeBytes(kMagicHandshakeLen);
     netState->awaitingHandshake = false;
     VLOG(jdwp) << "+++ handshake complete";
     return true;
@@ -674,7 +596,7 @@
   /*
    * Handle this packet.
    */
-  return handlePacket(state);
+  return state->HandlePacket();
 
  fail:
   closeConnection(state);
@@ -700,7 +622,7 @@
   }
 
   errno = 0;
-  ssize_t cc = netState->writePacket(pReq);
+  ssize_t cc = netState->WritePacket(pReq);
 
   if (cc != (ssize_t) expandBufGetLength(pReq)) {
     PLOG(ERROR) << "Failed sending req to debugger (" << cc << " of " << expandBufGetLength(pReq) << ")";
@@ -732,7 +654,7 @@
     expected += iov[i].iov_len;
   }
 
-  ssize_t actual = netState->writeBufferedPacket(iov, iov_count);
+  ssize_t actual = netState->WriteBufferedPacket(iov, iov_count);
 
   if ((size_t)actual != expected) {
     PLOG(ERROR) << "Failed sending b-req to debugger (" << actual << " of " << expected << ")";
diff --git a/src/thread.cc b/src/thread.cc
index d0401b6..0d3c5b9 100644
--- a/src/thread.cc
+++ b/src/thread.cc
@@ -624,9 +624,9 @@
         }
         // IsSuspended on the current thread will fail as the current thread is changed into
         // Runnable above. As the suspend count is now raised if this is the current thread
-        // it will self suspend on transition to Runnable, making it hard to work with. Its simpler
+        // it will self suspend on transition to Runnable, making it hard to work with. It's simpler
         // to just explicitly handle the current thread in the callers to this code.
-        CHECK_NE(thread, soa.Self()) << "Attempt to suspend for debugger the current thread";
+        CHECK_NE(thread, soa.Self()) << "Attempt to suspend the current thread for the debugger";
         // If thread is suspended (perhaps it was already not Runnable but didn't have a suspend
         // count, or else we've waited and it has self suspended) or is the current thread, we're
         // done.