[Lldb-commits] [PATCH] D116006: [lldb/gdb-remote] remove junk bytes, then decode the packet

Antonio Borneo via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Sun Dec 19 10:29:02 PST 2021


borneoa created this revision.
borneoa requested review of this revision.
Herald added a project: LLDB.
Herald added a subscriber: lldb-commits.

In case of receiving, in a single packet, some junk byte followed
by a complete gdb-remote reply, e.g. "junk+$OK#9a", the current
implementation of CheckForPacket() drops the junk bytes and
returns.
The caller will call again CheckForPacket() only if it receives
another packet. But the reply is already complete and the remote
will not send anything else, so the caller will timeout.

Check for junk bytes and drop them, then decode the packet.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D116006

Files:
  lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp


Index: lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
===================================================================
--- lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ lldb/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -647,6 +647,34 @@
       }
     }
 
+    // Check if we have an unexpected byte and we need to flush all bad data
+    // that is in m_bytes, so we need to find the first byte that is a '+'
+    // (ACK), '-' (NACK), \x03 (CTRL+C interrupt), or '$' character (start of
+    // packet header) or of course, the end of the data in m_bytes...
+    const size_t bytes_len = m_bytes.size();
+    bool done = false;
+    uint32_t idx;
+    for (idx = 0; !done && idx < bytes_len;) {
+      switch (m_bytes[idx]) {
+      case '+':
+      case '-':
+      case '\x03':
+      case '%':
+      case '$':
+        done = true;
+        break;
+
+      default:
+        ++idx;
+        break;
+      }
+    }
+    if (idx) {
+      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
+                __FUNCTION__, idx, idx, m_bytes.c_str());
+      m_bytes.erase(0, idx);
+    }
+
     switch (m_bytes[0]) {
     case '+':                            // Look for ack
     case '-':                            // Look for cancel
@@ -681,33 +709,9 @@
       }
       break;
 
-    default: {
-      // We have an unexpected byte and we need to flush all bad data that is
-      // in m_bytes, so we need to find the first byte that is a '+' (ACK), '-'
-      // (NACK), \x03 (CTRL+C interrupt), or '$' character (start of packet
-      // header) or of course, the end of the data in m_bytes...
-      const size_t bytes_len = m_bytes.size();
-      bool done = false;
-      uint32_t idx;
-      for (idx = 1; !done && idx < bytes_len;) {
-        switch (m_bytes[idx]) {
-        case '+':
-        case '-':
-        case '\x03':
-        case '%':
-        case '$':
-          done = true;
-          break;
-
-        default:
-           ++idx;
-          break;
-        }
-      }
-      LLDB_LOGF(log, "GDBRemoteCommunication::%s tossing %u junk bytes: '%.*s'",
-                __FUNCTION__, idx, idx, m_bytes.c_str());
-      m_bytes.erase(0, idx);
-    } break;
+    default:
+      // no more bytes after junk
+      break;
     }
 
     if (content_length == std::string::npos) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116006.395346.patch
Type: text/x-patch
Size: 2391 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20211219/eb98c22a/attachment.bin>


More information about the lldb-commits mailing list