[Lldb-commits] [PATCH] Reduce number of threads in lldb-mi.

Hafiz Abid Qadeer abidh.haq at gmail.com
Thu Feb 19 02:40:23 PST 2015


Hi ki.stfu, emaste,

LLDB-mi have 3 threads.

1. Wait for input.
2. Process commands.
3. Process events.

This revision merges 1 & 2. Same thread waits on input and then process the
command. This way, no synchronization is needed between first and 2nd. Also it is
easy to check when to exit.

This revision just makes the minimal changes to use one thread for reading intput
and then processnig the commands. A lot of code has become redundant now. I will
clean it up gradually.

All lldb-mi tests pass with gcc and clang as test compiler. Also did minimal testing
on command line and works ok. The "quit" and "-gdb-exit" command close the application
without needing any further return.

After this revision goes in, it will not be possible to give the name of the
executable on command line and lldb-mi accepting it. This functionality is already
stubbed out in the code by he following #ifdef so we will not lose anything.
MICONFIG_ENABLE_MI_DRIVER_MI_MODE_CMDLINE_ARG_EXECUTABLE_DEBUG_SESSION

For posix system, it is even possible to make lldb-mi a single thread app. But on
windows, it is difficult to wait on stdin with timeout like what you can do with
select. So next best thing is to use 2 threads instead of 3.

http://reviews.llvm.org/D7746

Files:
  tools/lldb-mi/MIDriver.cpp
  tools/lldb-mi/MIDriver.h

Index: tools/lldb-mi/MIDriver.cpp
===================================================================
--- tools/lldb-mi/MIDriver.cpp
+++ tools/lldb-mi/MIDriver.cpp
@@ -551,16 +551,6 @@
     // Grab the thread manager
     CMICmnThreadMgrStd &rThreadMgr = CMICmnThreadMgrStd::Instance();
 
-    // Start the stdin thread
-    bOk &= m_rStdin.SetVisitor(*this);
-    if (bOk && !rThreadMgr.ThreadStart<CMICmnStreamStdin>(m_rStdin))
-    {
-        const CMIUtilString errMsg = CMIUtilString::Format(MIRSRC(IDS_THREADMGR_ERR_THREAD_FAIL_CREATE),
-                                                           CMICmnThreadMgrStd::Instance().GetErrorDescription().c_str());
-        SetErrorDescriptionn(errMsg);
-        return MIstatus::failure;
-    }
-
     // Start the event polling thread
     if (bOk && !rThreadMgr.ThreadStart<CMICmnLLDBDebugger>(m_rLldbDebugger))
     {
@@ -589,6 +579,36 @@
 }
 
 //++ ------------------------------------------------------------------------------------
+// Details: Wait on new line of data from stdin stream (completed by '\n' or '\r').
+// Type:    Method.
+// Args:    cmd    - (W) Cmd buffer to fill.
+// Return:  bool   - false of error and true otherwise.
+// Throws:  None.
+//--
+bool
+ReadLine(CMIUtilString &cmd)
+{
+    const int SIZE = 2048;
+    char buf[SIZE];
+    // Read user input
+    const MIchar *pText = ::fgets(&buf[0], SIZE, stdin);
+    if (pText == nullptr)
+        return false;
+
+    // Strip off new line characters
+    for (MIchar *pI = buf; *pI != '\0'; pI++)
+    {
+        if ((*pI == '\n') || (*pI == '\r'))
+        {
+            *pI = '\0';
+            break;
+        }
+    }
+    cmd = CMIUtilString(buf);
+    return true;
+}
+
+//++ ------------------------------------------------------------------------------------
 // Details: Call this function puts *this driver to work.
 //          This function is used by the application's main thread.
 // Type:    Overridden.
@@ -627,11 +647,22 @@
     // While the app is active
     while (!m_bExitApp)
     {
-        // Poll stdin queue and dispatch
-        if (!ReadStdinLineQueue())
+        CMIUtilString lineText;
+        if (::ReadLine (lineText) && (!lineText.empty ()) )
         {
-            // Something went wrong
-            break;
+            if (lineText == "quit")
+            {
+                // We want to be exiting when receiving a quit command
+                m_bExitApp = true;
+                break;
+            }
+
+            // Process the command
+            bool bOk = InterpretCommand(lineText);
+
+            // Draw prompt if desired
+            if (bOk && m_rStdin.GetEnablePrompt())
+                m_rStdOut.WriteMIResponse(m_rStdin.GetPrompt());
         }
     }
 
Index: tools/lldb-mi/MIDriver.h
===================================================================
--- tools/lldb-mi/MIDriver.h
+++ tools/lldb-mi/MIDriver.h
@@ -180,3 +180,5 @@
     bool m_bDriverDebuggingArgExecutable; // True = The MI Driver (MI mode) is debugging executable passed as argument, false = running via
                                           // a client i.e Eclipse
 };
+
+bool ReadLine(CMIUtilString &cmd);

EMAIL PREFERENCES
  http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D7746.20265.patch
Type: text/x-patch
Size: 3177 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/lldb-commits/attachments/20150219/151ccb79/attachment.bin>


More information about the lldb-commits mailing list