Added the thread ID (tid) to each packet history item and the packet history now always dumps to a lldb_private::Stream.

Enable logging the packet history when registers fail to read due to not getting the sequence mutex if "--verbose" is enabled on the log channel for the "gdb-remote" log category.

This will help us track down some issues.



git-svn-id: https://llvm.org/svn/llvm-project/lldb/trunk@154704 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
index 0d39e98..a6d2d11 100644
--- a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
+++ b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.cpp
@@ -46,6 +46,41 @@
 }
 
 void
+GDBRemoteCommunication::History::AddPacket (char packet_char,
+                                            PacketType type,
+                                            uint32_t bytes_transmitted)
+{
+    const size_t size = m_packets.size();
+    if (size > 0)
+    {
+        const uint32_t idx = GetNextIndex();
+        m_packets[idx].packet.assign (1, packet_char);
+        m_packets[idx].type = type;
+        m_packets[idx].bytes_transmitted = bytes_transmitted;
+        m_packets[idx].packet_idx = m_total_packet_count;
+        m_packets[idx].tid = Host::GetCurrentThreadID();
+    }
+}
+
+void
+GDBRemoteCommunication::History::AddPacket (const std::string &src,
+                                            uint32_t src_len,
+                                            PacketType type,
+                                            uint32_t bytes_transmitted)
+{
+    const size_t size = m_packets.size();
+    if (size > 0)
+    {
+        const uint32_t idx = GetNextIndex();
+        m_packets[idx].packet.assign (src, 0, src_len);
+        m_packets[idx].type = type;
+        m_packets[idx].bytes_transmitted = bytes_transmitted;
+        m_packets[idx].packet_idx = m_total_packet_count;
+        m_packets[idx].tid = Host::GetCurrentThreadID();
+    }
+}
+
+void
 GDBRemoteCommunication::History::Dump (lldb_private::Stream &strm) const
 {
     const uint32_t size = GetNumPacketsInHistory ();
@@ -57,8 +92,9 @@
         const Entry &entry = m_packets[idx];
         if (entry.type == ePacketTypeInvalid || entry.packet.empty())
             break;
-        strm.Printf ("history[%u] <%4u> %s packet: %s\n",
+        strm.Printf ("history[%u] tid=0x%4.4llx <%4u> %s packet: %s\n",
                      entry.packet_idx,
+                     entry.tid,
                      entry.bytes_transmitted,
                      (entry.type == ePacketTypeSend) ? "send" : "read",
                      entry.packet.c_str());
@@ -80,8 +116,9 @@
             const Entry &entry = m_packets[idx];
             if (entry.type == ePacketTypeInvalid || entry.packet.empty())
                 break;
-            log->Printf ("history[%u] <%4u> %s packet: %s",
+            log->Printf ("history[%u] tid=0x%4.4llx <%4u> %s packet: %s",
                          entry.packet_idx,
+                         entry.tid,
                          entry.bytes_transmitted,
                          (entry.type == ePacketTypeSend) ? "send" : "read",
                          entry.packet.c_str());
@@ -598,12 +635,7 @@
 }
 
 void
-GDBRemoteCommunication::DumpHistory(const char *path)
+GDBRemoteCommunication::DumpHistory(Stream &strm)
 {
-    StreamFile strm;
-    Error error (strm.GetFile().Open(path, File::eOpenOptionWrite | File::eOpenOptionCanCreate));
-    if (error.Success())
-        m_history.Dump (strm);
-    else
-        fprintf (stderr, "error: unable to open '%s' -- %s\n", path, error.AsCString());
+    m_history.Dump (strm);
 }
diff --git a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
index 83bbfd5..b3bad7e 100644
--- a/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
+++ b/source/Plugins/Process/gdb-remote/GDBRemoteCommunication.h
@@ -114,7 +114,7 @@
                              lldb_private::ProcessLaunchInfo &launch_info); 
 
     void
-    DumpHistory(const char *path);
+    DumpHistory(lldb_private::Stream &strm);
     
 protected:
 
@@ -134,7 +134,8 @@
                 packet(),
                 type (ePacketTypeInvalid),
                 bytes_transmitted (0),
-                packet_idx (0)
+                packet_idx (0),
+                tid (LLDB_INVALID_THREAD_ID)
             {
             }
             
@@ -145,12 +146,13 @@
                 type = ePacketTypeInvalid;
                 bytes_transmitted = 0;
                 packet_idx = 0;
-                
+                tid = LLDB_INVALID_THREAD_ID;
             }
             std::string packet;
             PacketType type;
             uint32_t bytes_transmitted;
             uint32_t packet_idx;
+            lldb::tid_t tid;
         };
 
         History (uint32_t size);
