Skip to content
Snippets Groups Projects
Commit cac1a0f3 authored by Benoît Minisini's avatar Benoît Minisini :church:
Browse files

Make the debugger more robust when writing on a fifo fails. Reopen the fifo if needed.

[GB.DEBUG]
* BUG: Make the debugger more robust when writing on a fifo fails. Reopen the fifo if needed.
parent 42f9525c
No related branches found
No related tags found
No related merge requests found
Pipeline #797970320 passed
......@@ -75,6 +75,8 @@ static void callback_read(int fd, int type, intptr_t param)
{
int n, i, p;
//fprintf(stderr, "callback_read\n");
for(;;)
{
fcntl(_fdr, F_SETFL, fcntl(_fdr, F_GETFL) | O_NONBLOCK);
......@@ -91,10 +93,15 @@ static void callback_read(int fd, int type, intptr_t param)
else
n = read(_fdr, _buffer, BUFFER_SIZE);
//fprintf(stderr, "n = %d\n", n);
if (n <= 0)
{
if (n == 0 || (errno != EINTR && errno != EAGAIN))
{
//fprintf(stderr, "stop watch\n");
GB.Watch(fd, GB_WATCH_NONE, (void *)callback_read, 0);
}
break; // try again
}
......@@ -163,6 +170,8 @@ static void open_write_fifo()
GB.Error("Unable to open fifo: &1: &2", path, strerror(errno));
return;
}
//fprintf(stderr, "open_write_fifo: %d\n", _fdw);
}
BEGIN_METHOD_VOID(Debug_Begin)
......@@ -206,6 +215,7 @@ BEGIN_METHOD_VOID(Debug_Start)
GB.Alloc(POINTER(&_buffer), BUFFER_SIZE);
_buffer_left = 0;
//fprintf(stderr, "watch debugger on %d\n", _fdr);
GB.Watch(_fdr, GB_WATCH_READ, (void *)callback_read, 0);
_started = TRUE;
......@@ -253,9 +263,12 @@ END_METHOD
BEGIN_METHOD(Debug_Write, GB_STRING data)
int try = 0;
const char *data = STRING(data);
int len = LENGTH(data);
__TRY_AGAIN:
if (_fdw < 0)
open_write_fifo();
......@@ -271,6 +284,15 @@ BEGIN_METHOD(Debug_Write, GB_STRING data)
__ERROR:
close(_fdw);
_fdw = -1;
try++;
if (try < 3)
{
usleep(1000);
goto __TRY_AGAIN;
}
fprintf(stderr, "gb.debug: warning: unable to send data to the debugger: %s\n", strerror(errno));
END_METHOD
......
......@@ -253,6 +253,7 @@ DEBUG_INFO *DEBUG_init(GB_DEBUG_INTERFACE *debug, bool fifo, const char *fifo_na
}
}
//fprintf(stderr, "gb.debug: open input fifo: %d\n", _fdw);
_out = fdopen(_fdw, "w");
if (!_out)
......@@ -1321,6 +1322,8 @@ static void open_read_fifo()
if (_fifo)
{
//fprintf(stderr, "open_read_fifo\n");
snprintf(path, sizeof(path), "%sout", DEBUG_fifo);
for(;;)
......@@ -1336,6 +1339,7 @@ static void open_read_fifo()
usleep(20000);
}
//fprintf(stderr, "gb.debug: open read fifo: %d\n", _fdr);
_in = fdopen(_fdr, "r");
if (!_in)
......@@ -1352,6 +1356,7 @@ static void open_read_fifo()
}
}
void DEBUG_main(bool error)
{
static DEBUG_TYPE last_command = TC_NONE;
......@@ -1386,7 +1391,7 @@ void DEBUG_main(bool error)
int len;
DEBUG_COMMAND *tc = NULL;
int save_errno = errno;
/*static int cpt = 0;*/
int try = 0;
GB.FreeString(&_error);
if (error)
......@@ -1401,6 +1406,7 @@ void DEBUG_main(bool error)
if (_fifo)
{
fprintf(_out, first ? "!!\n" : "!\n");
fflush(_out);
first = FALSE;
}
......@@ -1426,6 +1432,8 @@ void DEBUG_main(bool error)
GB.FreeString(&cmd);
__TRY_AGAIN:
for(;;)
{
*cmdbuf = 0;
......@@ -1441,11 +1449,21 @@ void DEBUG_main(bool error)
len = GB.StringLength(cmd);
// A null string command means an I/O error
// A null string command means an I/O error or an end-of-file
// We try to reopen the fifo.
if (len == 0)
{
fprintf(stderr, "gb.debug: warning: debugger I/O error: %s\n", strerror(errno));
exit(1);
if (errno)
fprintf(stderr, "gb.debug: warning: unable to read debugger input: %s\n", strerror(errno));
else
usleep(1000);
fclose(_in);
open_read_fifo();
try++;
if (try < 3)
goto __TRY_AGAIN;
_exit(1);
}
if (len > 0 && cmd[len - 1] == '\n')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment