2007-12-30 17:41:49 +01:00
|
|
|
/***************************************************************************
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
CDebug.c
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2018-02-12 02:53:46 +01:00
|
|
|
(c) 2000-2017 Benoît Minisini <g4mba5@gmail.com>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2, or (at your option)
|
|
|
|
any later version.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
MA 02110-1301, USA.
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#define __CDEBUG_C
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
2012-07-03 20:21:54 +02:00
|
|
|
#include <string.h>
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
#include "main.h"
|
|
|
|
#include "gb_limit.h"
|
2009-08-01 23:28:50 +02:00
|
|
|
#include "gb.debug.h"
|
2007-12-30 17:41:49 +01:00
|
|
|
#include "CDebug.h"
|
|
|
|
|
|
|
|
/*#define DEBUG*/
|
|
|
|
|
|
|
|
|
|
|
|
DECLARE_EVENT(EVENT_Read);
|
|
|
|
|
|
|
|
static int _started = FALSE;
|
2008-01-23 20:56:18 +01:00
|
|
|
static int _fdr = -1;
|
|
|
|
static int _fdw = -1;
|
2007-12-30 17:41:49 +01:00
|
|
|
static CDEBUG *_debug_object = NULL;
|
|
|
|
|
2009-08-01 17:13:50 +02:00
|
|
|
#define BUFFER_SIZE DEBUG_OUTPUT_MAX_SIZE
|
2007-12-30 17:41:49 +01:00
|
|
|
static char *_buffer = NULL;
|
|
|
|
static int _buffer_left;
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
static void callback_read(int fd, int type, intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
{
|
2017-02-18 18:24:01 +01:00
|
|
|
int n, i, p;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2010-03-07 18:14:54 +01:00
|
|
|
for(;;)
|
2017-02-18 18:24:01 +01:00
|
|
|
{
|
2010-03-25 03:00:55 +01:00
|
|
|
fcntl(_fdr, F_SETFL, fcntl(_fdr, F_GETFL) | O_NONBLOCK);
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (_buffer_left)
|
|
|
|
{
|
|
|
|
n = read(_fdr, &_buffer[_buffer_left], BUFFER_SIZE - _buffer_left);
|
|
|
|
if (n < 0)
|
|
|
|
n = 0;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
n += _buffer_left;
|
|
|
|
_buffer_left = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
n = read(_fdr, _buffer, BUFFER_SIZE);
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (n <= 0)
|
2010-02-28 18:26:00 +01:00
|
|
|
{
|
2010-03-07 18:14:54 +01:00
|
|
|
//usleep(10000); // the callback is called again and again even if there is nothing to read, why?
|
2017-02-18 18:24:01 +01:00
|
|
|
break;
|
2010-02-28 18:26:00 +01:00
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
p = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
if (_buffer[i] == '\n')
|
|
|
|
{
|
|
|
|
/*fprintf(stderr, "CDEBUG_read: <<< %.*s >>>\n", i - p, &_buffer[p]);*/
|
|
|
|
GB.Raise(_debug_object, EVENT_Read, 1, GB_T_STRING, i <= p ? NULL : &_buffer[p], i - p);
|
|
|
|
if (!_buffer)
|
|
|
|
break;
|
|
|
|
p = i + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_buffer)
|
2014-05-18 23:24:27 +02:00
|
|
|
break;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (p == 0 && n >= BUFFER_SIZE)
|
|
|
|
{
|
|
|
|
GB.Raise(_debug_object, EVENT_Read, 1, GB_T_STRING, _buffer, BUFFER_SIZE);
|
|
|
|
if (!_buffer)
|
|
|
|
break;
|
|
|
|
_buffer_left = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
_buffer_left = n - p;
|
|
|
|
if (p && n > p)
|
|
|
|
memmove(_buffer, &_buffer[p], _buffer_left);
|
|
|
|
}
|
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2010-03-07 18:14:54 +01:00
|
|
|
fcntl(_fdr, F_SETFL, fcntl(_fdr, F_GETFL) & ~O_NONBLOCK);
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
2008-01-17 22:39:26 +01:00
|
|
|
// static void callback_read(int fd, int type, intptr_t param)
|
2007-12-30 17:41:49 +01:00
|
|
|
// {
|
|
|
|
// char *buffer = NULL;
|
|
|
|
// char tmp[256];
|
|
|
|
// int n;
|
2016-02-01 03:18:49 +01:00
|
|
|
//
|
2007-12-30 17:41:49 +01:00
|
|
|
// for(;;)
|
|
|
|
// {
|
|
|
|
// n = read(_fdr, tmp, 256);
|
|
|
|
// if (n <= 0)
|
|
|
|
// break;
|
|
|
|
// GB.AddString(&buffer, tmp, n);
|
|
|
|
// }
|
2016-02-01 03:18:49 +01:00
|
|
|
//
|
|
|
|
// GB.Raise(_debug_object, EVENT_Read, 1, GB_T_STRING, buffer, GB.StringLength(buffer));
|
2007-12-30 17:41:49 +01:00
|
|
|
// }
|
|
|
|
|
|
|
|
static char *input_fifo(char *path)
|
|
|
|
{
|
2019-03-21 23:14:22 +01:00
|
|
|
sprintf(path, FILE_TEMP_PREFIX "/%d.in", getuid(), getpid());
|
2017-02-18 18:24:01 +01:00
|
|
|
return path;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *output_fifo(char *path)
|
|
|
|
{
|
2019-03-21 23:14:22 +01:00
|
|
|
sprintf(path, FILE_TEMP_PREFIX "/%d.out", getuid(), getpid());
|
2017-02-18 18:24:01 +01:00
|
|
|
return path;
|
2007-12-30 17:41:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CDEBUG_begin)
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
char path[PATH_MAX];
|
2009-05-22 16:52:36 +02:00
|
|
|
char name[16];
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2019-03-21 23:14:22 +01:00
|
|
|
unlink(input_fifo(path));
|
2017-02-18 18:24:01 +01:00
|
|
|
if (mkfifo(path, 0600))
|
|
|
|
{
|
|
|
|
GB.Error("Cannot create input fifo in /tmp: &1", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2019-03-21 23:14:22 +01:00
|
|
|
unlink(output_fifo(path));
|
2017-02-18 18:24:01 +01:00
|
|
|
if (mkfifo(path, 0600))
|
|
|
|
{
|
|
|
|
GB.Error("Cannot create output fifo in /tmp: &1", strerror(errno));
|
|
|
|
return;
|
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2009-05-22 16:52:36 +02:00
|
|
|
sprintf(name, "%d", getpid());
|
|
|
|
GB.ReturnNewZeroString(name);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
END_METHOD
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CDEBUG_start)
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
char path[DEBUG_FIFO_PATH_MAX];
|
|
|
|
int i;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (_started)
|
|
|
|
return;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
for (i = 0; i < 25; i++)
|
|
|
|
{
|
|
|
|
_fdw = open(output_fifo(path), O_WRONLY | O_NONBLOCK);
|
|
|
|
if (_fdw >= 0)
|
|
|
|
break;
|
2009-05-22 21:20:33 +02:00
|
|
|
usleep(20000);
|
2009-01-28 01:22:26 +01:00
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2009-01-28 01:22:26 +01:00
|
|
|
if (_fdw < 0)
|
|
|
|
{
|
|
|
|
GB.Error("Unable to open fifo");
|
|
|
|
return;
|
|
|
|
}
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
_fdr = open(input_fifo(path), O_RDONLY | O_NONBLOCK);
|
2010-03-07 18:14:54 +01:00
|
|
|
fcntl(_fdr, F_SETFL, fcntl(_fdr, F_GETFL) & ~O_NONBLOCK);
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
_debug_object = GB.New(GB.FindClass("Debug"), "Debug", NULL);
|
|
|
|
GB.Ref(_debug_object);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB.Alloc(POINTER(&_buffer), BUFFER_SIZE);
|
|
|
|
_buffer_left = 0;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB.Watch(_fdr, GB_WATCH_READ, (void *)callback_read, 0);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
_started = TRUE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2016-02-01 03:18:49 +01:00
|
|
|
BEGIN_METHOD_VOID(CDEBUG_stop)
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (!_started)
|
|
|
|
return;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB.Watch(_fdr, GB_WATCH_NONE, (void *)callback_read, 0);
|
|
|
|
GB.Free(POINTER(&_buffer));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB.Unref(POINTER(&_debug_object));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
close(_fdw);
|
|
|
|
close(_fdr);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
_fdw = _fdr = -1;
|
|
|
|
_started = FALSE;
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD_VOID(CDEBUG_end)
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
char path[DEBUG_FIFO_PATH_MAX];
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
CALL_METHOD_VOID(CDEBUG_stop);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
unlink(input_fifo(path));
|
|
|
|
unlink(output_fifo(path));
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
signal(SIGPIPE, SIG_DFL);
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
|
|
|
BEGIN_METHOD(CDEBUG_write, GB_STRING data)
|
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
const char *data = STRING(data);
|
|
|
|
int len = LENGTH(data);
|
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (_fdw < 0)
|
|
|
|
return;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
if (data && len > 0)
|
2011-01-02 18:09:54 +01:00
|
|
|
{
|
2017-02-18 18:24:01 +01:00
|
|
|
if (write(_fdw, data, len) != len)
|
2011-01-02 18:09:54 +01:00
|
|
|
goto __ERROR;
|
|
|
|
}
|
2017-02-18 18:24:01 +01:00
|
|
|
if (write(_fdw, "\n", 1) != 1)
|
2011-01-02 18:09:54 +01:00
|
|
|
goto __ERROR;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
return;
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2011-01-02 18:09:54 +01:00
|
|
|
__ERROR:
|
|
|
|
|
|
|
|
fprintf(stderr, "gb.debug: warning: unable to send data to the debugger: %s\n", strerror(errno));
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
|
|
|
|
2012-07-03 20:21:54 +02:00
|
|
|
BEGIN_METHOD(Debug_GetSignal, GB_INTEGER signal)
|
|
|
|
|
|
|
|
GB.ReturnNewZeroString(strsignal(VARG(signal)));
|
|
|
|
|
|
|
|
END_METHOD
|
|
|
|
|
2007-12-30 17:41:49 +01:00
|
|
|
|
|
|
|
GB_DESC CDebugDesc[] =
|
|
|
|
{
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_DECLARE("Debug", sizeof(CDEBUG)),
|
|
|
|
GB_NOT_CREATABLE(),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_STATIC_METHOD("_exit", NULL, CDEBUG_end, NULL),
|
2016-02-01 03:18:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_STATIC_METHOD("Begin", "s", CDEBUG_begin, NULL),
|
|
|
|
GB_STATIC_METHOD("End", NULL, CDEBUG_end, NULL),
|
|
|
|
GB_STATIC_METHOD("Start", NULL, CDEBUG_start, NULL),
|
|
|
|
GB_STATIC_METHOD("Stop", NULL, CDEBUG_stop, NULL),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_STATIC_METHOD("GetSignal", "s", Debug_GetSignal, "(Signal)i"),
|
2012-07-03 20:21:54 +02:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_STATIC_METHOD("Write", NULL, CDEBUG_write, "(Data)s"),
|
|
|
|
|
|
|
|
GB_EVENT("Read", NULL, "(Data)s", &EVENT_Read),
|
2007-12-30 17:41:49 +01:00
|
|
|
|
2017-02-18 18:24:01 +01:00
|
|
|
GB_END_DECLARE
|
2007-12-30 17:41:49 +01:00
|
|
|
};
|
|
|
|
|