From 094860c984d96b4c45e11b70332c9f497277f0b5 Mon Sep 17 00:00:00 2001 From: Kirill Frolov Date: Thu, 15 Apr 2021 13:50:21 +0300 Subject: [PATCH] This commit fixes issue #61 Issue was caused by the fact, that SIGWINCH signal was sent every time then debugee is stopped, but in case, if input comes from not a terminal (from pipe, socket or a file), liblinenoise was unable to handle SIGWINCH correctly and this causes netcoredbg termination. --- src/protocols/cliprotocol.cpp | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/src/protocols/cliprotocol.cpp b/src/protocols/cliprotocol.cpp index fc1a71fd..ac1fbbc4 100644 --- a/src/protocols/cliprotocol.cpp +++ b/src/protocols/cliprotocol.cpp @@ -5,9 +5,12 @@ #ifdef _WIN32 #include #include +#include #else #include #include +#define _isatty(fd) ::isatty(fd) +#define _fileno(file) ::fileno(file) #endif #include "debugger/frames.h" @@ -2164,12 +2167,21 @@ void CLIProtocol::applyCommandMode() { lock_guard lock(m_mutex); - // setup function which is called after Stop/Exit events to redraw screen, etc... + if (_isatty(_fileno(stdin))) + { + // setup function which is called after Stop/Exit events to redraw screen, etc... #ifndef WIN32 - m_repaint_fn = std::bind(pthread_kill, pthread_self(), SIGWINCH); + m_repaint_fn = std::bind(pthread_kill, pthread_self(), SIGWINCH); #else - m_repaint_fn = []{ GenerateConsoleCtrlEvent(CTRL_C_EVENT , 0); }; + m_repaint_fn = []{ GenerateConsoleCtrlEvent(CTRL_C_EVENT , 0); }; #endif + } + else + { + // if input comes from non (pseudo) terminals (pipes, files, sockets, etc...) + // no special function required (because SIGWINCH might not be handled corretly in this case). + m_repaint_fn = nullptr; + } } @@ -2195,11 +2207,15 @@ void CLIProtocol::CommandLoop() m_commandMode = CommandMode::Synchronous; applyCommandMode(); - linenoiseInstallWindowChangeHandler(); - linenoiseHistorySetMaxLen(DefaultHistoryDepth); - linenoiseHistoryLoad(HistoryFileName); + // Use linenoise features only if input comes from (pseudo)terminal. + if (_isatty(_fileno(stdin))) + { + linenoiseInstallWindowChangeHandler(); + linenoiseHistorySetMaxLen(DefaultHistoryDepth); + linenoiseHistoryLoad(HistoryFileName); - linenoiseSetCompletionCallbackEx(completion_callback, this); + linenoiseSetCompletionCallbackEx(completion_callback, this); + } } // loop till eof, error, or exit request.