[Lldb-commits] [PATCH] D32087: Modify GDBRemoteCommunication::ScopedTimeout to not ever decrease a timeout

Greg Clayton via Phabricator via lldb-commits lldb-commits at lists.llvm.org
Fri Apr 14 10:41:31 PDT 2017


clayborg created this revision.

We use GDBRemoteCommunication::ScopedTimeout in many places to change the packet timeout that is used for individual packets. If someone modifies the default timeout manually or the GDB remote server requests a longer timeout in a 'q' packet, then don't ever reduce a timeout for a packet since that could make things fail.

This patch checks the timeout to ensure the new timeout is larger before it modifies the timeout. The GDBRemoteCommunication::ScopedTimeout object also remembers if it did update the timeout and will restore the old timeout only if it did.


https://reviews.llvm.org/D32087

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


Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
@@ -89,6 +89,10 @@
   private:
     GDBRemoteCommunication &m_gdb_comm;
     std::chrono::seconds m_saved_timeout;
+    // Don't ever reduce the timeout for a packet, only increase it. If the
+    // requested timeout if less than the current timeout, we don't set it
+    // and won't need to restore it.
+    bool m_timeout_modified;
   };
 
   GDBRemoteCommunication(const char *comm_name, const char *listener_name);
Index: source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
===================================================================
--- source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -1310,12 +1310,20 @@
 
 GDBRemoteCommunication::ScopedTimeout::ScopedTimeout(
     GDBRemoteCommunication &gdb_comm, std::chrono::seconds timeout)
-    : m_gdb_comm(gdb_comm) {
-  m_saved_timeout = m_gdb_comm.SetPacketTimeout(timeout);
+  : m_gdb_comm(gdb_comm), m_timeout_modified(false) {
+    auto curr_timeout = gdb_comm.GetPacketTimeout();
+    // Only update the timeout if the timeout is greater than the current
+    // timeout. If the current timeout is larger, then just use that.
+    if (curr_timeout < timeout) {
+      m_timeout_modified = true;
+      m_saved_timeout = m_gdb_comm.SetPacketTimeout(timeout);
+    }
 }
 
 GDBRemoteCommunication::ScopedTimeout::~ScopedTimeout() {
-  m_gdb_comm.SetPacketTimeout(m_saved_timeout);
+  // Only restore the timeout if we set it in the constructor.
+  if (m_timeout_modified)
+    m_gdb_comm.SetPacketTimeout(m_saved_timeout);
 }
 
 // This function is called via the Communications class read thread when bytes


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32087.95314.patch
Type: text/x-patch
Size: 1920 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20170414/fc86d380/attachment.bin>


More information about the lldb-commits mailing list