@@ -161,35 +163,12 @@
         void
         AddPacket (char packet_char,
                    PacketType type,
-                   uint32_t bytes_transmitted)
-        {
-            const size_t size = m_packets.size();
-            if (size > 0)
-            {
-                const uint32_t idx = GetNextIndex();
-                m_packets[idx].packet.assign (1, packet_char);
-                m_packets[idx].type = type;
-                m_packets[idx].bytes_transmitted = bytes_transmitted;
-                m_packets[idx].packet_idx = m_total_packet_count;
-            }
-        }
-
+                   uint32_t bytes_transmitted);
         void
         AddPacket (const std::string &src,
                    uint32_t src_len,
                    PacketType type,
-                   uint32_t bytes_transmitted)
-        {
-            const size_t size = m_packets.size();
-            if (size > 0)
-            {
-                const uint32_t idx = GetNextIndex();
-                m_packets[idx].packet.assign (src, 0, src_len);
-                m_packets[idx].type = type;
-                m_packets[idx].bytes_transmitted = bytes_transmitted;
-                m_packets[idx].packet_idx = m_total_packet_count;
-            }
-        }
+                   uint32_t bytes_transmitted);
         
         void
         Dump (lldb_private::Stream &strm) const;
diff --git a/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp b/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp
index ec2cfd8..7ace1eb 100644
--- a/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp
+++ b/source/Plugins/Process/gdb-remote/GDBRemoteRegisterContext.cpp
@@ -246,7 +246,18 @@
         {
             LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS));
             if (log)
-                log->Printf("error: failed to get packet sequence mutex, not sending read register for \"%s\"", reg_info->name);
+            {
+                if (log->GetVerbose())
+                {
+                    StreamString strm;
+                    gdb_comm.DumpHistory(strm);
+                    log->Printf("error: failed to get packet sequence mutex, not sending read register for \"%s\":\n%s", reg_info->name, strm.GetData());
+                }
+                else
+                {
+                    log->Printf("error: failed to get packet sequence mutex, not sending read register for \"%s\"", reg_info->name);
+                }
+            }
         }
 
         // Make sure we got a valid register value after reading it
@@ -431,7 +442,16 @@
         {
             LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS));
             if (log)
-                log->Printf("error: failed to get packet sequence mutex, not sending write register for \"%s\"", reg_info->name);
+            {
+                if (log->GetVerbose())
+                {
+                    StreamString strm;
+                    gdb_comm.DumpHistory(strm);
+                    log->Printf("error: failed to get packet sequence mutex, not sending write register for \"%s\":\n%s", reg_info->name, strm.GetData());
+                }
+                else
+                    log->Printf("error: failed to get packet sequence mutex, not sending write register for \"%s\"", reg_info->name);
+            }
         }
     }
     return false;
@@ -492,7 +512,16 @@
     {
         LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS));
         if (log)
-            log->Printf("error: failed to get packet sequence mutex, not sending read all registers");
+        {
+            if (log->GetVerbose())
+            {
+                StreamString strm;
+                gdb_comm.DumpHistory(strm);
+                log->Printf("error: failed to get packet sequence mutex, not sending read all registers:\n%s", strm.GetData());
+            }
+            else
+                log->Printf("error: failed to get packet sequence mutex, not sending read all registers");
+        }
     }
 
     data_sp.reset();
@@ -616,7 +645,16 @@
     {
         LogSP log (ProcessGDBRemoteLog::GetLogIfAnyCategoryIsSet (GDBR_LOG_THREAD | GDBR_LOG_PACKETS));
         if (log)
-            log->Printf("error: failed to get packet sequence mutex, not sending write all registers");
+        {
+            if (log->GetVerbose())
+            {
+                StreamString strm;
+                gdb_comm.DumpHistory(strm);
+                log->Printf("error: failed to get packet sequence mutex, not sending write all registers:\n%s", strm.GetData());
+            }
+            else
+                log->Printf("error: failed to get packet sequence mutex, not sending write all registers");
+        }
     }
     return false;
 }
diff --git a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
index 77981ef..ffb165d 100644
--- a/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
+++ b/source/Plugins/Process/gdb-remote/ProcessGDBRemote.cpp
@@ -32,6 +32,7 @@
 #include "lldb/Core/Module.h"
 #include "lldb/Core/PluginManager.h"
 #include "lldb/Core/State.h"
+#include "lldb/Core/StreamFile.h"
 #include "lldb/Core/StreamString.h"
 #include "lldb/Core/Timer.h"
 #include "lldb/Core/Value.h"
@@ -64,7 +65,10 @@
     void
     DumpProcessGDBRemotePacketHistory (void *p, const char *path)
     {
-        ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (path);
+        lldb_private::StreamFile strm;
+        lldb_private::Error error (strm.GetFile().Open(path, lldb_private::File::eOpenOptionWrite | lldb_private::File::eOpenOptionCanCreate));
+        if (error.Success())
+            ((ProcessGDBRemote *)p)->GetGDBRemote().DumpHistory (strm);
     }
 };