Fix a hang on OSX while executing -exec-run.

Now we wait for input to become available before blocking in fgets.
More details on problem can be found in 
http://lists.cs.uiuc.edu/pipermail/lldb-commits/Week-of-Mon-20141201/014290.html

Patch from dawn@burble.org.

llvm-svn: 223222
This commit is contained in:
Hafiz Abid Qadeer 2014-12-03 10:23:06 +00:00
parent c91fc11181
commit 25383ac01b
5 changed files with 54 additions and 22 deletions

View File

@ -434,3 +434,16 @@ CMICmnStreamStdin::SetOSStdinHandler(IOSStdinHandler &vrHandler)
return MIstatus::success;
}
//++ ------------------------------------------------------------------------------------
// Details: Do some actions before exiting.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void
CMICmnStreamStdin::OnExitHandler(void)
{
m_pStdinReadHandler->InterruptReadLine();
}

View File

@ -66,6 +66,7 @@ class CMICmnStreamStdin : public CMICmnBase, public CMIUtilThreadActiveObjBase,
public:
virtual bool InputAvailable(bool &vwbAvail) = 0;
virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg) = 0;
virtual void InterruptReadLine(void){};
/* dtor */ virtual ~IOSStdinHandler(void){};
};
@ -82,6 +83,7 @@ class CMICmnStreamStdin : public CMICmnBase, public CMIUtilThreadActiveObjBase,
void SetCtrlCHit(void);
bool SetVisitor(IStreamStdin &vrVisitor);
bool SetOSStdinHandler(IOSStdinHandler &vrHandler);
void OnExitHandler(void);
// Overridden:
public:

View File

@ -22,7 +22,9 @@
// Third Party Headers:
#if !defined(_MSC_VER)
#include <sys/select.h>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#endif // !defined( _MSC_VER )
#include <string.h> // For std::strerror()
@ -153,30 +155,29 @@ CMICmnStreamStdinLinux::Shutdown(void)
bool
CMICmnStreamStdinLinux::InputAvailable(bool &vwbAvail)
{
/* AD: Not used ATM but could come in handy just in case we need to do
this, poll for input
#if !defined(_WIN32)
// The code below is needed on OSX where lldb-mi hangs when doing -exec-run.
// The hang seems to come from calling fgets and fileno from different thread.
// Although this problem was not observed on Linux.
// A solution based on 'select' was also proposed but it seems to slow things down
// a lot.
static bool bInitialized = false;
static const int STDIN = 0;
static bool bInitialized = false;
if( !bInitialized )
{
// Use termios to turn off line buffering
::termios term;
::tcgetattr( STDIN, &term );
::term.c_lflag &= ~ICANON;
::tcsetattr( STDIN, TCSANOW, &term );
::setbuf( stdin, NULL );
bInitialized = true;
}
int nBytesWaiting;
::ioctl( STDIN, FIONREAD, &nBytesWaiting );
vwbAvail = (nBytesWaiting > 0);
return MIstatus::success;
*/
if (!bInitialized)
{
// Use termios to turn off line buffering
::termios term;
::tcgetattr(STDIN_FILENO, &term);
term.c_lflag &= ~ICANON;
::tcsetattr(STDIN_FILENO, TCSANOW, &term);
::setbuf(stdin, NULL);
bInitialized = true;
}
int nBytesWaiting;
::ioctl(STDIN_FILENO, FIONREAD, &nBytesWaiting);
vwbAvail = (nBytesWaiting > 0);
#endif
return MIstatus::success;
}
@ -213,3 +214,16 @@ CMICmnStreamStdinLinux::ReadLine(CMIUtilString &vwErrMsg)
return pText;
}
//++ ------------------------------------------------------------------------------------
// Details: Interrupt current and prevent new ReadLine operations.
// Type: Method.
// Args: None.
// Return: None.
// Throws: None.
//--
void
CMICmnStreamStdinLinux::InterruptReadLine(void)
{
fclose(stdin);
}

View File

@ -51,6 +51,7 @@ class CMICmnStreamStdinLinux : public CMICmnBase, public CMICmnStreamStdin::IOSS
// From CMICmnStreamStdin::IOSpecificReadStreamStdin
virtual bool InputAvailable(bool &vwbAvail);
virtual const MIchar *ReadLine(CMIUtilString &vwErrMsg);
virtual void InterruptReadLine(void);
// Methods:
private:

View File

@ -1075,6 +1075,7 @@ CMIDriver::SetExitApplicationFlag(const bool vbForceExit)
{
CMIUtilThreadLock lock(m_threadMutex);
m_bExitApp = true;
m_rStdin.OnExitHandler();
return;
}
@ -1089,6 +1090,7 @@ CMIDriver::SetExitApplicationFlag(const bool vbForceExit)
}
m_bExitApp = true;
m_rStdin.OnExitHandler();
}
//++ ------------------------------------------------------------------------------------