Merge branch '10.1' into 10.2

This commit is contained in:
Oleksandr Byelkin 2019-12-03 14:45:06 +01:00
commit f8b5e147da
23 changed files with 435 additions and 117 deletions

View file

@ -320,8 +320,36 @@ static void DbugVfprintf(FILE *stream, const char* format, va_list args);
*/
#include <my_pthread.h>
/*
** Protects writing to all file descriptors, init_settings.keywords
** pointer and it's pointee - a linked list with keywords.
*/
static pthread_mutex_t THR_LOCK_dbug;
static void LockMutex(CODE_STATE *cs)
{
if (!cs->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked++;
}
static void UnlockMutex(CODE_STATE *cs)
{
--cs->locked;
assert(cs->locked >= 0);
if (cs->locked == 0)
pthread_mutex_unlock(&THR_LOCK_dbug);
}
static void LockIfInitSettings(CODE_STATE *cs)
{
if (cs->stack == &init_settings)
LockMutex(cs);
}
static void UnlockIfInitSettings(CODE_STATE *cs)
{
if (cs->stack == &init_settings)
UnlockMutex(cs);
}
static CODE_STATE *code_state(void)
{
CODE_STATE *cs, **cs_ptr;
@ -449,16 +477,9 @@ static int DbugParse(CODE_STATE *cs, const char *control)
const char *end;
int rel, f_used=0;
struct settings *stack;
int org_cs_locked;
stack= cs->stack;
if (!(org_cs_locked= cs->locked))
{
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked= 1;
}
if (control[0] == '-' && control[1] == '#')
control+=2;
@ -472,7 +493,9 @@ static int DbugParse(CODE_STATE *cs, const char *control)
stack->sub_level= 0;
stack->out_file= sstderr;
stack->functions= NULL;
LockIfInitSettings(cs);
stack->keywords= NULL;
UnlockIfInitSettings(cs);
stack->processes= NULL;
}
else if (!stack->out_file)
@ -488,7 +511,9 @@ static int DbugParse(CODE_STATE *cs, const char *control)
{
/* never share with the global parent - it can change under your feet */
stack->functions= ListCopy(init_settings.functions);
LockIfInitSettings(cs);
stack->keywords= ListCopy(init_settings.keywords);
UnlockIfInitSettings(cs);
stack->processes= ListCopy(init_settings.processes);
}
else
@ -512,21 +537,31 @@ static int DbugParse(CODE_STATE *cs, const char *control)
case 'd':
if (sign < 0 && control == end)
{
LockIfInitSettings(cs);
if (!is_shared(stack, keywords))
FreeList(stack->keywords);
stack->keywords=NULL;
UnlockIfInitSettings(cs);
stack->flags &= ~DEBUG_ON;
break;
}
LockIfInitSettings(cs);
if (rel && is_shared(stack, keywords))
stack->keywords= ListCopy(stack->keywords);
UnlockIfInitSettings(cs);
if (sign < 0)
{
if (DEBUGGING)
{
LockIfInitSettings(cs);
stack->keywords= ListDel(stack->keywords, control, end);
UnlockIfInitSettings(cs);
}
break;
}
LockIfInitSettings(cs);
stack->keywords= ListAdd(stack->keywords, control, end);
UnlockIfInitSettings(cs);
stack->flags |= DEBUG_ON;
break;
case 'D':
@ -661,11 +696,6 @@ static int DbugParse(CODE_STATE *cs, const char *control)
control=end+1;
end= DbugStrTok(control);
}
if (!org_cs_locked)
{
cs->locked= 0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
return !rel || f_used;
}
@ -998,7 +1028,9 @@ int _db_explain_ (CODE_STATE *cs, char *buf, size_t len)
get_code_state_if_not_set_or_return *buf=0;
LockIfInitSettings(cs);
op_list_to_buf('d', cs->stack->keywords, DEBUGGING);
UnlockIfInitSettings(cs);
op_int_to_buf ('D', cs->stack->delay, 0);
op_list_to_buf('f', cs->stack->functions, cs->stack->functions);
op_bool_to_buf('F', cs->stack->flags & FILE_ON);
@ -1093,7 +1125,6 @@ int _db_explain_init_(char *buf, size_t len)
void _db_enter_(const char *_func_, const char *_file_,
uint _line_, struct _db_stack_frame_ *_stack_frame_)
{
int save_errno, org_cs_locked;
CODE_STATE *cs;
if (!((cs=code_state())))
{
@ -1101,7 +1132,6 @@ void _db_enter_(const char *_func_, const char *_file_,
_stack_frame_->prev= 0;
return;
}
save_errno= errno;
_stack_frame_->line= -1;
_stack_frame_->func= cs->func;
@ -1122,20 +1152,14 @@ void _db_enter_(const char *_func_, const char *_file_,
cs->stack->flags &= ~SANITY_CHECK_ON;
if (TRACING)
{
if (!(org_cs_locked= cs->locked))
{
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked= 1;
}
int save_errno= errno;
LockMutex(cs);
DoPrefix(cs, _line_);
Indent(cs, cs->level);
(void) fprintf(cs->stack->out_file->file, ">%s\n", cs->func);
DbugFlush(cs); /* This does a unlock */
if (!org_cs_locked)
{
cs->locked= 0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
UnlockMutex(cs);
DbugFlush(cs);
errno=save_errno;
}
break;
case DISABLE_TRACE:
@ -1144,7 +1168,6 @@ void _db_enter_(const char *_func_, const char *_file_,
case DONT_TRACE:
break;
}
errno=save_errno;
}
/*
@ -1169,7 +1192,6 @@ void _db_enter_(const char *_func_, const char *_file_,
void _db_return_(struct _db_stack_frame_ *_stack_frame_)
{
int save_errno=errno;
uint _slevel_= _stack_frame_->level & ~TRACE_ON;
CODE_STATE *cs;
get_code_state_or_return;
@ -1186,25 +1208,18 @@ void _db_return_(struct _db_stack_frame_ *_stack_frame_)
if (DoTrace(cs) & DO_TRACE)
{
int org_cs_locked;
if ((cs->stack->flags & SANITY_CHECK_ON) && sf_sanity())
cs->stack->flags &= ~SANITY_CHECK_ON;
if (TRACING)
{
if (!(org_cs_locked= cs->locked))
{
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked= 1;
}
int save_errno=errno;
LockMutex(cs);
DoPrefix(cs, _stack_frame_->line);
Indent(cs, cs->level);
(void) fprintf(cs->stack->out_file->file, "<%s\n", cs->func);
UnlockMutex(cs);
DbugFlush(cs);
if (!org_cs_locked)
{
cs->locked= 0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
errno=save_errno;
}
}
/*
@ -1216,7 +1231,6 @@ void _db_return_(struct _db_stack_frame_ *_stack_frame_)
cs->file= _stack_frame_->file;
if (cs->framep != NULL)
cs->framep= cs->framep->prev;
errno=save_errno;
}
@ -1281,18 +1295,14 @@ void _db_doprnt_(const char *format,...)
{
va_list args;
CODE_STATE *cs;
int save_errno, org_cs_locked;
int save_errno;
get_code_state_or_return;
va_start(args,format);
if (!(org_cs_locked= cs->locked))
{
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked= 1;
}
save_errno=errno;
LockMutex(cs);
DoPrefix(cs, cs->u_line);
if (TRACING)
Indent(cs, cs->level + 1);
@ -1300,12 +1310,8 @@ void _db_doprnt_(const char *format,...)
(void) fprintf(cs->stack->out_file->file, "%s: ", cs->func);
(void) fprintf(cs->stack->out_file->file, "%s: ", cs->u_keyword);
DbugVfprintf(cs->stack->out_file->file, format, args);
UnlockMutex(cs);
DbugFlush(cs);
if (!org_cs_locked)
{
cs->locked= 0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
errno=save_errno;
va_end(args);
@ -1345,17 +1351,13 @@ static void DbugVfprintf(FILE *stream, const char* format, va_list args)
void _db_dump_(uint _line_, const char *keyword,
const unsigned char *memory, size_t length)
{
int pos, org_cs_locked;
int pos;
CODE_STATE *cs;
get_code_state_or_return;
if (!(org_cs_locked= cs->locked))
{
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked= 1;
}
if (_db_keyword_(cs, keyword, 0))
{
LockMutex(cs);
DoPrefix(cs, _line_);
if (TRACING)
{
@ -1383,13 +1385,9 @@ void _db_dump_(uint _line_, const char *keyword,
fputc(' ',cs->stack->out_file->file);
}
(void) fputc('\n',cs->stack->out_file->file);
UnlockMutex(cs);
DbugFlush(cs);
}
if (!org_cs_locked)
{
cs->locked= 0;
pthread_mutex_unlock(&THR_LOCK_dbug);
}
}
@ -1617,8 +1615,10 @@ static void PushState(CODE_STATE *cs)
static void FreeState(CODE_STATE *cs, int free_state)
{
struct settings *state= cs->stack;
LockIfInitSettings(cs);
if (!is_shared(state, keywords))
FreeList(state->keywords);
UnlockIfInitSettings(cs);
if (!is_shared(state, functions))
FreeList(state->functions);
if (!is_shared(state, processes))
@ -1697,8 +1697,6 @@ void _db_end_()
static int DoTrace(CODE_STATE *cs)
{
int res= DONT_TRACE;
if (!cs->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
if ((cs->stack->maxdepth == 0 || cs->level <= cs->stack->maxdepth) &&
InList(cs->stack->processes, cs->process, 0) & (MATCHED|INCLUDE))
{
@ -1723,8 +1721,6 @@ static int DoTrace(CODE_STATE *cs)
break;
}
}
if (!cs->locked)
pthread_mutex_unlock(&THR_LOCK_dbug);
return res;
}
@ -1764,11 +1760,9 @@ BOOLEAN _db_keyword_(CODE_STATE *cs, const char *keyword, int strict)
if (!(DEBUGGING && (DoTrace(cs) & DO_TRACE)))
return 0;
if (!cs->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
LockIfInitSettings(cs);
res= (InList(cs->stack->keywords, keyword, strict) & match);
if (!cs->locked)
pthread_mutex_unlock(&THR_LOCK_dbug);
UnlockIfInitSettings(cs);
return res != 0;
}
@ -1995,16 +1989,16 @@ static void DBUGCloseFile(CODE_STATE *cs, sFILE *new_value)
sFILE *fp;
if (!cs || !cs->stack || !cs->stack->out_file)
return;
if (!cs->locked)
pthread_mutex_lock(&THR_LOCK_dbug);
fp= cs->stack->out_file;
if (--fp->used == 0)
{
if (fclose(fp->file) == EOF)
{
LockMutex(cs);
(void) fprintf(stderr, ERR_CLOSE, cs->process);
perror("");
UnlockMutex(cs);
}
else
{
@ -2012,8 +2006,6 @@ static void DBUGCloseFile(CODE_STATE *cs, sFILE *new_value)
}
}
cs->stack->out_file= new_value;
if (!cs->locked)
pthread_mutex_unlock(&THR_LOCK_dbug);
}
@ -2196,9 +2188,7 @@ void _db_flush_()
get_code_state_or_return;
if (DEBUGGING)
{
pthread_mutex_lock(&THR_LOCK_dbug);
(void) fflush(cs->stack->out_file->file);
pthread_mutex_unlock(&THR_LOCK_dbug);
}
}
@ -2226,16 +2216,14 @@ void _db_lock_file_()
{
CODE_STATE *cs;
get_code_state_or_return;
pthread_mutex_lock(&THR_LOCK_dbug);
cs->locked=1;
LockMutex(cs);
}
void _db_unlock_file_()
{
CODE_STATE *cs;
get_code_state_or_return;
cs->locked=0;
pthread_mutex_unlock(&THR_LOCK_dbug);
UnlockMutex(cs);
}
const char* _db_get_func_(void)